cmRtNet.h/c,cmRtSysMsg.h:Added cmRtNetMsg_t to suport use of rtSubIdx as part of endpoint description.

This commit is contained in:
kpl 2013-05-04 23:52:16 -07:00
parent dfb569b548
commit c1116f6fc0
3 changed files with 69 additions and 54 deletions

108
cmRtNet.c
View File

@ -39,6 +39,7 @@ typedef struct cmRtNetEnd_str
{ {
cmChar_t* label; cmChar_t* label;
unsigned id; unsigned id;
unsigned rtSubIdx;
struct cmRtNetNode_str* np; // Owner node. struct cmRtNetNode_str* np; // Owner node.
struct cmRtNetEnd_str* link; struct cmRtNetEnd_str* link;
} cmRtNetEnd_t; } cmRtNetEnd_t;
@ -75,10 +76,11 @@ typedef struct
// Network synchronization message format // Network synchronization message format
typedef struct typedef struct
{ {
cmRtSysMsgHdr_t hdr; // standard cmRtSys msg header cmRtSysMsgHdr_t hdr; // standard cmRtSys msg header
cmRtNetSelId_t selId; // message selector id (See kXXXSelNetId above) cmRtNetSelId_t selId; // message selector id (See kXXXSelNetId above)
const cmChar_t* label; // node or endpoint label const cmChar_t* label; // node or endpoint label
unsigned id; // endptCnt or endpoint id unsigned id; // endptCnt or endpoint id
unsigned rtSubIdx; // cmInvalidIdx or rtSubIdx
} cmRtNetSyncMsg_t; } cmRtNetSyncMsg_t;
cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE; cmRtNetH_t cmRtNetNullHandle = cmSTATIC_NULL_HANDLE;
@ -217,11 +219,11 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, const cmCha
return rc; return rc;
} }
cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, const cmChar_t* endPtLabel ) cmRtNetEnd_t* _cmRtNetFindNodeEnd(cmRtNetNode_t* np, unsigned rtSubIdx, const cmChar_t* endPtLabel )
{ {
cmRtNetEnd_t* ep = np->ends; cmRtNetEnd_t* ep = np->ends;
for(; ep!=NULL; ep=ep->link) for(; ep!=NULL; ep=ep->link)
if( strcmp(ep->label,endPtLabel)==0 ) if( ep->rtSubIdx==rtSubIdx && strcmp(ep->label,endPtLabel)==0 )
return ep; return ep;
return NULL; return NULL;
} }
@ -240,22 +242,23 @@ cmRtNetEnd_t* _cmRtNetIndexToEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned
return NULL; return NULL;
} }
cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, const cmChar_t* endPtLabel, unsigned endPtId ) cmRtNetRC_t _cmRtNetCreateEndpoint( cmRtNet_t* p, cmRtNetNode_t* np, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId )
{ {
if( endPtLabel == NULL ) if( endPtLabel == NULL )
return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered."); return cmErrMsg(&p->err,kInvalidLabelNetRC,"A null or blank node label was encountered.");
if( _cmRtNetFindNodeEnd( np, endPtLabel) != NULL) if( _cmRtNetFindNodeEnd( np, rtSubIdx, endPtLabel) != NULL)
return cmErrMsg(&p->err,kDuplEndNetRC,"A duplicate endpoint ('%s') was encountered on node '%s'.",endPtLabel,np->label); return cmErrMsg(&p->err,kDuplEndNetRC,"A duplicate endpoint ('%s') was encountered on node '%s'.",endPtLabel,np->label);
cmRtNetRC_t rc = kOkNetRC; cmRtNetRC_t rc = kOkNetRC;
cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1); cmRtNetEnd_t* ep = cmMemAllocZ(cmRtNetEnd_t,1);
ep->label = cmMemAllocStr(endPtLabel); ep->label = cmMemAllocStr(endPtLabel);
ep->id = endPtId; ep->id = endPtId;
ep->np = np; ep->rtSubIdx = rtSubIdx;
ep->link = np->ends; ep->np = np;
np->ends = ep; ep->link = np->ends;
np->ends = ep;
return rc; return rc;
} }
@ -295,7 +298,7 @@ cmRtNetRC_t _cmRtNetDeserializeSyncMsg( const void* buf, unsigned n, cmRtNetSync
return kOkNetRC; return kOkNetRC;
} }
cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* msgLabel, unsigned msgId ) cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t selId, const cmChar_t* msgLabel, unsigned msgId, unsigned msgRtSubIdx )
{ {
cmRtNetSyncMsg_t m; cmRtNetSyncMsg_t m;
cmRtNetRC_t rc = kOkNetRC; cmRtNetRC_t rc = kOkNetRC;
@ -306,6 +309,7 @@ cmRtNetRC_t _cmRtNetSendSyncMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t
m.selId = selId; m.selId = selId;
m.label = msgLabel; m.label = msgLabel;
m.id = msgId; m.id = msgId;
m.rtSubIdx = msgRtSubIdx;
// determine size of msg to send // determine size of msg to send
unsigned n = _cmRtNetSyncMsgSerialByteCount(&m); unsigned n = _cmRtNetSyncMsgSerialByteCount(&m);
@ -415,12 +419,13 @@ cmUdpH_t cmRtNetUdpPortHandle( cmRtNetH_t h )
cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t srcSelId ) cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRtNetSelId_t srcSelId )
{ {
cmRtNetRC_t rc = kOkNetRC; cmRtNetRC_t rc = kOkNetRC;
cmRtNetEnd_t* ep; cmRtNetEnd_t* ep = NULL;
const cmChar_t* msgLabel = NULL; const cmChar_t* msgLabel = NULL;
unsigned msgId = cmInvalidId; unsigned msgId = cmInvalidId;
cmRtNetSelId_t selId = kEndpointSelNetId; unsigned msgRtSubIdx = cmInvalidIdx;
const cmChar_t* rptLabel = "endpoint"; cmRtNetSelId_t selId = kEndpointSelNetId;
const cmChar_t* rptLabel = "endpoint";
if( np == NULL ) if( np == NULL )
return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found."); return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The net node associated with an endpoint reply was not found.");
@ -439,8 +444,9 @@ cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRt
// attempt to get the next local endpoint to send ... // attempt to get the next local endpoint to send ...
if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) != NULL ) if((ep = _cmRtNetIndexToEndpoint(p,p->localNode,np->endPtIdx)) != NULL )
{ {
msgLabel = ep->label; // ... send next local endpoint msgLabel = ep->label; // ... send next local endpoint
msgId = ep->id; msgId = ep->id;
msgRtSubIdx = ep->rtSubIdx;
} }
else // .... all local endpoints have been sent else // .... all local endpoints have been sent
{ {
@ -462,7 +468,7 @@ cmRtNetRC_t _cmRtNetSendEndpointReplyMsg( cmRtNet_t* p, cmRtNetNode_t* np, cmRt
// selId is set to kInvalidSelNetId when we encounter the (stop) criteria // selId is set to kInvalidSelNetId when we encounter the (stop) criteria
if( selId != kInvalidSelNetId ) if( selId != kInvalidSelNetId )
{ {
if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId )) != kOkNetRC ) if((rc = _cmRtNetSendSyncMsg(p,np,selId,msgLabel,msgId,msgRtSubIdx)) != kOkNetRC )
rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port); rc = cmErrMsg(&p->err,rc,"Send '%s' to %s:%s:%i failed.",rptLabel,cmStringNullGuard(np->label),cmStringNullGuard(np->addr),np->port);
else else
_cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel)); _cmRtNetRpt(p,"Sent %s.\n",cmStringNullGuard(rptLabel));
@ -545,7 +551,7 @@ cmRtNetRC_t _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
{ {
case kHelloSelNetId: case kHelloSelNetId:
_cmRtNetRpt(p,"rcv hello\n"); // reply with local node _cmRtNetRpt(p,"rcv hello\n"); // reply with local node
rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt ); rc = _cmRtNetSendSyncMsg( p, np, kNodeSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
break; break;
case kNodeSelNetId: case kNodeSelNetId:
@ -577,12 +583,12 @@ cmRtNetRC_t _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
} }
// attempt to find the end point // attempt to find the end point
if((ep = _cmRtNetFindNodeEnd(np,m.label)) != NULL ) if((ep = _cmRtNetFindNodeEnd(np, m.rtSubIdx, m.label)) != NULL )
ep->id = m.id; // the endpoint was found update the endPtId ep->id = m.id; // the endpoint was found update the endPtId
else else
{ {
// create a local proxy for the endpoint // create a local proxy for the endpoint
if((rc = _cmRtNetCreateEndpoint(p,np,m.label,m.id)) != kOkNetRC ) if((rc = _cmRtNetCreateEndpoint(p,np,m.rtSubIdx,m.label,m.id)) != kOkNetRC )
goto errLabel; goto errLabel;
} }
@ -666,7 +672,7 @@ bool cmRtNetIsInitialized( cmRtNetH_t h )
} }
cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId ) cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId )
{ {
cmRtNetRC_t rc = kOkNetRC; cmRtNetRC_t rc = kOkNetRC;
cmRtNet_t* p = _cmRtNetHandleToPtr(h); cmRtNet_t* p = _cmRtNetHandleToPtr(h);
@ -674,7 +680,7 @@ cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, u
if( p->localNode == NULL ) if( p->localNode == NULL )
return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined."); return cmErrMsg(&p->err,kLocalNodeNetRC,"Local endpoints may not be added if a local node has not been defined.");
if((rc = _cmRtNetCreateEndpoint(p, p->localNode,endPtLabel,endPtId )) == kOkNetRC ) if((rc = _cmRtNetCreateEndpoint(p, p->localNode,rtSubIdx,endPtLabel,endPtId )) == kOkNetRC )
p->localNode->endPtCnt += 1; p->localNode->endPtCnt += 1;
return rc; return rc;
@ -693,7 +699,7 @@ cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h )
cmRtNet_t* p = _cmRtNetHandleToPtr(h); cmRtNet_t* p = _cmRtNetHandleToPtr(h);
// broadcast 'node' msg // broadcast 'node' msg
return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, p->localNode->label, p->localNode->endPtCnt ); return _cmRtNetSendSyncMsg( p, p->localNode, kHelloSelNetId, p->localNode->label, p->localNode->endPtCnt, cmInvalidIdx );
} }
cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h ) cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
@ -710,7 +716,7 @@ cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
return rc; return rc;
} }
cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp ) cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
{ {
cmRtNetRC_t rc = kOkNetRC; cmRtNetRC_t rc = kOkNetRC;
cmRtNet_t* p = _cmRtNetHandleToPtr(h); cmRtNet_t* p = _cmRtNetHandleToPtr(h);
@ -721,7 +727,7 @@ cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, cons
return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel)); return cmErrMsg(&p->err,kNodeNotFoundNetRC,"The node '%s' was not found.",cmStringNullGuard(nodeLabel));
if(( ep = _cmRtNetFindNodeEnd(np, endptLabel )) == NULL ) if(( ep = _cmRtNetFindNodeEnd(np, rtSubIdx, endptLabel )) == NULL )
return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel)); return cmErrMsg(&p->err,kEndNotFoundNetRC,"The endpoint '%s' on '%s' on node was not found.",cmStringNullGuard(endptLabel),cmStringNullGuard(nodeLabel));
hp->h = ep; hp->h = ep;
@ -737,12 +743,13 @@ cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, uns
assert( ep != NULL ); assert( ep != NULL );
unsigned dN = sizeof(cmRtSysMsgHdr_t) + msgByteCnt; unsigned dN = sizeof(cmRtNetMsg_t) + msgByteCnt;
char data[ dN ]; char data[ dN ];
cmRtSysMsgHdr_t* hdr = (cmRtSysMsgHdr_t*)data; cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
hdr->rtSubIdx = ep->id; r->hdr.rtSubIdx = ep->rtSubIdx;
hdr->selId = kMsgSelRtId; r->hdr.selId = kMsgSelRtId;
r->endptId = ep->id;
memcpy(data+sizeof(cmRtSysMsgHdr_t),msg,msgByteCnt); memcpy(data+sizeof(cmRtSysMsgHdr_t),msg,msgByteCnt);
if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC ) if( cmUdpSendTo(p->udpH, data, dN, &ep->np->sockaddr ) != kOkUdpRC )
@ -751,12 +758,12 @@ cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, uns
return rc; return rc;
} }
cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt ) cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt )
{ {
cmRtNetRC_t rc = kOkNetRC; cmRtNetRC_t rc = kOkNetRC;
cmRtNetEndptH_t epH = cmRtNetEndptNullHandle; cmRtNetEndptH_t epH = cmRtNetEndptNullHandle;
if((rc = cmRtNetEndpointHandle(h,nodeLabel,endptLabel,&epH)) != kOkNetRC ) if((rc = cmRtNetEndpointHandle(h,nodeLabel,rtSubIdx,endptLabel,&epH)) != kOkNetRC )
return rc; return rc;
return cmRtNetSend(h,epH,msg,msgByteCnt); return cmRtNetSend(h,epH,msg,msgByteCnt);
@ -827,9 +834,9 @@ void _cmRtNetTestRecv( void* cbArg, const char* data, unsigned dataByteCnt, cons
{ {
//_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg; //_cmRtNetTest_t* p = (_cmRtNetTest_t*)cbArg;
cmRtSysMsgHdr_t* hdr = (cmRtSysMsgHdr_t*)data; cmRtNetMsg_t* r = (cmRtNetMsg_t*)data;
unsigned i = *(unsigned*)(data + sizeof(cmRtSysMsgHdr_t)); unsigned i = *(unsigned*)(data + sizeof(cmRtNetMsg_t));
printf("%i %i\n",hdr->rtSubIdx,i); printf("rtSubIdx:%i endptId:%i %i\n",r->hdr.rtSubIdx,r->endptId,i);
} }
@ -852,14 +859,15 @@ void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
{ {
char c; char c;
_cmRtNetTest_t t; _cmRtNetTest_t t;
cmUdpPort_t port = 5876; const unsigned rtSubIdx = 0;
_cmRtNetTest_t* p = &t; cmUdpPort_t port = 5876;
cmRtNetRC_t rc = kOkNetRC; _cmRtNetTest_t* p = &t;
const cmChar_t* localHostStr = mstrFl ? "master" : "slave"; cmRtNetRC_t rc = kOkNetRC;
const cmChar_t* localEndpStr = mstrFl ? "master_ep" : "slave_ep"; const cmChar_t* localHostStr = mstrFl ? "master" : "slave";
const cmChar_t* remoteHostStr = !mstrFl ? "master" : "slave"; const cmChar_t* localEndpStr = mstrFl ? "master_ep" : "slave_ep";
const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep"; const cmChar_t* remoteHostStr = !mstrFl ? "master" : "slave";
const cmChar_t* bcastAddr = "192.168.15.255"; const cmChar_t* remoteEndpStr = !mstrFl ? "master_ep" : "slave_ep";
const cmChar_t* bcastAddr = "192.168.15.255";
memset(&t,0,sizeof(t)); memset(&t,0,sizeof(t));
@ -874,7 +882,7 @@ void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC) if((rc = cmRtNetInitialize(p->netH, bcastAddr, localHostStr, NULL, port )) != kOkNetRC)
goto errLabel; goto errLabel;
if((rc = cmRtNetRegisterEndPoint(p->netH,localEndpStr, 0 )) != kOkNetRC ) if((rc = cmRtNetRegisterEndPoint(p->netH,rtSubIdx,localEndpStr, 0 )) != kOkNetRC )
goto errLabel; goto errLabel;
if( cmThreadPause(p->thH,0) != kOkThRC ) if( cmThreadPause(p->thH,0) != kOkThRC )
@ -896,7 +904,7 @@ void cmRtNetTest( cmCtx_t* ctx, bool mstrFl )
case 't': case 't':
{ {
if( cmRtNetSendByLabels(p->netH, remoteHostStr, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC ) if( cmRtNetSendByLabels(p->netH, remoteHostStr, rtSubIdx, remoteEndpStr, &p->msgVal, sizeof(p->msgVal)) == kOkNetRC )
p->msgVal += 1; p->msgVal += 1;
} }

View File

@ -55,7 +55,7 @@ extern "C" {
// cmRtNetInitialize(). // cmRtNetInitialize().
// Remote nodes will be able to send messages to these endpoints by // Remote nodes will be able to send messages to these endpoints by
// referring to (nodeLabel/endPtLabel) // referring to (nodeLabel/endPtLabel)
cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId ); cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, unsigned rtSubIdx, const cmChar_t* endPtLabel, unsigned endPtId );
// Delete all nodes and endpoints. // Delete all nodes and endpoints.
cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h ); cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h );
@ -69,14 +69,14 @@ extern "C" {
cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h ); cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
// Get an end point handle for use with cmRtNetSend. // Get an end point handle for use with cmRtNetSend.
cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp ); cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
// Send a message to a remote endpoint. // Send a message to a remote endpoint.
cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt ); cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
// Send a message to a remote endpoint. This function is a composite // Send a message to a remote endpoint. This function is a composite
// of cmRtNetEndpointHandle() and cmRtNetSend(). // of cmRtNetEndpointHandle() and cmRtNetSend().
cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt ); cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, const cmChar_t* nodeLabel, unsigned rtSubIdx, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
// Enable/disable synchronization protocol reporting. // Enable/disable synchronization protocol reporting.
// Return the previous state of the report sync. flag. // Return the previous state of the report sync. flag.

View File

@ -95,6 +95,13 @@ extern "C" {
// cmMidiMsg msgArray[msgCnt] // cmMidiMsg msgArray[msgCnt]
} cmRtSysMidi_t; } cmRtSysMidi_t;
typedef struct
{
cmRtSysMsgHdr_t hdr;
unsigned endptId;
// char msg[ msgByteCnt ]
} cmRtNetMsg_t;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif