-
Notifications
You must be signed in to change notification settings - Fork 0
/
node2.go
58 lines (50 loc) · 1.36 KB
/
node2.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 (
"Go-p2p/network"
"fmt"
"net/http"
"strconv"
"time"
mrand "math/rand"
"sync"
"github.com/opentracing/opentracing-go/log"
)
var wg sync.WaitGroup
func main() {
// Generate a random port number
mrand.Seed(time.Now().UTC().UnixNano())
randPort := mrand.Intn(45000) + 10000
// Start second node in the network
net1 := network.MakeNetwork()
network.InitMSGQueue()
http.HandleFunc("/JOIN", net1.JoinHandler)
http.HandleFunc("/PING", net1.PingHandler)
http.HandleFunc("/PONG", net1.PongHandler)
http.HandleFunc("/SendMSG", net1.SendMSGHandler)
http.HandleFunc("/BroadcastMSG", net1.BroadcastMSGHandler)
http.HandleFunc("/BroadcastMSGResponse", net1.BroadcastMSGResponseHandler)
wg.Add(1)
defer wg.Done()
go func() {
log.Error(http.ListenAndServe(":"+strconv.FormatInt(int64(randPort), 10), nil))
}()
err := net1.Join("127.0.0.1"+network.Port, randPort)
if err != nil {
fmt.Println(err)
return
}
// Ping the first client and any other clients that the
// second node may be connected to
for k := range net1.Nodes {
fmt.Printf("[+] Pinging node: %s\n", net1.Nodes[k].ID)
err := net1.Ping(net1.Nodes[k].ID)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("[+] SendMSG to node: %s\n", net1.Nodes[k].ID)
err = net1.SendMSG(net1.Nodes[k].ID, "", []byte("HELLO! HOW ARE YOU DOING RN?"))
}
// Wait now
wg.Wait()
}