Pārlūkot izejas kodu

cmRtNet.h/c: Implement simplified net synch. protocol.

master
kpl 11 gadus atpakaļ
vecāks
revīzija
44900567b8
2 mainītis faili ar 194 papildinājumiem un 341 dzēšanām
  1. 186
    323
      cmRtNet.c
  2. 8
    18
      cmRtNet.h

+ 186
- 323
cmRtNet.c Parādīt failu

11
 
11
 
12
 enum
12
 enum
13
 {
13
 {
14
-  kLocalNetFl    = 0x01,
15
-  kSockAddrNetFl = 0x02,
16
-  kRegNodeNetFl  = 0x04,
17
-  kRecvNodeNetFl = 0x08
14
+  kLocalNodeNetFl = 0x01,
15
+  kValidNodeNetFl = 0x02
18
 };
16
 };
19
 
17
 
20
 typedef enum
18
 typedef enum
21
 {
19
 {
22
-  kSendHelloStNetId = 0,
23
-  kWaitHelloAckStNetId,
24
-  kSendEndpointStNetId,
25
-  kWaitEndpointAckStNetId,
26
-  kDoneStNetId,
27
-  kErrorStNetId,
28
-  kInvalidStNetId,
29
-} cmRtNetNodeState_t;
30
-
31
-typedef enum
32
-{
33
   kHelloSelNetId,
20
   kHelloSelNetId,
34
-  kHelloAckSelNetId,
21
+  kNodeSelNetId,
35
   kEndpointSelNetId,
22
   kEndpointSelNetId,
36
   kEndpointAckSelNetId,
23
   kEndpointAckSelNetId,
37
-  kDoneSelNetId
24
+  kDoneSelNetId,
38
 } cmRtNetSelId_t;
25
 } cmRtNetSelId_t;
39
 
26
 
40
 typedef struct cmRtNetEnd_str
27
 typedef struct cmRtNetEnd_str
41
 {
28
 {
42
-  cmChar_t*              endPtLabel;
43
-  unsigned               endPtId;
29
+  cmChar_t*              label;
30
+  unsigned               id;
44
   struct cmRtNetEnd_str* link;
31
   struct cmRtNetEnd_str* link;
45
 } cmRtNetEnd_t;
32
 } cmRtNetEnd_t;
46
 
33
 
52
   cmChar_t*               addr;
39
   cmChar_t*               addr;
53
   cmUdpPort_t             port;
40
   cmUdpPort_t             port;
54
   unsigned                flags;
41
   unsigned                flags;
55
-  cmRtNetNodeState_t      state;
56
-  unsigned                epIdx;         // tracks the next endpoint to send during sync-mode
42
+  unsigned                endPtIdx;       // tracks the next endpoint to send during sync-mode
43
+  unsigned                endPtCnt;       // local-node=actual cnt of endpt's remote-node:expected cnt of endpt's
57
   cmTimeSpec_t            lastSendTime;
44
   cmTimeSpec_t            lastSendTime;
58
   cmRtNetEnd_t*           ends;
45
   cmRtNetEnd_t*           ends;
59
   struct cmRtNetNode_str* link;
46
   struct cmRtNetNode_str* link;
67
   void*           cbArg;
54
   void*           cbArg;
68
   cmRtNetNode_t*  nodes;
55
   cmRtNetNode_t*  nodes;
69
   cmRtNetNode_t*  localNode;
56
   cmRtNetNode_t*  localNode;
70
-  bool            syncModeFl;
71
-  bool            masterFl;
72
   unsigned        udpRecvBufByteCnt;
57
   unsigned        udpRecvBufByteCnt;
73
   unsigned        udpTimeOutMs;
58
   unsigned        udpTimeOutMs;
74
   unsigned        interSyncSendTimeMs;
59
   unsigned        interSyncSendTimeMs;
79
 {
64
 {
80
   cmRtSysMsgHdr_t hdr;
65
   cmRtSysMsgHdr_t hdr;
81
   cmRtNetSelId_t  selId;
66
   cmRtNetSelId_t  selId;
82
-  const cmChar_t* endPtLabel;
83
-  unsigned        endPtId;
67
+  const cmChar_t* label;  // node     or endpoint label
68
+  unsigned        id;     // endptCnt or endpoint id
84
 } cmRtNetSyncMsg_t;
69
 } cmRtNetSyncMsg_t;
85
 
70
 
86
 cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
71
 cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
125
 
110
 
126
   cmRtNetNode_t* np = p->nodes;
111
   cmRtNetNode_t* np = p->nodes;
127
   for(; np!=NULL; np=np->link)
112
   for(; np!=NULL; np=np->link)
128
-    if( cmIsFlag(np->flags,kSockAddrNetFl) && np->sockaddr.sin_addr.s_addr == saddr->sin_addr.s_addr && np->sockaddr.sin_port == saddr->sin_port )
113
+    if( np->sockaddr.sin_addr.s_addr == saddr->sin_addr.s_addr && np->sockaddr.sin_port == saddr->sin_port )
129
       return np;
114
       return np;
130
   
115
   
131
   return NULL;
116
   return NULL;
137
   while( ep != NULL )
122
   while( ep != NULL )
