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 25KB

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