/
node.go
757 lines (652 loc) · 25.5 KB
/
node.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
package drpmesh
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"time"
)
// CreateNode instantiates and returns a new node
func CreateNode(nodeRoles []string, hostID string, domainName string, meshKey string, zone string, scope string, listeningName *string, webServerConfig interface{}, drpRoute *string, debug bool) *Node {
nodeHostname, _ := os.Hostname()
nodePID := os.Getpid()
newNode := &Node{}
newNode.NodeRoles = nodeRoles
newNode.HostID = hostID
newNode.DomainName = domainName
newNode.meshKey = meshKey
newNode.Zone = zone
newNode.Scope = &scope
newNode.RegistryURL = nil
newNode.listeningName = listeningName
newNode.webServerConfig = webServerConfig
newNode.drpRoute = drpRoute
newNode.NodeID = fmt.Sprintf("%s-%d", nodeHostname, nodePID)
newNode.Debug = debug
newNode.ConnectedToControlPlane = false
newNode.HasConnectedToMesh = false
newNode.PacketRelayCount = 0
newNode.NodeDeclaration = &NodeDeclaration{newNode.NodeID, newNode.NodeRoles, newNode.HostID, newNode.listeningName, newNode.DomainName, newNode.meshKey, newNode.Zone, newNode.Scope}
newNode.NodeEndpoints = make(map[string]EndpointInterface)
newNode.ConsumerEndpoints = make(map[string]EndpointInterface)
newNode.Services = make(map[string]Service)
newNode.TopologyTracker = &TopologyTracker{}
newNode.TopologyTracker.Initialize(newNode)
var localDRPEndpoint = &Endpoint{}
localDRPEndpoint.Init()
newNode.ApplyNodeEndpointMethods(localDRPEndpoint)
var DRPService = Service{"DRP", newNode, "DRP", "", false, 10, 10, newNode.Zone, "local", []string{}, []string{}, 1, localDRPEndpoint.EndpointCmds, nil}
newNode.AddService(DRPService)
return newNode
}
// NodeDeclaration objects are traded between Node Endpoints, currently used for mesh auth
type NodeDeclaration struct {
NodeID string
NodeRoles []string
HostID string
NodeURL *string
DomainName string
MeshKey string
Zone string
Scope *string
}
// Node is the base object for DRP operations; service and endpoints are bound to this
type Node struct {
HostID string
NodeID string
DomainName string
meshKey string
Zone string
Scope *string
RegistryURL *string
webServerConfig interface{}
listeningName *string
drpRoute *string
NodeRoles []string
NodeDeclaration *NodeDeclaration
Services map[string]Service
TopicManager interface{}
TopologyTracker *TopologyTracker
NodeEndpoints map[string]EndpointInterface
ConsumerEndpoints map[string]EndpointInterface
Debug bool
ConnectedToControlPlane bool
HasConnectedToMesh bool
PacketRelayCount uint
onControlPlaneConnect *func()
}
// Log data to console using standard format
func (dn *Node) Log(logMessage string, isDebugMsg bool) {
if isDebugMsg && !dn.Debug {
return
}
timestamp := dn.GetTimestamp()
fmt.Printf("%s [%14s] -> %s\n", timestamp, dn.NodeID, logMessage)
}
// GetTimestamp returns the timestamp in a fixed format
func (dn *Node) GetTimestamp() string {
t := time.Now()
timestamp := t.Format("20060102150405")
return timestamp
}
/*
TO DO - IMPLEMENT THESE FUNCTIONS
GetConsumerToken
GetLastTokenForUser
EnableREST
AddSwaggerRouter
ListClassInstances
GetServiceDefinition
GetServiceDefinitions
GetClassRecords
SendPathCmdToNode
GetBaseObj
FindProvidersForStream
EvalPath
GetObjFromPath
VerifyConsumerConnection
*/
// ServiceCmd is used to execute a command against a local or remote Service
func (dn *Node) ServiceCmd(serviceName string, method string, params interface{}, targetNodeID *string, targetServiceInstanceID *string, useControlPlane bool, awaitResponse bool, callingEndpoint EndpointInterface) interface{} {
thisNode := dn
baseErrMsg := "ERROR - "
// If if no service or command is provided, return null
if serviceName == "" || method == "" {
return "ServiceCmd: must provide serviceName and method"
}
// If no targetNodeID was provided, we need to find a record in the ServiceTable
if targetNodeID == nil {
// If no targetServiceInstanceID was provided, we should attempt to locate a service instance
var targetServiceRecord *ServiceTableEntry = nil
if targetServiceInstanceID == nil {
// Update to use the DRP_TopologyTracker object
targetServiceRecord = thisNode.TopologyTracker.FindInstanceOfService(&serviceName, nil, nil, nil)
// If no match is found then return null
if targetServiceRecord == nil {
return nil
}
// Assign target Node & Instance IDs
targetServiceInstanceID = targetServiceRecord.InstanceID
targetNodeID = targetServiceRecord.NodeID
thisNode.Log(fmt.Sprintf("Best instance of service [%s] is [%s] on node [%s]", serviceName, *targetServiceRecord.InstanceID, *targetServiceRecord.NodeID), true)
} else {
targetServiceRecord = thisNode.TopologyTracker.ServiceTable.GetEntry(*targetServiceInstanceID).(*ServiceTableEntry)
// If no match is found then return null
if targetServiceRecord == nil {
return nil
}
// Assign target Node
targetNodeID = targetServiceRecord.NodeID
}
}
// We don't have a target NodeID
if targetNodeID == nil || !thisNode.TopologyTracker.NodeTable.HasEntry(*targetNodeID) {
return nil
}
// Where is the service?
if *targetNodeID == thisNode.NodeID {
// Execute locally
var localServiceProvider map[string]EndpointMethod = nil
if serviceName == "DRP" && callingEndpoint != nil {
// If the service is DRP and the caller is a remote endpoint, execute from that caller's EndpointCmds
localServiceProvider = callingEndpoint.GetEndpointCmds()
} else {
if _, ok := thisNode.Services[serviceName]; ok {
localServiceProvider = thisNode.Services[serviceName].ClientCmds
}
}
if localServiceProvider == nil {
thisNode.Log(fmt.Sprintf("%s service %s does not exist", baseErrMsg, serviceName), true)
return nil
}
if _, ok := localServiceProvider[method]; !ok {
thisNode.Log(fmt.Sprintf("%s service %s does not have method %s", baseErrMsg, serviceName, method), true)
return nil
}
if awaitResponse {
results := localServiceProvider[method](params.(*CmdParams), callingEndpoint, nil)
return results
}
localServiceProvider[method](params.(*CmdParams), callingEndpoint, nil)
return nil
}
// Execute on another Node
routeNodeID := targetNodeID
routeOptions := RouteOptions{}
if !useControlPlane {
// Make sure either the local Node or remote Node are listening; if not, route via control plane
localNodeEntry := thisNode.TopologyTracker.NodeTable.GetEntry(thisNode.NodeID).(*NodeTableEntry)
remoteNodeEntry := thisNode.TopologyTracker.NodeTable.GetEntry(*targetNodeID).(*NodeTableEntry)
if remoteNodeEntry == nil {
return fmt.Sprintf("Tried to contact Node[%s], not in NodeTable", *targetNodeID)
}
if localNodeEntry.NodeURL == nil && remoteNodeEntry.NodeURL == nil {
// Neither the local node nor the remote node are listening, use control plane
useControlPlane = true
}
}
if useControlPlane {
// We want to use to use the control plane instead of connecting directly to the target
if thisNode.ConnectedToControlPlane {
routeNodeID = thisNode.TopologyTracker.GetNextHop(*targetNodeID)
routeOptions = RouteOptions{&thisNode.NodeID, targetNodeID, []string{}}
} else {
// We're not connected to a Registry; fallback to VerifyNodeConnection
routeNodeID = targetNodeID
}
}
routeNodeConnection := thisNode.VerifyNodeConnection(*routeNodeID)
if routeNodeConnection == nil {
errMsg := fmt.Sprintf("Could not establish connection from Node[%s] to Node[%s]", thisNode.NodeID, *routeNodeID)
thisNode.Log(fmt.Sprintf("ERROR - %s", errMsg), false)
return errMsg
}
if awaitResponse {
cmdResponse := routeNodeConnection.SendCmdAwait(serviceName, method, params, &routeOptions, targetServiceInstanceID)
return cmdResponse.Payload
}
routeNodeConnection.SendCmd(serviceName, method, params, nil, &routeOptions, targetServiceInstanceID)
return nil
}
// TCPPingResults contains the TCP ping results to a given host and port
type TCPPingResults struct {
name string
port uint
pingInfo TCPPingMetrics
}
// TCPPingMetrics contains the metrics from a TCP ping
type TCPPingMetrics struct {
min uint
max uint
avg uint
}
// PingDomainRegistries returns the SRV records for a domain
func (dn *Node) PingDomainRegistries() map[string]TCPPingResults {
returnMap := make(map[string]TCPPingResults)
// Look up the SRV records
// TO DO - IMPLEMENT TCPPING LOGIC!
return returnMap
}
// RegistryClientHandler handles connection logic when making an outbound connection to a Registry Node
func (dn *Node) RegistryClientHandler(nodeClient *Client) {
thisNode := dn
// Get peer info
getDeclarationResponse := nodeClient.SendCmdAwait("DRP", "getNodeDeclaration", nil, nil, nil)
remoteNodeDeclaration := &NodeDeclaration{}
if getDeclarationResponse != nil && getDeclarationResponse.Payload != nil {
err := json.Unmarshal(*getDeclarationResponse.Payload, remoteNodeDeclaration)
if err != nil {
thisNode.Log(fmt.Sprintf("RegistryClientHandler Payload unmarshal error: %s", err), false)
return
}
registryNodeID := remoteNodeDeclaration.NodeID
nodeClient.EndpointID = ®istryNodeID
thisNode.NodeEndpoints[registryNodeID] = nodeClient
} else {
return
}
// Get Registry
thisNode.TopologyTracker.ProcessNodeConnect(nodeClient, remoteNodeDeclaration, false)
}
// ConnectToRegistry attempts a connection to a specific Registry Node URL
func (dn *Node) ConnectToRegistry(registryURL string, openCallback *func(), closeCallback *func()) {
retryOnClose := true
newRegistryClient := &Client{}
regClientOpenCallback := func() {
dn.RegistryClientHandler(newRegistryClient)
if openCallback != nil {
(*openCallback)()
}
}
if closeCallback != nil {
retryOnClose = false
}
newRegistryClient.Connect(registryURL, nil, dn, nil, retryOnClose, ®ClientOpenCallback, closeCallback)
}
// ConnectToRegistryByDomain locates a Registry for a given domain
func (dn *Node) ConnectToRegistryByDomain() {
// TO DO - IMPLEMENT
}
// ConnectToOtherRegistries locates other Registries for a given domain
func (dn *Node) ConnectToOtherRegistries() {
// TO DO - IMPLEMENT
}
// ConnectToMesh attempts to locate and connect to a Registry in the Node's domain
func (dn *Node) ConnectToMesh(onControlPlaneConnect func()) {
thisNode := dn
if onControlPlaneConnect != nil {
*thisNode.onControlPlaneConnect = onControlPlaneConnect
}
// If this is a Registry, seed the Registry with it's own declaration
if thisNode.IsRegistry() {
if thisNode.DomainName != "" {
// A domain name was provided; attempt to cluster with other registry hosts
thisNode.Log(fmt.Sprintf("This node is a Registry for %s, attempting to contact other Registry nodes", thisNode.DomainName), false)
thisNode.ConnectToOtherRegistries()
}
if thisNode.onControlPlaneConnect != nil {
(*thisNode.onControlPlaneConnect)()
}
} else {
if thisNode.RegistryURL != nil {
// A specific Registry URL was provided
thisNode.ConnectToRegistry(*thisNode.RegistryURL, nil, nil)
} else if thisNode.DomainName != "" {
// A domain name was provided; attempt to connect to a registry host
thisNode.ConnectToRegistryByDomain()
} else {
// No Registry URL or domain provided
panic("No DomainName or RegistryURL provided!")
}
}
}
// ConnectToNode attempts a connection to a specific Registry Node URL
func (dn *Node) ConnectToNode(targetNodeID string, targetURL string) {
thisNode := dn
// Initiate Node Connection
targetEndpoint := thisNode.NodeEndpoints[targetNodeID]
if targetEndpoint != nil && targetEndpoint.IsConnecting() {
// We already have this NodeEndpoint registered and the wsConn is opening or open
thisNode.Log(fmt.Sprintf("Received back request, already have NodeEndpoints[%s]", targetNodeID), true)
} else {
thisNode.Log(fmt.Sprintf("Received back request, connecting to [%s] @ %s", targetNodeID, targetURL), true)
thisNodeEndpoint := &Client{}
thisNode.NodeEndpoints[targetNodeID] = thisNodeEndpoint
thisNodeEndpoint.Connect(targetURL, nil, dn, &targetNodeID, false, nil, nil)
}
}
// ConnectToBroker attempts a connection to a specific Registry Node URL
/*
func (dn *Node) ConnectToBroker(wsTarget string, proxy *string) *Client {
newBrokerClient := &Client{}
newBrokerClient.Connect(wsTarget, proxy, dn, "someEndpointID", "Broker")
return newBrokerClient
}
*/
// AddService registers a new Service object to the local Node
func (dn *Node) AddService(serviceObj Service) {
thisNode := dn
newInstanceID := fmt.Sprintf("%s-%s-%d", dn.NodeID, serviceObj.ServiceName, rand.Intn(9999))
serviceObj.InstanceID = newInstanceID
thisNode.Services[serviceObj.ServiceName] = serviceObj
newServiceEntry := ServiceTableEntry{}
newServiceEntry.NodeID = &thisNode.NodeID
newServiceEntry.ProxyNodeID = nil
newServiceEntry.Scope = &serviceObj.Scope
newServiceEntry.Zone = &serviceObj.Zone
newServiceEntry.LearnedFrom = nil
newServiceEntry.LastModified = nil
newServiceEntry.Name = &serviceObj.ServiceName
newServiceEntry.Type = &serviceObj.Type
newServiceEntry.InstanceID = &serviceObj.InstanceID
newServiceEntry.Sticky = serviceObj.Sticky
newServiceEntry.Priority = serviceObj.Priority
newServiceEntry.Weight = serviceObj.Weight
newServiceEntry.Dependencies = serviceObj.Dependencies
newServiceEntry.Streams = serviceObj.Streams
newServiceEntry.Status = serviceObj.Status
addServicePacket := TopologyPacket{thisNode.NodeID, "add", "service", *newServiceEntry.InstanceID, *newServiceEntry.Scope, *newServiceEntry.Zone, newServiceEntry.ToJSON()}
thisNode.TopologyTracker.ProcessPacket(addServicePacket, thisNode.NodeID, false)
}
// RemoveService TO DO - IMPLEMENT
func (dn *Node) RemoveService() {}
// ApplyGenericEndpointMethods applies a mandatory set of methods to an Endpoint
// TO DO - REGISTER METHODS AS FUNCTIONS ARE PORTED
func (dn *Node) ApplyGenericEndpointMethods(targetEndpoint EndpointInterface) {
thisNode := dn
//type EndpointMethod func(*CmdParams, *websocket.Conn, *int) interface{}
targetEndpoint.RegisterMethod("getEndpointID", func(params *CmdParams, callingEndpoint EndpointInterface, token *int) interface{} {
return targetEndpoint.GetID()
})
targetEndpoint.RegisterMethod("getNodeDeclaration", func(params *CmdParams, callingEndpoint EndpointInterface, token *int) interface{} {
return thisNode.NodeDeclaration
})
/*
targetEndpoint.RegisterMethod("pathCmd", async (params, srcEndpoint, token) => {
return await thisNode.GetObjFromPath(params, thisNode.GetBaseObj(), srcEndpoint);
});
*/
targetEndpoint.RegisterMethod("getRegistry", func(params *CmdParams, callingEndpoint EndpointInterface, token *int) interface{} {
var reqNodeID *string = nil
if params != nil {
valueJSON := (*params)["reqNodeID"]
if valueJSON != nil {
json.Unmarshal(*valueJSON, reqNodeID)
}
}
return thisNode.TopologyTracker.GetRegistry(reqNodeID)
})
/*
targetEndpoint.RegisterMethod("getServiceDefinition", (...args) => {
return thisNode.GetServiceDefinition(...args);
});
targetEndpoint.RegisterMethod("getServiceDefinitions", async function (...args) {
return await thisNode.GetServiceDefinitions(...args);
});
*/
targetEndpoint.RegisterMethod("getLocalServiceDefinitions", func(params *CmdParams, callingEndpoint EndpointInterface, token *int) interface{} {
var serviceName *string = nil
if params != nil {
valueJSON := (*params)["serviceName"]
if valueJSON != nil {
json.Unmarshal(*valueJSON, serviceName)
}
}
var clientConnectionData = thisNode.GetLocalServiceDefinitions(serviceName)
return clientConnectionData
})
/*
targetEndpoint.RegisterMethod("getClassRecords", async (...args) => {
return await thisNode.GetClassRecords(...args);
});
targetEndpoint.RegisterMethod("listClassInstances", async (...args) => {
return await thisNode.ListClassInstances(...args);
});
targetEndpoint.RegisterMethod("sendToTopic", function (params, srcEndpoint, token) {
thisNode.TopicManager.SendToTopic(params.topicName, params.topicData);
});
targetEndpoint.RegisterMethod("getTopology", async function (...args) {
return await thisNode.GetTopology(...args);
});
*/
targetEndpoint.RegisterMethod("listClientConnections", func(params *CmdParams, callingEndpoint EndpointInterface, token *int) interface{} {
var clientConnectionData = thisNode.ListClientConnections()
return clientConnectionData
})
/*
targetEndpoint.RegisterMethod("tcpPing", async (...args) => {
return thisNode.TCPPing(...args);
});
targetEndpoint.RegisterMethod("findInstanceOfService", async (params) => {
return thisNode.TopologyTracker.FindInstanceOfService(params.serviceName, params.serviceType, params.zone);
});
targetEndpoint.RegisterMethod("listServices", async (params) => {
return thisNode.TopologyTracker.ListServices(params.serviceName, params.serviceType, params.zone);
});
targetEndpoint.RegisterMethod("subscribe", async function (params, srcEndpoint, token) {
// Only allow if the scope is local or this Node is a Broker
if (params.scope !== "local" && !thisNode.IsBroker()) return null;
let sendFunction = async (message) => {
// Returns send status; error if not null
return await srcEndpoint.SendReply(params.streamToken, 2, message);
};
let sendFailCallback = async (sendFailMsg) => {
// Failed to send; may have already disconnected, take no further action
};
let thisSubscription = new DRP_Subscriber(params.topicName, params.scope, params.filter, sendFunction, sendFailCallback);
srcEndpoint.Subscriptions[params.streamToken] = thisSubscription;
return await thisNode.Subscribe(thisSubscription);
});
targetEndpoint.RegisterMethod("unsubscribe", async function (params, srcEndpoint, token) {
let response = false;
let thisSubscription = srcEndpoint.Subscriptions[params.streamToken];
if (thisSubscription) {
thisSubscription.Terminate();
thisNode.SubscriptionManager.Subscribers.delete(thisSubscription);
response = true;
}
return response;
});
targetEndpoint.RegisterMethod("refreshSwaggerRouter", async function (params, srcEndpoint, token) {
let serviceName = null;
if (params && params.serviceName) {
// params was passed from cliGetPath
serviceName = params.serviceName;
} else if (params && params.pathList && params.pathList.length > 0) {
// params was passed from cliGetPath
serviceName = params.pathList.shift();
} else {
if (params && params.pathList) return `Format \\refreshSwaggerRouter\\{serviceName}`;
else return `FAIL - serviceName not defined`;
}
if (thisNode.SwaggerRouters[serviceName]) {
delete thisNode.SwaggerRouters[serviceName];
let serviceInstance = thisNode.TopologyTracker.FindInstanceOfService(serviceName);
if (!serviceInstance) return `FAIL - Service [${serviceName}] does not exist`;
await thisNode.AddSwaggerRouter(serviceName, serviceInstance.NodeID);
return `OK - Refreshed SwaggerRouters[${serviceName}]`;
} else {
return `FAIL - SwaggerRouters[${serviceName}] does not exist`;
}
});
*/
}
// ApplyNodeEndpointMethods applies a set of methods to an Endpoint if the peer is a Node
// TO DO - REGISTER METHODS AS FUNCTIONS ARE PORTED
func (dn *Node) ApplyNodeEndpointMethods(targetEndpoint EndpointInterface) {
thisNode := dn
thisNode.ApplyGenericEndpointMethods(targetEndpoint)
/*
targetEndpoint.RegisterMethod("topologyUpdate", async function (...args) {
return thisNode.TopologyUpdate(...args);
});
*/
targetEndpoint.RegisterMethod("connectToNode", func(params *CmdParams, callingEndpoint EndpointInterface, token *int) interface{} {
var targetNodeID *string = nil
var targetURL *string = nil
if params != nil {
targetNodeIDJSON := (*params)["targetNodeID"]
if targetNodeIDJSON != nil {
json.Unmarshal(*targetNodeIDJSON, targetNodeID)
}
targetURLJSON := (*params)["targetURL"]
if targetURLJSON != nil {
json.Unmarshal(*targetURLJSON, targetURL)
}
}
thisNode.ConnectToNode(*targetNodeID, *targetURL)
return nil
})
/*
targetEndpoint.RegisterMethod("addConsumerToken", async function (params, srcEndpoint, token) {
if (params.tokenPacket) {
thisNode.ConsumerTokens[params.tokenPacket.Token] = params.tokenPacket;
}
return;
});
if (targetEndpoint.IsServer && !targetEndpoint.IsServer()) {
// Add this command for DRP_Client endpoints
targetEndpoint.RegisterMethod("connectToRegistryInList", async function (...args) {
return await thisNode.ConnectToRegistryInList(...args);
});
}
*/
}
// RawMessageToString converts a json.RawMessage ([]byte) to a string for debug output
func (dn *Node) RawMessageToString(rawMessage *json.RawMessage) *string {
j, err := json.Marshal(rawMessage)
if err != nil {
return nil
}
jsonString := string(j)
return &jsonString
}
// IsRegistry tells whether or not the local Node holds the Registry role
func (dn *Node) IsRegistry() bool {
for _, a := range dn.NodeRoles {
if a == "Registry" {
return true
}
}
return false
}
// IsBroker tells whether or not the local Node hold the Broker role
func (dn *Node) IsBroker() bool {
for _, a := range dn.NodeRoles {
if a == "Broker" {
return true
}
}
return false
}
// IsConnectedTo tells whether or not the local Node is directly connected to another Node
func (dn *Node) IsConnectedTo(checkNodeID string) bool {
_, ok := dn.NodeEndpoints[checkNodeID]
return ok
}
// ListClientConnections tells whether or not the local Node hold the Broker role
func (dn *Node) ListClientConnections() map[string]map[string]interface{} {
nodeClientConnections := make(map[string]map[string]interface{})
nodeClientConnections["nodeClients"] = make(map[string]interface{})
nodeClientConnections["consumerClients"] = make(map[string]interface{})
// Loop over Node Endpoints
for nodeID, thisEndpoint := range dn.NodeEndpoints {
if thisEndpoint.IsServer() {
nodeClientConnections["nodeClients"][nodeID] = thisEndpoint.ConnectionStats()
}
}
// Loop over Client Endpoints
for consumerID, thisEndpoint := range dn.ConsumerEndpoints {
nodeClientConnections["consumerClients"][consumerID] = thisEndpoint.ConnectionStats()
}
return nodeClientConnections
}
// GetLocalServiceDefinitions return local service definitions
func (dn *Node) GetLocalServiceDefinitions(checkServiceName *string) map[string]ServiceDefinition {
serviceDefinitions := make(map[string]ServiceDefinition)
for serviceName, localServiceObj := range dn.Services {
if serviceName == "DRP" || checkServiceName != nil && *checkServiceName != serviceName {
continue
}
serviceDefinition := localServiceObj.GetDefinition()
serviceDefinitions[serviceName] = serviceDefinition
}
return serviceDefinitions
}
// VerifyNodeConnection tells whether or not the local Node hold the Broker role
func (dn *Node) VerifyNodeConnection(remoteNodeID string) EndpointInterface {
thisNode := dn
thisNodeEntry := thisNode.TopologyTracker.NodeTable.GetEntry(remoteNodeID).(*NodeTableEntry)
if thisNodeEntry == nil {
return nil
}
thisNodeEndpoint := thisNode.NodeEndpoints[remoteNodeID]
// Is the remote node listening? If so, try to connect
if thisNodeEndpoint == nil && thisNodeEntry.NodeURL != nil {
targetNodeURL := thisNodeEntry.NodeURL
// We have a target URL, wait a few seconds for connection to initiate
thisNode.Log(fmt.Sprintf("Connecting to Node [%s] @ '%s'", remoteNodeID, *targetNodeURL), true)
thisNodeEndpoint := &Client{}
thisNodeEndpoint.Connect(*targetNodeURL, nil, dn, &remoteNodeID, false, nil, nil)
thisNode.NodeEndpoints[remoteNodeID] = thisNodeEndpoint
for i := 0; i < 50; i++ {
// Are we still trying?
if thisNodeEndpoint.IsConnecting() {
// Yes - wait
time.Sleep(100 * time.Millisecond)
} else {
// No - break the for loop
break
}
}
}
// If this node is listening, try sending a back connection request to the remote node via the registry
if (thisNodeEndpoint != nil || !thisNodeEndpoint.IsReady()) && thisNode.listeningName != nil {
thisNode.Log("Sending back request...", true)
// Let's try having the Provider call us; send command through Registry
// Get next hop
nextHopNodeID := thisNode.TopologyTracker.GetNextHop(remoteNodeID)
if nextHopNodeID != nil {
// Found the next hop
thisNode.Log(fmt.Sprintf("Sending back request to %s, relaying to [%s]", remoteNodeID, *nextHopNodeID), true)
routeOptions := RouteOptions{
&thisNode.NodeID,
&remoteNodeID,
[]string{},
}
cmdParams := make(map[string]string)
cmdParams["targetNodeID"] = thisNode.NodeID
cmdParams["targetURL"] = *thisNode.listeningName
thisNode.NodeEndpoints[*nextHopNodeID].SendCmd("DRP", "connectToNode", cmdParams, nil, &routeOptions, nil)
} else {
// Could not find the next hop
thisNode.Log(fmt.Sprintf("Could not find next hop to [%s]", remoteNodeID), false)
}
thisNode.Log("Starting wait...", true)
// Wait a few seconds
for i := 0; i < 50; i++ {
// Are we still trying?
newEndpoint, ok := thisNode.NodeEndpoints[remoteNodeID]
if !ok || !newEndpoint.IsReady() {
// Yes - wait
time.Sleep(100 * time.Millisecond)
} else {
// No - break the for loop
thisNode.Log(fmt.Sprintf("Received back connection from remote node [%s]", remoteNodeID), true)
i = 50
}
}
// If still not successful, delete DRP_NodeClient
newEndpoint, ok := thisNode.NodeEndpoints[remoteNodeID]
if !ok || !newEndpoint.IsReady() {
thisNode.Log(fmt.Sprintf("Could not open connection to Node [%s]", remoteNodeID), true)
if ok {
delete(thisNode.NodeEndpoints, remoteNodeID)
}
//throw new Error(`Could not get connection to Provider ${remoteNodeID}`);
} else {
thisNodeEndpoint = thisNode.NodeEndpoints[remoteNodeID]
}
}
return thisNodeEndpoint
}