diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LossyCom.c	Wed Mar 07 19:46:37 2012 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +
     1.5 +/*
     1.6 + * For a detailed description see header file.
     1.7 + */
     1.8 +
     1.9 +#include "LossyCom.h"
    1.10 +
    1.11 +#include "VMS_Implementations/VMS_impl/vmalloc.h"
    1.12 +
    1.13 +/*
    1.14 + * Initializes the central exchange structure.
    1.15 + * Allocates memory to fit the number of endpoints.
    1.16 + * Returns NULL if an error occurs.
    1.17 + */
    1.18 +lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints)
    1.19 +{
    1.20 +    lossyCom__exchange_t* exchange;
    1.21 +    
    1.22 +    exchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t));
    1.23 +    if(exchange == NULL)
    1.24 +        return NULL;
    1.25 +    
    1.26 +    exchange->triggerCounter = 0;
    1.27 +    exchange->numEndpoints = numEndpoints;
    1.28 +    exchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints);
    1.29 +    if(exchange->outboxArray == NULL){
    1.30 +        VMS_WL__free(exchange);
    1.31 +        return NULL;
    1.32 +    }
    1.33 +    
    1.34 +    return exchange;    
    1.35 +}
    1.36 +
    1.37 +void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint,
    1.38 +                                   lossyCom__exchange_t* centralExchange,
    1.39 +                                   lossyCom__endpointID_t endpointID,
    1.40 +                                   lossyCom__msgHandler* msgHandler)
    1.41 +{
    1.42 +    localEndpoint->localTriggerCopy = 0;
    1.43 +    localEndpoint->endpointID = endpointID;
    1.44 +    localEndpoint->centralExchangePtr = centralExchange;
    1.45 +    localEndpoint->msgHandler = msgHandler;    
    1.46 +}
    1.47 +
    1.48 +void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint,
    1.49 +                                   lossyCom__msgBody_t msg)
    1.50 +{
    1.51 +    lossyCom__sendMsg(localEndpoint,
    1.52 +                      BROADCAST_ID,
    1.53 +                      msg);
    1.54 +}
    1.55 +
    1.56 +void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint,
    1.57 +                       lossyCom__endpointID_t receiverEndpointID,
    1.58 +                       lossyCom__msgBody_t msg)
    1.59 +{
    1.60 +    lossyCom__msg_t msgDraft;
    1.61 +    uint16_t triggerCopy;
    1.62 +    
    1.63 +    msgDraft = (0 | msg);
    1.64 +    msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT);
    1.65 +    
    1.66 +    triggerCopy = localEndpoint->centralExchangePtr->triggerCounter +1;
    1.67 +    msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT);
    1.68 +    
    1.69 +    //write msg to central exchange
    1.70 +    localEndpoint->centralExchangePtr->outboxArray[localEndpoint->endpointID] =
    1.71 +            msgDraft;
    1.72 +    
    1.73 +    localEndpoint->centralExchangePtr->triggerCounter = triggerCopy;
    1.74 +}
    1.75 +
    1.76 +void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint)
    1.77 +{
    1.78 +    uint16_t remoteTriggerCopy;
    1.79 +    lossyCom__endpointID_t senderEndpointID;
    1.80 +    lossyCom__msg_t msgCopy;
    1.81 +    uint16_t msgTrigger;
    1.82 +    lossyCom__msgBody_t msgBody;
    1.83 +    
    1.84 +    senderEndpointID = 0;
    1.85 +    remoteTriggerCopy = localEndpoint->centralExchangePtr->triggerCounter;
    1.86 +    //new message arrived if trigger counter is higher than the last time read
    1.87 +    while(senderEndpointID < localEndpoint->centralExchangePtr->numEndpoints)
    1.88 +    {
    1.89 +        if(senderEndpointID != localEndpoint->endpointID)
    1.90 +        {
    1.91 +            msgCopy = localEndpoint->centralExchangePtr->outboxArray[senderEndpointID];
    1.92 +            msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT);
    1.93 +            if(msgTrigger > localEndpoint->localTriggerCopy &&
    1.94 +                    msgTrigger <= remoteTriggerCopy)
    1.95 +            {
    1.96 +                msgBody = 0xFFFFFFFF & msgCopy;
    1.97 +                (*(localEndpoint->msgHandler))(senderEndpointID, msgBody);
    1.98 +            }
    1.99 +        }
   1.100 +        senderEndpointID++;
   1.101 +    }
   1.102 +    //save last parsed msg
   1.103 +    localEndpoint->localTriggerCopy = remoteTriggerCopy;
   1.104 +}
   1.105 \ No newline at end of file