Browse Source

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

master
kpl 11 years ago
parent
commit
44900567b8
2 changed files with 194 additions and 341 deletions
  1. 186
    323
      cmRtNet.c
  2. 8
    18
      cmRtNet.h

+ 186
- 323
cmRtNet.c View File

@@ -11,36 +11,23 @@
11 11
 
12 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 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 20
   kHelloSelNetId,
34
-  kHelloAckSelNetId,
21
+  kNodeSelNetId,
35 22
   kEndpointSelNetId,
36 23
   kEndpointAckSelNetId,
37
-  kDoneSelNetId
24
+  kDoneSelNetId,
38 25
 } cmRtNetSelId_t;
39 26
 
40 27
 typedef struct cmRtNetEnd_str
41 28
 {
42
-  cmChar_t*              endPtLabel;
43
-  unsigned               endPtId;
29
+  cmChar_t*              label;
30
+  unsigned               id;
44 31
   struct cmRtNetEnd_str* link;
45 32
 } cmRtNetEnd_t;
46 33
 
@@ -52,8 +39,8 @@ typedef struct cmRtNetNode_str
52 39
   cmChar_t*               addr;
53 40
   cmUdpPort_t             port;
54 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 44
   cmTimeSpec_t            lastSendTime;
58 45
   cmRtNetEnd_t*           ends;
59 46
   struct cmRtNetNode_str* link;
@@ -67,8 +54,6 @@ typedef struct
67 54
   void*           cbArg;
68 55
   cmRtNetNode_t*  nodes;
69 56
   cmRtNetNode_t*  localNode;
70
-  bool            syncModeFl;
71
-  bool            masterFl;
72 57
   unsigned        udpRecvBufByteCnt;
73 58
   unsigned        udpTimeOutMs;
74 59
   unsigned        interSyncSendTimeMs;
@@ -79,8 +64,8 @@ typedef struct
79 64
 {
80 65
   cmRtSysMsgHdr_t hdr;
81 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 69
 } cmRtNetSyncMsg_t;
85 70
 
86 71
 cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
@@ -125,7 +110,7 @@ cmRtNetNode_t* _cmRtNetFindNodeFromSockAddr( cmRtNet_t* p, const struct sockaddr
125 110
 
126 111
   cmRtNetNode_t* np = p->nodes;
127 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 114
       return np;
130 115
   
131 116
   return NULL;
@@ -137,7 +122,7 @@ void _cmRtNetFreeNode( cmRtNetNode_t* np )
137 122
   while( ep != NULL )
138 123
   {
139 124
     cmRtNetEnd_t* nep = ep->link;
140
-    cmMemFree(ep->endPtLabel);
125
+    cmMemFree(ep->label);
141 126
     cmMemFree(ep);
142 127
     ep = nep;
143 128
   }
@@ -164,9 +149,6 @@ void _cmRtNetReleaseNodes( cmRtNet_t* p )
164 149
 
165 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 152
   cmRtNetNode_t* cnp = p->nodes;
171 153
   cmRtNetNode_t* pnp = NULL;
172 154
 
@@ -193,7 +175,7 @@ cmRtNetRC_t  _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
193 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 180
   cmRtNetRC_t rc = kOkNetRC;
199 181
   cmRtNetNode_t* np;
@@ -203,37 +185,17 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmCha
203 185
 
204 186
   if((np = _cmRtNetFindNode(p,label)) != NULL )
205 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 199
   return rc;
238 200
 }
239 201
 
@@ -241,7 +203,7 @@ cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, const cmChar_t* endPtLabel
241 203
 {
242 204
   cmRtNetEnd_t* ep = np->ends;
243 205
   for(; ep!=NULL; ep=ep->link)
244
-    if( strcmp(ep->endPtLabel,endPtLabel)==0 )
206
+    if( strcmp(ep->label,endPtLabel)==0 )
245 207
       return ep;
246 208
   return NULL;
247 209
 }
@@ -271,8 +233,8 @@ cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmCha
271 233
   cmRtNetRC_t   rc = kOkNetRC;
272 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 238
   ep->link      = np->ends;
277 239
   np->ends      = ep;
278 240
 
@@ -280,7 +242,7 @@ cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmCha
280 242
 }
281 243
 
282 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 247
 cmRtNetRC_t _cmRtNetSerializeSyncMsg( cmRtNet_t* p, const cmRtNetSyncMsg_t* m, void* buf, unsigned n )
