2013-04-16 01:10:49 +00:00
|
|
|
#ifndef cmNet_h
|
|
|
|
#define cmNet_h
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-06-15 21:36:12 +00:00
|
|
|
/*
|
|
|
|
Nodes and Endpoints:
|
|
|
|
---------------------
|
|
|
|
A node corresponds to a process and owns a socket. It also has a label which is
|
|
|
|
unique among all other nodes on the network. A node also has a set of application
|
|
|
|
defined 'endpoints'. Each endpoint has a label and id that is unique among all
|
|
|
|
other endpoints on the same node. Endpoints on different nodes however may share
|
|
|
|
use the same label and id. Endpoints are used by remote senders to identify
|
|
|
|
a particular receiver which is sharing the node with other receivers. Endpoints
|
|
|
|
are therefore analogous to port numbers on sockets.
|
|
|
|
|
|
|
|
See gt/doc/notes.txt for more discussion of cmRtNet.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-04-16 01:10:49 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
kOkNetRC = cmOkRC,
|
|
|
|
kUdpPortFailNetRC,
|
|
|
|
kInvalidLabelNetRC,
|
|
|
|
kDuplLabelNetRC,
|
|
|
|
kDuplEndNetRC,
|
2013-04-28 22:20:33 +00:00
|
|
|
kDuplLocalNetRC,
|
2013-04-16 01:10:49 +00:00
|
|
|
kThreadFailNetRC,
|
|
|
|
kBufToSmallNetRC,
|
|
|
|
kNodeNotFoundNetRC,
|
2013-04-28 22:20:33 +00:00
|
|
|
kEndNotFoundNetRC,
|
2013-04-16 01:10:49 +00:00
|
|
|
kLocalNodeNetRC,
|
2013-04-30 19:40:27 +00:00
|
|
|
kInvalidArgNetRC,
|
2013-04-28 22:20:33 +00:00
|
|
|
kSyncFailNetRC,
|
|
|
|
kNodeEndCntErrNetRC
|
2013-04-16 01:10:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef cmRC_t cmRtNetRC_t;
|
|
|
|
typedef cmHandle_t cmRtNetH_t;
|
2013-04-28 22:20:33 +00:00
|
|
|
typedef cmHandle_t cmRtNetEndptH_t;
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
extern cmRtNetH_t cmRtNetNullHandle;
|
|
|
|
extern cmRtNetEndptH_t cmRtNetEndptNullHandle;
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2014-06-14 21:59:28 +00:00
|
|
|
|
|
|
|
// selector id's for cmRtNetSyncMsg_t.selId.
|
|
|
|
typedef enum
|
|
|
|
{
|
2014-06-16 22:21:39 +00:00
|
|
|
kHelloSelNetId, // 0 broadcast msg (label=node label, id=endpt cnt)
|
|
|
|
kNodeSelNetId, // 1 define remote node (label=remote node label, id=endpt cnt)
|
|
|
|
kEndpointSelNetId, // 2 define remote endpt (label=remote endpt label, id=endpt id)
|
|
|
|
kDoneSelNetId, // 3 declare all endpts sent
|
|
|
|
kInvalidSelNetId // 4
|
2014-06-14 21:59:28 +00:00
|
|
|
} cmRtNetSelId_t;
|
|
|
|
|
|
|
|
|
|
|
|
// Network synchronization message format.
|
|
|
|
// cmRtNetRC_t.hdr.selId == kNetSyncSelRtid.
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
cmRtSysMsgHdr_t hdr; // standard cmRtSys msg header
|
|
|
|
cmRtNetSelId_t selId; // message selector id (See kXXXSelNetId above)
|
2014-06-16 17:42:55 +00:00
|
|
|
unsigned hdrByteCnt; // size of the header record at transmission (used to locate the serialzed label)
|
2014-06-14 21:59:28 +00:00
|
|
|
unsigned rtSubIdx; // cmInvalidIdx or rtSubIdx
|
2014-06-14 23:45:37 +00:00
|
|
|
unsigned id; // endptCnt or endpoint id
|
|
|
|
const cmChar_t* label; // node or endpoint label
|
2014-06-14 21:59:28 +00:00
|
|
|
} cmRtNetSyncMsg_t;
|
|
|
|
|
2014-06-15 03:40:46 +00:00
|
|
|
const cmChar_t* cmRtNetSyncMsgLabel( const cmRtNetSyncMsg_t* m );
|
|
|
|
|
|
|
|
|
2014-06-14 21:59:28 +00:00
|
|
|
// NOTE: Messages passed between cmRtNet nodes during the synchronization
|
|
|
|
// process use the cmRtNetSyncMsg_t format (w/ the body of label following
|
|
|
|
// the record. All other messages use cmRtNetMsg_t (cmRtSysMsg.h) format.
|
|
|
|
|
2013-04-16 01:10:49 +00:00
|
|
|
// 'cbFunc' will be called within the context of cmRtNetReceive() to receive
|
|
|
|
// incoming network messages.
|
2014-06-15 21:36:12 +00:00
|
|
|
// rtSubIdx is the rtSubIdx of the cmRtSys which owns this cmRtNet.
|
|
|
|
cmRtNetRC_t cmRtNetAlloc( cmCtx_t* ctx, cmRtNetH_t* hp, unsigned rtSubIdx, cmUdpCallback_t cbFunc, void* cbArg );
|
2013-04-16 01:10:49 +00:00
|
|
|
cmRtNetRC_t cmRtNetFree( cmRtNetH_t* hp );
|
|
|
|
|
|
|
|
bool cmRtNetIsValid( cmRtNetH_t h );
|
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
// Get the local host name for this machine. This function
|
|
|
|
// is synonomous with gethostname().
|
2013-04-27 19:14:58 +00:00
|
|
|
const cmChar_t* cmRtNetLocalHostName( cmRtNetH_t h );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
// Initialize the local network node.
|
|
|
|
// 'bcastAddr' is the network broadcast address (e.g. 192.168.15.255).
|
|
|
|
// 'nodeLabel' is the local network node label
|
|
|
|
// 'ipAddr' may be set to NULL to use any available IP address.
|
|
|
|
// 'ipPort' refers to the socket port (which may need to be made available
|
|
|
|
// by the machine firewall cfg.)
|
|
|
|
cmRtNetRC_t cmRtNetInitialize( cmRtNetH_t h, const cmChar_t* bcastAddr, const cmChar_t* nodeLabel, const cmChar_t* ipAddr, cmUdpPort_t ipPort );
|
2013-04-30 19:40:27 +00:00
|
|
|
bool cmRtNetIsInitialized( cmRtNetH_t h );
|
2013-04-27 19:14:58 +00:00
|
|
|
|
2013-04-16 01:10:49 +00:00
|
|
|
// Register the local endpoints.
|
2013-04-28 22:20:33 +00:00
|
|
|
// Endpoints may only be registered once the network is initialized via
|
|
|
|
// cmRtNetInitialize().
|
2013-04-16 01:10:49 +00:00
|
|
|
// Remote nodes will be able to send messages to these endpoints by
|
|
|
|
// referring to (nodeLabel/endPtLabel)
|
2014-06-15 21:36:12 +00:00
|
|
|
cmRtNetRC_t cmRtNetRegisterEndPoint( cmRtNetH_t h, const cmChar_t* endPtLabel, unsigned endPtId );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
|
|
|
// Delete all nodes and endpoints.
|
2013-04-28 22:20:33 +00:00
|
|
|
cmRtNetRC_t cmRtNetFinalize( cmRtNetH_t h );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
// Broadcast the 'hello' to all machines listening on the
|
|
|
|
// broadcast addresss. This starts the synchronization sequence
|
|
|
|
cmRtNetRC_t cmRtNetDoSync( cmRtNetH_t h );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
|
|
|
// This function must be polled to receive incoming network messages
|
2014-06-14 20:54:30 +00:00
|
|
|
// via the callback funcion 'cbFunc' as passed to cmRtNetAlloc().
|
|
|
|
// Note that all messages received via 'cbFunc' will be prefixed with
|
2014-06-14 21:59:28 +00:00
|
|
|
// an cmRtSysMsgHdr_t header (See cmRtSysMsg.h).
|
2013-04-16 01:10:49 +00:00
|
|
|
cmRtNetRC_t cmRtNetReceive( cmRtNetH_t h );
|
|
|
|
|
2014-06-16 05:16:22 +00:00
|
|
|
|
2014-06-15 21:36:12 +00:00
|
|
|
// 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 );
|
2013-04-26 20:04:26 +00:00
|
|
|
|
2014-06-16 05:16:22 +00:00
|
|
|
bool cmRtNetEndpointIsValid( cmRtNetEndptH_t endPtH );
|
|
|
|
|
|
|
|
// Given an endpoint handle return the id/label of the associated endpoint.
|
|
|
|
unsigned cmRtNetEndpointId( cmRtNetEndptH_t endPtH );
|
|
|
|
const cmChar_t* cmRtNetEndpointLabel( cmRtNetEndptH_t endPtH );
|
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
// Send a message to a remote endpoint.
|
2014-06-16 05:16:22 +00:00
|
|
|
// Note that srcEndPtId is used only to inform the receiver of the endpoint
|
|
|
|
// of the transmitter. It is not used in any part of the transmit or receive
|
|
|
|
// process.
|
|
|
|
cmRtNetRC_t cmRtNetSend( cmRtNetH_t h, unsigned srcEndPtId, cmRtNetEndptH_t epH, const void* msg, unsigned msgByteCnt );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
// Send a message to a remote endpoint. This function is a composite
|
|
|
|
// of cmRtNetEndpointHandle() and cmRtNetSend().
|
2014-06-16 05:16:22 +00:00
|
|
|
cmRtNetRC_t cmRtNetSendByLabels( cmRtNetH_t h, unsigned srcEndPtId, const cmChar_t* nodeLabel, const cmChar_t* endptLabel, const void* msg, unsigned msgByteCnt );
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2014-06-16 05:16:22 +00:00
|
|
|
cmRtNetRC_t cmRtNetSendByIndex( cmRtNetH_t h, unsigned srcEndPtId, unsigned dstNodeIdx, unsigned dstEndptIdx, const void* msg, unsigned msgByteCnt );
|
2014-06-15 03:40:46 +00:00
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
// Enable/disable synchronization protocol reporting.
|
|
|
|
// Return the previous state of the report sync. flag.
|
|
|
|
bool cmRtNetReportSyncEnable( cmRtNetH_t h, bool enableFl );
|
|
|
|
bool cmRtNetReportSyncIsEnabled( cmRtNetH_t h );
|
|
|
|
|
2014-06-15 03:40:46 +00:00
|
|
|
// Query network configuration. Returns true on success or false if
|
|
|
|
// {nodeIdx, epIdx} does not identify a valid endpoint.
|
|
|
|
const cmChar_t* cmRtNetLocalNodeLabel( cmRtNetH_t h );
|
|
|
|
unsigned cmRtNetRemoteNodeCount( cmRtNetH_t h );
|
2014-06-16 22:21:39 +00:00
|
|
|
unsigned cmRtNetAddrToNodeIndex( cmRtNetH_t h, const struct sockaddr_in* a );
|
|
|
|
unsigned cmRtNetRemoteNodeIndex( cmRtNetH_t h, const cmChar_t* label );
|
2014-06-15 03:40:46 +00:00
|
|
|
const cmChar_t* cmRtNetRemoteNodeLabel( cmRtNetH_t h, unsigned idx );
|
|
|
|
unsigned cmRtNetRemoteNodeEndPointCount( cmRtNetH_t h, unsigned nodeIdx );
|
|
|
|
cmRtNetRC_t cmRtNetRemoteNodeEndPoint(
|
|
|
|
cmRtNetH_t h,
|
|
|
|
unsigned nodeIdx,
|
|
|
|
unsigned epIdx,
|
|
|
|
const cmChar_t** labelRef,
|
|
|
|
unsigned* idRef,
|
|
|
|
unsigned* rsiRef );
|
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
void cmRtNetReport( cmRtNetH_t h );
|
2014-06-15 03:40:46 +00:00
|
|
|
|
2013-04-16 01:10:49 +00:00
|
|
|
|
2013-04-28 22:20:33 +00:00
|
|
|
void cmRtNetTest( cmCtx_t* ctx, bool mstrFl );
|
2013-04-26 20:04:26 +00:00
|
|
|
|
|
|
|
/*
|
2013-04-28 22:20:33 +00:00
|
|
|
|
|
|
|
Synchronization Protocol:
|
|
|
|
|
|
|
|
Machine A Machine B
|
|
|
|
================================== ====================================
|
2013-05-04 15:56:44 +00:00
|
|
|
broadcast 'hello' ------------------=-> create node-A w/ ei=0 -------+
|
2013-04-28 22:20:33 +00:00
|
|
|
|
|
2013-05-04 15:56:44 +00:00
|
|
|
+<-- create node-B w/ ei=0 <--------=-- send 'node' <----------------+
|
2013-04-28 22:20:33 +00:00
|
|
|
|
|
|
|
|
+--> switch(ei,m_t)
|
2013-05-04 15:56:44 +00:00
|
|
|
| ei < en : send endpt[ei++] -=--> create endpt[] on node-A -->+
|
2013-04-28 22:20:33 +00:00
|
|
|
| |
|
2013-05-04 15:56:44 +00:00
|
|
|
| ei == en : ++ei,send 'done' -=------------------------------->+ |
|
2013-04-28 22:20:33 +00:00
|
|
|
| |
|
2013-05-04 15:56:44 +00:00
|
|
|
| m_t!='done': send 'done' -=------------------------------->+ |
|
2013-04-28 22:20:33 +00:00
|
|
|
| |
|
2013-05-04 15:56:44 +00:00
|
|
|
| (stop) : |
|
2013-04-28 22:20:33 +00:00
|
|
|
| |
|
|
|
|
| v
|
|
|
|
| switch(ei,m_t)
|
2013-05-04 15:56:44 +00:00
|
|
|
+<-- create endpt[] on node-B <---=----- send endpt[ei++] : ei < en
|
2013-04-28 22:20:33 +00:00
|
|
|
|
|
2013-05-04 15:56:44 +00:00
|
|
|
+<---------------------------------=----- send 'done',++ei : ei == en
|
2013-04-28 22:20:33 +00:00
|
|
|
|
|
2013-05-04 15:56:44 +00:00
|
|
|
+<---------------------------------=----- send 'done' : m_t!= 'done'
|
2013-04-28 22:20:33 +00:00
|
|
|
|
|
|
|
: (stop)
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
1) 'ei' is the index of the next local end point to transmit.
|
|
|
|
2) 'en' is the count of local endpoints.
|
|
|
|
3) 'm_t' is the msg type (i.e.'hello','node','endpoint','done')
|
|
|
|
of the incoming message.
|
2013-05-04 15:56:44 +00:00
|
|
|
4) The symbol -=- in the flow chart implies a network transmission.
|
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
|