Browse Source

cmRtNet.h/c: Many changes. Completed testing of cmRtNetTest().

master
kpl 11 years ago
parent
commit
63bba3ffa7
2 changed files with 299 additions and 203 deletions
  1. 230
    126
      cmRtNet.c
  2. 69
    77
      cmRtNet.h

+ 230
- 126
cmRtNet.c View File

@@ -9,66 +9,78 @@
9 9
 #include "cmTime.h"
10 10
 #include "cmRtSysMsg.h"
11 11
 
12
+// flags for cmRtNetNode_t.flags;
12 13
 enum
13 14
 {
14 15
   kLocalNodeNetFl = 0x01,
15 16
   kValidNodeNetFl = 0x02
16 17
 };
17 18
 
19
+// flags for cmRtNet_t.flags
20
+enum
21
+{
22
+  kReportSyncNetFl = 0x01
23
+};
24
+
18 25
 typedef enum
19 26
 {
20
-  kHelloSelNetId,
21
-  kNodeSelNetId,
22
-  kEndpointSelNetId,
23
-  kEndpointAckSelNetId,
24
-  kDoneSelNetId,
27
+  kHelloSelNetId,         // broadcast msg (label=node label, id=endpt cnt)
28
+  kNodeSelNetId,          // define remote node  (label=remote node label, id=endpt cnt)
29
+  kEndpointSelNetId,      // define remote endpt (label=remote endpt label, id=endpt id)
30
+  kDoneSelNetId,          // declare all endpts sent
31
+  kInvalidSelNetId
25 32
 } cmRtNetSelId_t;
26 33
 
34
+struct cmRtNetNode_str;
35
+
27 36
 typedef struct cmRtNetEnd_str
28 37
 {
29
-  cmChar_t*              label;
30
-  unsigned               id;
31
-  struct cmRtNetEnd_str* link;
38
+  cmChar_t*               label;
39
+  unsigned                id;
40
+  struct cmRtNetNode_str* np;   // Owner node.
41
+  struct cmRtNetEnd_str*  link;
32 42
 } cmRtNetEnd_t;
33 43
 
34 44
 
35 45
 typedef struct cmRtNetNode_str
36 46
 {
37
-  cmChar_t*               label;
38
-  struct sockaddr_in      sockaddr;
39
-  cmChar_t*               addr;
40
-  cmUdpPort_t             port;
41
-  unsigned                flags;
47
+  cmChar_t*               label;          // Node label.
48
+  struct sockaddr_in      sockaddr;       // Socket address
49
+  cmChar_t*               addr;           // IP Address (human readable)
50
+  cmUdpPort_t             port;           // Socket port
51
+  unsigned                flags;          // See kXXXNodeNetFl flags above.
42 52
   unsigned                endPtIdx;       // tracks the next endpoint to send during sync-mode
43 53
   unsigned                endPtCnt;       // local-node=actual cnt of endpt's remote-node:expected cnt of endpt's
44
-  cmTimeSpec_t            lastSendTime;
45
-  cmRtNetEnd_t*           ends;
54
+  cmTimeSpec_t            lastSendTime;   // Time of last message sent
55
+  cmRtNetEnd_t*           ends;           // End point list for this node
46 56
   struct cmRtNetNode_str* link;
47 57
 } cmRtNetNode_t;
48 58
 
49 59
 typedef struct
50 60
 {
51
-  cmErr_t         err;
52
-  cmUdpH_t        udpH;
53
-  cmUdpCallback_t cbFunc;
54
-  void*           cbArg;
55
-  cmRtNetNode_t*  nodes;
56
-  cmRtNetNode_t*  localNode;
57
-  unsigned        udpRecvBufByteCnt;
58
-  unsigned        udpTimeOutMs;
59
-  unsigned        interSyncSendTimeMs;
61
+  cmErr_t         err;                   // Error state object
62
+  unsigned        flags;                 // See kXXXNetFl above.
63
+  cmUdpH_t        udpH;                  // UDP port handle
64
+  cmUdpCallback_t cbFunc;                // Client callback to receive incoming messages from network.
65
+  void*           cbArg;                 //
66
+  cmRtNetNode_t*  nodes;                 // Node list.
67
+  cmRtNetNode_t*  localNode;             // Pointer to local node (which is also in node list)
68
+  unsigned        udpRecvBufByteCnt;     // UDP port receive buffer size.
69
+  unsigned        udpTimeOutMs;          // UDP time-out period
70
+  cmChar_t*       bcastAddr;             // Network broadcast address
60 71
 } cmRtNet_t;
61 72
 
62
-
73
+// Network synchronization message format
63 74
 typedef struct
64 75
 {
65
-  cmRtSysMsgHdr_t hdr;
66
-  cmRtNetSelId_t  selId;
76
+  cmRtSysMsgHdr_t hdr;    // standard cmRtSys msg  header 
77
+  cmRtNetSelId_t  selId;  // message selector id (See kXXXSelNetId above)
67 78
   const cmChar_t* label;  // node     or endpoint label
68 79
   unsigned        id;     // endptCnt or endpoint id
69 80
 } cmRtNetSyncMsg_t;
70 81
 
71
-cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
82
+cmRtNetH_t      cmRtNetNullHandle      = cmSTATIC_NULL_HANDLE;
83
+cmRtNetEndptH_t cmRtNetEndptNullHandle = cmSTATIC_NULL_HANDLE;
72 84
 
73 85
 cmRtNet_t* _cmRtNetHandleToPtr( cmRtNetH_t h )
74 86
 {
@@ -79,7 +91,8 @@ cmRtNet_t* _cmRtNetHandleToPtr( cmRtNetH_t h )
79 91
 
80 92
 void _cmRtNetVRpt( cmRtNet_t* p, const cmChar_t* fmt, va_list vl )
81 93
 {
82
-  cmRptVPrintf(p->err.rpt,fmt,vl);
94
+  if( cmIsFlag(p->flags,kReportSyncNetFl) )
95
+    cmRptVPrintf(p->err.rpt,fmt,vl);
83 96
 }
84 97
 
85 98
 void _cmRtNetRpt( cmRtNet_t* p, const cmChar_t* fmt, ... )
@@ -143,7 +156,7 @@ void _cmRtNetReleaseNodes( cmRtNet_t* p )
143 156
 
144 157
     np = nnp;
145 158
   }
146
-  p->nodes = NULL;
159
+  p->nodes     = NULL;
147 160
   p->localNode = NULL;
148 161
 }
149 162
 
@@ -188,7 +201,10 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmCha
188 201
   
189 202
   np           = cmMemAllocZ(cmRtNetNode_t,1);
190 203
   np->label    = cmMemAllocStr(label);
191
-  np->sockaddr = *saddr;
204
+
205
+  if( saddr != NULL )
206
+    np->sockaddr = *saddr;
207
+
192 208
   np->addr     = addr==NULL ? NULL : cmMemAllocStr(addr);
193 209
   np->port     = port;
194 210
   np->flags    = flags;
@@ -235,12 +251,22 @@ cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmCha
235 251
 
236 252
   ep->label = cmMemAllocStr(endPtLabel);
237 253
   ep->id    = endPtId;
238
-  ep->link      = np->ends;
239
-  np->ends      = ep;
254
+  ep->np    = np;
255
+  ep->link  = np->ends;
256
+  np->ends  = ep;
240 257
 
241 258
   return rc;
242 259
 }
243 260
 
261
+unsigned _cmRtNetNodeEndpointCount( cmRtNetNode_t* np )
262
+{
263
+  cmRtNetEnd_t* ep = np->ends;
264
+  unsigned      n  = 0;
265
+  for(; ep!=NULL; ep=ep->link)
266
+    ++n;
267
+  return n;
268
+}
269
+
244 270
 unsigned _cmRtNetSyncMsgSerialByteCount( const cmRtNetSyncMsg_t* m )
245 271
 { return sizeof(cmRtNetSyncMsg_t) + (m->label==NULL ? 1 : strlen(m->label) + 1); }
246 272
 
@@ -287,10 +313,9 @@ cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t
287 313
   if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
288 314
     return rc;
289 315
 
290
- 
291 316
   // send the msg
292
-  if( np==p->localNode )
293
-    udpRC = cmUdpSend2(p->udpH, buf, n, "255.255.255.255", np->port );
317
+  if( selId == kHelloSelNetId )
318
+    udpRC = cmUdpSend2(p->udpH, buf, n, p->bcastAddr, np->port );
294 319
   else
295 320
     udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
296 321
 
@@ -317,11 +342,12 @@ cmRtNetRC_t _cmRtNetFree( cmRtNet_t* p )
317 342
 
318 343
   _cmRtNetReleaseNodes(p);
319 344
 
345
+  cmMemFree(p->bcastAddr);
346
+
320 347
   cmMemFree(p);
321 348
   return rc;
322 349
 }
323 350
 
324
-
325 351
 cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg )
