-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (58 loc) · 1.58 KB
/
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
59
60
61
62
63
64
package main
import (
"fmt"
"kademliago/helpers"
"kademliago/kademlia"
"kademliago/listener"
"kademliago/network"
"kademliago/types"
"os"
)
func main() {
args := os.Args
rt := kademlia.CreatRoutingTable(160, args[4])
ch := make(chan types.Msgq)
go listener.BindServer(args[1], args[2], args[3], args[4], &rt, ch)
var addr string
var op uint
var msg string
for {
fmt.Scanf("%s %d %s", &addr, &op, &msg)
if !(len(addr) == 40) {
conn := helpers.CreateConnection(addr)
encodedmsg := helpers.CreateMessage(op, msg)
stat := network.Send(conn, encodedmsg)
if stat {
fmt.Println("msg sent")
}
} else {
if op == 3 {
_, ip, port, found := rt.FindNode(addr, args[4])
if !found && ip != "NULL" {
// fmt.Println("herre",ip,port)
conn := helpers.CreateConnection(ip + ":" + port)
encodedmsg := helpers.CreateMessage(4, "/"+args[4]+"/"+args[3]+"/"+args[1]+":"+args[2]+"+"+addr)
stat := network.Send(conn, encodedmsg)
if stat {
fmt.Println("request sent")
}
m := types.Msgq{}
m = <-ch
conn2 := helpers.CreateConnection(m.IP)
encodedmsg2 := helpers.CreateMessage(3, "/"+args[4]+"/"+args[3]+"/"+args[1]+":"+args[2]+"+"+msg)
stat2 := network.Send(conn2, encodedmsg2)
if stat2 {
fmt.Println("msg sent")
}
} else if ip != "NULL" {
conn := helpers.CreateConnection(ip + ":" + port)
encodedmsg := helpers.CreateMessage(op, "/"+args[4]+"/"+args[3]+"/"+args[1]+":"+args[2]+"+"+msg)
stat := network.Send(conn, encodedmsg)
if stat {
fmt.Println("msg sent")
}
}
}
}
}
}