cmRtNet.h/c: Added cmRtNetNodeIndex().

This commit is contained in:
kpl 2014-06-16 15:21:39 -07:00
parent 4012928a4b
commit 335ca94b6c
2 changed files with 38 additions and 19 deletions

View File

@ -97,15 +97,24 @@ void _cmRtNetRpt( cmRtNet_t* p, const cmChar_t* fmt, ... )
va_end(vl); va_end(vl);
} }
cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label ) cmRtNetNode_t* _cmRtNetFindNode( cmRtNet_t* p, const cmChar_t* label, unsigned* idxRef )
{ {
if( label == NULL ) if( label == NULL )
return NULL; return NULL;
if( idxRef != NULL )
*idxRef = cmInvalidIdx;
cmRtNetNode_t* np = p->nodes; cmRtNetNode_t* np = p->nodes;
for(; np!=NULL; np=np->link) unsigned i;
for(i=0; np!=NULL; np=np->link,++i)
if( strcmp(label,np->label)==0) if( strcmp(label,np->label)==0)
{
if( idxRef != NULL )
*idxRef = i;
return np; return np;
}
return NULL; return NULL;
} }
@ -190,8 +199,8 @@ cmRtNetRC_t _cmRtNetCreateNode( cmRtNet_t* p, const cmChar_t* label, unsigned rt
if( cmTextIsEmpty(label) ) if( cmTextIsEmpty(label) )
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((np = _cmRtNetFindNode(p,label)) != NULL ) if((np = _cmRtNetFindNode(p,label,NULL)) != NULL )
cmErrWarnMsg(&p->err,kDuplLabelNetRC,"The node label '%s' is already in use.",cmStringNullGuard(label)); cmErrWarnMsg(&p->err,kOkNetRC/*kDuplLabelNetRC*/,"The node label '%s' is being reused.",cmStringNullGuard(label));
else else
{ {
np = cmMemAllocZ(cmRtNetNode_t,1); np = cmMemAllocZ(cmRtNetNode_t,1);
@ -569,7 +578,7 @@ cmRtNetRC_t _cmRtNetSyncModeRecv( cmRtNet_t* p, const char* data, unsigned data
{ {
case kHelloSelNetId: case kHelloSelNetId:
// if this is a response to a broadcast from the local node then ignore it // if this is a response to a broadcast from the local node then ignore it
if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label)) != NULL && strcmp(p->localNode->label,m.label)==0 ) if(m.label!=NULL && p->localNode->label!=NULL && (np = _cmRtNetFindNode(p,m.label,NULL)) != NULL && strcmp(p->localNode->label,m.label)==0 )
{ {
const cmChar_t* fromAddrStr = cmUdpAddrToString(p->udpH,fromAddr); const cmChar_t* fromAddrStr = cmUdpAddrToString(p->udpH,fromAddr);
const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH)); const cmChar_t* localAddrStr = cmUdpAddrToString(p->udpH,cmUdpLocalAddr(p->udpH));
@ -794,12 +803,6 @@ cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h )
return rc; return rc;
} }
unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a )
{
cmRtNet_t* p = _cmRtNetHandleToPtr(h);
return _cmRtNetAddrToNodeIndex(p,a);
}
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, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp )
{ {
@ -808,7 +811,7 @@ cmRtNetRC_t cmRtNetEndpointHandle( cmRtNetH_t h, const cmChar_t* nodeLabel, cons
cmRtNetNode_t* np; cmRtNetNode_t* np;
cmRtNetEnd_t* ep; cmRtNetEnd_t* ep;
if(( np = _cmRtNetFindNode(p,nodeLabel)) == NULL ) if(( np = _cmRtNetFindNode(p,nodeLabel,NULL)) == NULL )
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));
@ -966,6 +969,22 @@ unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h )
return n; return n;
} }
unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a )
{
cmRtNet_t* p = _cmRtNetHandleToPtr(h);
return _cmRtNetAddrToNodeIndex(p,a);
}
unsigned cmRtNetRemoteNodeIndex( cmRtNetH_t h, const cmChar_t* label )
{
cmRtNet_t* p = _cmRtNetHandleToPtr( h );
unsigned idx = cmInvalidIdx;
_cmRtNetFindNode(p,label,&idx);
return idx;
}
const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx ) const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx )
{ {
cmRtNet_t* p = _cmRtNetHandleToPtr( h ); cmRtNet_t* p = _cmRtNetHandleToPtr( h );

View File

@ -49,11 +49,11 @@ extern "C" {
// selector id's for cmRtNetSyncMsg_t.selId. // selector id's for cmRtNetSyncMsg_t.selId.
typedef enum typedef enum
{ {
kHelloSelNetId, // broadcast msg (label=node label, id=endpt cnt) kHelloSelNetId, // 0 broadcast msg (label=node label, id=endpt cnt)
kNodeSelNetId, // define remote node (label=remote node label, id=endpt cnt) kNodeSelNetId, // 1 define remote node (label=remote node label, id=endpt cnt)
kEndpointSelNetId, // define remote endpt (label=remote endpt label, id=endpt id) kEndpointSelNetId, // 2 define remote endpt (label=remote endpt label, id=endpt id)
kDoneSelNetId, // declare all endpts sent kDoneSelNetId, // 3 declare all endpts sent
kInvalidSelNetId kInvalidSelNetId // 4
} cmRtNetSelId_t; } cmRtNetSelId_t;
@ -117,8 +117,6 @@ extern "C" {
// an cmRtSysMsgHdr_t header (See cmRtSysMsg.h). // an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h ); cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
// Return the index of the node associated with sockaddr_in.
unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
// Get a remote end point handle for use with cmRtNetSend. // Get a remote 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, const cmChar_t* endptLabel, cmRtNetEndptH_t* hp );
@ -150,6 +148,8 @@ extern "C" {
// {nodeIdx, epIdx} does not identify a valid endpoint. // {nodeIdx, epIdx} does not identify a valid endpoint.
const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h ); const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h );
unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h ); unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h );
unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
unsigned cmRtNetRemoteNodeIndex( cmRtNetH_t h, const cmChar_t* label );
const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx ); const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx );
unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx ); unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx );
cmRtNetRC_t cmRtNetRemoteNodeEndPoint( cmRtNetRC_t cmRtNetRemoteNodeEndPoint(