326 352
 {
327 353
   cmRtNetRC_t rc;
@@ -340,7 +366,6 @@ cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc,
340 366
 
341 367
   p->udpTimeOutMs        = 50;
342 368
   p->udpRecvBufByteCnt   = 8192;
343
-  p->interSyncSendTimeMs = 10;
344 369
   p->cbFunc = cbFunc;
345 370
   p->cbArg  = cbArg;
346 371
 
@@ -386,7 +411,7 @@ cmUdpH_t  cmRtNetUdpPortHandle( cmRtNetH_t h )
386 411
   return p->udpH;
387 412
 }
388 413
 
389
-cmRtNetRC_t  _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np )
414
+cmRtNetRC_t  _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t srcSelId )
390 415
 {
391 416
   cmRtNetRC_t     rc = kOkNetRC;
392 417
   cmRtNetEnd_t*   ep;
@@ -398,37 +423,58 @@ cmRtNetRC_t  _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np )
398 423
   if( np == NULL )
399 424
     return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found.");
400 425
 
401
-  // if all of the endpoints have been sent to this node ...
402
-  if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) == NULL )
426
+  // if we got here by receiving a 'done' msg from the remote node ...
427
+  if( srcSelId == kDoneSelNetId )
403 428
   {
404
-    if( np->endPtIdx == p->localNode->endPtCnt )
405
-    {
406
-      selId = kDoneSelNetId;
407
-      rptLabel = "done";
408
-    }
429
+    // ... then mark the remote node as having recieved all endpoints
430
+    unsigned n;
431
+    if((n = _cmRtNetNodeEndpointCount(np)) != np->endPtCnt )
432
+      rc = cmErrMsg(&p->err,kNodeEndCntErrNetRC,"The node '%s' expected %i endpoints but received %i.",cmStringNullGuard(np->label),np->endPtCnt,n);
409 433
     else
410
-    {
411
-      selId = kEndpointAckSelNetId;
412
-      rptLabel = "ep ack";
413
-    }
414
-   
434
+      np->flags = cmSetFlag(np->flags,kValidNodeNetFl);
415 435
   }
416
-  else
436
+
437
+  // attempt to get the next local endpoint to send ...
438
+  if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) != NULL )
417 439
   {
418
-    msgLabel = ep->label;
440
+    msgLabel = ep->label;  // ... send next local endpoint
419 441
     msgId    = ep->id;
420 442
   }
443
+  else // .... all local endpoints have been sent
444
+  {
445
+    selId    = kInvalidSelNetId;  
446
+    rptLabel = "done";
421 447
 
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));
448
+    // verify that no endpoints are available
449
+    if( np->endPtIdx < p->localNode->endPtCnt )
450
+      rc = cmErrMsg(&p->err,kSyncFailNetRC,"More endpoints are available to send but are not reachable.");
451
+    else
452
+    {
453
+      // if the remote node still has endpts to send then continue
454
+      // sending 'done' messages.
455
+      if( np->endPtIdx==p->localNode->endPtCnt || srcSelId != kDoneSelNetId )
456
+        selId = kDoneSelNetId;
457
+    }   
458
+  }
427 459
 
428
-  np->endPtIdx += 1;
460
+  // selId is set to kInvalidSelNetId when we encounter the (stop) criteria
461
+  if( selId != kInvalidSelNetId )
462
+  {
463
+    if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId )) != kOkNetRC )
464
+      rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
465
+    else
466
+      _cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel));
467
+
468
+    np->endPtIdx += 1;
469
+  }
429 470
 
430 471
   return rc;
431
-  
472
+}
473
+
474
+bool    _cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt )
475
+{
476
+  cmRtNetSyncMsg_t* m = (cmRtNetSyncMsg_t*)data;
477
+  return dataByteCnt >= sizeof(cmRtNetSyncMsg_t) && m->hdr.selId == kNetSyncSelRtId;
432 478
 }
433 479
 
434 480
 // When the network message recieve function (See cmRtNetAlloc() 'cbFunc') 
@@ -438,10 +484,10 @@ cmRtNetRC_t  _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np )
438 484
 cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
