This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
/
communicationHandler.go
298 lines (255 loc) · 7.25 KB
/
communicationHandler.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package comm
import (
"context"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"log"
"sync"
"time"
"github.com/alexandrainst/agentlogic"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
libp2ptls "github.com/libp2p/go-libp2p-tls"
"github.com/libp2p/go-libp2p/p2p/discovery"
"github.com/multiformats/go-multiaddr"
)
// DiscoveryInterval is how often we re-publish our mDNS records.
const DiscoveryInterval = time.Hour
// DiscoveryServiceTag is used in our mDNS advertisements to discover other chat peers.
const DiscoveryServiceTag = "D2D_Comunidad"
var channels = make(map[string]*Channel)
var channelsMux = &sync.Mutex{}
var SelfId peer.ID
var ps *pubsub.PubSub
//vars for registration
var myType agentlogic.AgentType
var discoveryPath string
var statePath string
var reorganizationPath string
var recalculationPath string
var DiscoveryChannel = make(chan *DiscoveryMessage, BufferSize)
var StateChannel = make(chan *StateMessage, BufferSize)
var ReorganizationChannel = make(chan *DiscoveryMessage, BufferSize)
var RecalculationChannel = make(chan *DiscoveryMessage, BufferSize)
var MissionChannel = make(chan *MissionMessage, BufferSize)
func InitD2DCommuncation(agentType agentlogic.AgentType) {
myType = agentType
ctx := context.Background()
// Create a new libp2p host
h, err := createHost()
if err != nil {
panic(err)
}
fmt.Println("Host id is ", h.ID())
SelfId = h.ID()
// create a new PubSub service using the GossipSub router
ps, err = pubsub.NewGossipSub(ctx, h)
if err != nil {
panic(err)
}
// setup local mDNS discovery
err = setupDiscovery(ctx, h)
if err != nil {
panic(err)
}
}
func InitCommunicationType(path string, messageType MessageType) {
channelsMux.Lock()
if channels[path] != nil {
//channel already created - ignoring
//log.Println("channel with path: " + path + " already in list. returning")
channelsMux.Unlock()
return
}
channelsMux.Unlock()
switch messageType {
case StateMessageType:
statePath = path
break
case DiscoveryMessageType:
discoveryPath = path
break
case ReorganizationMessageType:
reorganizationPath = path
break
case RecalculatorMessageType:
recalculationPath = path
break
}
ctx := context.Background()
cr, err := JoinPath(ctx, ps, SelfId, path, messageType)
if err != nil {
panic(err)
}
go cr.readMessages()
}
func SendMission(senderId string, mission *agentlogic.Mission, channelPath string) error {
channelsMux.Lock()
channel := channels[channelPath]
channelsMux.Unlock()
if channel == nil {
return errors.New("channel not found with path: " + channelPath)
}
m := MissionMessage{
MessageMeta: MessageMeta{MsgType: MissionMessageType, SenderId: senderId, SenderType: myType},
Content: *mission,
}
msgBytes, err := json.Marshal(m)
if err != nil {
return err
}
channel.topic.Publish(channel.ctx, msgBytes)
return nil
}
func SendState(state *agentlogic.State) {
//log.Println("Stating me")
//state channel
channelsMux.Lock()
channel := channels[statePath]
channelsMux.Unlock()
m := StateMessage{
MessageMeta: MessageMeta{MsgType: MissionMessageType, SenderId: state.ID, SenderType: myType},
Content: *state,
}
// log.Println("state msg:")
// log.Println(m)
msgBytes, err := json.Marshal(m)
if err != nil {
panic(err)
}
channel.topic.Publish(channel.ctx, msgBytes)
}
func AnnounceSelf(metadata *agentlogic.Agent) {
//log.Println("Announce:")
// log.Println(*metadata)
//registration channel
channelsMux.Lock()
channel := channels[discoveryPath]
channelsMux.Unlock()
m := DiscoveryMessage{
MessageMeta: MessageMeta{MsgType: MissionMessageType, SenderId: metadata.UUID, SenderType: myType},
Content: *metadata,
}
msgBytes, err := json.Marshal(m)
if err != nil {
panic(err)
}
channel.topic.Publish(channel.ctx, msgBytes)
}
func SendReorganization(metadata agentlogic.Agent, selfId string) {
//registration channel
channelsMux.Lock()
channel := channels[reorganizationPath]
channelsMux.Unlock()
m := DiscoveryMessage{
MessageMeta: MessageMeta{MsgType: ReorganizationMessageType, SenderId: selfId, SenderType: myType},
Content: metadata,
}
//log.Println(m)
msgBytes, err := json.Marshal(m)
if err != nil {
panic(err)
}
channel.topic.Publish(channel.ctx, msgBytes)
}
func SendRecalculation(metadata agentlogic.Agent, selfId string) {
log.Println(selfId + ": send recalc message on " + recalculationPath)
//registration channel
channelsMux.Lock()
channel := channels[recalculationPath]
channelsMux.Unlock()
m := DiscoveryMessage{
MessageMeta: MessageMeta{MsgType: RecalculatorMessageType, SenderId: selfId, SenderType: myType},
Content: metadata,
}
msgBytes, err := json.Marshal(m)
if err != nil {
panic(err)
}
channel.topic.Publish(channel.ctx, msgBytes)
}
func createHost() (host.Host, error) {
// Creates a new RSA key pair for this host.
var r = rand.Reader
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, r)
if err != nil {
panic(err)
}
//host will listens on a random local TCP port on the IPv4.
sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/0"))
// libp2p.New constructs a new libp2p Host.
host, err := libp2p.New(
context.Background(),
libp2p.ListenAddrs(sourceMultiAddr),
libp2p.Identity(prvKey),
// support TLS connections
libp2p.Security(libp2ptls.ID, libp2ptls.New),
)
if err != nil {
return nil, err
}
return host, nil
}
func (cr *Channel) readMessages() {
//log.Println("Starting cr.readMessages() with path: " + cr.path)
for {
message := <-cr.Messages
if cr.Close {
log.Println("closing channel: " + cr.path)
return
}
switch cr.roomType {
case DiscoveryMessageType:
msg := (*message).(*DiscoveryMessage)
DiscoveryChannel <- msg
break
case StateMessageType:
msg := (*message).(*StateMessage)
StateChannel <- msg
break
case ReorganizationMessageType:
msg := (*message).(*DiscoveryMessage)
ReorganizationChannel <- msg
break
case MissionMessageType:
msg := (*message).(*MissionMessage)
MissionChannel <- msg
break
case RecalculatorMessageType:
msg := (*message).(*DiscoveryMessage)
RecalculationChannel <- msg
break
}
}
}
// discoveryNotifee gets notified when we find a new peer via mDNS discovery
type discoveryNotifee struct {
h host.Host
}
// HandlePeerFound connects to peers discovered via mDNS. Once they're connected,
// the PubSub system will automatically start interacting with them if they also
// support PubSub.
func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) {
//fmt.Printf("discovered new peer %s\n", pi.ID.Pretty())
err := n.h.Connect(context.Background(), pi)
if err != nil {
fmt.Printf("error connecting to peer %s: %s\n", pi.ID.Pretty(), err)
}
}
// setupDiscovery creates an mDNS discovery service and attaches it to the libp2p Host.
// This lets us automatically discover peers on the same LAN and connect to them.
func setupDiscovery(ctx context.Context, h host.Host) error {
// setup mDNS discovery to find local peers
disc, err := discovery.NewMdnsService(ctx, h, DiscoveryInterval, DiscoveryServiceTag)
if err != nil {
return err
}
n := discoveryNotifee{h: h}
disc.RegisterNotifee(&n)
return nil
}