Browse Source

cmRtNet.h/c : The rtSubIdx of the owning cmRtSys sub-system is now set in

cmNetAlloc().  rtSubIdx is no longer required to locate a remote endpoint
because IP/"node label" is enough information to determine the rtSubIdx.
Several changes were made to simplify the interface to cmRtNet based on this
observation.
master
kpl 10 years ago
parent
commit
10943e18e3
2 changed files with 62 additions and 38 deletions
  1. 41
    33
      cmRtNet.c
  2. 21
    5
      cmRtNet.h

+ 41
- 33
cmRtNet.c View File

@@ -30,14 +30,15 @@ typedef struct cmRtNetEnd_str
30 30
 {
31 31
   cmChar_t*               label;
32 32
   unsigned                id;
33
-  unsigned                rtSubIdx;
34 33
   struct cmRtNetNode_str* np;   // Owner node.
35 34
   struct cmRtNetEnd_str*  link;
36 35
 } cmRtNetEnd_t;
37 36
 
37
+struct cmRtNet_str;
38 38
 
39 39
 typedef struct cmRtNetNode_str
40 40
 {
41
+  unsigned                rtSubIdx;       // rtSubIdx of the sub-system which owns this node
41 42
   cmChar_t*               label;          // Node label.
42 43
   struct sockaddr_in      sockaddr;       // Socket address
43 44
   cmChar_t*               addr;           // IP Address (human readable)
@@ -50,10 +51,11 @@ typedef struct cmRtNetNode_str
50 51
   struct cmRtNetNode_str* link;
51 52
 } cmRtNetNode_t;
52 53
 
53
-typedef struct
54
+typedef struct cmRtNet_str
54 55
 {
55 56
   cmErr_t         err;                   // Error state object
56 57
   unsigned        flags;                 // See kXXXNetFl above.
58
+  unsigned        rtSubIdx;              // rtSubIdx of the owning sub-system
57 59
   cmUdpH_t        udpH;                  // UDP port handle
58 60
   cmUdpCallback_t cbFunc;                // Client callback to receive incoming messages from network.
59 61
   void*           cbArg;                 //
@@ -173,7 +175,7 @@ cmRtNetRC_t  _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
173 175
   return cmErrMsg(&p->err,kNodeNotFoundNetRC,"Node to release not found.");
174 176
 }
175 177
 
176
-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 )
178
+cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, unsigned rtSubIdx, const cmChar_t* addr, cmUdpPort_t port, const struct sockaddr_in* saddr, unsigned flags, unsigned endPtCnt )
177 179
 {
178 180
   cmRtNetRC_t rc = kOkNetRC;
179 181
   cmRtNetNode_t* np;
@@ -190,6 +192,7 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmCha
190 192
   if( saddr != NULL )
191 193
     np->sockaddr = *saddr;
192 194
 
195
+  np->rtSubIdx = rtSubIdx;
193 196
   np->addr     = addr==NULL ? NULL : cmMemAllocStr(addr);
194 197
   np->port     = port;
195 198
   np->flags    = flags;
@@ -200,11 +203,11 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmCha
200 203
   return rc;
201 204
 }
202 205
 
203
-cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, unsigned rtSubIdx, const cmChar_t* endPtLabel )
206
+cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, const cmChar_t* endPtLabel )
204 207
 {
205 208
   cmRtNetEnd_t* ep = np->ends;
206 209
   for(; ep!=NULL; ep=ep->link)
207
-    if( ep->rtSubIdx==rtSubIdx && strcmp(ep->label,endPtLabel)==0 )
210
+    if( strcmp(ep->label,endPtLabel)==0 )
208 211
       return ep;
209 212
   return NULL;
210 213
 }
@@ -223,12 +226,12 @@ cmRtNetEnd_t* _cmRtNetIndexToEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned
223 226
   return NULL;
224 227
 }
225 228
 
