-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (133 loc) · 3.44 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
core "github.com/mm-uh/go-agent-platform/src"
kademlia "github.com/mm-uh/go-kademlia/src"
)
var Node *kademlia.LocalKademlia
func main() {
if len(os.Args) < 3 {
fmt.Println("Invalid arguments")
if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
fmt.Println("Usage:\n" +
"app <ip_to_run> <port_to_run> [remote_ip] [remote_port]\n" +
"\n" +
"<ip_to_run> -> IP that the app will take\n" +
"<host_to_run> -> Port that the app will run\n" +
"[remote_ip] -> (Optional) IP of another platform\n" +
"[remote_port] -> (Optional) Port of another platform")
} else {
fmt.Println("Run '<app_name> -h'")
}
return
}
ip := os.Args[1]
portStr := os.Args[2]
port, err := strconv.Atoi(portStr)
if err != nil {
panic("Invalid port")
}
gateway := len(os.Args) == 3
ln := kademlia.NewLocalKademlia(ip, port, 20, 3)
Node = ln
exited := make(chan bool)
key := kademlia.KeyNode{}
id := sha1.Sum([]byte("PORT"))
err = key.GetFromString(hex.EncodeToString(id[:]))
if err != nil {
return
}
val := strconv.FormatInt(int64(port+1000), 10)
err = ln.Store(ln.GetContactInformation(), &key, val)
if err != nil {
return
}
ln.RunServer(exited)
db := core.DatabaseAndPexBasedOnKademlia{Kd: ln}
if !gateway {
if len(os.Args) == 5 {
ipForJoin := os.Args[3]
portForJoinStr := os.Args[4]
portForJoin, err := strconv.Atoi(portForJoinStr)
if err != nil {
panic("Invalid port for join")
}
rn := kademlia.NewRemoteKademliaWithoutKey(ipForJoin, portForJoin)
err = ln.JoinNetwork(rn)
if err != nil {
panic("Can't Join")
}
} else if len(os.Args) == 4 {
addr, err := net.ResolveUDPAddr("udp", "255.255.255.255:"+os.Args[3])
if err != nil {
panic("Invalid port for join")
}
myAddr, err := net.ResolveUDPAddr("udp", ip+":")
if err != nil {
panic("Invalid port for join")
}
recvConn, err := net.ListenUDP("udp", myAddr)
if err != nil {
panic("Can not Listen")
}
for {
_, err = recvConn.WriteToUDP([]byte("JOIN"), addr)
if err != nil {
continue
}
response := make([]byte, 1024)
recvConn.SetDeadline(time.Now().Add(time.Second * 2))
_, from, err := recvConn.ReadFromUDP(response)
if err != nil {
continue
}
ipForJoin := strings.Split(string(response), ":")[1]
portForJoin, err := strconv.Atoi(strings.Split(string(response), ":")[2])
if err != nil {
continue
}
fmt.Println("JOINING TO ", from.IP)
rn := kademlia.NewRemoteKademliaWithoutKey(ipForJoin, portForJoin)
err = ln.JoinNetwork(rn)
if err != nil {
continue
}
break
}
}
} else {
pltId := fmt.Sprintf("%s:%d:%s", ip, port, time.Now().String())
hash := sha1.Sum([]byte(pltId))
pltIdHash := hex.EncodeToString(hash[:])
err = db.Store(core.PlatformId, pltIdHash)
if err != nil {
return
}
fmt.Println("Platform Id: ", pltIdHash)
trie := core.NewTrie()
err = db.Store(core.Name, trie)
if err != nil {
return
}
err = db.Store(core.Function, trie)
if err != nil {
return
}
}
platform := core.NewPlatform(core.Addr{Ip: ip, Port: port + 1000}, &db, &db)
go platform.ListenBroadcast(6001, ln.GetPort())
server := core.NewServer("", *platform, platform.Addr)
go server.RunServer()
if s := <-exited; s {
// Handle Error in method
fmt.Println("We get an error listen server")
return
}
}