forked from jessta/gobit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gobit.go
101 lines (90 loc) · 2.28 KB
/
gobit.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import "./gotorrent"
import "./bencode"
import "bufio"
import "os"
import "fmt"
import "net"
import "container/list"
import "strconv"
import "runtime"
const lowerPort = 6881
const upperPort = 6999
/*
client manager:
there is a list of clients that we have connected too or have connected to us.
the manager loops through all the clients and gives them instructions the clients reply when their task is complete
Clients are given tasks, eg. get piece, etc
*/
func main(){
runtime.GOMAXPROCS(3);
clientList := list.New();
servererr := make(chan os.Error);
reportclient := make(chan *gotorrent.Client);
trackererr := make(chan os.Error);
_=trackererr;
buff := new(bencode.BeString);
reader := bufio.NewReader(os.Stdin);
be, err := buff.Decode(reader);
if err != nil {
fmt.Printf("%s\n", err.String());
os.Exit(1);
}
//numPieces = be['pieces']/pieceHashSize;
//build piece map
go server(servererr,reportclient);
// we listen for connections
// and we make connections
/* find clients to connect to from tracker or DHT*/
/*tracker := new(gotracker);
go gotracker.Run(trackererr,reportclient);*/
//main management loop
print("entering select\n");
var newclient *gotorrent.Client;
var error os.Error;
for {
//print("selecting...\n");
select {
case newclient = <-reportclient:
clientList.PushBack(newclient);
print("new client spawned\n");
case error= <-servererr:
print("server had error:",error.String(),"\n");
os.Exit(1);
}
}
_=be;
}
func server(error chan os.Error, reportclient chan *gotorrent.Client){
var conn net.Listener;
var err os.Error;
for port := lowerPort; port <= upperPort; port++ {
netstr,err := net.ResolveTCPAddr("0.0.0.0:"+strconv.Itoa(port));
if err != nil {error<-err;}
conn,err =net.ListenTCP("tcp4", netstr);
if err != nil{
if(err == os.EADDRINUSE){
continue;
} else {
error<-err;
}
} else {
print("spawned server on port: "+strconv.Itoa(port)+"\n");
break;
}
}
if err != nil {error<-err;}
for{
print("waiting for connections\n");
if clientConn, err := conn.Accept(); err==nil {
print("client accepted\n");
client := gotorrent.NewClient(conn.Addr());
client.SetConn(clientConn);
go client.Run();
reportclient <- client;
}else {
error <- err;
return;
}
}
}