439 485
 {
440 486
   cmRtNetRC_t      rc = kOkNetRC;
441
-  cmRtNetSyncMsg_t m;
487
+  cmRtNetSyncMsg_t  m;
442 488
   m.label = NULL;
443 489
 
444
-  assert( cmRtNetIsSyncModeMsg(data,dataByteCnt));
490
+  assert( _cmRtNetIsSyncModeMsg(data,dataByteCnt));
445 491
   
446 492
   if( _cmRtNetDeserializeSyncMsg(data,dataByteCnt,&m) != kOkNetRC )
447 493
   {
@@ -449,14 +495,32 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
449 495
     goto errLabel;
450 496
   }
451 497
 
498
+  _cmRtNetRpt(p,"recv from:%s\n",cmUdpAddrToString(p->udpH, fromAddr ));
499
+
500
+
452 501
   assert( m.hdr.selId == kNetSyncSelRtId );
453 502
 
454 503
   // attempt to locate the remote node which sent the msg
455 504
   cmRtNetNode_t* np = _cmRtNetFindNodeFromSockAddr(p,fromAddr);
456 505
 
506
+
457 507
   switch( m.selId )
458 508
   {
459 509
     case kHelloSelNetId:    
510
+      // if this is a response to a broadcast from the local node then ignore it
511
+      if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label)) != NULL && strcmp(p->localNode->label,m.label)==0 )
512
+      {
513
+        const cmChar_t* fromAddrStr  = cmUdpAddrToString(p->udpH,fromAddr);
514
+        const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH));
515
+
516
+        if( fromAddrStr!=NULL && localAddrStr!=NULL && strcmp(fromAddrStr,localAddrStr)==0)
517
+          cmErrMsg(&p->err,kDuplLocalNetRC,"The node label '%s' appears to be duplicated at address %s and locally.",cmStringNullGuard(m.label),fromAddrStr);
518
+
519
+        np->sockaddr = *fromAddr;
520
+        goto errLabel;
521
+      } 
522
+      // fall through
523
+
460 524
     case kNodeSelNetId: 
461 525
       {
462 526
         // if the node already exists ...
@@ -472,17 +536,19 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
472 536
         if(( rc = _cmRtNetCreateNode(p,m.label,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
473 537
           goto errLabel;        
474 538
 
539
+        np = p->nodes; // newest node is always the first node
540
+
475 541
         // send response
476 542
         switch( m.selId )
477 543
         {
478 544
           case kHelloSelNetId:
479 545
             _cmRtNetRpt(p,"rcv hello\n"); // reply with local node
480
-            rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, NULL, p->localNode->endPtCnt );
546
+            rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt );
481 547
             break;
482 548
 
483 549
           case kNodeSelNetId:
484 550
             _cmRtNetRpt(p,"rcv node\n");
485
-            _cmRtNetSendEndpointReplyMsg( p, np ); // reply with first endpoint
551
+            _cmRtNetSendEndpointReplyMsg( p, np, m.selId ); // reply with first endpoint
486 552
             break;
487 553
 
488 554
           default:
@@ -492,17 +558,15 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
492 558
       }
493 559
       break;
494 560
 
495
-    
496
-    case kEndpointAckSelNetId:
497 561
     case kDoneSelNetId:
498
-      rc = _cmRtNetSendEndpointReplyMsg(p,np);
562
+      //case kEndpointAckSelNetId:
563
+      rc = _cmRtNetSendEndpointReplyMsg(p,np,m.selId);
499 564
       break;
500 565
 
501 566
     case kEndpointSelNetId: 
502 567
       {
503 568
         cmRtNetEnd_t* ep;
504 569
 
505
-
506 570
         // verify the remote node exists.
507 571
         if( np == NULL )
508 572
         {
@@ -521,7 +585,7 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
521 585
         }
522 586
 
523 587
         // reply with a local endpoint or 'done' msg
524
-        rc = _cmRtNetSendEndpointReplyMsg( p, np );
588
+        rc = _cmRtNetSendEndpointReplyMsg( p, np, m.selId );
525 589
       }
526 590
       break;
527 591
 
@@ -540,27 +604,21 @@ void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const st
540 604
 {
541 605
   cmRtNet_t* p = (cmRtNet_t*)cbArg;
542 606
   
543
-  if( cmRtNetIsSyncModeMsg(data,dataByteCnt))
607
+  if( _cmRtNetIsSyncModeMsg(data,dataByteCnt))
544 608
     _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
545 609
   else
546 610
     p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
547 611
   
548 612
 }
549 613
 
550
-
551
-
552
-cmRtNetRC_t cmRtNetRegisterLocalNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
614
+cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
553 615
 {
554 616
   cmRtNet_t*  p = _cmRtNetHandleToPtr(h);
555 617
   cmRtNetRC_t rc;
556
-  struct sockaddr_in sockaddr;
557 618
 
558 619
   // release the local node
559
-  if( p->localNode != NULL )
560
-  {    
561
-    _cmRtNetReleaseNode(p,p->localNode);
562
-    p->localNode = NULL;
563
-  }
620
+  if((rc = cmRtNetFinalize(h)) != kOkNetRC )
621
+    goto errLabel;
564 622
 
565 623
   // if this is the local node then initialze the local socket
566 624
   if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl | kBroadcastUdpFl,_cmRtNetRecv,p,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
@@ -569,19 +627,13 @@ cmRtNetRC_t cmRtNetRegisterLocalNode( cmRtNetH_t h, const cmChar_t* nodeLabel, c
569 627
     goto errLabel;
570 628
   }
571 629
 
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
-  }
578
-
579 630
   // create the local node
580
-  if((rc = _cmRtNetCreateNode(p,nodeLabel, ipAddr, port, &sockaddr, kLocalNodeNetFl, 0)) != kOkNetRC )
631
+  if((rc = _cmRtNetCreateNode(p,nodeLabel, ipAddr, port, NULL, kLocalNodeNetFl, 0)) != kOkNetRC )
581 632
     goto errLabel;
582 633
 
583 634
   // the last created node is always the first node on the list
584 635
   p->localNode = p->nodes;
636
+  p->bcastAddr = cmMemResizeStr(p->bcastAddr,bcastAddr);
585 637
 
586 638
   // begin listening on the local port
587 639
   if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
@@ -609,23 +661,22 @@ cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, u
609 661
   return rc;
610 662
 }
611 663
 
612
-cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h )
664
+cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h )
613 665
 {
614 666
   cmRtNet_t* p = _cmRtNetHandleToPtr(h);
615 667
   _cmRtNetReleaseNodes(p);
668
+  
616 669
   return kOkNetRC;
617 670
 }
