Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > BestEffortMessaging
view LossyCom.c @ 0:29d8b41926f0
Initial commit - compiles but has still the wrong name
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 07 Mar 2012 19:46:37 +0100 |
| parents | |
| children | 826448e34e80 |
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 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint,
35 lossyCom__exchange_t* centralExchange,
36 lossyCom__endpointID_t endpointID,
37 lossyCom__msgHandler* msgHandler)
38 {
39 localEndpoint->localTriggerCopy = 0;
40 localEndpoint->endpointID = endpointID;
41 localEndpoint->centralExchangePtr = centralExchange;
42 localEndpoint->msgHandler = msgHandler;
43 }
45 void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint,
46 lossyCom__msgBody_t msg)
47 {
48 lossyCom__sendMsg(localEndpoint,
49 BROADCAST_ID,
50 msg);
51 }
53 void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint,
54 lossyCom__endpointID_t receiverEndpointID,
55 lossyCom__msgBody_t msg)
56 {
57 lossyCom__msg_t msgDraft;
58 uint16_t triggerCopy;
60 msgDraft = (0 | msg);
61 msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT);
63 triggerCopy = localEndpoint->centralExchangePtr->triggerCounter +1;
64 msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT);
66 //write msg to central exchange
67 localEndpoint->centralExchangePtr->outboxArray[localEndpoint->endpointID] =
68 msgDraft;
70 localEndpoint->centralExchangePtr->triggerCounter = triggerCopy;
71 }
73 void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint)
74 {
75 uint16_t remoteTriggerCopy;
76 lossyCom__endpointID_t senderEndpointID;
77 lossyCom__msg_t msgCopy;
78 uint16_t msgTrigger;
79 lossyCom__msgBody_t msgBody;
81 senderEndpointID = 0;
82 remoteTriggerCopy = localEndpoint->centralExchangePtr->triggerCounter;
83 //new message arrived if trigger counter is higher than the last time read
84 while(senderEndpointID < localEndpoint->centralExchangePtr->numEndpoints)
85 {
86 if(senderEndpointID != localEndpoint->endpointID)
87 {
88 msgCopy = localEndpoint->centralExchangePtr->outboxArray[senderEndpointID];
89 msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT);
90 if(msgTrigger > localEndpoint->localTriggerCopy &&
91 msgTrigger <= remoteTriggerCopy)
92 {
93 msgBody = 0xFFFFFFFF & msgCopy;
94 (*(localEndpoint->msgHandler))(senderEndpointID, msgBody);
95 }
96 }
97 senderEndpointID++;
98 }
99 //save last parsed msg
100 localEndpoint->localTriggerCopy = remoteTriggerCopy;
101 }
