-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (55 loc) · 1.81 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
package main
import (
"fmt"
"time"
"log"
"github.com/Boice-Technology/P2PNetworkProtocol/peer"
"github.com/Boice-Technology/P2PNetworkProtocol/utils"
)
func main() {
peer1MessageSender := make(chan peer.MessageToSend, 10)
peer1, err := peer.NewChatPeer("127.0.0.1", 5000, peer1MessageSender, func(msg string, senderAdd string) {
fmt.Println("peer1 Received:", msg, "From:", senderAdd)
// Handle the received message
if msg == "Hello peer" {
peer1MessageSender <- peer.MessageToSend{"Hi peer", senderAdd}
} else {
peer1MessageSender <- peer.MessageToSend{":)", senderAdd}
}
})
if err != nil {
log.Fatalln(err)
}
peer2MessageSender := make(chan peer.MessageToSend, 10)
_, err = peer.NewChatPeer("127.0.0.1", 5001, peer2MessageSender, func(msg string, senderAdd string) {
fmt.Println("peer2 Received:", msg, "From:", senderAdd)
// Handle the received message
if msg == "Hi peer" {
peer2MessageSender <- peer.MessageToSend{"Bye peer", senderAdd}
} else {
peer2MessageSender <- peer.MessageToSend{utils.END, senderAdd}
}
})
if err != nil {
log.Fatalln(err)
}
peer3MessageSender := make(chan peer.MessageToSend, 10)
_, err = peer.NewChatPeer("127.0.0.1", 5002, peer3MessageSender, func(msg string, senderAdd string) {
fmt.Println("peer3 Received:", msg, "From:", senderAdd)
// Handle the received message
if msg == "Hi peer" {
peer3MessageSender <- peer.MessageToSend{"Bye peer", senderAdd}
} else {
peer3MessageSender <- peer.MessageToSend{utils.END, senderAdd}
}
})
if err != nil {
log.Fatalln(err)
}
peer2MessageSender <- peer.MessageToSend{"Hello peer", peer1.GetMultiaddr()}
peer3MessageSender <- peer.MessageToSend{"Hello peer", peer1.GetMultiaddr()}
time.Sleep(2 * time.Second)
close(peer1MessageSender)
close(peer2MessageSender)
close(peer3MessageSender)
}