138
   {
123
   {
139
     cmRtNetEnd_t* nep = ep->link;
124
     cmRtNetEnd_t* nep = ep->link;
140
-    cmMemFree(ep->endPtLabel);
125
+    cmMemFree(ep->label);
141
     cmMemFree(ep);
126
     cmMemFree(ep);
142
     ep = nep;
127
     ep = nep;
143
   }
128
   }
164
 
149
 
165
 cmRtNetRC_t  _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
150
 cmRtNetRC_t  _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
166
 {
151
 {
167
-  // we should never release the local node via this function
168
-  assert( np != p->localNode );
169
-
170
   cmRtNetNode_t* cnp = p->nodes;
152
   cmRtNetNode_t* cnp = p->nodes;
171
   cmRtNetNode_t* pnp = NULL;
153
   cmRtNetNode_t* pnp = NULL;
172
 
154
 
193
   return cmErrMsg(&p->err,kNodeNotFoundNetRC,"Node to release not found.");
175
   return cmErrMsg(&p->err,kNodeNotFoundNetRC,"Node to release not found.");
194
 }
176
 }
195
 
177
 
196
-cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmChar_t* addr, cmUdpPort_t port, const struct sockaddr_in* saddr, unsigned flags )
178
+cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmChar_t* addr, cmUdpPort_t port, const struct sockaddr_in* saddr, unsigned flags, unsigned endPtCnt )
197
 {
179
 {
198
   cmRtNetRC_t rc = kOkNetRC;
180
   cmRtNetRC_t rc = kOkNetRC;
199
   cmRtNetNode_t* np;
181
   cmRtNetNode_t* np;
203
 
185
 
204
   if((np = _cmRtNetFindNode(p,label)) != NULL )
186
   if((np = _cmRtNetFindNode(p,label)) != NULL )
205
     return cmErrMsg(&p->err,kDuplLabelNetRC,"The node label '%s' is already in use.",cmStringNullGuard(label));
187
     return cmErrMsg(&p->err,kDuplLabelNetRC,"The node label '%s' is already in use.",cmStringNullGuard(label));
206
-
207
-  bool localNodeFl = addr==NULL && saddr==NULL;
208
   
188
   
209
-  if( localNodeFl && p->localNode != NULL )
210
-    return cmErrMsg(&p->err,kDuplLocalNetRC,"The local node '%s' has already been set.",cmStringNullGuard(p->localNode->label));
211
-
212
-  np = cmMemAllocZ(cmRtNetNode_t,1);
213
-  np->label = cmMemAllocStr(label);
214
-  np->addr  = addr==NULL ? NULL : cmMemAllocStr(addr);
215
-  np->port  = port;
216
-  np->flags = cmEnaFlag(flags,kLocalNetFl,localNodeFl);
217
-  np->link  = p->nodes;
218
-  p->nodes  = np;
219
-
220
-  if( localNodeFl )
221
-    p->localNode = np;
222
-
223
-  if( saddr != NULL )
224
-    np->sockaddr = *saddr;
225
-  else
226
-  {
227
-    if( cmUdpInitAddr(p->udpH, np->addr, np->port, &np->sockaddr ) != kOkUdpRC )
228
-    {
229
-      rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"IP::port to socket address conversion failed.");
230
-      goto errLabel;
231
-    }
232
-  }
233
-
234
-  np->flags = cmSetFlag(np->flags,kSockAddrNetFl);
189
+  np           = cmMemAllocZ(cmRtNetNode_t,1);
190
+  np->label    = cmMemAllocStr(label);
191
+  np->sockaddr = *saddr;
192
+  np->addr     = addr==NULL ? NULL : cmMemAllocStr(addr);
193
+  np->port     = port;
194
+  np->flags    = flags;
195
+  np->endPtCnt = endPtCnt;
196
+  np->link     = p->nodes;
197
+  p->nodes     = np;
235
 
198
 
236
- errLabel:
237
   return rc;
199
   return rc;
238
 }
200
 }
239
 
201
 
241
 {
203
 {
242
   cmRtNetEnd_t* ep = np->ends;
204
   cmRtNetEnd_t* ep = np->ends;
243
   for(; ep!=NULL; ep=ep->link)
205
   for(; ep!=NULL; ep=ep->link)
244
-    if( strcmp(ep->endPtLabel,endPtLabel)==0 )
206
+    if( strcmp(ep->label,endPtLabel)==0 )
245
       return ep;
207
       return ep;
246
   return NULL;
208
   return NULL;
247
 }
209
 }
271
   cmRtNetRC_t   rc = kOkNetRC;
233
   cmRtNetRC_t   rc = kOkNetRC;
272
   cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1);
234
   cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1);
273
 
235
 
274
-  ep->endPtLabel = cmMemAllocStr(endPtLabel);
275
-  ep->endPtId    = endPtId;
236
+  ep->label = cmMemAllocStr(endPtLabel);
237
+  ep->id    = endPtId;
276
   ep->link      = np->ends;
238
   ep->link      = np->ends;
277
   np->ends      = ep;
239
   np->ends      = ep;
278
 
240
 
280
 }
242
 }
281
 
243
 
282
 unsigned _cmRtNetSyncMsgSerialByteCount( const cmRtNetSyncMsg_t* m )
244
 unsigned _cmRtNetSyncMsgSerialByteCount( const cmRtNetSyncMsg_t* m )
