comparison LossyCom.c @ 5:95a03e431480

corrected comments and variable renaming
author Merten Sach <msach@mailbox.tu-berlin.de>
date Tue, 13 Mar 2012 19:07:49 +0100
parents 7ba5a3a6102d
children
comparison
equal deleted inserted replaced
4:78b75071ac2a 5:023baab2c7b6
1
2 /* 1 /*
3 * For a detailed description see header file. 2 * For a detailed description see header file.
4 */ 3 */
5 4
6 #include <string.h> 5 #include <string.h>
7 6
8 #include "LossyCom.h" 7 #include "LossyCom.h"
9 #include "VMS_Implementations/VMS_impl/vmalloc.h" 8 #include "VMS_Implementations/VMS_impl/vmalloc.h"
10 9
11 /* 10 /*
12 * Initializes the central exchange structure. 11 * Initializes the central exchange structure and sets all trigger counter to 0
13 * Allocates memory to fit the number of endpoints. 12 * Allocates memory to fit the number of endpoints.
14 * Returns NULL if an error occurs. 13 * Returns NULL if an error occurs.
15 */ 14 */
16 lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints) 15 lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints)
17 { 16 {
18 lossyCom__exchange_t* exchange; 17 lossyCom__exchange_t* centralExchange;
19 18
20 exchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t)); 19 centralExchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t));
21 if(exchange == NULL) 20 if(centralExchange == NULL)
22 return NULL; 21 return NULL;
23 22
24 exchange->BroadcastTriggerCounter = 0; 23 centralExchange->broadcastTriggerCounter = 0;
25 exchange->numEndpoints = numEndpoints; 24 centralExchange->numEndpoints = numEndpoints;
26 exchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints); 25 centralExchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints);
27 if(exchange->outboxArray == NULL){ 26 if(centralExchange->outboxArray == NULL){
28 VMS_WL__free(exchange); 27 VMS_WL__free(centralExchange);
29 return NULL; 28 return NULL;
30 } 29 }
31 30
32 exchange->p2pTriggerCounter = VMS_WL__malloc(sizeof(uint16_t)*numEndpoints); 31 centralExchange->p2pTriggerCounters = VMS_WL__malloc(sizeof(uint16_t)*numEndpoints);
33 if(exchange->p2pTriggerCounter == NULL){ 32 if(centralExchange->p2pTriggerCounters == NULL){
34 VMS_WL__free(exchange->outboxArray); 33 VMS_WL__free(centralExchange->outboxArray);
35 VMS_WL__free(exchange); 34 VMS_WL__free(centralExchange);
36 return NULL; 35 return NULL;
37 } 36 }
38 37
39 //reset all point 2 point trigger counter 38 //reset all point 2 point trigger counter
40 memset((void*)exchange->p2pTriggerCounter, 0, sizeof(uint16_t)*numEndpoints); 39 memset((void*)centralExchange->p2pTriggerCounters, 0, sizeof(uint16_t)*numEndpoints);
41 40
42 return exchange; 41 return centralExchange;
43 } 42 }
44 43
45 /* 44 /*
46 * Connects the local endpoint to the central exchange structure. 45 * Connects the local endpoint to the central exchange structure.
47 * This registers a message handler that handles all the incomming messages. 46 * This registers a message handler that handles all incoming messages.
48 * Also an endpointID is set this ID has to be between 0 and total number of 47 * Also an endpointID is set this ID has to be between 0 and total number of
49 * endpoints-1 and the number has to be unique. 48 * endpoints-1 and unique.
50 */ 49 */
51 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, 50 void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint,
52 lossyCom__exchange_t* centralExchange, 51 lossyCom__exchange_t* centralExchange,
53 lossyCom__endpointID_t endpointID, 52 lossyCom__endpointID_t endpointID,
54 lossyCom__msgHandler msgHandler, 53 lossyCom__msgHandler msgHandler,
59 localEndpoint->centralExchange = centralExchange; 58 localEndpoint->centralExchange = centralExchange;
60 localEndpoint->msgHandler = msgHandler; 59 localEndpoint->msgHandler = msgHandler;
61 localEndpoint->msgHandlerData = msgHandlerData; 60 localEndpoint->msgHandlerData = msgHandlerData;
62 } 61 }
63 62
63 /*
64 * Prepare a lossyCom__msg_t in the correct format that contains the trigger
65 * value, the receiver endpoint ID and the message body
66 */
64 inline lossyCom__msg_t prepareMsg(uint16_t triggerValue, 67 inline lossyCom__msg_t prepareMsg(uint16_t triggerValue,
65 lossyCom__endpointID_t receiverEndpointID, 68 lossyCom__endpointID_t receiverEndpointID,
66 lossyCom__msgBody_t msg) 69 lossyCom__msgBody_t msgBody)
67 { 70 {
68 lossyCom__msg_t msgDraft; 71 lossyCom__msg_t msgDraft;
69 72
70 msgDraft = (0 | msg); 73 msgDraft = (0 | msgBody);
71 msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT); 74 msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT);
72 msgDraft |= ((lossyCom__msg_t)triggerValue << TRIGGER_SHIFT); 75 msgDraft |= ((lossyCom__msg_t)triggerValue << TRIGGER_SHIFT);
73 76
74 return msgDraft; 77 return msgDraft;
75 } 78 }
82 { 85 {
83 lossyCom__exchange_t* centralExchange = localEndpoint->centralExchange; 86 lossyCom__exchange_t* centralExchange = localEndpoint->centralExchange;
84 uint16_t increasedTrigger; 87 uint16_t increasedTrigger;
85 lossyCom__msg_t msg; 88 lossyCom__msg_t msg;
86 89
87 increasedTrigger = centralExchange->BroadcastTriggerCounter +1; 90 increasedTrigger = centralExchange->broadcastTriggerCounter +1;
88 91
89 //build message 92 //build message
90 msg = prepareMsg(increasedTrigger, BROADCAST_ID, msgBody); 93 msg = prepareMsg(increasedTrigger, BROADCAST_ID, msgBody);
91 94
92 //write msg to central exchange 95 //write msg to central exchange
93 centralExchange->outboxArray[localEndpoint->endpointID] = msg; 96 centralExchange->outboxArray[localEndpoint->endpointID] = msg;
94 97
95 //update broadcast trigger counter 98 //update broadcast trigger counter
96 centralExchange->BroadcastTriggerCounter = increasedTrigger; 99 centralExchange->broadcastTriggerCounter = increasedTrigger;
97 } 100 }
98 101
99 /* 102 /*
100 * This sends a message another endpoint. Again it is not guaranteed that the 103 * This sends a message another endpoint. Again it is not guaranteed that the
101 * message is received. But in most cases it will. 104 * message is received. But in most cases it will.
107 lossyCom__exchange_t* centralExchange = localEndpoint->centralExchange; 110 lossyCom__exchange_t* centralExchange = localEndpoint->centralExchange;
108 lossyCom__msg_t msg; 111 lossyCom__msg_t msg;
109 uint16_t increasedTrigger; 112 uint16_t increasedTrigger;
110 113
111 increasedTrigger = 114 increasedTrigger =
112 centralExchange->p2pTriggerCounter[receiverEndpointID] +1; 115 centralExchange->p2pTriggerCounters[receiverEndpointID] +1;
113 116
114 //build message 117 //build message
115 msg = prepareMsg(increasedTrigger, receiverEndpointID, msgBody); 118 msg = prepareMsg(increasedTrigger, receiverEndpointID, msgBody);
116 119
117 //write msg to central exchange 120 //write msg to central exchange
118 centralExchange->outboxArray[localEndpoint->endpointID] = msg; 121 centralExchange->outboxArray[localEndpoint->endpointID] = msg;
119 122
120 //write back increased trigger counter 123 //write back increased trigger counter
121 centralExchange->p2pTriggerCounter[receiverEndpointID] = increasedTrigger; 124 centralExchange->p2pTriggerCounters[receiverEndpointID] = increasedTrigger;
122 } 125 }
123 126
124 int inline isUnreceivedMsg(uint16_t msgTrigger, 127 int inline isUnreceivedMsg(uint16_t msgTrigger,
125 uint16_t lastReceivedTrigger, 128 uint16_t lastReceivedTrigger,
126 uint16_t triggerSnapshot) 129 uint16_t triggerSnapshot)
151 uint16_t msgTrigger; 154 uint16_t msgTrigger;
152 lossyCom__msgBody_t msgBody; 155 lossyCom__msgBody_t msgBody;
153 156
154 centralExchange = localEndpoint->centralExchange; 157 centralExchange = localEndpoint->centralExchange;
155 //save trigger counter to know find valid messages 158 //save trigger counter to know find valid messages
156 broadcastTriggerSnapshot = centralExchange->BroadcastTriggerCounter; 159 broadcastTriggerSnapshot = centralExchange->broadcastTriggerCounter;
157 p2pTriggerSnapshot = 160 p2pTriggerSnapshot =
158 centralExchange->p2pTriggerCounter[localEndpoint->endpointID]; 161 centralExchange->p2pTriggerCounters[localEndpoint->endpointID];
159 162
160 //new message arrived if trigger counter is higher than the last time read 163 //new message arrived if trigger counter is higher than the last time read
161 if( broadcastTriggerSnapshot > localEndpoint->lastReceivedBroadcastTrigger || 164 if( broadcastTriggerSnapshot > localEndpoint->lastReceivedBroadcastTrigger ||
162 p2pTriggerSnapshot > localEndpoint->lastReceivedp2pTrigger) 165 p2pTriggerSnapshot > localEndpoint->lastReceivedp2pTrigger)
163 { 166 {