2013-04-16 01:10:49 +00:00
|
|
|
#ifndef cmNet_h
|
|
|
|
#define cmNet_h
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kOkNetRC = cmOkRC,
|
|
|
|
kUdpPortFailNetRC,
|
|
|
|
kInvalidLabelNetRC,
|
|
|
|
kDuplLabelNetRC,
|
|
|
|
kDuplLocalNetRC,
|
|
|
|
kDuplEndNetRC,
|
|
|
|
kThreadFailNetRC,
|
|
|
|
kBufToSmallNetRC,
|
|
|
|
kNodeNotFoundNetRC,
|
|
|
|
kNodeStateErrNetRC,
|
|
|
|
kTimeOutErrNetRC,
|
|
|
|
kLocalNodeNetRC,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef cmRC_t cmRtNetRC_t;
|
|
|
|
typedef cmHandle_t cmRtNetH_t;
|
|
|
|
|
|
|
|
|
|
|
|
extern cmRtNetH_t cmRtNetNullHandle;
|
|
|
|
|
|
|
|
// 'cbFunc' will be called within the context of cmRtNetReceive() to receive
|
|
|
|
// incoming network messages.
|
|
|
|
cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, cmUdpCallback_t cbFunc, void* cbArg );
|
|
|
|
cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp );
|
|
|
|
|
|
|
|
bool cmRtNetIsValid( cmRtNetH_t h );
|
|
|
|
|
2013-04-27 19:14:58 +00:00
|
|
|
const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
|
|
|
// Create a network node.
|
|
|
|
// The 'nodeLabel' refers to a network device cfg. (see cmDevCfg).
|
|
|
|
// Set 'ipAddr' to NULL if this is the local node.
|
|
|
|
// During sync mode this node will attempt to sync with all
|
|
|
|
// nodes in the node list.
|
2013-04-27 22:51:21 +00:00
|
|
|
cmRtNetRC_t cmRtNetRegisterLocalNode( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2013-04-27 19:14:58 +00:00
|
|
|
|
2013-04-16 01:10:49 +00:00
|
|
|
// Register the local endpoints.
|
|
|
|
// Remote nodes will be able to send messages to these endpoints by
|
|
|
|
// referring to (nodeLabel/endPtLabel)
|
|
|
|
cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
|
|
|
|
|
|
|
|
// Delete all nodes and endpoints.
|
|
|
|
cmRtNetRC_t cmRtNetClearAll( cmRtNetH_t h );
|
|
|
|
|
|
|
|
|
|
|
|
// Go into 'sync' node.
|
2013-04-26 20:04:26 +00:00
|
|
|
// When a node enters sync mode it systematically transmits all of it's
|
|
|
|
// local endpoint information to each registered remote node. Prior to
|
|
|
|
// entering sync mode a node must therefore have been setup with a list
|
|
|
|
// of remote nodes (via cmRtNetCreateNode()) and a list of local endpoints
|
|
|
|
// (cmRtNetRegisterEndpoint()). During sync mode a node sends it's local
|
|
|
|
// endpoint list to each registered remote node. When a remote node receives
|
|
|
|
// an endpoint it updates it's own remote node/endpoint
|
2013-04-16 01:10:49 +00:00
|
|
|
// list.
|
|
|
|
cmRtNetRC_t cmRtNetBeginSyncMode( cmRtNetH_t h );
|
|
|
|
|
|
|
|
|
|
|
|
// This function must be polled to receive incoming network messages
|
|
|
|
// via the callback funcion 'cbFunc' as passed to cmRtNetAlloc()
|
|
|
|
cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
|
|
|
|
|
2013-04-26 20:04:26 +00:00
|
|
|
bool cmRtNetIsSyncModeMsg( const void* data, unsigned dataByteCnt );
|
|
|
|
|
2013-04-16 01:10:49 +00:00
|
|
|
unsigned cmRtNetEndPointIndex( cmRtNetH_t h, const cmChar_t* nodeLabel, const cmChar_t* endPtLabel );
|
|
|
|
|
|
|
|
|
|
|
|
cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned endPointIndex, const void* msg, unsigned msgByteCnt );
|
|
|
|
|
|
|
|
void cmRtNetReport( cmRtNetH_t h );
|
|
|
|
|
2013-04-26 20:19:55 +00:00
|
|
|
void cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
|
2013-04-26 20:04:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Master:
|
|
|
|
cmRtNetBeginSyncMode().
|
|
|
|
while( cmRtNetIsSyncMode())
|
|
|
|
{
|
|
|
|
// Give the master an oppurtunity to advance it's sync mode state.
|
|
|
|
// When the master is has sync'd with all remote nodes in it's
|
|
|
|
// remote node list then it will automatically exit sync mode.
|
|
|
|
cmRtNetSyncModeSend()
|
|
|
|
}
|
|
|
|
|
|
|
|
_myNetRecv(dataV,dataN,addr)
|
|
|
|
{
|
|
|
|
if( cmRtNetIsSyncModeMsg(dataV,dataN) )
|
|
|
|
cmRtNetSyncModeRecv(dataV,dataN,addr)
|
2013-04-27 18:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
The 'master' is the machine which cmRtNetBeginSyncMode() is called on.
|
|
|
|
1) 'master' sends local endpoints to all registered remote nodes.
|
|
|
|
2) When a 'slave' receives the kDoneSelNetId msg it transmits
|
|
|
|
it's own local endpoints back to the master.
|
|
|
|
|
|
|
|
a. Each node in the node list has a type id:
|
|
|
|
1. local
|
|
|
|
2. registered - remote node that was explicitely registered on a master
|
|
|
|
3. received - remote node that was received from a master
|
|
|
|
|
|
|
|
b.
|
|
|
|
1. All nodes are created in the 'send-hello' state.
|
|
|
|
2. If a master machine is in 'sync-mode' then it systematically sends
|
|
|
|
each of it's local endpoints to all 'registered' nodes.
|
|
|
|
3. When a slave machine recives a 'hello' it creates a
|
|
|
|
'received' node.
|
|
|
|
4. When a slave machine recieves a 'done' it enters sync mode
|
|
|
|
and systematically sends each of its local endpoints to
|
|
|
|
the 'done' source.
|
|
|
|
|
|
|
|
|
|
|
|
Protocol:
|
2013-04-27 22:51:21 +00:00
|
|
|
1. A: on init bcast 'hello'
|
|
|
|
2. B: on 'hello' - create node-A w/ ei=0 - send 'node'
|
|
|
|
3. A: on 'node' - create node-B w/ ei=0 - send first 'endpt'
|
|
|
|
4. B: on 'endpt' - create endpt on node-A - ei!=en ? send 'endpt' or send 'done'
|
|
|
|
5. A: on 'endpt' - create endpt on node-B - ei!=en ? send 'endpt' or send 'done'
|
|
|
|
6. B: on 'done' - mark node-A as 'valid'
|
|
|
|
7. A: on 'done' - mark node-B as 'valid'.
|
2013-04-27 18:02:01 +00:00
|
|
|
|
2013-04-26 20:04:26 +00:00
|
|
|
*/
|
2013-04-16 01:10:49 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|