283
-{ return sizeof(cmRtNetSyncMsg_t) + (m->endPtLabel==NULL ? 1 : strlen(m->endPtLabel) + 1); }
245
+{ return sizeof(cmRtNetSyncMsg_t) + (m->label==NULL ? 1 : strlen(m->label) + 1); }
284
 
246
 
285
 cmRtNetRC_t _cmRtNetSerializeSyncMsg( cmRtNet_t* p, const cmRtNetSyncMsg_t* m, void* buf, unsigned n )
247
 cmRtNetRC_t _cmRtNetSerializeSyncMsg( cmRtNet_t* p, const cmRtNetSyncMsg_t* m, void* buf, unsigned n )
286
 {
248
 {
291
     return cmErrMsg(&p->err,kBufToSmallNetRC,"Serialize buffer too small.");
253
     return cmErrMsg(&p->err,kBufToSmallNetRC,"Serialize buffer too small.");
292
 
254
 
293
   memcpy(b,m,sizeof(*m));
255
   memcpy(b,m,sizeof(*m));
294
-  strcpy(b + sizeof(*m),m->endPtLabel==NULL ? "" : m->endPtLabel);
256
+  strcpy(b + sizeof(*m),m->label==NULL ? "" : m->label);
295
   return kOkNetRC;
257
   return kOkNetRC;
296
 }
258
 }
297
 
259
 
301
   memcpy(m,buf,sizeof(*m));
263
   memcpy(m,buf,sizeof(*m));
302
   const cmRtNetSyncMsg_t* mp = (const cmRtNetSyncMsg_t*)buf;
264
   const cmRtNetSyncMsg_t* mp = (const cmRtNetSyncMsg_t*)buf;
303
   const cmChar_t*   s  = (const cmChar_t*)(mp+1);
265
   const cmChar_t*   s  = (const cmChar_t*)(mp+1);
304
-  m->endPtLabel = cmMemAllocStr(s);
266
+  m->label = cmMemAllocStr(s);
305
   return kOkNetRC;
267
   return kOkNetRC;
306
 }
268
 }
307
 
269
 
308
-cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* endPtLabel, unsigned endPtId, cmRtNetNodeState_t nextStId )
270
+cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* msgLabel, unsigned msgId )
309
 {
271
 {
310
   cmRtNetSyncMsg_t m;
272
   cmRtNetSyncMsg_t m;
311
   cmRtNetRC_t      rc = kOkNetRC;
273
   cmRtNetRC_t      rc = kOkNetRC;
314
   m.hdr.rtSubIdx = cmInvalidIdx;
276
   m.hdr.rtSubIdx = cmInvalidIdx;
315
   m.hdr.selId    = kNetSyncSelRtId;
277
   m.hdr.selId    = kNetSyncSelRtId;
316
   m.selId        = selId;
278
   m.selId        = selId;
317
-  m.endPtLabel    = endPtLabel;
318
-  m.endPtId       = endPtId;
279
+  m.label        = msgLabel;
280
+  m.id           = msgId;
319
 
281
 
320
   // determine size of msg to send
282
   // determine size of msg to send
321
   unsigned n  = _cmRtNetSyncMsgSerialByteCount(&m);
283
   unsigned n  = _cmRtNetSyncMsgSerialByteCount(&m);
325
   if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
287
   if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
326
     return rc;
288
     return rc;
327
 
289
 
328
-  // store this nodes current sync state
329
-  cmRtNetNodeState_t orgState = np->state;
330
-
331
-  if( nextStId != kInvalidStNetId )
332
-    np->state = nextStId;
333
-
334
  
290
  
335
   // send the msg
291
   // send the msg
336
-  if( cmIsFlag(np->flags,kSockAddrNetFl) == false )
337
-    udpRC = cmUdpSend2(p->udpH, buf, n, np->addr, np->port );
292
+  if( np==p->localNode )
293
+    udpRC = cmUdpSend2(p->udpH, buf, n, "255.255.255.255", np->port );
338
   else
294
   else
339
     udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
295
     udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
340
 
296
 
342
   if( udpRC != kOkUdpRC )
298
   if( udpRC != kOkUdpRC )
343
   {
299
   {
344
     rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"Sync msg. send on UDP port failed.");
300
     rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"Sync msg. send on UDP port failed.");
345
-    np->state = orgState;  // restore node state so we can try again
346
   }
301
   }
347
   else
302
   else
348
   {
303
   {
431
   return p->udpH;
386
   return p->udpH;
432
 }
387
 }
433
 
388
 
434
-
435
-cmRtNetRC_t cmRtNetCreateNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
389
+cmRtNetRC_t  _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np )
436
 {
390
 {
437
-  cmRtNet_t*  p = _cmRtNetHandleToPtr(h);
438
-  cmRtNetRC_t rc;
391
+  cmRtNetRC_t     rc = kOkNetRC;
392
+  cmRtNetEnd_t*   ep;
393
+  const cmChar_t* msgLabel = NULL;
394
+  unsigned        msgId    = cmInvalidId;  
395
+  cmRtNetSelId_t  selId    = kEndpointSelNetId;
396
+  const cmChar_t* rptLabel = "endpoint";
439
 
397
 
440
-  // create a node
441
-  if((rc = _cmRtNetCreateNode(p,nodeLabel,ipAddr, port, NULL, kRegNodeNetFl)) != kOkNetRC )
442
-    return rc;
398
+  if( np == NULL )
399
+    return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found.");
443
 
400
 
444
-  // if this is not the local node
445
-  if( ipAddr != NULL )
446
-    return rc;
447
-
448
-  // if this is the local node then initialze the local socket
449
-  if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl,p->cbFunc,p->cbArg,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
401
+  // if all of the endpoints have been sent to this node ...
402
+  if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) == NULL )
450
   {
403
   {
451
-    rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port initialization failed.");
452
-    goto errLabel;
453
-  }
454
-
455
-  // begin listening on the local port
456
-  if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
457
-  {
458
-    rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port failed to enter 'listen' mode.");
459
-    goto errLabel;
460
-  }
461
-
462
-
463
- errLabel:
464
-  return rc;
465
-}
466
-
467
-cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId )
468
-{
469
-  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
470
-
471
-  if( p->localNode == NULL )
472
-    return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
473
-
474
-  return _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId );
475
-
476
-}
477
-
478
-cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h )
479
-{
480
-  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
481
-  _cmRtNetReleaseNodes(p);
482
-  return kOkNetRC;
483
-}
484
-
485
-cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h )
486
-{
487
-  cmRtNetRC_t rc = kOkNetRC;
488
-  
489
-  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
490
-
491
-  p->syncModeFl = true;
492
-  p->masterFl   = true;
493
-  return rc;
494
-}
495
-
496
-bool      cmRtNetIsInSyncMode(  cmRtNetH_t h )
497
-{
498
-  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
499
-
500
-  return p->syncModeFl;
501
-}
502
-
503
-
504
-// Used by slaves to send the master an 'ack' msg.
505
-cmRtNetRC_t _cmRtNetSendAck( cmRtNet_t* p, cmRtNetSelId_t ackSelId, const struct sockaddr_in* saddr )
506
-{
507
-  cmRtNetNode_t* np;
508
-
509
-  if((np = _cmRtNetFindNodeFromSockAddr(p,saddr)) == NULL )
510
-    return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an ack cmd was not found. Ack not sent.");
511
-
512
-  return _cmRtNetSendSyncMsg(p,np,ackSelId,NULL,cmInvalidId,kInvalidStNetId);
513
-}
514
-
515
-// Used by master to update state upon receipt of 'ack' msg
516
-cmRtNetRC_t _cmRtNetRecvAck( cmRtNet_t* p, const struct sockaddr_in* fromAddr, cmRtNetNodeState_t expectedState, cmRtNetNodeState_t nextState )
517
-{
518
-  cmRtNetNode_t* np;
519
-  cmRtNetRC_t rc = kOkNetRC;
520
-
521
-  if((np = _cmRtNetFindNodeFromSockAddr(p,fromAddr)) == NULL )
522
-  {
523
-    rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with a  ack receive was not found.");
524
-    goto errLabel;
404
+    if( np->endPtIdx == p->localNode->endPtCnt )
405
+    {
406
+      selId = kDoneSelNetId;
407
+      rptLabel = "done";
408
+    }
409
+    else
410
+    {
411
+      selId = kEndpointAckSelNetId;
412
+      rptLabel = "ep ack";
413
+    }
414
+   
525
   }
415
   }
526
-
527
-  if( np->state != expectedState )
416
+  else
528
   {
417
   {
529
-    rc = cmErrMsg(&p->err,kNodeStateErrNetRC,"Node '%s' expected in state %i was in state %i.",cmStringNullGuard(np->label),kWaitHelloAckStNetId,np->state);
530
-    np->state = kErrorStNetId;
531
-    goto errLabel;
418
+    msgLabel = ep->label;
419
+    msgId    = ep->id;
532
   }
420
   }
533
 
421
 
534
-  np->state = nextState;
422
+  // notify the remote node that all endpoints have been sent
423
+  if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId )) != kOkNetRC )
424
+    rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
425
+  else
426
+    _cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel));
535
 
427
 
536
-  // if we are about to send another endpoint - incr the endpoint index
537
-  if( nextState == kSendEndpointStNetId )
538
-    np->epIdx += 1;
428
+  np->endPtIdx += 1;
539
 
429
 
540
- errLabel:
541
   return rc;
430
   return rc;
431
+  
542
 }
432
 }
543
 
433
 