226
-cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId )
229
+cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmChar_t* endPtLabel, unsigned endPtId )
227 230
 {
228 231
   if( endPtLabel == NULL )
229 232
     return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
230 233
 
231
-  if( _cmRtNetFindNodeEnd( np, rtSubIdx, endPtLabel) != NULL)
234
+  if( _cmRtNetFindNodeEnd( np, endPtLabel) != NULL)
232 235
     return cmErrMsg(&p->err,kDuplEndNetRC,"A duplicate endpoint ('%s') was encountered on node '%s'.",endPtLabel,np->label);
233 236
 
234 237
   cmRtNetRC_t   rc = kOkNetRC;
@@ -236,7 +239,6 @@ cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned rt
236 239
 
237 240
   ep->label    = cmMemAllocStr(endPtLabel);
238 241
   ep->id       = endPtId;
239
-  ep->rtSubIdx = rtSubIdx;
240 242
   ep->np       = np;
241 243
   ep->link     = np->ends;
242 244
   np->ends     = ep;
@@ -386,7 +388,7 @@ const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m )
386 388
   return "";
387 389
 }
388 390
 
389
-cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg )
391
+cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg )
390 392
 {
391 393
   cmRtNetRC_t rc;
392 394
   if((rc = cmRtNetFree(hp)) != kOkNetRC )
@@ -402,10 +404,11 @@ cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc,
402 404
     goto errLabel;
403 405
   }
404 406
 
405
-  p->udpTimeOutMs        = 50;
406
-  p->udpRecvBufByteCnt   = 8192;
407
-  p->cbFunc = cbFunc;
408
-  p->cbArg  = cbArg;
407
+  p->rtSubIdx          = rtSubIdx;
408
+  p->udpTimeOutMs      = 50;
409
+  p->udpRecvBufByteCnt = 8192;
410
+  p->cbFunc            = cbFunc;
411
+  p->cbArg             = cbArg;
409 412
 
410 413
   hp->h = p;
411 414
 
@@ -478,7 +481,7 @@ cmRtNetRC_t  _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRt
478 481
   {
479 482
     msgLabel    = ep->label;  // ... send next local endpoint
480 483
     msgId       = ep->id;
481
-    msgRtSubIdx = ep->rtSubIdx;
484
+    msgRtSubIdx = ep->np->rtSubIdx;
482 485
   }
483 486
   else // .... all local endpoints have been sent
484 487
   {
@@ -573,7 +576,7 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
573 576
 
574 577
         //  create a node proxy to represent the remote node
575 578
         // (Note:m.id == remote node endpoint count (i.e. the count of endpoints expected for the remote node.))
576
-        if(( rc = _cmRtNetCreateNode(p,m.label,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
579
+        if(( rc = _cmRtNetCreateNode(p,m.label,m.rtSubIdx,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
577 580
           goto errLabel;        
578 581
 
579 582
         np = p->nodes; // newest node is always the first node
@@ -583,7 +586,7 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
583 586
         {
584 587
           case kHelloSelNetId:
585 588
             _cmRtNetRpt(p,"rcv hello\n"); // reply with local node
586
-            rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
589
+            rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt, p->localNode->rtSubIdx );
587 590
             break;
588 591
 
589 592
           case kNodeSelNetId:
@@ -615,12 +618,12 @@ cmRtNetRC_t  _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
615 618
         }
616 619
         
617 620
         // attempt to find the end point 
618
-        if((ep = _cmRtNetFindNodeEnd(np, m.rtSubIdx, m.label)) != NULL )
621
+        if((ep = _cmRtNetFindNodeEnd(np, m.label)) != NULL )
619 622
           ep->id = m.id; // the endpoint was found update the endPtId
620 623
         else
621 624
         {
622 625
           // create a local proxy for the endpoint
623
-          if((rc = _cmRtNetCreateEndpoint(p,np,m.rtSubIdx,m.label,m.id)) != kOkNetRC )
626
+          if((rc = _cmRtNetCreateEndpoint(p,np,m.label,m.id)) != kOkNetRC )
624 627
             goto errLabel;
625 628
         }
626 629
 
@@ -675,7 +678,7 @@ cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cm
675 678
   }
676 679
 
677 680
   // create the local node
678
-  if((rc = _cmRtNetCreateNode(p,nodeLabel, ipAddr, port, NULL, kLocalNodeNetFl, 0)) != kOkNetRC )
681
+  if((rc = _cmRtNetCreateNode(p,nodeLabel, p->rtSubIdx, ipAddr, port, NULL, kLocalNodeNetFl, 0)) != kOkNetRC )
679 682
     goto errLabel;
680 683
 
681 684
   // the last created node is always the first node on the list
@@ -705,7 +708,7 @@ bool cmRtNetIsInitialized( cmRtNetH_t h )
705 708
 }
706 709
 
707 710
 
708
-cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId )
711
+cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId )
709 712
 {
710 713
   cmRtNetRC_t rc = kOkNetRC;
711 714
   cmRtNet_t* p = _cmRtNetHandleToPtr(h);
@@ -713,7 +716,7 @@ cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, unsigned rtSubIdx, const cmCh
713 716
   if( p->localNode == NULL )
714 717
     return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
715 718
 
716
-  if((rc = _cmRtNetCreateEndpoint(p, p->localNode,rtSubIdx,endPtLabel,endPtId )) == kOkNetRC )
719
+  if((rc = _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId )) == kOkNetRC )
717 720
     p->localNode->endPtCnt += 1;
718 721
 
719 722
   return rc;
@@ -750,7 +753,7 @@ cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
750 753
   return rc;
751 754
 }
752 755
 
753
-cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
756
+cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
754 757
 {
755 758
   cmRtNetRC_t     rc = kOkNetRC;
756 759
   cmRtNet_t*      p  = _cmRtNetHandleToPtr(h);
@@ -761,7 +764,7 @@ cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, unsi
761 764
     return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
762 765
 
763 766
 
764
-  if(( ep = _cmRtNetFindNodeEnd(np, rtSubIdx, endptLabel )) == NULL )
767
+  if(( ep = _cmRtNetFindNodeEnd(np, endptLabel )) == NULL )
765 768
     return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel));