618 671
 
619
-cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h )
672
+cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h )
620 673
 {
621 674
   cmRtNet_t* p = _cmRtNetHandleToPtr(h);
622 675
 
623 676
   // broadcast 'node' msg
624
-  return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, NULL, p->localNode->endPtCnt );
677
+  return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, p->localNode->label, p->localNode->endPtCnt );
625 678
 }
626 679
 
627
-
628
-
629 680
 cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
630 681
 {
631 682
   cmRtNetRC_t rc = kOkNetRC;
@@ -640,24 +691,70 @@ cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
640 691
   return rc;
641 692
 }
642 693
 
643
-bool    cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt )
694
+cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
644 695
 {
645
-  cmRtNetSyncMsg_t* m = (cmRtNetSyncMsg_t*)data;
646
-  return dataByteCnt >= sizeof(cmRtNetSyncMsg_t) && m->hdr.selId == kNetSyncSelRtId;
696
+  cmRtNetRC_t     rc = kOkNetRC;
697
+  cmRtNet_t*      p  = _cmRtNetHandleToPtr(h);
698
+  cmRtNetNode_t* np;
699
+  cmRtNetEnd_t*  ep;
700
+
701
+  if(( np = _cmRtNetFindNode(p,nodeLabel)) == NULL )
702
+    return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
703
+
704
+
705
+  if(( ep = _cmRtNetFindNodeEnd(np, endptLabel )) == NULL )
706
+    return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel));
707
+
708
+  hp->h = ep;
709
+
710
+  return rc;
647 711
 }
648 712
 
713
+cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt )
714
+{
715
+  cmRtNetRC_t     rc = kOkNetRC;
716
+  cmRtNet_t*      p  = _cmRtNetHandleToPtr(h);
717
+  cmRtNetEnd_t*   ep = (cmRtNetEnd_t*)epH.h;
718
+  
719
+  assert( ep != NULL );
720
+  
721
+  unsigned dN = sizeof(unsigned) + msgByteCnt; 
722
+  char data[ dN ];
723
+  unsigned *hdr = (unsigned*)data;
724
+  hdr[0] = ep->id;
725
+  memcpy(hdr+1,msg,msgByteCnt);
726
+  
727
+  if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC )
728
+    return cmErrMsg(&p->err,kUdpPortFailNetRC,"Send to node:%s endpt:%s failed.\n",cmStringNullGuard(ep->np->label),cmStringNullGuard(ep->label));
729
+
730
+  return rc;
731
+}
649 732
 
