libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmRtNet.c 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. #include "cmGlobal.h"
  2. #include "cmRpt.h"
  3. #include "cmErr.h"
  4. #include "cmCtx.h"
  5. #include "cmMem.h"
  6. #include "cmMallocDebug.h"
  7. #include "cmLinkedHeap.h"
  8. #include "cmUdpPort.h"
  9. #include "cmRtSysMsg.h"
  10. #include "cmRtNet.h"
  11. #include "cmTime.h"
  12. #include "cmText.h"
  13. // flags for cmRtNetNode_t.flags;
  14. enum
  15. {
  16. kLocalNodeNetFl = 0x01,
  17. kValidNodeNetFl = 0x02
  18. };
  19. // flags for cmRtNet_t.flags
  20. enum
  21. {
  22. kReportSyncNetFl = 0x01
  23. };
  24. struct cmRtNetNode_str;
  25. typedef struct cmRtNetEnd_str
  26. {
  27. cmChar_t* label;
  28. unsigned id;
  29. struct cmRtNetNode_str* np; // Owner node.
  30. struct cmRtNetEnd_str* link;
  31. } cmRtNetEnd_t;
  32. struct cmRtNet_str;
  33. typedef struct cmRtNetNode_str
  34. {
  35. unsigned rtSubIdx; // rtSubIdx of the sub-system which owns this node
  36. cmChar_t* label; // Node label.
  37. struct sockaddr_in sockaddr; // Socket address
  38. cmChar_t* addr; // IP Address (human readable)
  39. cmUdpPort_t port; // Socket port
  40. unsigned flags; // See kXXXNodeNetFl flags above.
  41. unsigned endPtIdx; // tracks the next endpoint to send during sync-mode
  42. unsigned endPtCnt; // local-node=actual cnt of endpt's remote-node:expected cnt of endpt's
  43. cmTimeSpec_t lastSendTime; // Time of last message sent
  44. cmRtNetEnd_t* ends; // End point list for this node
  45. struct cmRtNetNode_str* link;
  46. } cmRtNetNode_t;
  47. typedef struct cmRtNet_str
  48. {
  49. cmErr_t err; // Error state object
  50. unsigned flags; // See kXXXNetFl above.
  51. unsigned rtSubIdx; // rtSubIdx of the owning sub-system
  52. cmUdpH_t udpH; // UDP port handle
  53. cmUdpCallback_t cbFunc; // Client callback to receive incoming messages from network.
  54. void* cbArg; //
  55. cmRtNetNode_t* nodes; // Node list.
  56. cmRtNetNode_t* localNode; // Pointer to local node (which is also in node list)
  57. unsigned udpRecvBufByteCnt; // UDP port receive buffer size.
  58. unsigned udpTimeOutMs; // UDP time-out period
  59. cmChar_t* bcastAddr; // Network broadcast address
  60. } cmRtNet_t;
  61. cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
  62. cmRtNetEndptH_t cmRtNetEndptNullHandle = cmSTATIC_NULL_HANDLE;
  63. cmRtNet_t* _cmRtNetHandleToPtr( cmRtNetH_t h )
  64. {
  65. cmRtNet_t* p = (cmRtNet_t*)h.h;
  66. assert( p != NULL );
  67. return p;
  68. }
  69. cmRtNetEnd_t* _cmRtNetEndptHandleToPtr( cmRtNetEndptH_t h )
  70. {
  71. cmRtNetEnd_t* p = (cmRtNetEnd_t*)h.h;
  72. assert( p != NULL );
  73. return p;
  74. }
  75. void _cmRtNetVRpt( cmRtNet_t* p, const cmChar_t* fmt, va_list vl )
  76. {
  77. if( cmIsFlag(p->flags,kReportSyncNetFl) )
  78. cmRptVPrintf(p->err.rpt,fmt,vl);
  79. }
  80. void _cmRtNetRpt( cmRtNet_t* p, const cmChar_t* fmt, ... )
  81. {
  82. va_list vl;
  83. va_start(vl,fmt);
  84. _cmRtNetVRpt(p,fmt,vl);
  85. va_end(vl);
  86. }
  87. cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label )
  88. {
  89. if( label == NULL )
  90. return NULL;
  91. cmRtNetNode_t* np = p->nodes;
  92. for(; np!=NULL; np=np->link)
  93. if( strcmp(label,np->label)==0)
  94. return np;
  95. return NULL;
  96. }
  97. cmRtNetNode_t* _cmRtNetFindNodeFromSockAddr( cmRtNet_t* p, const struct sockaddr_in* saddr )
  98. {
  99. if( saddr == NULL )
  100. return NULL;
  101. cmRtNetNode_t* np = p->nodes;
  102. for(; np!=NULL; np=np->link)
  103. if( np->sockaddr.sin_addr.s_addr == saddr->sin_addr.s_addr && np->sockaddr.sin_port == saddr->sin_port )
  104. return np;
  105. return NULL;
  106. }
  107. void _cmRtNetFreeNode( cmRtNetNode_t* np )
  108. {
  109. cmRtNetEnd_t* ep = np->ends;
  110. while( ep != NULL )
  111. {
  112. cmRtNetEnd_t* nep = ep->link;
  113. cmMemFree(ep->label);
  114. cmMemFree(ep);
  115. ep = nep;
  116. }
  117. cmMemFree(np->label);
  118. cmMemFree(np->addr);
  119. cmMemFree(np);
  120. }
  121. void _cmRtNetReleaseNodes( cmRtNet_t* p )
  122. {
  123. cmRtNetNode_t* np = p->nodes;
  124. while( np != NULL )
  125. {
  126. cmRtNetNode_t* nnp = np->link;
  127. _cmRtNetFreeNode(np);
  128. np = nnp;
  129. }
  130. p->nodes = NULL;
  131. p->localNode = NULL;
  132. }
  133. cmRtNetRC_t _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
  134. {
  135. cmRtNetNode_t* cnp = p->nodes;
  136. cmRtNetNode_t* pnp = NULL;
  137. while( cnp != NULL )
  138. {
  139. cmRtNetNode_t* nnp = cnp->link;
  140. if( np == cnp )
  141. {
  142. if( pnp == NULL )
  143. p->nodes = np->link;
  144. else
  145. pnp->link = np->link;
  146. _cmRtNetFreeNode(np);
  147. return kOkNetRC;
  148. }
  149. pnp = np;
  150. cnp = nnp;
  151. }
  152. assert(0);
  153. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"Node to release not found.");
  154. }
  155. 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 )
  156. {
  157. cmRtNetRC_t rc = kOkNetRC;
  158. cmRtNetNode_t* np;
  159. if( cmTextIsEmpty(label) )
  160. return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
  161. if((np = _cmRtNetFindNode(p,label)) != NULL )
  162. return cmErrMsg(&p->err,kDuplLabelNetRC,"The node label '%s' is already in use.",cmStringNullGuard(label));
  163. np = cmMemAllocZ(cmRtNetNode_t,1);
  164. np->label = cmMemAllocStr(label);
  165. if( saddr != NULL )
  166. np->sockaddr = *saddr;
  167. np->rtSubIdx = rtSubIdx;
  168. np->addr = addr==NULL ? NULL : cmMemAllocStr(addr);
  169. np->port = port;
  170. np->flags = flags;
  171. np->endPtCnt = endPtCnt;
  172. np->link = p->nodes;
  173. p->nodes = np;
  174. return rc;
  175. }
  176. cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, const cmChar_t* endPtLabel )
  177. {
  178. cmRtNetEnd_t* ep = np->ends;
  179. for(; ep!=NULL; ep=ep->link)
  180. if( strcmp(ep->label,endPtLabel)==0 )
  181. return ep;
  182. return NULL;
  183. }
  184. cmRtNetEnd_t* _cmRtNetIndexToEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned endIndex )
  185. {
  186. cmRtNetEnd_t* ep = np->ends;
  187. unsigned i;
  188. for(i=0; ep!=NULL; ep=ep->link)
  189. {
  190. if( i == endIndex )
  191. return ep;
  192. ++i;
  193. }
  194. return NULL;
  195. }
  196. cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmChar_t* endPtLabel, unsigned endPtId )
  197. {
  198. if( endPtLabel == NULL )
  199. return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
  200. if( _cmRtNetFindNodeEnd( np, endPtLabel) != NULL)
  201. return cmErrMsg(&p->err,kDuplEndNetRC,"A duplicate endpoint ('%s') was encountered on node '%s'.",endPtLabel,np->label);
  202. cmRtNetRC_t rc = kOkNetRC;
  203. cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1);
  204. ep->label = cmMemAllocStr(endPtLabel);
  205. ep->id = endPtId;
  206. ep->np = np;
  207. ep->link = np->ends;
  208. np->ends = ep;
  209. return rc;
  210. }
  211. unsigned _cmRtNetNodeEndpointCount( cmRtNetNode_t* np )
  212. {
  213. cmRtNetEnd_t* ep = np->ends;
  214. unsigned n = 0;
  215. for(; ep!=NULL; ep=ep->link)
  216. ++n;
  217. return n;
  218. }
  219. unsigned _cmRtNetSyncMsgSerialByteCount( const cmRtNetSyncMsg_t* m )
  220. { return sizeof(cmRtNetSyncMsg_t) + (m->label==NULL ? 1 : strlen(m->label) + 1); }
  221. cmRtNetRC_t _cmRtNetSerializeSyncMsg( cmRtNet_t* p, const cmRtNetSyncMsg_t* m, void* buf, unsigned n )
  222. {
  223. unsigned bn = _cmRtNetSyncMsgSerialByteCount(m);
  224. char* b = (char*)buf;
  225. if( bn > n )
  226. return cmErrMsg(&p->err,kBufToSmallNetRC,"Serialize buffer too small.");
  227. memcpy(b,m,sizeof(*m));
  228. strcpy(b + sizeof(*m),m->label==NULL ? "" : m->label);
  229. return kOkNetRC;
  230. }
  231. cmRtNetRC_t _cmRtNetDeserializeSyncMsg( const void* buf, unsigned n, cmRtNetSyncMsg_t* m )
  232. {
  233. assert( n > sizeof(*m));
  234. memcpy(m,buf,sizeof(*m));
  235. const cmRtNetSyncMsg_t* mp = (const cmRtNetSyncMsg_t*)buf;
  236. const cmChar_t* s = (const cmChar_t*)(mp+1);
  237. m->label = cmMemAllocStr(s);
  238. return kOkNetRC;
  239. }
  240. cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* msgLabel, unsigned msgId, unsigned msgRtSubIdx )
  241. {
  242. cmRtNetSyncMsg_t m;
  243. cmRtNetRC_t rc = kOkNetRC;
  244. cmUdpRC_t udpRC = kOkUdpRC;
  245. m.hdr.rtSubIdx = cmInvalidIdx;
  246. m.hdr.selId = kNetSyncSelRtId;
  247. m.selId = selId;
  248. m.label = msgLabel;
  249. m.id = msgId;
  250. m.rtSubIdx = msgRtSubIdx;
  251. // determine size of msg to send
  252. unsigned n = _cmRtNetSyncMsgSerialByteCount(&m);
  253. cmChar_t buf[n];
  254. // serialize msg into buf[]
  255. if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
  256. return rc;
  257. // send the msg
  258. if( selId == kHelloSelNetId )
  259. udpRC = cmUdpSend2(p->udpH, buf, n, p->bcastAddr, np->port );
  260. else
  261. udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
  262. // check for send errors
  263. if( udpRC != kOkUdpRC )
  264. {
  265. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"Sync msg. send on UDP port failed.");
  266. }
  267. else
  268. {
  269. // record the last send time
  270. cmTimeGet(&np->lastSendTime);
  271. }
  272. return rc;
  273. }
  274. cmRtNetRC_t _cmRtNetFree( cmRtNet_t* p )
  275. {
  276. cmRtNetRC_t rc = kOkNetRC;
  277. if( cmUdpFree(&p->udpH) != kOkUdpRC )
  278. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port free failed.");
  279. _cmRtNetReleaseNodes(p);
  280. cmMemFree(p->bcastAddr);
  281. cmMemFree(p);
  282. return rc;
  283. }
  284. const cmRtNetNode_t* _cmRtNetIndexToRemoteNode( cmRtNet_t* p, unsigned idx )
  285. {
  286. const cmRtNetNode_t* np = p->nodes;
  287. unsigned i = 0;
  288. for(; np!=NULL; np=np->link)
  289. if( np != p->localNode )
  290. {
  291. if( i == idx )
  292. return np;
  293. ++i;
  294. }
  295. return NULL;
  296. }
  297. const cmRtNetEnd_t* _cmRtNetIndexToEndpt( const cmRtNetNode_t* np, unsigned endIdx )
  298. {
  299. const cmRtNetEnd_t* ep = np->ends;
  300. unsigned i = 0;
  301. for(; ep!=NULL; ep=ep->link,++i)
  302. if( i==endIdx )
  303. return ep;
  304. return NULL;
  305. }
  306. const cmRtNetEnd_t* _cmRtNetFindEndpt( cmRtNet_t* p, unsigned nodeIdx, unsigned epIdx )
  307. {
  308. const cmRtNetNode_t* np;
  309. const cmRtNetEnd_t* ep;
  310. if((np = _cmRtNetIndexToRemoteNode( p, nodeIdx )) == NULL )
  311. return NULL;
  312. if((ep = _cmRtNetIndexToEndpt( np, epIdx )) == NULL )
  313. return NULL;
  314. return ep;
  315. }
  316. const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m )
  317. {
  318. if( m->selId==kNodeSelNetId || m->selId==kEndpointSelNetId )
  319. return (const cmChar_t*)(m+1);
  320. return "";
  321. }
  322. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg )
  323. {
  324. cmRtNetRC_t rc;
  325. if((rc = cmRtNetFree(hp)) != kOkNetRC )
  326. return rc;
  327. cmRtNet_t* p = cmMemAllocZ(cmRtNet_t,1);
  328. cmErrSetup(&p->err,&ctx->rpt,"cmRtNet");
  329. // allocate the UDP port
  330. if(cmUdpAlloc(ctx,&p->udpH) != kOkUdpRC )
  331. {
  332. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port allocate failed.");
  333. goto errLabel;
  334. }
  335. p->rtSubIdx = rtSubIdx;
  336. p->udpTimeOutMs = 50;
  337. p->udpRecvBufByteCnt = 8192;
  338. p->cbFunc = cbFunc;
  339. p->cbArg = cbArg;
  340. hp->h = p;
  341. errLabel:
  342. if(rc != kOkNetRC )
  343. _cmRtNetFree(p);
  344. return rc;
  345. }
  346. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp )
  347. {
  348. cmRtNetRC_t rc = kOkNetRC;
  349. if( hp==NULL || cmRtNetIsValid(*hp)==false )
  350. return rc;
  351. cmRtNet_t* p = _cmRtNetHandleToPtr(*hp);
  352. if((rc = _cmRtNetFree(p)) != kOkNetRC )
  353. return rc;
  354. hp->h = NULL;
  355. return rc;
  356. }
  357. const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h )
  358. {
  359. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  360. return cmUdpHostName(p->udpH);
  361. }
  362. bool cmRtNetIsValid( cmRtNetH_t h )
  363. { return h.h !=NULL; }
  364. cmUdpH_t cmRtNetUdpPortHandle( cmRtNetH_t h )
  365. {
  366. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  367. return p->udpH;
  368. }
  369. cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t srcSelId )
  370. {
  371. cmRtNetRC_t rc = kOkNetRC;
  372. cmRtNetEnd_t* ep = NULL;
  373. const cmChar_t* msgLabel = NULL;
  374. unsigned msgId = cmInvalidId;
  375. unsigned msgRtSubIdx = cmInvalidIdx;
  376. cmRtNetSelId_t selId = kEndpointSelNetId;
  377. const cmChar_t* rptLabel = "endpoint";
  378. if( np == NULL )
  379. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found.");
  380. // if we got here by receiving a 'done' msg from the remote node ...
  381. if( srcSelId == kDoneSelNetId )
  382. {
  383. // ... then mark the remote node as having recieved all endpoints
  384. unsigned n;
  385. if((n = _cmRtNetNodeEndpointCount(np)) != np->endPtCnt )
  386. rc = cmErrMsg(&p->err,kNodeEndCntErrNetRC,"The node '%s' expected %i endpoints but received %i.",cmStringNullGuard(np->label),np->endPtCnt,n);
  387. else
  388. np->flags = cmSetFlag(np->flags,kValidNodeNetFl);
  389. }
  390. // attempt to get the next local endpoint to send ...
  391. if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) != NULL )
  392. {
  393. msgLabel = ep->label; // ... send next local endpoint
  394. msgId = ep->id;
  395. msgRtSubIdx = ep->np->rtSubIdx;
  396. }
  397. else // .... all local endpoints have been sent
  398. {
  399. selId = kInvalidSelNetId;
  400. rptLabel = "done";
  401. // verify that no endpoints are available
  402. if( np->endPtIdx < p->localNode->endPtCnt )
  403. rc = cmErrMsg(&p->err,kSyncFailNetRC,"More endpoints are available to send but are not reachable.");
  404. else
  405. {
  406. // if the remote node still has endpts to send then continue
  407. // sending 'done' messages.
  408. if( np->endPtIdx==p->localNode->endPtCnt || srcSelId != kDoneSelNetId )
  409. selId = kDoneSelNetId;
  410. }
  411. }
  412. // selId is set to kInvalidSelNetId when we encounter the (stop) criteria
  413. if( selId != kInvalidSelNetId )
  414. {
  415. if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId,msgRtSubIdx)) != kOkNetRC )
  416. rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
  417. else
  418. _cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel));
  419. np->endPtIdx += 1;
  420. }
  421. return rc;
  422. }
  423. bool _cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt )
  424. {
  425. cmRtNetSyncMsg_t* m = (cmRtNetSyncMsg_t*)data;
  426. return dataByteCnt >= sizeof(cmRtNetSyncMsg_t) && m->hdr.selId == kNetSyncSelRtId;
  427. }
  428. // When the network message recieve function (See cmRtNetAlloc() 'cbFunc')
  429. // receives a message with the cmRtSysMsgHdr_t.selId == kNetSyncSelRtId
  430. // it should call this function to update the current sync state of the
  431. // cmRtNet.
  432. cmRtNetRC_t _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  433. {
  434. cmRtNetRC_t rc = kOkNetRC;
  435. cmRtNetSyncMsg_t m;
  436. m.label = NULL;
  437. assert( _cmRtNetIsSyncModeMsg(data,dataByteCnt));
  438. if( _cmRtNetDeserializeSyncMsg(data,dataByteCnt,&m) != kOkNetRC )
  439. {
  440. rc = cmErrMsg(&p->err,rc,"Net sync. receive failed due to deserialize fail.");
  441. goto errLabel;
  442. }
  443. _cmRtNetRpt(p,"recv from:%s\n",cmUdpAddrToString(p->udpH, fromAddr ));
  444. assert( m.hdr.selId == kNetSyncSelRtId );
  445. // attempt to locate the remote node which sent the msg
  446. cmRtNetNode_t* np = _cmRtNetFindNodeFromSockAddr(p,fromAddr);
  447. switch( m.selId )
  448. {
  449. case kHelloSelNetId:
  450. // if this is a response to a broadcast from the local node then ignore it
  451. if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label)) != NULL && strcmp(p->localNode->label,m.label)==0 )
  452. {
  453. const cmChar_t* fromAddrStr = cmUdpAddrToString(p->udpH,fromAddr);
  454. const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH));
  455. if( fromAddrStr!=NULL && localAddrStr!=NULL && strcmp(fromAddrStr,localAddrStr)!=0)
  456. cmErrMsg(&p->err,kDuplLocalNetRC,"The node label '%s' appears to be duplicated at address %s and locally.",cmStringNullGuard(m.label),fromAddrStr);
  457. np->sockaddr = *fromAddr;
  458. goto errLabel;
  459. }
  460. // fall through
  461. case kNodeSelNetId:
  462. {
  463. // if the node already exists ...
  464. if( np != NULL )
  465. {
  466. // ... delete it because we are about to get new info. about it.
  467. if((rc = _cmRtNetReleaseNode(p,np )) != kOkNetRC )
  468. goto errLabel;
  469. }
  470. // create a node proxy to represent the remote node
  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,m.rtSubIdx,NULL,0,fromAddr,0,m.id)) != kOkNetRC )
  473. goto errLabel;
  474. np = p->nodes; // newest node is always the first node
  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, p->localNode->label, p->localNode->endPtCnt, p->localNode->rtSubIdx );
  481. break;
  482. case kNodeSelNetId:
  483. _cmRtNetRpt(p,"rcv node\n");
  484. _cmRtNetSendEndpointReplyMsg( p, np, m.selId ); // reply with first endpoint
  485. break;
  486. default:
  487. assert(0);
  488. }
  489. }
  490. break;
  491. case kDoneSelNetId:
  492. //case kEndpointAckSelNetId:
  493. rc = _cmRtNetSendEndpointReplyMsg(p,np,m.selId);
  494. break;
  495. case kEndpointSelNetId:
  496. {
  497. cmRtNetEnd_t* ep;
  498. // verify the remote node exists.
  499. if( np == NULL )
  500. {
  501. rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
  502. goto errLabel;
  503. }
  504. // attempt to find the end point
  505. if((ep = _cmRtNetFindNodeEnd(np, m.label)) != NULL )
  506. ep->id = m.id; // the endpoint was found update the endPtId
  507. else
  508. {
  509. // create a local proxy for the endpoint
  510. if((rc = _cmRtNetCreateEndpoint(p,np,m.label,m.id)) != kOkNetRC )
  511. goto errLabel;
  512. }
  513. // reply with a local endpoint or 'done' msg
  514. rc = _cmRtNetSendEndpointReplyMsg( p, np, m.selId );
  515. }
  516. break;
  517. default:
  518. assert(0);
  519. break;
  520. }
  521. errLabel:
  522. cmMemFree((cmChar_t*)m.label);
  523. return rc;
  524. }
  525. unsigned _cmRtNetAddrToNodeIndex( cmRtNet_t* p, const struct sockaddr_in* addr )
  526. {
  527. unsigned i;
  528. cmRtNetNode_t* np = p->nodes;
  529. for(i=0; np!=NULL; np=np->link,++i)
  530. if( cmUdpAddrIsEqual( addr, &np->sockaddr ) )
  531. return i;
  532. return cmInvalidIdx;
  533. }
  534. // This is called in the context of cmRtNetReceive().
  535. void _cmRtNetRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  536. {
  537. cmRtNet_t* p = (cmRtNet_t*)cbArg;
  538. // if this is a sync msg - then handle it here
  539. if( _cmRtNetIsSyncModeMsg(data,dataByteCnt))
  540. _cmRtNetSyncModeRecv(p,data,dataByteCnt,fromAddr);
  541. else
  542. {
  543. // All non-sync messages arriving here are prefixed by a cmRtNetMsg_t header - fill in the source addr here.
  544. // NOTE: the source addr could be filled in by the sender but this would increase the size
  545. // of the msg. Instead we choose the more time consuming method of looking up the
  546. // soure node here - hmmmm????.
  547. cmRtNetMsg_t* hdr = (cmRtNetMsg_t*)(data);
  548. hdr->srcNodeIdx = _cmRtNetAddrToNodeIndex(p,fromAddr);
  549. }
  550. p->cbFunc(p->cbArg,data,dataByteCnt,fromAddr);
  551. }
  552. cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
  553. {
  554. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  555. cmRtNetRC_t rc;
  556. // release the local node
  557. if((rc = cmRtNetFinalize(h)) != kOkNetRC )
  558. goto errLabel;
  559. if( cmTextIsEmpty(bcastAddr) )
  560. {
  561. rc = cmErrMsg(&p->err,kInvalidArgNetRC,"The 'broadcast address' is not valid.");
  562. goto errLabel;
  563. }
  564. // if this is the local node then initialze the local socket
  565. if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl | kBroadcastUdpFl,_cmRtNetRecv,p,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
  566. {
  567. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port initialization failed.");
  568. goto errLabel;
  569. }
  570. // create the local node
  571. if((rc = _cmRtNetCreateNode(p,nodeLabel, p->rtSubIdx, ipAddr, port, NULL, kLocalNodeNetFl, 0)) != kOkNetRC )
  572. goto errLabel;
  573. // the last created node is always the first node on the list
  574. p->localNode = p->nodes;
  575. p->bcastAddr = cmMemResizeStr(p->bcastAddr,bcastAddr);
  576. // begin listening on the local port
  577. if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
  578. {
  579. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port failed to enter 'listen' mode.");
  580. goto errLabel;
  581. }
  582. errLabel:
  583. return rc;
  584. }
  585. bool cmRtNetIsInitialized( cmRtNetH_t h )
  586. {
  587. if( cmRtNetIsValid(h) == false )
  588. return false;
  589. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  590. return p->localNode != NULL && cmTextIsNotEmpty(p->bcastAddr);
  591. }
  592. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId )
  593. {
  594. cmRtNetRC_t rc = kOkNetRC;
  595. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  596. if( p->localNode == NULL )
  597. return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
  598. if((rc = _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId )) == kOkNetRC )
  599. p->localNode->endPtCnt += 1;
  600. return rc;
  601. }
  602. cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h )
  603. {
  604. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  605. _cmRtNetReleaseNodes(p);
  606. return kOkNetRC;
  607. }
  608. cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h )
  609. {
  610. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  611. // broadcast 'node' msg
  612. return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
  613. }
  614. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
  615. {
  616. cmRtNetRC_t rc = kOkNetRC;
  617. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  618. // Calling this function results in callbacks to _cmRtNetRecv() (above)
  619. if( cmUdpGetAvailData(p->udpH, NULL, NULL, NULL ) != kOkUdpRC )
  620. {
  621. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP port query failed.");
  622. goto errLabel;
  623. }
  624. errLabel:
  625. return rc;
  626. }
  627. unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a )
  628. {
  629. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  630. return _cmRtNetAddrToNodeIndex(p,a);
  631. }
  632. cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
  633. {
  634. cmRtNetRC_t rc = kOkNetRC;
  635. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  636. cmRtNetNode_t* np;
  637. cmRtNetEnd_t* ep;
  638. if(( np = _cmRtNetFindNode(p,nodeLabel)) == NULL )
  639. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
  640. if(( ep = _cmRtNetFindNodeEnd(np, endptLabel )) == NULL )
  641. return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel));
  642. hp->h = ep;
  643. return rc;
  644. }
  645. bool cmRtNetEndpointIsValid( cmRtNetEndptH_t endPtH )
  646. { return endPtH.h != NULL; }
  647. unsigned cmRtNetEndpointId( cmRtNetEndptH_t endPtH )
  648. {
  649. if( !cmRtNetEndpointIsValid(endPtH) )
  650. return cmInvalidId;
  651. cmRtNetEnd_t* ep = _cmRtNetEndptHandleToPtr( endPtH );
  652. return ep->id;
  653. }
  654. const cmChar_t* cmRtNetEndpointLabel( cmRtNetEndptH_t endPtH )
  655. {
  656. if( !cmRtNetEndpointIsValid(endPtH) )
  657. return NULL;
  658. cmRtNetEnd_t* ep = _cmRtNetEndptHandleToPtr( endPtH );
  659. return ep->label;
  660. }
  661. cmRtNetRC_t _cmRtNetSend( cmRtNet_t* p, unsigned srcEndPtId, const cmRtNetEnd_t* ep, const void* msg, unsigned msgByteCnt )
  662. {
  663. cmRtNetRC_t rc = kOkNetRC;
  664. unsigned hN = sizeof(cmRtNetMsg_t);
  665. unsigned dN = hN + msgByteCnt;
  666. char data[ dN ];
  667. cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
  668. r->hdr.rtSubIdx = ep->np->rtSubIdx;
  669. r->hdr.selId = kMsgSelRtId;
  670. r->dstEndPtId = ep->id;
  671. r->srcEndPtId = srcEndPtId;
  672. memcpy(data+hN,msg,msgByteCnt);
  673. // ep->np->sockaddr identifies the node on the receiving cmRtNet.
  674. // cmRtNetMsg_t* r.endptId is then used by the receiving cmRtNet to indicate which endpoint on
  675. // the node the incoming message should be associated with.
  676. if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC )
  677. return cmErrMsg(&p->err,kUdpPortFailNetRC,"Send to node:%s endpt:%s failed.\n",cmStringNullGuard(ep->np->label),cmStringNullGuard(ep->label));
  678. return rc;
  679. }
  680. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned srcEndPtId, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt )
  681. {
  682. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  683. cmRtNetEnd_t* ep = _cmRtNetEndptHandleToPtr(epH);
  684. assert( ep != NULL );
  685. return _cmRtNetSend(p,srcEndPtId,ep,msg,msgByteCnt);
  686. }
  687. cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, unsigned srcEndPtId, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
  688. {
  689. cmRtNetRC_t rc = kOkNetRC;
  690. cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
  691. if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC )
  692. return rc;
  693. return cmRtNetSend(h,srcEndPtId,epH,msg,msgByteCnt);
  694. }
  695. cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned srcEndPtId, unsigned nodeIdx, unsigned endptIdx, const void* msg, unsigned msgByteCnt )
  696. {
  697. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  698. const cmRtNetEnd_t* ep;
  699. if((ep = _cmRtNetFindEndpt(p, nodeIdx, endptIdx )) == NULL )
  700. return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint at node index %i endpoint index %i was not found.",nodeIdx,endptIdx);
  701. return _cmRtNetSend( p, srcEndPtId, ep, msg, msgByteCnt );
  702. }
  703. bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl )
  704. {
  705. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  706. bool fl = cmIsFlag(p->flags,kReportSyncNetFl);
  707. p->flags = cmEnaFlag(p->flags,kReportSyncNetFl,enableFl);
  708. return fl;
  709. }
  710. bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h )
  711. {
  712. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  713. return cmIsFlag(p->flags,kReportSyncNetFl);
  714. }
  715. void cmRtNetReport( cmRtNetH_t h )
  716. {
  717. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  718. cmRpt_t* rpt = p->err.rpt;
  719. cmRtNetNode_t* np = p->nodes;
  720. for(; np!=NULL; np=np->link)
  721. {
  722. cmRptPrintf(rpt,"Node: %s ",np->label);
  723. if( np->addr != NULL )
  724. cmRptPrintf(rpt,"%s ",np->addr );
  725. if( cmIsFlag(np->flags,kLocalNodeNetFl) )
  726. cmRptPrintf(rpt,"LOCAL ");
  727. cmRptPrintf(rpt,"%s ",cmStringNullGuard(cmUdpAddrToString(p->udpH,&np->sockaddr)));
  728. if( np->port != kInvalidUdpPortNumber )
  729. cmRptPrintf(rpt,"%i ",np->port );
  730. cmRptPrintf(rpt,"\n");
  731. cmRtNetEnd_t* ep = np->ends;
  732. for(; ep!=NULL; ep=ep->link)
  733. {
  734. cmRptPrintf(rpt," endpt: %i %s\n",ep->id,cmStringNullGuard(ep->label ));
  735. }
  736. }
  737. }
  738. const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h )
  739. {
  740. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  741. return p->localNode->label;
  742. }
  743. unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h )
  744. {
  745. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  746. const cmRtNetNode_t* np = p->nodes;
  747. unsigned n = 0;
  748. for(; np!=NULL; np=np->link)
  749. if( np != p->localNode )
  750. ++n;
  751. return n;
  752. }
  753. const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx )
  754. {
  755. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  756. const cmRtNetNode_t* np;
  757. if((np = _cmRtNetIndexToRemoteNode( p, idx )) == NULL )
  758. return NULL;
  759. return np->label;
  760. }
  761. unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx )
  762. {
  763. const cmRtNetNode_t* np;
  764. const cmRtNetEnd_t* ep;
  765. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  766. unsigned n = 0;
  767. if((np = _cmRtNetIndexToRemoteNode( p, nodeIdx )) == NULL )
  768. return 0;
  769. for(ep=np->ends; ep!=NULL; ep=ep->link)
  770. ++n;
  771. return n;
  772. }
  773. cmRtNetRC_t cmRtNetRemoteNodeEndPoint(
  774. cmRtNetH_t h,
  775. unsigned nodeIdx,
  776. unsigned epIdx,
  777. const cmChar_t** labelRef,
  778. unsigned* idRef,
  779. unsigned* rsiRef )
  780. {
  781. const cmRtNetEnd_t* ep;
  782. cmRtNet_t* p = _cmRtNetHandleToPtr( h );
  783. if(( ep = _cmRtNetFindEndpt(p, nodeIdx, epIdx )) == NULL )
  784. {
  785. *labelRef = NULL;
  786. *idRef = cmInvalidId;
  787. *rsiRef = cmInvalidIdx;
  788. return kEndNotFoundNetRC;
  789. }
  790. *labelRef = ep->label;
  791. *idRef = ep->id;
  792. *rsiRef = ep->np->rtSubIdx;
  793. return kOkNetRC;
  794. }
  795. //==========================================================================
  796. #include "cmThread.h"
  797. typedef struct
  798. {
  799. cmThreadH_t thH;
  800. cmRtNetH_t netH;
  801. unsigned msgVal;
  802. } _cmRtNetTest_t;
  803. // This function is called within the context of cmRtNetReceive().
  804. void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  805. {
  806. //_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
  807. cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
  808. unsigned i = *(unsigned*)(data + sizeof(cmRtNetMsg_t));
  809. printf("rtSubIdx:%i endptId:%i %i\n",r->hdr.rtSubIdx,r->dstEndPtId,i);
  810. }
  811. bool _cmRtNetTestThreadFunc(void* param)
  812. {
  813. _cmRtNetTest_t* p = (_cmRtNetTest_t*)param;
  814. if( cmRtNetIsValid(p->netH) )
  815. {
  816. cmRtNetReceive(p->netH);
  817. }
  818. cmSleepMs(40);
  819. return true;
  820. }
  821. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
  822. {
  823. char c;
  824. _cmRtNetTest_t t;
  825. const unsigned rtSubIdx = 0;
  826. cmUdpPort_t port = 5876;
  827. _cmRtNetTest_t* p = &t;
  828. cmRtNetRC_t rc = kOkNetRC;
  829. const cmChar_t* localHostStr = mstrFl ? "master" : "slave";
  830. const cmChar_t* localEndpStr = mstrFl ? "master_ep" : "slave_ep";
  831. const cmChar_t* remoteHostStr = !mstrFl ? "master" : "slave";
  832. const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep";
  833. const cmChar_t* bcastAddr = "192.168.15.255";
  834. cmRtNetEndptH_t eH = cmRtNetEndptNullHandle;
  835. unsigned srcEndPtId = cmInvalidId;
  836. memset(&t,0,sizeof(t));
  837. if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
  838. goto errLabel;
  839. if((rc = cmRtNetAlloc(ctx,&p->netH,rtSubIdx,_cmRtNetTestRecv,p)) != kOkNetRC )
  840. goto errLabel;
  841. cmRtNetReportSyncEnable(p->netH,true); // enable sync. protocol reporting
  842. if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC)
  843. goto errLabel;
  844. if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC )
  845. goto errLabel;
  846. if((rc = cmRtNetEndpointHandle(p->netH, localHostStr, localEndpStr, &eH )) != kOkNetRC )
  847. goto errLabel;
  848. if((srcEndPtId = cmRtNetEndpointId(eH)) == cmInvalidIdx )
  849. goto errLabel;
  850. if( cmThreadPause(p->thH,0) != kOkThRC )
  851. goto errLabel;
  852. cmRptPrintf(&ctx->rpt,"%s t=transmit s=sync r=report q=quit\n", localHostStr );
  853. while( (c=getchar()) != 'q' )
  854. {
  855. switch(c)
  856. {
  857. case 'r':
  858. cmRtNetReport(p->netH);
  859. break;
  860. case 's':
  861. cmRtNetDoSync(p->netH);
  862. break;
  863. case 't':
  864. {
  865. if( cmRtNetSendByLabels(p->netH, srcEndPtId, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
  866. p->msgVal += 1;
  867. }
  868. break;
  869. }
  870. }
  871. errLabel:
  872. cmThreadDestroy(&p->thH);
  873. cmRtNetFree(&p->netH);
  874. return;
  875. }