766 769
 
767 770
   hp->h = ep;
@@ -773,14 +776,19 @@ cmRtNetRC_t _cmRtNetSend( cmRtNet_t* p, const cmRtNetEnd_t* ep, const void* msg,
773 776
 {
774 777
   cmRtNetRC_t     rc = kOkNetRC;
775 778
   
776
-  unsigned dN = sizeof(cmRtNetMsg_t) + msgByteCnt; 
779
+  unsigned hN = sizeof(cmRtNetMsg_t);
780
+  unsigned dN = hN + msgByteCnt; 
777 781
   char data[ dN ];
778 782
 
779 783
   cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
780
-  r->hdr.rtSubIdx     = ep->rtSubIdx;
784
+  r->hdr.rtSubIdx     = ep->np->rtSubIdx;
781 785
   r->hdr.selId        = kMsgSelRtId;
782
-  r->endptId          = ep->id;
783
-  memcpy(data+sizeof(cmRtNetMsg_t),msg,msgByteCnt);
786
+  r->endptId          = ep->id;          
787
+  memcpy(data+hN,msg,msgByteCnt);
788
+
789
+  // ep->np->sockaddr identifies the node on the receiving cmRtNet.
790
+  // cmRtNetMsg_t* r.endptId is then used by the receiving cmRtNet to indicate which endpoint on
791
+  // the node the incoming message should be associated with.
784 792
   
785 793
   if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC )
786 794
     return cmErrMsg(&p->err,kUdpPortFailNetRC,"Send to node:%s endpt:%s failed.\n",cmStringNullGuard(ep->np->label),cmStringNullGuard(ep->label));
@@ -798,12 +806,12 @@ cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, uns
798 806
 }
799 807
 
800 808
 
801
-cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
809
+cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
802 810
 {
803 811
   cmRtNetRC_t     rc  = kOkNetRC;
804 812
   cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
805 813
 
806
-  if((rc = cmRtNetEndpointHandle(h,nodeLabel,rtSubIdx,endptLabel,&epH)) != kOkNetRC )
814
+  if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC )
807 815
     return rc;
808 816
 
809 817
   return cmRtNetSend(h,epH,msg,msgByteCnt);
