Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > BestEffortMessaging
view LossyCom.c @ 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 |
line source
2 /*
3 * For a detailed description see header file.
4 */
6 #include "LossyCom.h"
8 #include "VMS_Implementations/VMS_impl/vmalloc.h"
10 /*
11 * Initializes the central exchange structure.
12 * Allocates memory to fit the number of endpoints.
13 * Returns NULL if an error occurs.
14 */
15 lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints)
16 {
17 lossyCom__exchange_t* exchange;
19 exchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t));
20 if(exchange == NULL)
21 return NULL;
23 exchange->triggerCounter = 0;
24 exchange->numEndpoints = numEndpoints;
25 exchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints);
26 if(exchange->outboxArray == NULL){
27 VMS_WL__free(exchange);
28 return NULL;
29 }
31 return exchange;
32 }
34 /*
35 * Connects the local endpoint to the central exchange structure.
36 * This registers a message handler that handles all the incomming messages.
37 * Also an endpointID is set this ID has to be between 0 and total number of
38 * endpoints-1 and the number has to be unique.
39 */
40 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint,
41 lossyCom__exchange_t* centralExchange,
42 lossyCom__endpointID_t endpointID,
43 lossyCom__msgHandler msgHandler,
44 void* msgHandlerData)
45 {
46 localEndpoint->localTriggerCopy = 0;
47 localEndpoint->endpointID = endpointID;
48 localEndpoint->centralExchange = centralExchange;
49 localEndpoint->msgHandler = msgHandler;
50 localEndpoint->msgHandlerData = msgHandlerData;
51 }
53 /*
54 * This broadcasts a message to all connected receivers
55 */
56 void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint,
57 lossyCom__msgBody_t msg)
58 {
59 lossyCom__sendMsg(localEndpoint,
60 BROADCAST_ID,
61 msg);
62 }
64 /*
65 * This sends a message another endpoint. Again it is not guaranteed that the
66 * message is received. But in most cases it will.
67 */
68 void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint,
69 lossyCom__endpointID_t receiverEndpointID,
70 lossyCom__msgBody_t msg)
71 {
72 lossyCom__msg_t msgDraft;
73 uint16_t triggerCopy;
75 //build message
76 msgDraft = (0 | msg);
77 msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT);
79 triggerCopy = localEndpoint->centralExchange->triggerCounter +1;
80 msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT);
82 //write msg to central exchange
83 localEndpoint->centralExchange->outboxArray[localEndpoint->endpointID] =
84 msgDraft;
86 localEndpoint->centralExchange->triggerCounter = triggerCopy;
87 }
89 void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint)
90 {
91 uint16_t currentTriggerCopy;
92 lossyCom__endpointID_t senderEndpointID;
93 lossyCom__msg_t msgCopy;
94 uint16_t msgTrigger;
95 lossyCom__msgBody_t msgBody;
97 senderEndpointID = 0;
98 currentTriggerCopy = localEndpoint->centralExchange->triggerCounter;
100 //new message arrived if trigger counter is higher than the last time read
101 while(senderEndpointID < localEndpoint->centralExchange->numEndpoints)
102 {
103 if(senderEndpointID != localEndpoint->endpointID)
104 {
105 msgCopy = localEndpoint->centralExchange->outboxArray[senderEndpointID];
106 msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT);
107 // check if the message is new (msg trigger > archived trigger)
108 // and already valid (msgTrigger <= currentTriggerCopy)
109 if(msgTrigger > localEndpoint->localTriggerCopy &&
110 msgTrigger <= currentTriggerCopy)
111 {
112 //let the message handler parse the message
113 msgBody = 0xFFFFFFFF & msgCopy;
114 (*(localEndpoint->msgHandler))(senderEndpointID,
115 msgBody,
116 localEndpoint->msgHandlerData);
117 }
118 }
119 senderEndpointID++;
120 }
121 //save last parsed msg
122 localEndpoint->localTriggerCopy = currentTriggerCopy;
123 }
