-
Notifications
You must be signed in to change notification settings - Fork 0
/
node1.go
46 lines (39 loc) · 990 Bytes
/
node1.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
package main
import (
"Go-p2p/network"
"fmt"
"net/http"
"sync"
"github.com/opentracing/opentracing-go/log"
)
var wg sync.WaitGroup
func main() {
network.SeedRand()
net := network.MakeNetwork()
network.InitMSGQueue()
net.MyIP = "127.0.0.1" + network.Port
net.MyID = network.GenRandBytes(32)
http.HandleFunc("/JOIN", net.JoinHandler)
http.HandleFunc("/PING", net.PingHandler)
http.HandleFunc("/PONG", net.PongHandler)
http.HandleFunc("/SendMSG", net.SendMSGHandler)
http.HandleFunc("/BroadcastMSG", net.BroadcastMSGHandler)
http.HandleFunc("/BroadcastMSGResponse", net.BroadcastMSGResponseHandler)
wg.Add(1)
defer wg.Done()
go func() {
log.Error(http.ListenAndServe(network.Port, nil))
}()
fmt.Println("Client sending JOIN request")
// Just echo the messages now
for {
for k := range net.Nodes {
packets := network.HandleMsgQueuePackets(net.Nodes[k].ID)
for _, v := range packets {
fmt.Println(string(v.Data))
}
}
}
// Wait now
wg.Wait()
}