Browse Source

cmRtNet.h/c : Added cmRtNetAddrToNodeIndex(),

cmRtNetEndpointIsValid(),cmRtNetEndPointId(),cmRtNetEndPointLabel().

Added srcEndPtId to all 'sending' functions.

_cmRtNetRecv() now sets cmRtNetMsg_t.srcNodeIdx.
master
kpl 10 years ago
parent
commit
c69698716c
2 changed files with 97 additions and 16 deletions
  1. 82
    13
      cmRtNet.c
  2. 15
    3
      cmRtNet.h

+ 82
- 13
cmRtNet.c View File

76
   return p;
76
   return p;
77
 }
77
 }
78
 
78
 
79
+cmRtNetEnd_t* _cmRtNetEndptHandleToPtr( cmRtNetEndptH_t h )
80
+{
81
+  cmRtNetEnd_t* p = (cmRtNetEnd_t*)h.h;
82
+  assert( p != NULL );
83
+  return p;
84
+}
85
+
79
 void _cmRtNetVRpt( cmRtNet_t* p, const cmChar_t* fmt, va_list vl )
86
 void _cmRtNetVRpt( cmRtNet_t* p, const cmChar_t* fmt, va_list vl )
80
 {
87
 {
81
   if( cmIsFlag(p->flags,kReportSyncNetFl) )
88
   if( cmIsFlag(p->flags,kReportSyncNetFl) )
643
   return rc;
650
   return rc;
644
 }
651
 }
645
 
652
 
653
+unsigned _cmRtNetAddrToNodeIndex( cmRtNet_t* p, const struct sockaddr_in* addr )
654
+{
655
+  unsigned i;
656
+  cmRtNetNode_t* np = p->nodes;
657
+  for(i=0; np!=NULL; np=np->link,++i)
658
+    if( cmUdpAddrIsEqual( addr, &np->sockaddr ) )
659
+      return i;
660
+
661
+  return cmInvalidIdx;
662
+}
663
+
664
+
646
 // This is called in the context of cmRtNetReceive().
665
 // This is called in the context of cmRtNetReceive().
647
 void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
666
 void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
648
 {
667
 {
649
   cmRtNet_t* p = (cmRtNet_t*)cbArg;
668
   cmRtNet_t* p = (cmRtNet_t*)cbArg;
650
   
669
   
670
+  // if this is a sync msg - then handle it here
651
   if( _cmRtNetIsSyncModeMsg(data,dataByteCnt))
671
   if( _cmRtNetIsSyncModeMsg(data,dataByteCnt))
652
     _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
672
     _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
653
-  
673
+  else
674
+  {
675
+    // All non-sync messages arriving here are prefixed by a cmRtNetMsg_t header - fill in the source addr here.
676
+
677
+    // NOTE: the source addr could be filled in by the sender but this would increase the size
678
+    // of the msg.  Instead we choose the more time consuming method of looking up the
679
+    // soure node here - hmmmm????.
680
+
681
+    cmRtNetMsg_t* hdr = (cmRtNetMsg_t*)(data);
682
+    hdr->srcNodeIdx = _cmRtNetAddrToNodeIndex(p,fromAddr);
683
+  }
684
+
654
   p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
685
   p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
655
   
686
   
656
 }
687
 }
753
   return rc;
784
   return rc;
754
 }
785
 }
755
 
786
 
787
+unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a )
788
+{
789
+  cmRtNet_t* p = _cmRtNetHandleToPtr(h);
790
+  return _cmRtNetAddrToNodeIndex(p,a);
791
+}
792
+
793
+
756
 cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
794
 cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
757
 {
795
 {
758
   cmRtNetRC_t     rc = kOkNetRC;
796
   cmRtNetRC_t     rc = kOkNetRC;
772
   return rc;
810
   return rc;
773
 }
811
 }
774
 
812
 
