-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (51 loc) · 995 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"flag"
"fmt"
)
var (
shares = make(map[string]share) //map[secret]share
port = flag.Int("p", 6667, "Port to run on")
sem = make(chan int, MAX_CONNS)
//local = flag.Int("l", 3838, "Port for local broadcast")
)
const (
BLOCK_SIZE = 1 << 14 //2^14 = 16K
PIECE_LENGTH = 1 << 18 //2^18 = 256K
MAX_CONNS = 64
)
//TODO stop littering this all over, go check actual errors
func check(err error) {
if err != nil {
fmt.Println(err)
}
}
func init() {
for i := 0; i < MAX_CONNS; i++ {
sem <- 1
}
}
func main() {
flag.Parse()
parseConfig()
go sendMultiCast() //TODO more robust discovering
incoming, outgoing := make(chan *UDPMessage), make(chan *UDPMessage)
go listen(incoming)
for {
fmt.Println(len(sem))
select {
case m := <-incoming:
go func(m *UDPMessage) {
<-sem
handleMessage(m, outgoing)
sem <- 1
}(m)
case o := <-outgoing:
go func(o *UDPMessage) {
<-sem
sendMessage(o)
sem <- 1
}(o)
}
}
}