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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. enum
  12. {
  13. kLocalNetFl = 0x01,
  14. kSockAddrNetFl = 0x02
  15. };
  16. typedef enum
  17. {
  18. kSendHelloStNetId = 0,
  19. kWaitHelloAckStNetId,
  20. kSendEndpointStNetId,
  21. kWaitEndpointAckStNetId,
  22. kDoneStNetId,
  23. kErrorStNetId,
  24. kInvalidStNetId,
  25. } cmRtNetNodeState_t;
  26. typedef enum
  27. {
  28. kHelloSelNetId,
  29. kHelloAckSelNetId,
  30. kEndpointSelNetId,
  31. kEndpointAckSelNetId
  32. } cmRtNetSelId_t;
  33. typedef struct cmRtNetEnd_str
  34. {
  35. cmChar_t* endPtLabel;
  36. unsigned endPtId;
  37. struct cmRtNetEnd_str* link;
  38. } cmRtNetEnd_t;
  39. typedef struct cmRtNetNode_str
  40. {
  41. cmChar_t* label;
  42. struct sockaddr_in sockaddr;
  43. cmChar_t* addr;
  44. cmUdpPort_t port;
  45. unsigned flags;
  46. cmRtNetNodeState_t state;
  47. unsigned epIdx;
  48. cmTimeSpec_t lastSendTime;
  49. cmRtNetEnd_t* ends;
  50. struct cmRtNetNode_str* link;
  51. } cmRtNetNode_t;
  52. typedef struct
  53. {
  54. cmErr_t err;
  55. cmUdpH_t udpH;
  56. cmUdpCallback_t cbFunc;
  57. void* cbArg;
  58. cmRtNetNode_t* nodes;
  59. cmRtNetNode_t* localNode;
  60. bool syncModeFl;
  61. unsigned udpRecvBufByteCnt;
  62. unsigned udpTimeOutMs;
  63. unsigned interSyncSendTimeMs;
  64. } cmRtNet_t;
  65. typedef struct
  66. {
  67. cmRtSysMsgHdr_t hdr;
  68. cmRtNetSelId_t selId;
  69. const cmChar_t* endPtLabel;
  70. unsigned endPtId;
  71. } cmRtNetSyncMsg_t;
  72. cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
  73. cmRtNet_t* _cmRtNetHandleToPtr( cmRtNetH_t h )
  74. {
  75. cmRtNet_t* p = (cmRtNet_t*)h.h;
  76. assert( p != NULL );
  77. return p;
  78. }
  79. void _cmRtNetVRpt( cmRtNet_t* p, const cmChar_t* fmt, va_list vl )
  80. {
  81. cmRptVPrintf(p->err.rpt,fmt,vl);
  82. }
  83. void _cmRtNetRpt( cmRtNet_t* p, const cmChar_t* fmt, ... )
  84. {
  85. va_list vl;
  86. va_start(vl,fmt);
  87. _cmRtNetVRpt(p,fmt,vl);
  88. va_end(vl);
  89. }
  90. cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label )
  91. {
  92. if( label == NULL )
  93. return NULL;
  94. cmRtNetNode_t* np = p->nodes;
  95. for(; np!=NULL; np=np->link)
  96. if( strcmp(label,np->label)==0)
  97. return np;
  98. return NULL;
  99. }
  100. cmRtNetNode_t* _cmRtNetFindNodeFromSockAddr( cmRtNet_t* p, const struct sockaddr_in* saddr )
  101. {
  102. if( saddr == NULL )
  103. return NULL;
  104. cmRtNetNode_t* np = p->nodes;
  105. for(; np!=NULL; np=np->link)
  106. if( cmIsFlag(np->flags,kSockAddrNetFl) && np->sockaddr.sin_addr.s_addr == saddr->sin_addr.s_addr && np->sockaddr.sin_port == saddr->sin_port )
  107. return np;
  108. return NULL;
  109. }
  110. void _cmRtNetFreeNode( cmRtNetNode_t* np )
  111. {
  112. cmRtNetEnd_t* ep = np->ends;
  113. while( ep != NULL )
  114. {
  115. cmRtNetEnd_t* nep = ep->link;
  116. cmMemFree(ep->endPtLabel);
  117. cmMemFree(ep);
  118. ep = nep;
  119. }
  120. cmMemFree(np->label);
  121. cmMemFree(np->addr);
  122. cmMemFree(np);
  123. }
  124. void _cmRtNetReleaseNodes( cmRtNet_t* p )
  125. {
  126. cmRtNetNode_t* np = p->nodes;
  127. while( np != NULL )
  128. {
  129. cmRtNetNode_t* nnp = np->link;
  130. _cmRtNetFreeNode(np);
  131. np = nnp;
  132. }
  133. p->nodes = NULL;
  134. p->localNode = NULL;
  135. }
  136. cmRtNetRC_t _cmRtNetReleaseNode( cmRtNet_t* p, cmRtNetNode_t* np )
  137. {
  138. // we should never release the local node via this function
  139. assert( np != p->localNode );
  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 )
  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. bool localNodeFl = addr==NULL && saddr==NULL;
  169. if( localNodeFl && p->localNode != NULL )
  170. return cmErrMsg(&p->err,kDuplLocalNetRC,"The local node '%s' has already been set.",cmStringNullGuard(p->localNode->label));
  171. np = cmMemAllocZ(cmRtNetNode_t,1);
  172. np->label = cmMemAllocStr(label);
  173. np->addr = addr==NULL ? NULL : cmMemAllocStr(addr);
  174. np->port = port;
  175. np->flags = cmEnaFlag(np->flags,kLocalNetFl,localNodeFl);
  176. np->link = p->nodes;
  177. p->nodes = np;
  178. if( localNodeFl )
  179. p->localNode = np;
  180. if( saddr != NULL )
  181. np->sockaddr = *saddr;
  182. else
  183. {
  184. if( cmUdpInitAddr(p->udpH, np->addr, np->port, &np->sockaddr ) != kOkUdpRC )
  185. {
  186. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"IP::port to socket address conversion failed.");
  187. goto errLabel;
  188. }
  189. }
  190. np->flags = cmSetFlag(np->flags,kSockAddrNetFl);
  191. errLabel:
  192. return rc;
  193. }
  194. cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, const cmChar_t* endPtLabel )
  195. {
  196. cmRtNetEnd_t* ep = np->ends;
  197. for(; ep!=NULL; ep=ep->link)
  198. if( strcmp(ep->endPtLabel,endPtLabel)==0 )
  199. return ep;
  200. return NULL;
  201. }
  202. cmRtNetEnd_t* _cmRtNetIndexToEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned endIndex )
  203. {
  204. cmRtNetEnd_t* ep = np->ends;
  205. unsigned i;
  206. for(i=0; ep!=NULL; ep=ep->link)
  207. {
  208. if( i == endIndex )
  209. return ep;
  210. ++i;
  211. }
  212. return NULL;
  213. }
  214. cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmChar_t* endPtLabel, unsigned endPtId )
  215. {
  216. if( endPtLabel == NULL )
  217. return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
  218. if( _cmRtNetFindNodeEnd( np, endPtLabel) != NULL)
  219. return cmErrMsg(&p->err,kDuplEndNetRC,"A duplicate endpoint ('%s') was encountered on node '%s'.",endPtLabel,np->label);
  220. cmRtNetRC_t rc = kOkNetRC;
  221. cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1);
  222. ep->endPtLabel = cmMemAllocStr(endPtLabel);
  223. ep->endPtId = endPtId;
  224. ep->link = np->ends;
  225. np->ends = ep;
  226. return rc;
  227. }
  228. unsigned _cmRtNetSyncMsgSerialByteCount( const cmRtNetSyncMsg_t* m )
  229. { return sizeof(cmRtNetSyncMsg_t) + (m->endPtLabel==NULL ? 1 : strlen(m->endPtLabel) + 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->endPtLabel==NULL ? "" : m->endPtLabel);
  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->endPtLabel = cmMemAllocStr(s);
  247. return kOkNetRC;
  248. }
  249. cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* endPtLabel, unsigned endPtId, cmRtNetNodeState_t nextStId )
  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.endPtLabel = endPtLabel;
  258. m.endPtId = endPtId;
  259. // determine size of msg to send
  260. unsigned n = _cmRtNetSyncMsgSerialByteCount(&m);
  261. cmChar_t buf[n];
  262. // serialize msg into buf[]
  263. if((rc = _cmRtNetSerializeSyncMsg(p,&m,buf,n)) != kOkNetRC )
  264. return rc;
  265. // store this nodes current sync state
  266. cmRtNetNodeState_t orgState = np->state;
  267. np->state = nextStId;
  268. // send the msg
  269. if( cmIsFlag(np->flags,kSockAddrNetFl) == false )
  270. udpRC = cmUdpSend2(p->udpH, buf, n, np->addr, np->port );
  271. else
  272. udpRC = cmUdpSendTo(p->udpH, buf, n, &np->sockaddr );
  273. // check for send errors
  274. if( udpRC != kOkUdpRC )
  275. {
  276. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"Sync msg. send on UDP port failed.");
  277. np->state = orgState; // restore node state so we can try again
  278. }
  279. else
  280. {
  281. // record the last send time
  282. cmTimeGet(&np->lastSendTime);
  283. }
  284. return rc;
  285. }
  286. cmRtNetRC_t _cmRtNetFree( cmRtNet_t* p )
  287. {
  288. cmRtNetRC_t rc = kOkNetRC;
  289. if( cmUdpFree(&p->udpH) != kOkUdpRC )
  290. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port free failed.");
  291. _cmRtNetReleaseNodes(p);
  292. cmMemFree(p);
  293. return rc;
  294. }
  295. cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg )
  296. {
  297. cmRtNetRC_t rc;
  298. if((rc = cmRtNetFree(hp)) != kOkNetRC )
  299. return rc;
  300. cmRtNet_t* p = cmMemAllocZ(cmRtNet_t,1);
  301. cmErrSetup(&p->err,&ctx->rpt,"cmRtNet");
  302. // allocate the UDP port
  303. if(cmUdpAlloc(ctx,&p->udpH) != kOkUdpRC )
  304. {
  305. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP Port allocate failed.");
  306. goto errLabel;
  307. }
  308. p->udpTimeOutMs = 50;
  309. p->udpRecvBufByteCnt = 8192;
  310. p->interSyncSendTimeMs = 10;
  311. p->cbFunc = cbFunc;
  312. p->cbArg = cbArg;
  313. hp->h = p;
  314. errLabel:
  315. if(rc != kOkNetRC )
  316. _cmRtNetFree(p);
  317. return rc;
  318. }
  319. cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp )
  320. {
  321. cmRtNetRC_t rc = kOkNetRC;
  322. if( hp==NULL || cmRtNetIsValid(*hp)==false )
  323. return rc;
  324. cmRtNet_t* p = _cmRtNetHandleToPtr(*hp);
  325. if((rc = _cmRtNetFree(p)) != kOkNetRC )
  326. return rc;
  327. hp->h = NULL;
  328. return rc;
  329. }
  330. bool cmRtNetIsValid( cmRtNetH_t h )
  331. { return h.h !=NULL; }
  332. cmUdpH_t cmRtNetUdpPortHandle( cmRtNetH_t h )
  333. {
  334. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  335. return p->udpH;
  336. }
  337. cmRtNetRC_t cmRtNetCreateNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t port )
  338. {
  339. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  340. cmRtNetRC_t rc;
  341. // create a node
  342. if((rc = _cmRtNetCreateNode(p,nodeLabel,ipAddr, port, NULL)) != kOkNetRC )
  343. return rc;
  344. // if this is not the local node
  345. if( ipAddr != NULL )
  346. return rc;
  347. // if this is the local node then initialze the local socket
  348. if( cmUdpInit(p->udpH,port,kNonBlockingUdpFl,p->cbFunc,p->cbArg,NULL,0,p->udpRecvBufByteCnt,p->udpTimeOutMs) != kOkUdpRC )
  349. {
  350. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port initialization failed.");
  351. goto errLabel;
  352. }
  353. // begin listening on the local port
  354. if( cmUdpEnableListen(p->udpH, true ) != kOkUdpRC )
  355. {
  356. rc = cmErrMsg(&p->err,kUdpPortFailNetRC,"The UDP port failed to enter 'listen' mode.");
  357. goto errLabel;
  358. }
  359. errLabel:
  360. return rc;
  361. }
  362. cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId )
  363. {
  364. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  365. if( p->localNode == NULL )
  366. return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
  367. return _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId );
  368. }
  369. cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h )
  370. {
  371. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  372. _cmRtNetReleaseNodes(p);
  373. return kOkNetRC;
  374. }
  375. cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h )
  376. {
  377. cmRtNetRC_t rc = kOkNetRC;
  378. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  379. p->syncModeFl = true;
  380. return rc;
  381. }
  382. bool cmRtNetIsInSyncMode( cmRtNetH_t h )
  383. {
  384. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  385. return p->syncModeFl;
  386. }
  387. // Used by slaves to send the master an 'ack' msg.
  388. cmRtNetRC_t _cmRtNetSendAck( cmRtNet_t* p, cmRtNetSelId_t ackSelId, const struct sockaddr_in* saddr )
  389. {
  390. cmRtNetNode_t* np;
  391. if((np = _cmRtNetFindNodeFromSockAddr(p,saddr)) == NULL )
  392. return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an ack cmd was not found. Ack not sent.");
  393. return _cmRtNetSendSyncMsg(p,np,ackSelId,NULL,cmInvalidId,kInvalidStNetId);
  394. }
  395. // Used by master to update state upon receipt of 'ack' msg
  396. cmRtNetRC_t _cmRtNetRecvAck( cmRtNet_t* p, const struct sockaddr_in* fromAddr, cmRtNetNodeState_t expectedState, cmRtNetNodeState_t nextState )
  397. {
  398. cmRtNetNode_t* np;
  399. cmRtNetRC_t rc = kOkNetRC;
  400. if((np = _cmRtNetFindNodeFromSockAddr(p,fromAddr)) == NULL )
  401. {
  402. rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with a ack receive was not found.");
  403. goto errLabel;
  404. }
  405. if( np->state != expectedState )
  406. {
  407. rc = cmErrMsg(&p->err,kNodeStateErrNetRC,"Node '%s' expected in state %i was in state %i.",kWaitHelloAckStNetId,np->state);
  408. np->state = kErrorStNetId;
  409. goto errLabel;
  410. }
  411. np->state = nextState;
  412. errLabel:
  413. return rc;
  414. }
  415. cmRtNetRC_t cmRtNetSyncModeRecv( cmRtNetH_t h, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  416. {
  417. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  418. cmRtNetRC_t rc = kOkNetRC;
  419. cmRtNetNode_t* np = NULL;
  420. cmRtNetSyncMsg_t m;
  421. assert( cmRtNetIsSyncModeMsg(data,dataByteCnt));
  422. if( _cmRtNetDeserializeSyncMsg(data,dataByteCnt,&m) != kOkNetRC )
  423. {
  424. rc = cmErrMsg(&p->err,rc,"Net sync. receive failed due to deserialize fail.");
  425. goto errLabel;
  426. }
  427. assert( m.hdr.selId == kNetSyncSelRtId );
  428. switch( m.selId )
  429. {
  430. case kHelloSelNetId: // slave response
  431. {
  432. _cmRtNetRpt(p,"rcv hello\n");
  433. // attempt to locate the remote node which sent the endpoint
  434. if((np = _cmRtNetFindNodeFromSockAddr(p,fromAddr)) != NULL )
  435. {
  436. // delete the existing node 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. if(( rc = _cmRtNetCreateNode(p,m.endPtLabel,NULL,0,fromAddr)) != kOkNetRC )
  442. goto errLabel;
  443. // send an ackknowledgement of the 'hello' msg
  444. rc = _cmRtNetSendAck(p,kHelloAckSelNetId,fromAddr);
  445. }
  446. break;
  447. case kEndpointSelNetId: // slave response
  448. {
  449. cmRtNetEnd_t* ep;
  450. _cmRtNetRpt(p,"rcv endpoint\n");
  451. // locate the remote node which sent the endpoint
  452. if((np = _cmRtNetFindNodeFromSockAddr(p,fromAddr)) == NULL )
  453. {
  454. rc = cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint receive was not found.");
  455. goto errLabel;
  456. }
  457. // attempt to find the end point
  458. if((ep = _cmRtNetFindNodeEnd(np,m.endPtLabel)) != NULL )
  459. ep->endPtId = m.endPtId; // the endpoint was found update the endPtId
  460. else
  461. {
  462. // create a local proxy for the endpoint
  463. if((rc = _cmRtNetCreateEndpoint(p,np,m.endPtLabel,m.endPtId)) != kOkNetRC )
  464. goto errLabel;
  465. }
  466. // ack. the endpoint msg
  467. rc = _cmRtNetSendAck(p,kEndpointAckSelNetId,fromAddr);
  468. }
  469. break;
  470. case kHelloAckSelNetId: // master response
  471. assert( p->syncModeFl );
  472. _cmRtNetRpt(p,"rcv hello ack\n");
  473. rc = _cmRtNetRecvAck(p,fromAddr,kWaitHelloAckStNetId,kSendEndpointStNetId);
  474. break;
  475. case kEndpointAckSelNetId: // master response
  476. assert( p->syncModeFl );
  477. _cmRtNetRpt(p,"rcv endpoint ack\n");
  478. rc = _cmRtNetRecvAck(p,fromAddr,kWaitEndpointAckStNetId,kSendEndpointStNetId);
  479. break;
  480. default:
  481. break;
  482. }
  483. errLabel:
  484. return rc;
  485. }
  486. cmRtNetRC_t _cmRtNetSendNodeSync( cmRtNet_t* p, cmRtNetNode_t* np )
  487. {
  488. cmRtNetRC_t rc = kOkNetRC;
  489. switch( np->state )
  490. {
  491. case kSendHelloStNetId:
  492. // send a 'hello' to this remote node
  493. if((rc = _cmRtNetSendSyncMsg(p,np,kHelloSelNetId,p->localNode->label, cmInvalidId, kWaitHelloAckStNetId )) != kOkNetRC )
  494. rc = cmErrMsg(&p->err,rc,"Send 'hello' to %s:%s:%i failed.",cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
  495. else
  496. _cmRtNetRpt(p,"send hello\n");
  497. break;
  498. case kSendEndpointStNetId:
  499. {
  500. cmRtNetEnd_t* ep;
  501. // if all of the endpoints have been sent to this node ...
  502. if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->epIdx)) == NULL )
  503. np->state = kDoneStNetId; // ... we are done
  504. else
  505. {
  506. // send an endpoint to this node
  507. if((rc = _cmRtNetSendSyncMsg(p,np,kHelloSelNetId,ep->endPtLabel, ep->endPtId, kWaitEndpointAckStNetId )) != kOkNetRC )
  508. rc = cmErrMsg(&p->err,rc,"Endpoint (%s index:%i) transmission to %s:%s:%i failed.",cmStringNullGuard(ep->endPtLabel),cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
  509. else
  510. _cmRtNetRpt(p,"send endpoint\n");
  511. }
  512. }
  513. break;
  514. case kWaitHelloAckStNetId:
  515. case kWaitEndpointAckStNetId:
  516. {
  517. cmTimeSpec_t t;
  518. cmTimeGet(&t);
  519. unsigned twentySecs = 20000000;
  520. if( cmTimeElapsedMicros(&np->lastSendTime,&t) > twentySecs)
  521. {
  522. const cmChar_t* ackStr = np->state==kWaitHelloAckStNetId ? "hello" : "endpoint";
  523. rc = cmErrMsg(&p->err,kTimeOutErrNetRC,"The node %s:%s:%i did not give a '%s' acknowledge.",cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port,ackStr);
  524. }
  525. }
  526. break;
  527. default:
  528. break;
  529. }
  530. // if an error occurred put the node into an error state
  531. if( rc != kOkNetRC )
  532. np->state = kErrorStNetId;
  533. return rc;
  534. }
  535. cmRtNetRC_t cmRtNetSyncModeSend( cmRtNetH_t h )
  536. {
  537. cmRtNetRC_t rc = kOkNetRC;
  538. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  539. if( p->syncModeFl == false )
  540. return rc;
  541. unsigned activeCnt = 0;
  542. cmRtNetNode_t* np = p->nodes;
  543. for(; np != NULL; np=np->link )
  544. if( np != p->localNode && np->state != kDoneStNetId && np->state != kErrorStNetId )
  545. {
  546. _cmRtNetSendNodeSync(p,np);
  547. activeCnt += 1;
  548. }
  549. if( activeCnt == 0 )
  550. p->syncModeFl = false;
  551. return rc;
  552. }
  553. cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
  554. {
  555. cmRtNetRC_t rc = kOkNetRC;
  556. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  557. if( cmUdpGetAvailData(p->udpH, NULL, NULL, NULL ) != kOkUdpRC )
  558. {
  559. cmErrMsg(&p->err,kUdpPortFailNetRC,"UDP port query failed.");
  560. goto errLabel;
  561. }
  562. errLabel:
  563. return rc;
  564. }
  565. bool cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt )
  566. {
  567. cmRtNetSyncMsg_t* m = (cmRtNetSyncMsg_t*)data;
  568. return dataByteCnt >= sizeof(cmRtNetSyncMsg_t) && m->hdr.selId == kNetSyncSelRtId;
  569. }
  570. unsigned cmRtNetEndPointIndex( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endPtLabel )
  571. {
  572. //cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  573. return cmInvalidIdx;
  574. }
  575. cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned endPointIndex, const void* msg, unsigned msgByteCnt )
  576. {
  577. cmRtNetRC_t rc = kOkNetRC;
  578. //cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  579. return rc;
  580. }
  581. void cmRtNetReport( cmRtNetH_t h )
  582. {
  583. cmRtNet_t* p = _cmRtNetHandleToPtr(h);
  584. cmRpt_t* rpt = p->err.rpt;
  585. cmRptPrintf(rpt,"Sync Mode:%s\n",p->syncModeFl ? "ON" : "OFF");
  586. cmRtNetNode_t* np = p->nodes;
  587. for(; np!=NULL; np=np->link)
  588. {
  589. cmRptPrintf(rpt,"Node: %s ",np->label);
  590. if( np->addr != NULL )
  591. cmRptPrintf(rpt,"%s ",np->addr );
  592. if( cmIsFlag(np->flags,kLocalNetFl) )
  593. cmRptPrintf(rpt,"LOCAL ");
  594. if( cmIsFlag(np->flags,kSockAddrNetFl) )
  595. cmRptPrintf(rpt,"%s ",cmStringNullGuard(cmUdpAddrToString(p->udpH,&np->sockaddr)));
  596. if( np->port != cmInvalidId )
  597. cmRptPrintf(rpt,"%i ",np->port );
  598. cmRptPrintf(rpt,"\n");
  599. cmRtNetEnd_t* ep = np->ends;
  600. for(; ep!=NULL; ep=ep->link)
  601. {
  602. cmRptPrintf(rpt," endpt: %i %s\n",ep->endPtId,cmStringNullGuard(ep->endPtLabel));
  603. }
  604. }
  605. }
  606. //==========================================================================
  607. #include "cmThread.h"
  608. typedef struct
  609. {
  610. cmThreadH_t thH;
  611. cmRtNetH_t netH;
  612. } _cmRtNetTest_t;
  613. void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, const struct sockaddr_in* fromAddr )
  614. {
  615. _cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
  616. if( cmRtNetIsSyncModeMsg(data,dataByteCnt))
  617. cmRtNetSyncModeRecv(p->netH,data,dataByteCnt,fromAddr);
  618. }
  619. bool _cmRtNetTestThreadFunc(void* param)
  620. {
  621. _cmRtNetTest_t* p = (_cmRtNetTest_t*)param;
  622. if( cmRtNetIsValid(p->netH) )
  623. {
  624. if( cmRtNetIsInSyncMode(p->netH) )
  625. cmRtNetSyncModeSend(p->netH);
  626. cmRtNetReceive(p->netH);
  627. }
  628. cmSleepMs(40);
  629. return true;
  630. }
  631. void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
  632. {
  633. char c;
  634. _cmRtNetTest_t t;
  635. cmUdpPort_t port = 5876;
  636. _cmRtNetTest_t* p = &t;
  637. cmRtNetRC_t rc = kOkNetRC;
  638. unsigned hn = cmUdpHostNameMaxCharCount();
  639. cmChar_t hostNameStr[ hn ];
  640. memset(&t,0,sizeof(t));
  641. if( cmUdpHostName(hostNameStr,hn) != kOkUdpRC )
  642. goto errLabel;
  643. if( cmThreadCreate(&p->thH,_cmRtNetTestThreadFunc,p,&ctx->rpt) != kOkThRC )
  644. goto errLabel;
  645. if((rc = cmRtNetAlloc(ctx,&p->netH,_cmRtNetTestRecv,p)) != kOkNetRC )
  646. goto errLabel;
  647. if((rc = cmRtNetCreateNode(p->netH, hostNameStr, NULL, port )) != kOkNetRC)
  648. goto errLabel;
  649. if( mstrFl )
  650. {
  651. if((rc = cmRtNetCreateNode(p->netH,"whirl", "192.168.15.109", port )) != kOkNetRC )
  652. goto errLabel;
  653. if((rc = cmRtNetRegisterEndPoint(p->netH,"thunk_ep0", 0 )) != kOkNetRC )
  654. goto errLabel;
  655. if(( rc = cmRtNetBeginSyncMode(p->netH)) != kOkNetRC )
  656. goto errLabel;
  657. }
  658. else
  659. {
  660. if((rc = cmRtNetRegisterEndPoint(p->netH,"whirl_ep0", 0 )) != kOkNetRC )
  661. goto errLabel;
  662. }
  663. if( cmThreadPause(p->thH,0) != kOkThRC )
  664. goto errLabel;
  665. cmRptPrintf(&ctx->rpt,"%s q=quit\n", mstrFl ? "Master: " : "Slave: ");
  666. while( (c=getchar()) != 'q' )
  667. {
  668. }
  669. errLabel:
  670. cmThreadDestroy(&p->thH);
  671. cmRtNetFree(&p->netH);
  672. return;
  673. }