-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
383 lines (327 loc) · 10.8 KB
/
endpoint.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package drpmesh
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
)
// ConnectionStats provides latency and uptime stats
type ConnectionStats struct {
pingTimeMs int
uptimeSeconds int
}
// EndpointMethod defines the interface for a DRP Endpoint method
type EndpointMethod func(*CmdParams, EndpointInterface, *int) interface{}
// EndpointAuthInfo tracks the auth info provided by a remote Node
type EndpointAuthInfo struct {
Type string
Value string
UserInfo interface{}
}
// EndpointInterface declares the set of functions that should be implemented for any Endpoint object
type EndpointInterface interface {
GetID() *string
GetType() string
GetToken() int
AddReplyHandler() int
DeleteReplyHandler(int)
RegisterMethod(string, EndpointMethod)
SendPacketBytes([]byte)
SendCmd(string, string, interface{}, *int, *RouteOptions, *string)
SendCmdAwait(string, string, interface{}, *RouteOptions, *string) *ReplyIn
IsServer() bool
ConnectionStats() ConnectionStats
GetEndpointCmds() map[string]EndpointMethod
IsReady() bool
IsConnecting() bool
}
// Endpoint - DRP endpoint
type Endpoint struct {
wsConn *websocket.Conn
drpNode *Node
EndpointID *string
EndpointType string
EndpointCmds map[string]EndpointMethod
AuthInfo EndpointAuthInfo
ReplyHandlerQueue map[int](chan *ReplyIn)
TokenNum int
Subscriptions interface{}
openCallback *func()
closeCallback *func()
closeChan chan bool
//sendChan chan interface{}
}
// Init initializes Endpoint attributes
func (e *Endpoint) Init() {
e.EndpointCmds = make(map[string]EndpointMethod)
e.ReplyHandlerQueue = make(map[int](chan *ReplyIn))
e.closeChan = make(chan bool)
//e.sendChan = make(chan interface{}, 100)
e.TokenNum = 1
}
// GetID returns the ID of the Endpoint
func (e *Endpoint) GetID() *string {
return e.EndpointID
}
// GetType returns the type of the Endpoint
func (e *Endpoint) GetType() string {
return e.EndpointType
}
// GetToken returns the next token to be used for the Endpoint
func (e *Endpoint) GetToken() int {
//returnToken := strconv.Itoa(e.TokenNum)
returnToken := e.TokenNum
e.TokenNum++
return returnToken
}
// AddReplyHandler is used to track responses for a command
func (e *Endpoint) AddReplyHandler() int {
replyToken := e.GetToken()
e.ReplyHandlerQueue[replyToken] = make(chan *ReplyIn)
return replyToken
}
// DeleteReplyHandler removes a reply handler
func (e *Endpoint) DeleteReplyHandler(handlerToken int) {
delete(e.ReplyHandlerQueue, handlerToken)
return
}
// RegisterMethod adds a command that is allowed to be executed by the remote Endpoint
func (e *Endpoint) RegisterMethod(methodName string, method EndpointMethod) {
e.EndpointCmds[methodName] = method
}
// SendPacketBytes abstracts sending functions from the communication channel functions
func (e *Endpoint) SendPacketBytes(drpPacketBytes []byte) {
// This function listend to e.sendChan and sends commands
wsSendErr := e.wsConn.WriteMessage(websocket.TextMessage, drpPacketBytes)
if wsSendErr != nil {
e.drpNode.Log(fmt.Sprint("error writing message to WS channel:", wsSendErr), false)
//return wsSendErr
}
}
// SendCmd sends a command to a remote Endpoint
func (e *Endpoint) SendCmd(serviceName string, methodName string, cmdParams interface{}, token *int, routeOptions *RouteOptions, serviceInstanceID *string) {
sendCmd := &CmdOut{}
sendCmd.ServiceName = &serviceName
sendCmd.Type = "cmd"
sendCmd.Method = &methodName
sendCmd.Params = cmdParams
sendCmd.Token = token
sendCmd.RouteOptions = routeOptions
sendCmd.ServiceInstanceID = serviceInstanceID
packetBytes := sendCmd.ToJSON()
e.SendPacketBytes(packetBytes)
//e.sendChan <- *sendCmd
}
// SendCmdAwait sends a command to a remote Endpoint and awaits a response
func (e *Endpoint) SendCmdAwait(serviceName string, cmdName string, cmdParams interface{}, routeOptions *RouteOptions, serviceInstanceID *string) *ReplyIn {
replyToken := e.AddReplyHandler()
e.SendCmd(serviceName, cmdName, cmdParams, &replyToken, routeOptions, serviceInstanceID)
responseData := <-e.ReplyHandlerQueue[replyToken]
return responseData
}
// SendReply returns data to a remote Endpoint which originally executed a command
func (e *Endpoint) SendReply(replyToken *int, returnStatus int, returnPayload interface{}) {
replyCmd := &ReplyOut{}
replyCmd.Type = "reply"
replyCmd.Token = replyToken
replyCmd.Status = returnStatus
replyCmd.Payload = returnPayload
packetBytes := replyCmd.ToJSON()
e.drpNode.Log(fmt.Sprintf("SendReply -> %s", string(packetBytes)), true)
e.SendPacketBytes(packetBytes)
//e.sendChan <- *replyCmd
}
// ProcessCmd processes an inbound packet as a Cmd
func (e *Endpoint) ProcessCmd(msgIn *Cmd) {
cmdResults := make(map[string]interface{})
cmdResults["status"] = 0
cmdResults["output"] = nil
if _, ok := e.EndpointCmds[*msgIn.Method]; ok {
// Execute command
e.drpNode.Log(fmt.Sprintf("Executing method '%s'...", *msgIn.Method), true)
cmdResults["output"] = e.EndpointCmds[*msgIn.Method](msgIn.Params, e, msgIn.Token)
cmdResults["status"] = 1
} else {
cmdResults["output"] = "Endpoint does not have this method"
e.drpNode.Log(fmt.Sprintf("Remote Endpoint tried to execute unrecognized method '%s'", *msgIn.Method), true)
}
e.SendReply(msgIn.Token, cmdResults["status"].(int), cmdResults["output"])
}
// ProcessReply processes an inbound packet as a Reply
func (e *Endpoint) ProcessReply(msgIn *ReplyIn) {
e.ReplyHandlerQueue[*msgIn.Token] <- msgIn
// If the receive is complete, delete handler
if msgIn.Status < 2 {
e.DeleteReplyHandler(*msgIn.Token)
}
}
// ShouldRelay determines whether or not an inbound packet should be relayed
func (e *Endpoint) ShouldRelay(msgIn *PacketIn) bool {
var shouldForward = false
if msgIn.RouteOptions != nil && msgIn.RouteOptions.TgtNodeID != nil && *msgIn.RouteOptions.TgtNodeID != e.drpNode.NodeID {
shouldForward = true
}
return shouldForward
}
// ReceiveMessage determines whether a packet should be relayed or processed locally
func (e *Endpoint) ReceiveMessage(rawMessage []byte) {
packetIn := &PacketIn{}
err := json.Unmarshal(rawMessage, packetIn)
if err != nil {
e.drpNode.Log(fmt.Sprintf("ReceiveMessage Packet unmarshal error: %s", err), false)
return
}
if e.ShouldRelay(packetIn) {
e.RelayPacket(packetIn)
return
}
switch packetIn.Type {
case "cmd":
cmdPacket := &Cmd{}
err := json.Unmarshal(rawMessage, cmdPacket)
if err != nil {
e.drpNode.Log(fmt.Sprintf("ReceiveMessage Cmd unmarshal error: %s", err), false)
return
}
e.ProcessCmd(cmdPacket)
case "reply":
replyPacket := &ReplyIn{}
err := json.Unmarshal(rawMessage, replyPacket)
if err != nil {
e.drpNode.Log(fmt.Sprintf("ReceiveMessage Reply unmarshal error: %s", err), false)
return
}
e.ProcessReply(replyPacket)
}
}
// RelayPacket routes a packet to another Node
func (e *Endpoint) RelayPacket(packetIn *PacketIn) {
thisEndpoint := e
var errMsg *string = nil
// Validate sending endpoint
if thisEndpoint.EndpointID == nil {
// Sending endpoint has not authenticated
tmpErr := "sending endpoint has not authenticated"
errMsg = &tmpErr
// Validate source node
} else if !thisEndpoint.drpNode.TopologyTracker.ValidateNodeID(*packetIn.RouteOptions.SrcNodeID) {
// Source NodeID is invalid
tmpErr := fmt.Sprintf("srcNodeID %s not found", *packetIn.RouteOptions.SrcNodeID)
errMsg = &tmpErr
// Validate destination node
} else if !thisEndpoint.drpNode.TopologyTracker.ValidateNodeID(*packetIn.RouteOptions.TgtNodeID) {
// Target NodeID is invalid
tmpErr := fmt.Sprintf("tgtNodeID %s not found", *packetIn.RouteOptions.TgtNodeID)
errMsg = &tmpErr
}
if errMsg != nil {
thisEndpoint.drpNode.Log(fmt.Sprintf("Could not relay message: %s", *errMsg), false)
return
}
nextHopNodeID := thisEndpoint.drpNode.TopologyTracker.GetNextHop(*packetIn.RouteOptions.TgtNodeID)
targetNodeEndpoint := thisEndpoint.drpNode.VerifyNodeConnection(*nextHopNodeID)
// Add this node to the routing history
packetIn.RouteOptions.RouteHistory = append(packetIn.RouteOptions.RouteHistory, thisEndpoint.drpNode.NodeID)
// Repackage
packetBytesOut := []byte{}
switch packetIn.Type {
case "cmd":
cmdPacket := &Cmd{
BasePacket{
packetIn.Type,
packetIn.RouteOptions,
packetIn.Token,
},
packetIn.Method,
packetIn.Params,
packetIn.ServiceName,
packetIn.ServiceInstanceID,
}
packetBytesOut = cmdPacket.ToJSON()
case "reply":
replyPacket := &ReplyIn{
BasePacket{
packetIn.Type,
packetIn.RouteOptions,
packetIn.Token,
},
packetIn.Status,
packetIn.Payload,
}
packetBytesOut = replyPacket.ToJSON()
}
// Send packet to next hop
targetNodeEndpoint.SendPacketBytes(packetBytesOut)
// Increment local Node's PacketRelayCount
thisEndpoint.drpNode.PacketRelayCount++
return
}
// GetCmds returns the list of method available for the peer Endpoint to execute
func (e *Endpoint) GetCmds() interface{} {
keys := make([]string, 0)
for key := range e.EndpointCmds {
keys = append(keys, key)
}
return keys
}
// OpenHandler specifies actions to be taken after a connection is opened
func (e *Endpoint) OpenHandler() {
}
// CloseHandler specifies actions to be taken after a connection is closed
func (e *Endpoint) CloseHandler(code int, text string) error {
e.closeChan <- true
return nil
}
// ErrorHandler specifies actions to be taken after a connection encounters an error
func (e *Endpoint) ErrorHandler() {
}
// StartListening begins listening loop on wsConn
func (e *Endpoint) StartListening(wsConn *websocket.Conn) {
e.wsConn = wsConn
e.wsConn.SetCloseHandler(e.CloseHandler)
// Start receive loop
go func() {
for {
_, p, err := e.wsConn.ReadMessage()
if err != nil {
e.drpNode.Log(fmt.Sprintf("Could not read from wsConn: %s", err), true)
break
} else {
// Need to update this so the receive loop isn't tied up processing inbound commands
e.ReceiveMessage(p)
}
}
}()
}
// IsServer tells whether or not this endpoint is the server side of the connection
func (e *Endpoint) IsServer() bool {
return false
}
// PingTime TO DO - IMPLEMENT
func (e *Endpoint) PingTime() int {
return 0
}
// UpTime TO DO - IMPLEMENT
func (e *Endpoint) UpTime() int {
return 0
}
// IsReady tells whether or not the Endpoint's socket connection is ready to communicate
// TO DO - IMPLEMENT
func (e *Endpoint) IsReady() bool {
return true
}
// IsConnecting tells whether or not the Endpoint's socket connection is attempting to establish a connection
// TO DO - IMPLEMENT
func (e *Endpoint) IsConnecting() bool {
return false
}
// ConnectionStats returns uptime and latency info
func (e *Endpoint) ConnectionStats() ConnectionStats {
return ConnectionStats{
e.PingTime(),
e.UpTime(),
}
}
// GetEndpointCmds returns the commands available to be executed on this endpoint
func (e *Endpoint) GetEndpointCmds() map[string]EndpointMethod {
return e.EndpointCmds
}