Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > BestEffortMessaging
changeset 1:826448e34e80
added data to message handler
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Thu, 08 Mar 2012 20:06:00 +0100 |
| parents | 29d8b41926f0 |
| children | b6dd31dbab8c |
| files | LossyCom.c LossyCom.h |
| diffstat | 2 files changed, 51 insertions(+), 23 deletions(-) [+] |
line diff
1.1 --- a/LossyCom.c Wed Mar 07 19:46:37 2012 +0100 1.2 +++ b/LossyCom.c Thu Mar 08 20:06:00 2012 +0100 1.3 @@ -31,17 +31,28 @@ 1.4 return exchange; 1.5 } 1.6 1.7 +/* 1.8 + * Connects the local endpoint to the central exchange structure. 1.9 + * This registers a message handler that handles all the incomming messages. 1.10 + * Also an endpointID is set this ID has to be between 0 and total number of 1.11 + * endpoints-1 and the number has to be unique. 1.12 + */ 1.13 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, 1.14 lossyCom__exchange_t* centralExchange, 1.15 lossyCom__endpointID_t endpointID, 1.16 - lossyCom__msgHandler* msgHandler) 1.17 + lossyCom__msgHandler msgHandler, 1.18 + void* msgHandlerData) 1.19 { 1.20 localEndpoint->localTriggerCopy = 0; 1.21 localEndpoint->endpointID = endpointID; 1.22 - localEndpoint->centralExchangePtr = centralExchange; 1.23 - localEndpoint->msgHandler = msgHandler; 1.24 + localEndpoint->centralExchange = centralExchange; 1.25 + localEndpoint->msgHandler = msgHandler; 1.26 + localEndpoint->msgHandlerData = msgHandlerData; 1.27 } 1.28 1.29 +/* 1.30 + * This broadcasts a message to all connected receivers 1.31 + */ 1.32 void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint, 1.33 lossyCom__msgBody_t msg) 1.34 { 1.35 @@ -50,6 +61,10 @@ 1.36 msg); 1.37 } 1.38 1.39 +/* 1.40 + * This sends a message another endpoint. Again it is not guaranteed that the 1.41 + * message is received. But in most cases it will. 1.42 + */ 1.43 void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint, 1.44 lossyCom__endpointID_t receiverEndpointID, 1.45 lossyCom__msgBody_t msg) 1.46 @@ -57,45 +72,52 @@ 1.47 lossyCom__msg_t msgDraft; 1.48 uint16_t triggerCopy; 1.49 1.50 + //build message 1.51 msgDraft = (0 | msg); 1.52 msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT); 1.53 1.54 - triggerCopy = localEndpoint->centralExchangePtr->triggerCounter +1; 1.55 + triggerCopy = localEndpoint->centralExchange->triggerCounter +1; 1.56 msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT); 1.57 1.58 //write msg to central exchange 1.59 - localEndpoint->centralExchangePtr->outboxArray[localEndpoint->endpointID] = 1.60 + localEndpoint->centralExchange->outboxArray[localEndpoint->endpointID] = 1.61 msgDraft; 1.62 1.63 - localEndpoint->centralExchangePtr->triggerCounter = triggerCopy; 1.64 + localEndpoint->centralExchange->triggerCounter = triggerCopy; 1.65 } 1.66 1.67 void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint) 1.68 { 1.69 - uint16_t remoteTriggerCopy; 1.70 + uint16_t currentTriggerCopy; 1.71 lossyCom__endpointID_t senderEndpointID; 1.72 lossyCom__msg_t msgCopy; 1.73 uint16_t msgTrigger; 1.74 lossyCom__msgBody_t msgBody; 1.75 1.76 senderEndpointID = 0; 1.77 - remoteTriggerCopy = localEndpoint->centralExchangePtr->triggerCounter; 1.78 + currentTriggerCopy = localEndpoint->centralExchange->triggerCounter; 1.79 + 1.80 //new message arrived if trigger counter is higher than the last time read 1.81 - while(senderEndpointID < localEndpoint->centralExchangePtr->numEndpoints) 1.82 + while(senderEndpointID < localEndpoint->centralExchange->numEndpoints) 1.83 { 1.84 if(senderEndpointID != localEndpoint->endpointID) 1.85 { 1.86 - msgCopy = localEndpoint->centralExchangePtr->outboxArray[senderEndpointID]; 1.87 + msgCopy = localEndpoint->centralExchange->outboxArray[senderEndpointID]; 1.88 msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT); 1.89 + // check if the message is new (msg trigger > archived trigger) 1.90 + // and already valid (msgTrigger <= currentTriggerCopy) 1.91 if(msgTrigger > localEndpoint->localTriggerCopy && 1.92 - msgTrigger <= remoteTriggerCopy) 1.93 + msgTrigger <= currentTriggerCopy) 1.94 { 1.95 + //let the message handler parse the message 1.96 msgBody = 0xFFFFFFFF & msgCopy; 1.97 - (*(localEndpoint->msgHandler))(senderEndpointID, msgBody); 1.98 + (*(localEndpoint->msgHandler))(senderEndpointID, 1.99 + msgBody, 1.100 + localEndpoint->msgHandlerData); 1.101 } 1.102 } 1.103 senderEndpointID++; 1.104 } 1.105 //save last parsed msg 1.106 - localEndpoint->localTriggerCopy = remoteTriggerCopy; 1.107 + localEndpoint->localTriggerCopy = currentTriggerCopy; 1.108 } 1.109 \ No newline at end of file
2.1 --- a/LossyCom.h Wed Mar 07 19:46:37 2012 +0100 2.2 +++ b/LossyCom.h Thu Mar 08 20:06:00 2012 +0100 2.3 @@ -21,23 +21,27 @@ 2.4 2.5 #include <stdint.h> 2.6 2.7 - 2.8 +/*********************************** 2.9 + * General Defines 2.10 + ***********************************/ 2.11 +//never use this number as a endpoint! 2.12 +#define BROADCAST_ID 65535 2.13 2.14 /*********************************** 2.15 * Type Definitions 2.16 ***********************************/ 2.17 2.18 -#define BROADCAST_ID 65535 2.19 -#define ENDPOINT_ID_SHIFT 32 2.20 -#define TRIGGER_SHIFT 48 2.21 - 2.22 /* 2.23 * A message consists of the 2.24 - * 16bit saved trigger value when the message was sent 2.25 + * 16bit saved trigger value +1 when the message was sent 2.26 * 16bit receiver identifier, this is the index of the slot in the array 2.27 * 65536 is the broadcast identifier 2.28 * 32bit message body, the message format is defined by the endpoints 2.29 + * 2.30 + * | 16bit trigger value | 16bit receiverID | 32bit message body | 2.31 */ 2.32 +#define ENDPOINT_ID_SHIFT 32 2.33 +#define TRIGGER_SHIFT 48 2.34 typedef uint64_t lossyCom__msg_t; 2.35 typedef uint32_t lossyCom__msgBody_t; 2.36 typedef uint16_t lossyCom__endpointID_t; 2.37 @@ -47,7 +51,7 @@ 2.38 * Example: 2.39 * void handler_func(lossyCom__endpointID_t senderID, lossyCom__msgBody_t msg); 2.40 */ 2.41 -typedef void (*lossyCom__msgHandler) (lossyCom__endpointID_t, lossyCom__msgBody_t); 2.42 +typedef void (*lossyCom__msgHandler) (lossyCom__endpointID_t, lossyCom__msgBody_t, void*); 2.43 2.44 /* 2.45 * Central communication structure. 2.46 @@ -64,8 +68,9 @@ 2.47 typedef struct { 2.48 uint16_t localTriggerCopy; 2.49 lossyCom__endpointID_t endpointID; 2.50 - lossyCom__exchange_t* centralExchangePtr; 2.51 - lossyCom__msgHandler* msgHandler; 2.52 + lossyCom__exchange_t* centralExchange; 2.53 + lossyCom__msgHandler msgHandler; 2.54 + void* msgHandlerData; 2.55 } lossyCom__endpoint_t; 2.56 2.57 /*********************************** 2.58 @@ -77,7 +82,8 @@ 2.59 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, 2.60 lossyCom__exchange_t* exchange, 2.61 lossyCom__endpointID_t endpointID, 2.62 - lossyCom__msgHandler* msgHandler); 2.63 + lossyCom__msgHandler msgHandler, 2.64 + void* msgHandlerData); 2.65 2.66 void lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint, 2.67 lossyCom__msgBody_t msg);