544
-
545
-cmRtNetRC_t  cmRtNetSyncModeRecv( cmRtNetH_t h, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
434
+// When the network message recieve function (See cmRtNetAlloc() 'cbFunc') 
435
+// receives a message with the cmRtSysMsgHdr_t.selId == kNetSyncSelRtId
436
+// it should call this function to update the current sync state of the
437
+// cmRtNet.
438
+cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
546
 {
439
 {
547
-  cmRtNet_t*       p  = _cmRtNetHandleToPtr(h);
548
   cmRtNetRC_t      rc = kOkNetRC;
440
   cmRtNetRC_t      rc = kOkNetRC;
549
-  cmRtNetNode_t*   np = NULL;
550
   cmRtNetSyncMsg_t m;
441
   cmRtNetSyncMsg_t m;
551
-
552
-  m.endPtLabel = NULL;
442
+  m.label = NULL;
553
 
443
 
554
   assert( cmRtNetIsSyncModeMsg(data,dataByteCnt));
444
   assert( cmRtNetIsSyncModeMsg(data,dataByteCnt));
555
   
445
   
561
 
451
 
562
   assert( m.hdr.selId == kNetSyncSelRtId );
452
   assert( m.hdr.selId == kNetSyncSelRtId );
563
 
453
 
454
+  // attempt to locate the remote node which sent the msg
455
+  cmRtNetNode_t* np = _cmRtNetFindNodeFromSockAddr(p,fromAddr);
456
+
564
   switch( m.selId )
457
   switch( m.selId )
565
   {
458
   {
566
-    
567
-    case kHelloSelNetId: // slave response
459
+    case kHelloSelNetId:    
460
+    case kNodeSelNetId: 
568
       {
461
       {
569
-        _cmRtNetRpt(p,"rcv hello\n");
570
-
571
-        // attempt to locate the remote node which sent the endpoint 
572
-        if((np = _cmRtNetFindNodeFromSockAddr(p,fromAddr)) != NULL )
462
+        // if the node already exists ...
463
+        if( np != NULL )
573
         {
464
         {
574
-          // delete the existing node because we are about to get new info. about it.
465
+          // ... delete it because we are about to get new info. about it.
575
           if((rc =  _cmRtNetReleaseNode(p,np )) != kOkNetRC )
466
           if((rc =  _cmRtNetReleaseNode(p,np )) != kOkNetRC )
576
             goto errLabel;
467
             goto errLabel;
577
         }
468
         }
578
 
469
 
579
         //  create a node proxy to represent the remote node
470
         //  create a node proxy to represent the remote node
580
-        if(( rc = _cmRtNetCreateNode(p,m.endPtLabel,NULL,0,fromAddr,kRecvNodeNetFl)) != kOkNetRC )
581
-          goto errLabel;
471
+        // (Note:m.id == remote node endpoint count (i.e. the count of endpoints expected for the remote node.))
472
+        if(( rc = _cmRtNetCreateNode(p,m.label,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
473
+          goto errLabel;        
582
 
474
 
583
-        // send an ackknowledgement of the 'hello' msg
584
-        rc = _cmRtNetSendAck(p,kHelloAckSelNetId,fromAddr);
475
+        // send response
476
+        switch( m.selId )
477
+        {
478
+          case kHelloSelNetId:
479
+            _cmRtNetRpt(p,"rcv hello\n"); // reply with local node
480
+            rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, NULL, p->localNode->endPtCnt );
481
+            break;
482
+
483
+          case kNodeSelNetId:
484
+            _cmRtNetRpt(p,"rcv node\n");
485
+            _cmRtNetSendEndpointReplyMsg( p, np ); // reply with first endpoint
486
+            break;
487
+
488
+          default:
489
+            assert(0);
490
+        }
585
 
491
 
586
       }
492
       }
587
       break;
493
       break;
588
 
494
 
589
     
495
     
590
-    case kEndpointSelNetId: // slave response
496
+    case kEndpointAckSelNetId:
497
+    case kDoneSelNetId:
498
+      rc = _cmRtNetSendEndpointReplyMsg(p,np);
499
+      break;
500
+
501
+    case kEndpointSelNetId: 
591
       {
502
       {
592
         cmRtNetEnd_t* ep;
503
         cmRtNetEnd_t* ep;
593
 
504
 
594
-        _cmRtNetRpt(p,"rcv endpoint\n");
595
 
505
 
596
-        // locate the remote node which sent the endpoint
597
-        if((np = _cmRtNetFindNodeFromSockAddr(p,fromAddr)) == NULL )
506
+        // verify the remote node exists.
507
+        if( np == NULL )
598
         {
508
         {
599
           rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
509
           rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
600
           goto errLabel;
510
           goto errLabel;
601
         }
511
         }
602
         
512
         
603
         // attempt to find the end point 
513
         // attempt to find the end point 
604
-        if((ep = _cmRtNetFindNodeEnd(np,m.endPtLabel)) != NULL )
605
-          ep->endPtId = m.endPtId; // the endpoint was found update the endPtId
514
+        if((ep = _cmRtNetFindNodeEnd(np,m.label)) != NULL )
515
+          ep->id = m.id; // the endpoint was found update the endPtId
606
         else
516
         else
607
         {
517
         {
608
           // create a local proxy for the endpoint
518
           // create a local proxy for the endpoint
609
-          if((rc = _cmRtNetCreateEndpoint(p,np,m.endPtLabel,m.endPtId)) != kOkNetRC )
519
+          if((rc = _cmRtNetCreateEndpoint(p,np,m.label,m.id)) != kOkNetRC )
610
             goto errLabel;
520
             goto errLabel;
611
         }
521
         }
612
 
522
 
613
-        // ack. the endpoint msg
614
-        rc = _cmRtNetSendAck(p,kEndpointAckSelNetId,fromAddr);
615
-      }
616
-      break;
617
-
618
-    case kDoneSelNetId:
619
-      {
620
-        _cmRtNetRpt(p,"rcv done\n");
621
-
622
-        if( p->masterFl==false  )
623
-          p->syncModeFl = true;
624
-      }
625
-      break;
626
-
627
-    case kHelloAckSelNetId: // master response
628
-      {
629
-        assert( p->syncModeFl );
630
-        _cmRtNetRpt(p,"rcv hello ack\n");
631
-        rc = _cmRtNetRecvAck(p,fromAddr,kWaitHelloAckStNetId,kSendEndpointStNetId);
632
-      }
633
-      break;
634
-
635
-    case kEndpointAckSelNetId: // master response
636
-      {
637
-        assert( p->syncModeFl );  
638
-        _cmRtNetRpt(p,"rcv endpoint ack\n");
639
-        rc = _cmRtNetRecvAck(p,fromAddr,kWaitEndpointAckStNetId,kSendEndpointStNetId);
523
+        // reply with a local endpoint or 'done' msg
524
+        rc = _cmRtNetSendEndpointReplyMsg( p, np );
640
       }
525
       }
641
       break;
526
       break;
642
 
527
 
643
     default:
528
     default:
529
+      assert(0);
644
       break;
530
       break;
645
   }
531
   }
646
 
532
 
647
  errLabel:
533
  errLabel:
648
 
534
 
649
-  cmMemFree((cmChar_t*)m.endPtLabel);
535
+  cmMemFree((cmChar_t*)m.label);
650
   return rc;
536
   return rc;
651
 }
537
 }
652
 
538
 
539
+void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
540
+{
541
+  cmRtNet_t* p = (cmRtNet_t*)cbArg;
542
+  
543
+  if( cmRtNetIsSyncModeMsg(data,dataByteCnt))
544
+    _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
545
+  else
546
+    p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
547
+  
548
+}
549
+
550
+
653
 
551
 
654
-cmRtNetRC_t _cmRtNetSendNodeSync( cmRtNet_t* p, cmRtNetNode_t* np )
552
+cmRtNetRC_t cmRtNetRegisterLocalNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
655
 {
553
 {
656
-  cmRtNetRC_t rc = kOkNetRC;
554
+  cmRtNet_t*  p = _cmRtNetHandleToPtr(h);
555
+  cmRtNetRC_t rc;
556
+  struct sockaddr_in sockaddr;
557
+
558
+  // release the local node
559
+  if( p->localNode != NULL )
560
+  {    
561
+    _cmRtNetReleaseNode(p,p->localNode);
562
+    p->localNode = NULL;
563
+  }
657
 
564
 
658
-  switch( np->state )
565
+  // if this is the local node then initialze the local socket
566
+  if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl,_cmRtNetRecv,p,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
659
   {
567
   {
660
-    case kSendHelloStNetId:
661
-      {
662
-        np->epIdx = -1;
568
+    rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port initialization failed.");
569
+    goto errLabel;
570
+  }
663
 
571
 
664
-        // send a 'hello' to this remote node
665
-        if((rc = _cmRtNetSendSyncMsg(p,np,kHelloSelNetId,p->localNode->label, cmInvalidId, kWaitHelloAckStNetId )) != kOkNetRC )
666
-          rc = cmErrMsg(&p->err,rc,"Send 'hello' to %s:%s:%i failed.",cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
667
-        else
668
-          _cmRtNetRpt(p,"%s sent hello\n",cmStringNullGuard(np->label));
669
-      }
670
-      break;
572
+  // get the socket address
573
+  if( cmUdpInitAddr(p->udpH, ipAddr, port, &sockaddr ) != kOkUdpRC )
574
+  {
575
+    rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"IP::port to socket address conversion failed.");
576
+    goto errLabel;
577
+  }
671
 
578
 
672
-    case kSendEndpointStNetId:
673
-      {
674
-        cmRtNetEnd_t* ep;
579
+  // create the local node
580
+  if((rc = _cmRtNetCreateNode(p,nodeLabel, ipAddr, port, &sockaddr, kLocalNodeNetFl, 0)) != kOkNetRC )
581
+    goto errLabel;
675
 
582
 
676
-        // if all of the endpoints have been sent to this node ...
677
-        if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->epIdx)) == NULL )
678
-        {
679
-          // notify the remote node that all endpoints have been sent
680
-          if((rc = _cmRtNetSendSyncMsg(p,np,kDoneSelNetId,p->localNode->label,cmInvalidId, kDoneStNetId )) != kOkNetRC )
681
-            rc = cmErrMsg(&p->err,rc,"Send 'done' to %s:%s:%i failed.",cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
682
-          else
683
-            _cmRtNetRpt(p,"Node %s done.\n",cmStringNullGuard(np->label));
684
-        }
685
-        else
686
-        {
687
-          // send an endpoint to this node 
688
-          if((rc = _cmRtNetSendSyncMsg(p,np,kEndpointSelNetId,ep->endPtLabel, ep->endPtId, kWaitEndpointAckStNetId )) != kOkNetRC )
689
-            rc = cmErrMsg(&p->err,rc,"Endpoint (%s index:%i) transmission to %s:%s:%i failed.",cmStringNullGuard(ep->endPtLabel),cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
690
-          else
691
-            _cmRtNetRpt(p,"%s sent endpoint %s\n",cmStringNullGuard(np->label),cmStringNullGuard(ep->endPtLabel));
583
+  // the last created node is always the first node on the list
584
+  p->localNode = p->nodes;
692
 
585
 
693
-        }
694
-      }
695
-      break;
586
+  // begin listening on the local port
587
+  if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
588
+  {
589
+    rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port failed to enter 'listen' mode.");
590
+    goto errLabel;
591
+  }
696
 
592
 
697
-    case kWaitHelloAckStNetId:
698
-    case kWaitEndpointAckStNetId:
699
-      {
700
-        cmTimeSpec_t t;
701
-        cmTimeGet(&t);
702
-        unsigned twentySecs = 20000000;
703
-        if( cmTimeElapsedMicros(&np->lastSendTime,&t) > twentySecs)
704
-        {
705
-          const cmChar_t* ackStr = np->state==kWaitHelloAckStNetId ? "hello" : "endpoint";
706
-          rc = cmErrMsg(&p->err,kTimeOutErrNetRC,"The node %s:%s:%i did not give a '%s' acknowledge.",cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port,ackStr);         
707
-        }
708
-      }
709
-      break;
710
 
593
 
711
-    default:
712
-      break;
713
-  }
714
-      
715
-  // if an error occurred put the node into an error state
716
-  if( rc != kOkNetRC )
717
-    np->state = kErrorStNetId;
718
-      
594
+ errLabel:
719
   return rc;
595
   return rc;
720
 }
596
 }
721
 
597
 
722
-
723
-
724
-cmRtNetRC_t cmRtNetSyncModeSend( cmRtNetH_t h )
598
+cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId )
725
 {
599
 {
726
   cmRtNetRC_t rc = kOkNetRC;
600
   cmRtNetRC_t rc = kOkNetRC;
727
-  cmRtNet_t*  p  = _cmRtNetHandleToPtr(h);
601
+  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
728
 
602
 
729
-  if( p->syncModeFl == false )
730
-    return rc;
731
-  
732
-  unsigned     activeCnt = 0;
733
-  cmRtNetNode_t* np        = p->nodes;
734
-  for(; np != NULL; np=np->link )
735
-  {
736
-    bool fl = (p->masterFl && cmIsFlag(np->flags,kRegNodeNetFl)) || (p->masterFl==false && cmIsFlag(np->flags,kRecvNodeNetFl));
603
+  if( p->localNode == NULL )
604
+    return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
737
 
605
 
738
-    if( fl && np != p->localNode && np->state != kDoneStNetId && np->state != kErrorStNetId  )
739
-    {
740
-      _cmRtNetSendNodeSync(p,np);
741
-      activeCnt += 1;
742
-    }
743
-  }
744
- 
745
-  if( activeCnt == 0 )
746
-  {
747
-    p->syncModeFl = false;
748
-    _cmRtNetRpt(p,"sync mode complete.\n");
749
-  }
606
+  if((rc = _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId )) == kOkNetRC )
607
+    p->localNode->endPtCnt += 1;
750
 
608
 
751
   return rc;
609
   return rc;
752
 }
610
 }