286 248
 {
@@ -291,7 +253,7 @@ cmRtNetRC_t _cmRtNetSerializeSyncMsg( cmRtNet_t* p, const cmRtNetSyncMsg_t* m, v
291 253
     return cmErrMsg(&p->err,kBufToSmallNetRC,"Serialize buffer too small.");
292 254
 
293 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 257
   return kOkNetRC;
296 258
 }
297 259
 
@@ -301,11 +263,11 @@ cmRtNetRC_t _cmRtNetDeserializeSyncMsg( const void* buf, unsigned n, cmRtNetSync
301 263
   memcpy(m,buf,sizeof(*m));
302 264
   const cmRtNetSyncMsg_t* mp = (const cmRtNetSyncMsg_t*)buf;
303 265
   const cmChar_t*   s  = (const cmChar_t*)(mp+1);
304
-  m->endPtLabel = cmMemAllocStr(s);
266
+  m->label = cmMemAllocStr(s);
305 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 272
   cmRtNetSyncMsg_t m;
311 273
   cmRtNetRC_t      rc = kOkNetRC;
@@ -314,8 +276,8 @@ cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t
314 276
   m.hdr.rtSubIdx = cmInvalidIdx;
315 277
   m.hdr.selId    = kNetSyncSelRtId;
316 278
   m.selId        = selId;
317
-  m.endPtLabel    = endPtLabel;
318
-  m.endPtId       = endPtId;
279
+  m.label        = msgLabel;
280
+  m.id           = msgId;
319 281
 
320 282
   // determine size of msg to send
321 283
   unsigned n  = _cmRtNetSyncMsgSerialByteCount(&m);
@@ -325,16 +287,10 @@ cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t
325 287
   if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
326 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 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 294
   else
339 295
     udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
340 296
 
@@ -342,7 +298,6 @@ cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t
342 298
   if( udpRC != kOkUdpRC )
343 299
   {
344 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 302
   else
348 303
   {
@@ -431,125 +386,60 @@ cmUdpH_t  cmRtNetUdpPortHandle( cmRtNetH_t h )
431 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 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 440
   cmRtNetRC_t      rc = kOkNetRC;
549
-  cmRtNetNode_t*   np = NULL;
550 441
   cmRtNetSyncMsg_t m;
551
-
552
-  m.endPtLabel = NULL;
442
+  m.label = NULL;
553 443
 
554 444
   assert( cmRtNetIsSyncModeMsg(data,dataByteCnt));
555 445
   
@@ -561,196 +451,180 @@ cmRtNetRC_t  cmRtNetSyncModeRecv( cmRtNetH_t h, const char* data, unsigned dataB
561 451
 
562 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 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 466
           if((rc =  _cmRtNetReleaseNode(p,np )) != kOkNetRC )
576 467
             goto errLabel;
577 468
         }
578 469
 
579 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 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 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 509
           rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
600 510
           goto errLabel;
601 511
         }
602 512
         
603 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 516
         else
607 517
         {
608 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 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 526
       break;
642 527
 
643 528
     default:
529
+      assert(0);
644 530
       break;
645 531
   }
646 532
 
647 533
  errLabel:
648 534
 
649
-  cmMemFree((cmChar_t*)m.endPtLabel);
535
+  cmMemFree((cmChar_t*)m.label);
650 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 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 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 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 629
 cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
756 630
 {
@@ -791,7 +665,6 @@ void   cmRtNetReport( cmRtNetH_t h )
791 665
   cmRtNet_t* p = _cmRtNetHandleToPtr(h);
792 666
   cmRpt_t* rpt = p->err.rpt;
793 667
 
794
-  cmRptPrintf(rpt,"Sync Mode:%s\n",p->syncModeFl ? "ON" : "OFF");
795 668
 
796 669
   cmRtNetNode_t* np = p->nodes;
797 670
   for(; np!=NULL; np=np->link)
@@ -801,11 +674,10 @@ void   cmRtNetReport( cmRtNetH_t h )
801 674
     if( np->addr != NULL )
802 675
       cmRptPrintf(rpt,"%s ",np->addr );
803 676
 
804
-    if( cmIsFlag(np->flags,kLocalNetFl) )
677
+    if( cmIsFlag(np->flags,kLocalNodeNetFl) )
805 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 682
     if( np->port != cmInvalidId )
811 683
       cmRptPrintf(rpt,"%i ",np->port );
@@ -815,7 +687,7 @@ void   cmRtNetReport( cmRtNetH_t h )
815 687
     cmRtNetEnd_t* ep = np->ends;
816 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,10 +704,8 @@ typedef struct
832 704
 
833 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,9 +716,6 @@ bool _cmRtNetTestThreadFunc(void* param)
846 716
 
847 717
   if( cmRtNetIsValid(p->netH) )
848 718
   {
849
-    if( cmRtNetIsInSyncMode(p->netH) )
850
-      cmRtNetSyncModeSend(p->netH);
851
-
852 719
     cmRtNetReceive(p->netH);
853 720
   }
854 721
 
@@ -867,7 +734,6 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
867 734
   cmRtNetRC_t     rc   = kOkNetRC;
868 735
   memset(&t,0,sizeof(t));
869 736
 
870
-
871 737
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
872 738
     goto errLabel;
873 739
 
@@ -878,14 +744,11 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
878 744
   if( hostNameStr == NULL )
879 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 748
     goto errLabel;
883 749
 
884 750
   if( mstrFl )
885 751
   {
886
-    if((rc = cmRtNetCreateNode(p->netH,"whirl", "192.168.15.109", port )) != kOkNetRC )
887
-      goto errLabel;
888
-
889 752
     if((rc = cmRtNetRegisterEndPoint(p->netH,"thunk_ep0", 0 )) != kOkNetRC )
890 753
       goto errLabel;
891 754
 

+ 8
- 18
cmRtNet.h View File

@@ -41,7 +41,7 @@ extern "C" {
41 41
   // Set 'ipAddr' to NULL if this is the local node.
42 42
   // During sync mode this node will attempt to sync with all
43 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 47
   // Register the local endpoints.
@@ -63,18 +63,7 @@ extern "C" {
63 63
   // an endpoint it updates it's own remote node/endpoint 
64 64
   // list.
65 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 68
   // This function must be polled to receive incoming network messages
80 69
   // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc()
@@ -131,12 +120,13 @@ extern "C" {
131 120
        
132 121
 
133 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
 

Loading…
Cancel
Save