650
-unsigned  cmRtNetEndPointIndex( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endPtLabel )
733
+cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
651 734
 {
652
-  //cmRtNet_t* p = _cmRtNetHandleToPtr(h);
653
-  return cmInvalidIdx;
654
-}  
735
+  cmRtNetRC_t     rc  = kOkNetRC;
736
+  cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
737
+
738
+  if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC )
739
+    return rc;
740
+
741
+  return cmRtNetSend(h,epH,msg,msgByteCnt);
742
+}
655 743
 
656
-cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned endPointIndex, const void* msg, unsigned msgByteCnt )
744
+
745
+
746
+bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl )
657 747
 {
658
-  cmRtNetRC_t rc = kOkNetRC;
659
-  //cmRtNet_t* p = _cmRtNetHandleToPtr(h);
660
-  return rc;
748
+  cmRtNet_t* p  = _cmRtNetHandleToPtr(h);
749
+  bool       fl =  cmIsFlag(p->flags,kReportSyncNetFl);
750
+  p->flags = cmEnaFlag(p->flags,kReportSyncNetFl,enableFl);
751
+  return fl;  
752
+}
753
+
754
+bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h )
755
+{
756
+  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
757
+  return cmIsFlag(p->flags,kReportSyncNetFl);
661 758
 }
662 759
 
663 760
 void   cmRtNetReport( cmRtNetH_t h )
@@ -699,17 +796,20 @@ void   cmRtNetReport( cmRtNetH_t h )
699 796
 typedef struct
700 797
 {
701 798
   cmThreadH_t thH;
702
-  cmRtNetH_t netH;
799
+  cmRtNetH_t  netH;
800
+  unsigned    msgVal;
703 801
 } _cmRtNetTest_t;
704 802
 
705 803
 void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
706 804
 {
707 805
   //_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
708
-
806
+  
807
+  unsigned* hdr = (unsigned*)data;
808
+  printf("%i %i\n",hdr[0],hdr[1]);
709 809
 
710 810
 }
711 811
 
712
-
812
+             
713 813
 bool _cmRtNetTestThreadFunc(void* param)
714 814
 {
715 815
   _cmRtNetTest_t* p = (_cmRtNetTest_t*)param;
@@ -728,10 +828,15 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
728 828
 {
729 829
   char            c;
730 830
   _cmRtNetTest_t  t;
731
-  const cmChar_t* hostNameStr;
732 831
   cmUdpPort_t     port = 5876;
733 832
   _cmRtNetTest_t* p    = &t;
734 833
   cmRtNetRC_t     rc   = kOkNetRC;
834
+  const cmChar_t* localHostStr   = mstrFl  ? "master"    : "slave";
835
+  const cmChar_t* localEndpStr   = mstrFl  ? "master_ep" : "slave_ep";
836
+  const cmChar_t* remoteHostStr  = !mstrFl ? "master"    : "slave";
837
+  const cmChar_t* remoteEndpStr  = !mstrFl ? "master_ep" : "slave_ep";
838
+  const cmChar_t* bcastAddr      = "192.168.15.255";
839
+
735 840
   memset(&t,0,sizeof(t));
736 841
 
737 842
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
@@ -740,32 +845,19 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
740 845
   if((rc = cmRtNetAlloc(ctx,&p->netH,_cmRtNetTestRecv,p)) != kOkNetRC )
741 846
     goto errLabel;
742 847
 
743
-  hostNameStr = cmRtNetLocalHostName(p->netH);
744
-  if( hostNameStr == NULL )
745
-    hostNameStr = "<no-host-name>";
848
+  cmRtNetReportSyncEnable(p->netH,true); // enable sync. protocol reporting
746 849
 
747
-  if((rc = cmRtNetRegisterLocalNode(p->netH, hostNameStr, NULL, port )) != kOkNetRC)
850
+  if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC)
748 851
     goto errLabel;
749 852
 
750
-  if( mstrFl )
751
-  {
752
-    if((rc = cmRtNetRegisterEndPoint(p->netH,"thunk_ep0", 0 )) != kOkNetRC )
753
-      goto errLabel;
754
-
755
-    if(( rc = cmRtNetBeginSyncMode(p->netH)) != kOkNetRC )
756
-      goto errLabel;
757
-    
758
-  }
759
-  else
760
-  {
761
-    if((rc = cmRtNetRegisterEndPoint(p->netH,"whirl_ep0", 0 )) != kOkNetRC )
762
-      goto errLabel;
763
-  }
853
+  if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC )
854
+    goto errLabel;
764 855
   
765 856
   if( cmThreadPause(p->thH,0) != kOkThRC )
766 857
     goto errLabel;
767 858
 