753
 
611
 
612
+cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h )
613
+{
614
+  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
615
+  _cmRtNetReleaseNodes(p);
616
+  return kOkNetRC;
617
+}
618
+
619
+cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h )
620
+{
621
+  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
622
+
623
+  // broadcast 'node' msg
624
+  return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, NULL, p->localNode->endPtCnt );
625
+}
626
+
627
+
754
 
628
 
755
 cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
629
 cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
756
 {
630
 {
791
   cmRtNet_t* p = _cmRtNetHandleToPtr(h);
665
   cmRtNet_t* p = _cmRtNetHandleToPtr(h);
792
   cmRpt_t* rpt = p->err.rpt;
666
   cmRpt_t* rpt = p->err.rpt;
793
 
667
 
794
-  cmRptPrintf(rpt,"Sync Mode:%s\n",p->syncModeFl ? "ON" : "OFF");
795
 
668
 
796
   cmRtNetNode_t* np = p->nodes;
669
   cmRtNetNode_t* np = p->nodes;
797
   for(; np!=NULL; np=np->link)
670
   for(; np!=NULL; np=np->link)
801
     if( np->addr != NULL )
674
     if( np->addr != NULL )
802
       cmRptPrintf(rpt,"%s ",np->addr );
675
       cmRptPrintf(rpt,"%s ",np->addr );
803
 
676
 
804
-    if( cmIsFlag(np->flags,kLocalNetFl) )
677
+    if( cmIsFlag(np->flags,kLocalNodeNetFl) )
805
       cmRptPrintf(rpt,"LOCAL ");
678
       cmRptPrintf(rpt,"LOCAL ");
806
 
679
 
807
-    if( cmIsFlag(np->flags,kSockAddrNetFl) )
808
-      cmRptPrintf(rpt,"%s ",cmStringNullGuard(cmUdpAddrToString(p->udpH,&np->sockaddr)));
680
+    cmRptPrintf(rpt,"%s ",cmStringNullGuard(cmUdpAddrToString(p->udpH,&np->sockaddr)));
809
 
681
 
810
     if( np->port != cmInvalidId )
682
     if( np->port != cmInvalidId )
811
       cmRptPrintf(rpt,"%i ",np->port );
683
       cmRptPrintf(rpt,"%i ",np->port );
815
     cmRtNetEnd_t* ep = np->ends;
687
     cmRtNetEnd_t* ep = np->ends;
816
     for(; ep!=NULL; ep=ep->link)
688
     for(; ep!=NULL; ep=ep->link)
817
     {
689
     {
818
-      cmRptPrintf(rpt,"  endpt: %i %s\n",ep->endPtId,cmStringNullGuard(ep->endPtLabel));
690
+      cmRptPrintf(rpt,"  endpt: %i %s\n",ep->id,cmStringNullGuard(ep->label ));
819
     }
691
     }
820
   }
692
   }