@@ -935,7 +943,7 @@ cmRtNetRC_t  cmRtNetRemoteNodeEndPoint(
935 943
   
936 944
   *labelRef = ep->label;
937 945
   *idRef    = ep->id;
938
-  *rsiRef   = ep->rtSubIdx;
946
+  *rsiRef   = ep->np->rtSubIdx;
939 947
 
940 948
   return kOkNetRC;
941 949
 }
@@ -998,7 +1006,7 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
998 1006
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
999 1007
     goto errLabel;
1000 1008
 
1001
-  if((rc = cmRtNetAlloc(ctx,&p->netH,_cmRtNetTestRecv,p)) != kOkNetRC )
1009
+  if((rc = cmRtNetAlloc(ctx,&p->netH,rtSubIdx,_cmRtNetTestRecv,p)) != kOkNetRC )
1002 1010
     goto errLabel;
1003 1011
 
1004 1012
   cmRtNetReportSyncEnable(p->netH,true); // enable sync. protocol reporting
@@ -1006,7 +1014,7 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
1006 1014
   if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC)
1007 1015
     goto errLabel;
1008 1016
 
1009
-  if((rc = cmRtNetRegisterEndPoint(p->netH,rtSubIdx,localEndpStr, 0 )) != kOkNetRC )
1017
+  if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC )
1010 1018
     goto errLabel;
1011 1019
   
1012 1020
   if( cmThreadPause(p->thH,0) != kOkThRC )
@@ -1028,7 +1036,7 @@ void  cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
1028 1036
 
1029 1037
       case 't':
1030 1038
         {
1031
-          if( cmRtNetSendByLabels(p->netH, remoteHostStr, rtSubIdx, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
1039
+          if( cmRtNetSendByLabels(p->netH, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
1032 1040
             p->msgVal += 1;
1033 1041
 
1034 1042
         }        

+ 21
- 5
cmRtNet.h View File

@@ -5,6 +5,21 @@
5 5
 extern "C" {
6 6
 #endif
7 7
 
8
+  /*
9
+   Nodes and Endpoints:
10
+   ---------------------
11
+   A node corresponds to a process and owns a socket. It also has a label which is 
12
+   unique among all other nodes on the network. A node also has a set of application 
13
+   defined 'endpoints'.  Each endpoint has a label and id that is unique among all 
14
+   other endpoints on the same node.  Endpoints on different nodes however may share
15
+   use the same label and id.  Endpoints are used by remote senders to identify 
16
+   a particular receiver which is sharing the node with other receivers.  Endpoints
17
+   are therefore analogous to port numbers on sockets.
18
+
19
+   See gt/doc/notes.txt for more discussion of cmRtNet.
20
+   
21
+   */
22
+
8 23
   enum
9 24
   {
10 25
     kOkNetRC = cmOkRC,
@@ -62,7 +77,8 @@ extern "C" {
62 77
 
63 78
   // 'cbFunc' will be called within the context of cmRtNetReceive() to receive
64 79
   // incoming network messages.
65
-  cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg );
80
+  // rtSubIdx is the rtSubIdx of the cmRtSys which owns this cmRtNet.  
81
+  cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg );
66 82
   cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp );
67 83
 
68 84
   bool      cmRtNetIsValid( cmRtNetH_t h );
@@ -85,7 +101,7 @@ extern "C" {
85 101
   // cmRtNetInitialize().
86 102
   // Remote nodes will be able to send messages to these endpoints by
87 103
   // referring to (nodeLabel/endPtLabel)
88
-  cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId );
104
+  cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
89 105
 
90 106
   // Delete all nodes and endpoints.
91 107
   cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h );
@@ -100,15 +116,15 @@ extern "C" {
100 116
   // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
101 117
   cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
102 118
 
103
-  // Get an end point handle for use with cmRtNetSend.
104
-  cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
119
+  // Get a remote end point handle for use with cmRtNetSend.
120
+  cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
105 121
 
106 122
   // Send a message to a remote endpoint.
107 123
   cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
108 124
 
109 125
   // Send a message to a remote endpoint. This function is a composite
110 126
   // of cmRtNetEndpointHandle() and cmRtNetSend().
111
-  cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
127
+  cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
112 128
 
113 129
   cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt ); 
114 130
 

Loading…
Cancel
Save