768
-  cmRptPrintf(&ctx->rpt,"%s q=quit\n", mstrFl ? "Master: " : "Slave: ");
859
+  cmRptPrintf(&ctx->rpt,"%s t=transmit s=sync r=report q=quit\n", localHostStr );
860
+
769 861
   while( (c=getchar()) != 'q' )
770 862
   {
771 863
     switch(c)
@@ -773,6 +865,18 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
773 865
       case 'r':
774 866
         cmRtNetReport(p->netH);
775 867
         break;
868
+
869
+      case 's':
870
+        cmRtNetDoSync(p->netH);
871
+        break;
872
+
873
+      case 't':
874
+        {
875
+          if( cmRtNetSendByLabels(p->netH, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
876
+            p->msgVal += 1;
877
+
878
+        }        
879
+        break;
776 880
     }
777 881
     
778 882
   }

+ 69
- 77
cmRtNet.h View File

@@ -11,21 +11,23 @@ extern "C" {
11 11
     kUdpPortFailNetRC,
12 12
     kInvalidLabelNetRC,
13 13
     kDuplLabelNetRC,
14
-    kDuplLocalNetRC,
15 14
     kDuplEndNetRC,
15
+    kDuplLocalNetRC,
16 16
     kThreadFailNetRC,
17 17
     kBufToSmallNetRC,
18 18
     kNodeNotFoundNetRC,
19
-    kNodeStateErrNetRC,
20
-    kTimeOutErrNetRC,
19
+    kEndNotFoundNetRC,
21 20
     kLocalNodeNetRC,
21
+    kSyncFailNetRC,
22
+    kNodeEndCntErrNetRC
22 23
   };
23 24
 
24 25
   typedef cmRC_t     cmRtNetRC_t;
25 26
   typedef cmHandle_t cmRtNetH_t;
27
+  typedef cmHandle_t cmRtNetEndptH_t;
26 28
 
27
-
28
-  extern cmRtNetH_t cmRtNetNullHandle;
29
+  extern cmRtNetH_t      cmRtNetNullHandle;
30
+  extern cmRtNetEndptH_t cmRtNetEndptNullHandle;
29 31
 
30 32
   // 'cbFunc' will be called within the context of cmRtNetReceive() to receive
31 33
   // incoming network messages.
@@ -34,99 +36,89 @@ extern "C" {
34 36
 
35 37
   bool      cmRtNetIsValid( cmRtNetH_t h );
36 38
 
39
+  // Get the local host name for this machine. This function
40
+  // is synonomous with gethostname().
37 41
   const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h );
38 42
 
39
-  // Create a network node.
40
-  // The 'nodeLabel' refers to a network device cfg. (see cmDevCfg).
41
-  // Set 'ipAddr' to NULL if this is the local node.
42
-  // During sync mode this node will attempt to sync with all
43
-  // nodes in the node list.
44
-  cmRtNetRC_t cmRtNetRegisterLocalNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
45
-
43
+  // Initialize the local network node.
44
+  // 'bcastAddr' is the network broadcast address (e.g. 192.168.15.255).
45
+  // 'nodeLabel' is the local network node label
46
+  // 'ipAddr' may be set to NULL to use any available IP address.
47
+  // 'ipPort' refers to the socket port (which may need to be made available 
48
+  // by the machine firewall cfg.)
49
+  cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
46 50
 
47 51
   // Register the local endpoints.
52
+  // Endpoints may only be registered once the network is initialized via
53
+  // cmRtNetInitialize().
48 54
   // Remote nodes will be able to send messages to these endpoints by
49 55
   // referring to (nodeLabel/endPtLabel)
50 56
   cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
51 57
 
52 58
   // Delete all nodes and endpoints.
53
-  cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h );
54
-
55
-
56
-  // Go into 'sync' node.
57
-  // When a node enters sync mode it systematically transmits all of it's 
58
-  // local endpoint information to each registered remote node.  Prior to 
59
-  // entering sync mode a node must therefore have been setup with a list 
60
-  // of remote nodes (via cmRtNetCreateNode()) and a list of local endpoints 
61
-  // (cmRtNetRegisterEndpoint()).  During sync mode a node sends it's local 
62
-  // endpoint list to each registered remote node. When a remote node receives 
63
-  // an endpoint it updates it's own remote node/endpoint 
64
-  // list.
65
-  cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h );
59
+  cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h );
66 60
 
61
+  // Broadcast the 'hello' to all machines listening on the 
62
+  // broadcast addresss. This starts the synchronization sequence
63
+  cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h );
67 64
 