821
 }
693
 }
832
 
704
 
833
 void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
705
 void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
834
 {
706
 {
835
-  _cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
707
+  //_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
836
 
708
 
837
-  if( cmRtNetIsSyncModeMsg(data,dataByteCnt))
838
-    cmRtNetSyncModeRecv(p->netH,data,dataByteCnt,fromAddr);
839
 
709
 
840
 }
710
 }
841
 
711
 
846
 
716
 
847
   if( cmRtNetIsValid(p->netH) )
717
   if( cmRtNetIsValid(p->netH) )
848
   {
718
   {
849
-    if( cmRtNetIsInSyncMode(p->netH) )
850
-      cmRtNetSyncModeSend(p->netH);
851
-
852
     cmRtNetReceive(p->netH);
719
     cmRtNetReceive(p->netH);
853
   }
720
   }
854
 
721
 
867
   cmRtNetRC_t     rc   = kOkNetRC;
734
   cmRtNetRC_t     rc   = kOkNetRC;
868
   memset(&t,0,sizeof(t));
735
   memset(&t,0,sizeof(t));
869
 
736
 
870
-
871
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
737
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
872
     goto errLabel;
738
     goto errLabel;
873
 
739
 
878
   if( hostNameStr == NULL )
744
   if( hostNameStr == NULL )
