diff 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 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