775
-cmRtNetRC_t _cmRtNetSend( cmRtNet_t* p, const cmRtNetEnd_t* ep, const void* msg, unsigned msgByteCnt )
813
+bool        cmRtNetEndpointIsValid( cmRtNetEndptH_t endPtH )
814
+{ return endPtH.h != NULL; }
815
+
816
+unsigned        cmRtNetEndpointId( cmRtNetEndptH_t endPtH )
817
+{
818
+  if( !cmRtNetEndpointIsValid(endPtH) )
819
+    return cmInvalidId;
820
+
821
+  cmRtNetEnd_t* ep = _cmRtNetEndptHandleToPtr( endPtH );
822
+  return ep->id;
823
+}
824
+
825
+const cmChar_t* cmRtNetEndpointLabel( cmRtNetEndptH_t endPtH )
826
+{
827
+  if( !cmRtNetEndpointIsValid(endPtH) )
828
+    return NULL;
829
+
830
+  cmRtNetEnd_t* ep = _cmRtNetEndptHandleToPtr( endPtH );
831
+  return ep->label;
832
+}
833
+
834
+
835
+cmRtNetRC_t _cmRtNetSend( cmRtNet_t* p, unsigned srcEndPtId, const cmRtNetEnd_t* ep, const void* msg, unsigned msgByteCnt )
776
 {
836
 {
777
   cmRtNetRC_t     rc = kOkNetRC;
837
   cmRtNetRC_t     rc = kOkNetRC;
778
   
838
   
783
   cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
843
   cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
784
   r->hdr.rtSubIdx     = ep->np->rtSubIdx;
844
   r->hdr.rtSubIdx     = ep->np->rtSubIdx;
785
   r->hdr.selId        = kMsgSelRtId;
845
   r->hdr.selId        = kMsgSelRtId;
786
-  r->endptId          = ep->id;          
846
+  r->dstEndPtId       = ep->id;  
847
+  r->srcEndPtId       = srcEndPtId;
787
   memcpy(data+hN,msg,msgByteCnt);
848
   memcpy(data+hN,msg,msgByteCnt);
788
 
849
 
789
   // ep->np->sockaddr identifies the node on the receiving cmRtNet.
850
   // ep->np->sockaddr identifies the node on the receiving cmRtNet.
796
   return rc;
857
   return rc;
797
 }
858
 }
798
 
859
 
799
-cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt )
860
+cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned srcEndPtId, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt )
800
 {
861
 {
801
   cmRtNet_t*      p  = _cmRtNetHandleToPtr(h);
862
   cmRtNet_t*      p  = _cmRtNetHandleToPtr(h);
802
-  cmRtNetEnd_t*   ep = (cmRtNetEnd_t*)epH.h;
863
+  cmRtNetEnd_t*   ep = _cmRtNetEndptHandleToPtr(epH);
803
  
864
  
804
   assert( ep != NULL );
865
   assert( ep != NULL );
805
-  return _cmRtNetSend(p,ep,msg,msgByteCnt);
866
+  return _cmRtNetSend(p,srcEndPtId,ep,msg,msgByteCnt);
806
 }
867
 }
807
 
868
 
808
 
869
 
809
-cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
870
+cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, unsigned srcEndPtId, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
810
 {
871
 {
811
   cmRtNetRC_t     rc  = kOkNetRC;
872
   cmRtNetRC_t     rc  = kOkNetRC;
812
   cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
873
   cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
814
   if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC )
875
   if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC )
815
     return rc;
876
     return rc;
816
 
877
 
817
-  return cmRtNetSend(h,epH,msg,msgByteCnt);
878
+  return cmRtNetSend(h,srcEndPtId,epH,msg,msgByteCnt);
818
 }
879
 }
819
 
880
 
820
-cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt )
881
+cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned srcEndPtId, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt )
821
 {
882
 {
822
   cmRtNet_t* p  = _cmRtNetHandleToPtr(h);
883
   cmRtNet_t* p  = _cmRtNetHandleToPtr(h);
823
 
884
 
826
   if((ep = _cmRtNetFindEndpt(p, nodeIdx, endptIdx )) == NULL )
887
   if((ep = _cmRtNetFindEndpt(p, nodeIdx, endptIdx )) == NULL )
827
     return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint at node index %i endpoint index %i was not found.",nodeIdx,endptIdx);
888
     return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint at node index %i endpoint index %i was not found.",nodeIdx,endptIdx);
828
 
889
 
829
-  return  _cmRtNetSend( p, ep, msg, msgByteCnt );
890
+  return  _cmRtNetSend( p, srcEndPtId, ep, msg, msgByteCnt );
830
 }
891
 }
831
 
892
 
832
 
893
 
968
   
1029
   
969
   cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
1030
   cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