879
     hostNameStr = "<no-host-name>";
745
     hostNameStr = "<no-host-name>";
880
 
746
 
881
-  if((rc = cmRtNetCreateNode(p->netH, hostNameStr, NULL, port )) != kOkNetRC)
747
+  if((rc = cmRtNetRegisterLocalNode(p->netH, hostNameStr, NULL, port )) != kOkNetRC)
882
     goto errLabel;
748
     goto errLabel;
883
 
749
 
884
   if( mstrFl )
750
   if( mstrFl )
885
   {
751
   {
886
-    if((rc = cmRtNetCreateNode(p->netH,"whirl", "192.168.15.109", port )) != kOkNetRC )
887
-      goto errLabel;
888
-
889
     if((rc = cmRtNetRegisterEndPoint(p->netH,"thunk_ep0", 0 )) != kOkNetRC )
752
     if((rc = cmRtNetRegisterEndPoint(p->netH,"thunk_ep0", 0 )) != kOkNetRC )
890
       goto errLabel;
753
       goto errLabel;
891
 
754
 

+ 8
- 18
cmRtNet.h Parādīt failu

41
   // Set 'ipAddr' to NULL if this is the local node.
41
   // Set 'ipAddr' to NULL if this is the local node.
42
   // During sync mode this node will attempt to sync with all
42
   // During sync mode this node will attempt to sync with all
43
   // nodes in the node list.
43
   // nodes in the node list.
44
-  cmRtNetRC_t cmRtNetCreateNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
44
+  cmRtNetRC_t cmRtNetRegisterLocalNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
45
 
45
 
46
 
46
 
47
   // Register the local endpoints.
47
   // Register the local endpoints.
63
   // an endpoint it updates it's own remote node/endpoint 
63
   // an endpoint it updates it's own remote node/endpoint 
64
   // list.
64
   // list.
65
   cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h );
65
   cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h );
66
-  bool        cmRtNetIsInSyncMode(  cmRtNetH_t h );
67
 
66
 
68
-  // When the network message recieve function (See cmRtNetAlloc() 'cbFunc') 
69
-  // receives a message with the cmRtSysMsgHdr_t.selId == kNetSyncSelRtId
70
-  // it should call this function to update the current sync state of the
71
-  // cmRtNet.
72
-  cmRtNetRC_t  cmRtNetSyncModeRecv( cmRtNetH_t h, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr );
73
-
74
-  
75
-  // When in the network is in sync mode (cmRtNetIsSync()==true) 
76
-  // the client system must poll this function to update the networks sync state.
77
-  cmRtNetRC_t cmRtNetSyncModeSend( cmRtNetH_t h );
78
 
67
 
79
   // This function must be polled to receive incoming network messages
68
   // This function must be polled to receive incoming network messages
80
   // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc()
69
   // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc()
131
        
120
        
132
 
121
 
133
    Protocol:
122
    Protocol:
134
-     1. A: broadcast - 'hello'
135
-     2. Bs: respond 'hello' ack
136
-     3. A: send local node and endpoints to each responder
137
-     4. A: send done
138
-     5. Bs: send local endpoints to A
139
-
123
+     1.  A: on init bcast 'hello'
124
+     2.  B: on 'hello'     -  create node-A w/ ei=0      - send 'node'
125
+     3.  A: on 'node'      -  create node-B w/ ei=0      - send first 'endpt'
126
+     4.  B: on 'endpt'     -  create endpt on node-A     - ei!=en ? send 'endpt' or send 'done'
127
+     5.  A: on 'endpt'     -  create endpt on node-B     - ei!=en ? send 'endpt' or send 'done'
128
+     6.  B: on 'done'      -  mark node-A as 'valid'
129
+     7.  A: on 'done'      -  mark node-B as 'valid'.
140
 
130
 
141
    */  
131
    */  
142
 
132
 

Notiek ielāde…
Atcelt
Saglabāt