68 65
   // This function must be polled to receive incoming network messages
69 66
   // via the callback funcion 'cbFunc' as passed to cmRtNetAlloc()
70 67
   cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
71 68
 
72
-  bool        cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt );
69
+  // Get an end point handle for use with cmRtNetSend.
70
+  cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
73 71
 
74
-  unsigned  cmRtNetEndPointIndex( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endPtLabel );
75
-  
72
+  // Send a message to a remote endpoint.
73
+  cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
76 74
 
77
-  cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned endPointIndex, const void* msg, unsigned msgByteCnt );
75
+  // Send a message to a remote endpoint. This function is a composite
76
+  // of cmRtNetEndpointHandle() and cmRtNetSend().
77
+  cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
78 78
 
79
-  void      cmRtNetReport( cmRtNetH_t h );
79
+  // Enable/disable synchronization protocol reporting.
80
+  // Return the previous state of the report sync. flag.
81
+  bool        cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl );
82
+  bool        cmRtNetReportSyncIsEnabled( cmRtNetH_t h );
83
+
84
+  void        cmRtNetReport( cmRtNetH_t h );
80 85
     
81
-  void      cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
86
+  void        cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
82 87
 
83 88
   /*
84
-    Master:
85
-      cmRtNetBeginSyncMode().
86
-      while( cmRtNetIsSyncMode())
87
-      {
88
-       // Give the master an oppurtunity to advance it's sync mode state.
89
-       // When the master is has sync'd with all remote nodes in it's
90
-       // remote node list then it will automatically exit sync mode.
91
-       cmRtNetSyncModeSend()
92
-      }
93
-
94
-      _myNetRecv(dataV,dataN,addr)
95
-     {
96
-       if( cmRtNetIsSyncModeMsg(dataV,dataN) )
97
-         cmRtNetSyncModeRecv(dataV,dataN,addr)
98
-     } 
99
-
100
-
101
-     The 'master' is the machine which cmRtNetBeginSyncMode() is called on.
102
-     1) 'master' sends local endpoints to all registered remote nodes.
103
-     2) When a 'slave' receives the kDoneSelNetId msg it transmits
104
-     it's own local endpoints back to the master.
105
-
106
-     a. Each node in the node list has a type id:
107
-       1. local 
108
-       2. registered - remote node that was explicitely registered on a master
109
-       3. received   - remote node that was received from a master
110
-
111
-     b. 
112
-       1. All nodes are created in the 'send-hello' state.
113
-       2. If a master machine is in 'sync-mode' then it systematically sends
114
-       each of it's local endpoints to all 'registered' nodes.
115
-       3. When a slave machine recives a 'hello' it creates a
116
-       'received' node.
117
-       4. When a slave machine recieves a 'done' it enters sync mode
118
-       and systematically sends each of its local endpoints to
119
-       the 'done' source.
120
-       
121
-
122
-   Protocol:
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'.
89
+
90
+   Synchronization Protocol:
91
+
92
+                  Machine A                          Machine B
93
+     ==================================    ====================================
94
+     broadcast 'hello' --------------------> create node-A w/ ei=0 -------+
95
+                                                                          |
96
+     +<-- create node-B w/ ei=0 <----------- send 'node' <----------------+
97
+     |
98
+     +--> switch(ei,m_t)
99
+     |     ei  < en   : send endpt[ei++] ---> create endpt[] on node-A -->+
100
+     |                                                                    |
101
+     |     ei == en   : ++ei,send 'done' -------------------------------->+                                                    |
102
+     |                                                                    |
103
+     |    m_t!='done' :      send 'done' -------------------------------->+                                                              |
104
+     |                                                                    |
105
+     |    (stop)      :                                                   |
106
+     |                                                                    |
107
+     |                                                                    v
108
+     |                                                           switch(ei,m_t)
109
+     +<-- create endpt[] on node-B  <--------- send endpt[ei++] : ei < en
110
+     | 
111
+     +<--------------------------------------- send 'done',++ei : ei == en 
112
+     |
113
+     +<--------------------------------------- send 'done'      : m_t!= 'done'
114
+                                                                  
115
+                                                                :  (stop)
116
+
117
+     Notes:
118
+        1)  'ei' is the index of the next local end point to transmit.
119
+        2)  'en' is the count of local endpoints.
120
+        3)  'm_t' is the msg type (i.e.'hello','node','endpoint','done') 
121
+            of the incoming message.
130 122
 
131 123
    */  
132 124
 

Loading…
Cancel
Save