970
   unsigned      i = *(unsigned*)(data + sizeof(cmRtNetMsg_t));
1031
   unsigned      i = *(unsigned*)(data + sizeof(cmRtNetMsg_t));
971
-  printf("rtSubIdx:%i endptId:%i %i\n",r->hdr.rtSubIdx,r->endptId,i);
1032
+  printf("rtSubIdx:%i endptId:%i %i\n",r->hdr.rtSubIdx,r->dstEndPtId,i);
972
 
1033
 
973
 }
1034
 }
974
 
1035
 
1000
   const cmChar_t* remoteHostStr = !mstrFl ? "master"    : "slave";
1061
   const cmChar_t* remoteHostStr = !mstrFl ? "master"    : "slave";
1001
   const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep";
1062
   const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep";
1002
   const cmChar_t* bcastAddr     = "192.168.15.255";
1063
   const cmChar_t* bcastAddr     = "192.168.15.255";
1003
-  
1064
+  cmRtNetEndptH_t eH            = cmRtNetEndptNullHandle;
1065
+  unsigned        srcEndPtId    = cmInvalidId;
1066
+
1004
   memset(&t,0,sizeof(t));
1067
   memset(&t,0,sizeof(t));
1005
 
1068
 
1006
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
1069
   if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
1016
 
1079
 
1017
   if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC )
1080
   if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC )
1018
     goto errLabel;
1081
     goto errLabel;
1082
+
1083
+  if((rc = cmRtNetEndpointHandle(p->netH, localHostStr, localEndpStr, &eH )) != kOkNetRC )
1084
+    goto errLabel;
1085
+
1086
+  if((srcEndPtId = cmRtNetEndpointId(eH)) == cmInvalidIdx )
1087
+    goto errLabel;
1019
   
1088
   
1020
   if( cmThreadPause(p->thH,0) != kOkThRC )
1089
   if( cmThreadPause(p->thH,0) != kOkThRC )
1021
     goto errLabel;
1090
     goto errLabel;
1036
 
1105
 
1037
       case 't':
1106
       case 't':
1038
         {
1107
         {
1039
-          if( cmRtNetSendByLabels(p->netH, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
1108
+          if( cmRtNetSendByLabels(p->netH, srcEndPtId, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
1040
             p->msgVal += 1;
1109
             p->msgVal += 1;
1041
 
1110
 
1042
         }        
1111
         }        

+ 15
- 3
cmRtNet.h View File

116
   // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
116
   // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
117
   cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
117
   cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
118
 
118
 
119
+  // Return the index of the node associated with sockaddr_in.
120
+  unsigned    cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
121
+
119
   // Get a remote end point handle for use with cmRtNetSend.
122
   // 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 );
123
   cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
121
 
124
 
125
+  bool        cmRtNetEndpointIsValid( cmRtNetEndptH_t endPtH );
126
+
127
+  // Given an endpoint handle return the id/label of the associated endpoint.
128
+  unsigned        cmRtNetEndpointId( cmRtNetEndptH_t endPtH );
129
+  const cmChar_t* cmRtNetEndpointLabel( cmRtNetEndptH_t endPtH );
130
+
122
   // Send a message to a remote endpoint.
131
   // Send a message to a remote endpoint.
123
-  cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
132
+  // Note that srcEndPtId is used only to inform the receiver of the endpoint
133
+  // of the transmitter. It is not used in any part of the transmit or receive
134
+  // process.
135
+  cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned srcEndPtId, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
124
 
136
 
125
   // Send a message to a remote endpoint. This function is a composite
137
   // Send a message to a remote endpoint. This function is a composite
126
   // of cmRtNetEndpointHandle() and cmRtNetSend().
138
   // of cmRtNetEndpointHandle() and cmRtNetSend().
127
-  cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
139
+  cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, unsigned srcEndPtId, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
128
 
140
 
129
-  cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt ); 
141
+  cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned srcEndPtId, unsigned dstNodeIdx, unsigned dstEndptIdx, const void* msg, unsigned msgByteCnt ); 
130
 
142
 
131
   // Enable/disable synchronization protocol reporting.
143
   // Enable/disable synchronization protocol reporting.
132
   // Return the previous state of the report sync. flag.
144
   // Return the previous state of the report sync. flag.

Loading…
Cancel
Save