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

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