Example #1
0
void AckController::newIncomingMessage (const void *, uint16, DisServiceMsg *pDisServiceMsg, uint32, const char *)
{
    switch (pDisServiceMsg->getType()) {
        case DisServiceMsg::DSMT_DataReq: {
            DisServiceDataReqMsg *pReqMsg = (DisServiceDataReqMsg*) pDisServiceMsg;
            PtrLList<DisServiceDataReqMsg::FragmentRequest> *pRequests = pReqMsg->getRequests();
            DisServiceDataReqMsg::FragmentRequest *pRequest;
            DisServiceDataReqMsg::FragmentRequest *pRequestTmp = pRequests->getFirst();
            // Call messageRequested() for each requested message 
            while ((pRequest = pRequestTmp) != NULL) {
                pRequestTmp = pRequests->getNext();
                messageRequested (pRequest->pMsgHeader->getMsgId(),
                                  pDisServiceMsg->getSenderNodeId());
            }
            break;
        }

        case DisServiceMsg::DSMT_AcknowledgmentMessage: {
            DisServiceAcknowledgmentMessage *pAckMsg = (DisServiceAcknowledgmentMessage*) pDisServiceMsg;
            messageAcknowledged (pAckMsg->getAckedMsgId(), pDisServiceMsg->getSenderNodeId());
            break;
        }

        default:
            break;
    }
}
Example #2
0
DArray<uint16> * LocalNodeInfo::getSubscribingClients (Message *pMsg)
{
    DArray<uint16> *pSubscribingClients = NULL;
    uint16 j = 0;
     _m.lock (314);
    for (UInt32Hashtable<SubscriptionList>::Iterator i = _localSubscriptions.getAllElements(); !i.end(); i.nextElement()) {
        PtrLList<Subscription> *pSubscriptions = i.getValue()->getSubscriptionWild (pMsg->getMessageHeader()->getGroupName());
        // Get every subscribed group that matches with the message's group
        if (pSubscriptions != NULL) {
            for (Subscription *pSub = pSubscriptions->getFirst(); pSub != NULL; pSub = pSubscriptions->getNext()) {
                if ((pSub->getSubscriptionType() == Subscription::GROUP_PREDICATE_SUBSCRIPTION) || pSub->matches(pMsg)) {
                    if (pSubscribingClients == NULL) {
                        pSubscribingClients = new DArray<uint16>();
                    }
                    bool bFound = false;
                    for (unsigned int k = 0; k < pSubscribingClients->size(); k++) {
                        if ((*pSubscribingClients)[k] == i.getKey()) {
                            bFound = true;
                            break;
                        }
                    }
                    if (!bFound) {
                        (*pSubscribingClients)[j] = i.getKey();
                        j++;
                    }
                }
            }
            delete pSubscriptions;
            pSubscriptions = NULL;
        }
    }
    _m.unlock (314);
    return pSubscribingClients;
}
Example #3
0
/**
 * Add an event.
 */
int Cron::addEvent (Event *e)
{
    e->_id = getId ();
    PtrLList<Cron::Event> *pEventList = (PtrLList<Cron::Event>*) _pEventList;
    pEventList->append (e);  
    return e->_id;
}
PtrLList<String> * TopologyWorldState::getInterestedRemoteNodes (DisServiceDataMsg *pDSDMsg)
{
    // Returns a list with the remote nodes interested in pDSDMsg
    _m.lock(143);
    PtrLList<String> *pInterestedRemoteNodes = NULL;
    // Alive nodes
    for (StringHashtable<Thing>::Iterator iNeighbors = _pLocalNodeInfo->getAllElements(); !iNeighbors.end(); iNeighbors.nextElement()) {
        RemoteNodeInfo *pRNI = (RemoteNodeInfo *) iNeighbors.getValue();
        if (pRNI->isNodeInterested (pDSDMsg)) {
            if (pInterestedRemoteNodes == NULL) {
                pInterestedRemoteNodes = new PtrLList<String>();
            }
            pInterestedRemoteNodes->insert (new String (pRNI->getNodeId()));
        }
    }
    // Dead peers
    for (StringHashtable<RemoteNodeInfo>::Iterator iDead = _deadPeers.getAllElements(); !iDead.end(); iDead.nextElement()) {
        RemoteNodeInfo *pRNI = (RemoteNodeInfo *) iDead.getValue();
        if (pRNI->isNodeInterested (pDSDMsg)) {
            if (pInterestedRemoteNodes == NULL) {
                pInterestedRemoteNodes = new PtrLList<String>();
            }
            pInterestedRemoteNodes->insert (new String (pRNI->getNodeId()));
        }
    }
    _m.unlock(143);
    return pInterestedRemoteNodes;
}
Example #5
0
void RemoteNodeInfo::printAllSubscribedGroups (void)
{
    PtrLList<String> *pGroupNames = getAllSubscribedGroups();
    if (pGroupNames) {
        for (String *pGroupName = pGroupNames->getFirst(); pGroupName; pGroupName = pGroupNames->getNext()) {
            checkAndLogMsg ("RemoteNodeInfo::printAllSubscribedGroups", Logger::L_Info, "       Subscribed group: %s\n", pGroupName->c_str());
        }
    }
}
Example #6
0
void LocalNodeInfo::recomputeConsolidateSubsciptionList (void)
{
    // TODO CHECK: I don't think the includes applies anymore, so I commented it
    // In fact, even if the includes returns true,
    // I still need to merge subscriptions in terms of priority, reliability, sequenced, etc

    CRC * pCRC = new CRC();
    pCRC->init();
    for (StringHashtable<Subscription>::Iterator iterator = _consolidatedSubscriptions.getIterator(); !iterator.end(); iterator.nextElement()) {
        pCRC->update ((const char *)iterator.getKey());
        pCRC->update32 (iterator.getValue());
    }
    uint16 oldCRC = pCRC->getChecksum();

    // Delete the current Consolidate Subscription List and compute a new one
    _consolidatedSubscriptions.clear();

    // For each client
    for (UInt32Hashtable<SubscriptionList>::Iterator i = _localSubscriptions.getAllElements(); !i.end(); i.nextElement()) {
        SubscriptionList *pSL = i.getValue();
        if (pSL != NULL) {
            // Get all its subscriptions
            PtrLList<String> *pSubgroups = pSL->getAllSubscribedGroups();
            for (String *pSubGroupName = pSubgroups->getFirst(); pSubGroupName != NULL; pSubGroupName = pSubgroups->getNext()) {
                // For each group, get the subscription the client has
                const char *pszGroupName = pSubGroupName->c_str();
                Subscription *pClientSub = pSL->getSubscription (pszGroupName);
                // And the subscription in the consolidate subscription list if any
                Subscription *pSubInConsolidateList = _consolidatedSubscriptions.getSubscription (pszGroupName);
                if (pSubInConsolidateList == NULL) {
                    _consolidatedSubscriptions.addSubscription (pszGroupName, pClientSub->clone());
                }
                else {
                    /*if (pClientSub->includes (pSubInConsolidateList)) {
                        _consolidatedSubscriptions.removeGroup (pszGroupName);
                        _consolidatedSubscriptions.addSubscription (pszGroupName, pClientSub->clone());
                    }
                    else {*/
                        pClientSub->merge (pSubInConsolidateList);
                    /*}*/
                }
            }
        }
    }

    pCRC->reset();
    for (StringHashtable<Subscription>::Iterator iterator = _consolidatedSubscriptions.getIterator(); !iterator.end(); iterator.nextElement()) {
        pCRC->update ((const char *) iterator.getKey());
        pCRC->update32 (iterator.getValue());
    }
    uint16 newCRC = pCRC->getChecksum();
    if (oldCRC != newCRC) {
        ui32SubscriptionStateSeqID++;
        GroupSubscription *pSubscription = new GroupSubscription(); // Void subscription to respect method interface
        _notifier.modifiedSubscriptionForPeer (_pszId, pSubscription);
    }
}
Example #7
0
/**
 * Delete an event, given the eventId
 */
int Cron::deleteEvent (int iEventId)
{
    Event *pEvent;
    PtrLList<Cron::Event> *pEventList = (PtrLList<Cron::Event>*) _pEventList;
    pEventList->resetGet();
    while ((pEvent = pEventList->getNext()) != NULL) {
        if (pEvent->_id == iEventId) {
            pEventList->remove (pEvent);
            return 1;
        }
    }
    return 0;
}
Example #8
0
PtrLList<HistoryRequest> * LocalNodeInfo::getHistoryRequests (uint16 ui16ClientId)
{
    _m.lock (310);
    PtrLList<HistoryRequest> *pRet = new PtrLList<HistoryRequest>();
    SubscriptionList *pSL = _localSubscriptions.get (ui16ClientId);
    if (pSL != NULL) {
        pSL->getHistoryRequests (*pRet);
    }
    if (!pRet->getFirst()) {
        delete pRet;
        pRet = NULL;
    }
    _m.unlock (310);
    return pRet;
}
Example #9
0
int GroupTagSubscription::getHistoryRequest (const char * pszGroupName, PtrLList<HistoryRequest> &historyRequest)
{
    UInt32Hashtable<TagInfo>::Iterator i = _ui16Tags.getAllElements();
    TagInfo *pTI;
    History *pHistory;
    while (!i.end()) {
        pTI = i.getValue();
        pHistory = pTI->pHistory;
        if (pHistory) {
            if (pHistory->isExpired()) {
                delete pHistory;
                pHistory = NULL;
            }
            else {
                HistoryRequestGroupTag * pReq = new HistoryRequestGroupTag;
                pReq->_pHistory = pTI->pHistory;
                pReq->_pszGroupName = pszGroupName;
                pReq->_ui16Tag = (uint16) i.getKey();
                historyRequest.prepend(pReq);
            }
        }
        i.nextElement();
    }
    return 0;
}
Example #10
0
int GroupSubscription::getHistoryRequest (const char * pszGroupName, PtrLList<HistoryRequest> &historyRequest)
{
    HistoryRequestGroupTag * pReq = new HistoryRequestGroupTag;
    pReq->_pHistory = _pHistory;
    pReq->_pszGroupName = pszGroupName;
    pReq->_ui16Tag = 0;
    historyRequest.prepend(pReq);
    return 0;
}
Example #11
0
int GroupPredicateSubscription::getHistoryRequest (const char * pszGroupName, PtrLList<HistoryRequest> &historyRequest)
{
    HistoryRequestPredicate * pReq = new HistoryRequestPredicate;
    pReq->_pHistory = _pHistory;
    pReq->_pszGroupName = pszGroupName;
    pReq->ui8PredicateType = _ui8PredicateType;
    pReq->_pszPredicate =  _predicate;
    historyRequest.prepend (pReq);
    return 0;
}
Example #12
0
bool LocalNodeInfo::requireSequentiality (const char *pszGroupName, uint16 ui16Tag)
{
    _m.lock (321);
    PtrLList<Subscription> *pSubscriptions = _consolidatedSubscriptions.getSubscriptionWild(pszGroupName);
    if (pSubscriptions != NULL) {
        for (Subscription *pSub = pSubscriptions->getFirst(); pSub != NULL; pSub = pSubscriptions->getNext()) {
            if (pSub->getSubscriptionType() == Subscription::GROUP_SUBSCRIPTION) {
                GroupSubscription *pGS = (GroupSubscription *) pSub;
                if (pGS->isSequenced()) {
                    delete pSubscriptions;
                    pSubscriptions = NULL;
                    _m.unlock (321);
                    return true;
                }
            }
            else if (pSub->getSubscriptionType() == Subscription::GROUP_TAG_SUBSCRIPTION) {
                GroupTagSubscription *pGTS = (GroupTagSubscription *) pSub;
                if (pGTS->isSequenced(ui16Tag)) {
                    delete pSubscriptions;
                    pSubscriptions = NULL;
                    _m.unlock (321);
                    return true;
                }
            }
            else if (pSub->getSubscriptionType() == Subscription::GROUP_PREDICATE_SUBSCRIPTION) {
                GroupPredicateSubscription *pGPS = (GroupPredicateSubscription *) pSub;
                if (pGPS->isSequenced()) {
                    delete pSubscriptions;
                    pSubscriptions = NULL;
                    _m.unlock (321);
                    return true;
                }
            }
        }

        delete pSubscriptions;
        pSubscriptions = NULL;
    }

    _m.unlock (321);
    return false;
}
Example #13
0
void DSProImpl::sendWaypointMessage (const void *pBuf, uint32 ui32BufLen)
{
    if (pBuf == NULL || ui32BufLen == 0) {
        return;
    }

    PtrLList<String> *pNeighborList = _pTopology->getNeighbors();
    if (pNeighborList == NULL) {
        return;
    }
    if (pNeighborList->getFirst () == NULL) {
        delete pNeighborList;
        return;
    }

    NodeIdSet nodeIdSet;
    PreviousMessageIds previousMessageIds;
    String *pszNextPeerId = pNeighborList->getFirst ();
    for (String *pszCurrPeerId; (pszCurrPeerId = pszNextPeerId) != NULL;) {
        pszNextPeerId = pNeighborList->getNext ();
        previousMessageIds.add (pszCurrPeerId->c_str (), _pScheduler->getLatestMessageReplicatedToPeer (pszCurrPeerId->c_str ()));
        nodeIdSet.add (pszCurrPeerId->c_str ());
        delete pNeighborList->remove (pszCurrPeerId);
    }
    delete pNeighborList;

    uint32 ui32TotalLen = 0;
    void *pData = WaypointMessageHelper::writeWaypointMessageForTarget (previousMessageIds, pBuf, ui32BufLen, ui32TotalLen);
    Targets **ppTargets = _pTopology->getNextHopsAsTarget (nodeIdSet);
    if ((ppTargets != NULL) && (ppTargets[0] != NULL)) {
        // Send the waypoint message on each available interface that reaches the recipients
        int rc = _adaptMgr.sendWaypointMessage (pData, ui32TotalLen, _nodeId, ppTargets);
        String sLatestMsgs (previousMessageIds);
        String sPeers (nodeIdSet);
        checkAndLogMsg ("DSPro::sendWaypointMessage", Logger::L_Info, "sending waypoint message "
            "to %s (%s); last message pushed to this node was %s.\n", sPeers.c_str (),
            (rc == 0 ? "succeeded" : "failed"), sLatestMsgs.c_str ());
    }
    Targets::deallocateTargets (ppTargets);
    free (pData);
}
Example #14
0
PtrLList<String> * LocalNodeInfo::getAllSubscriptions (void)
{
    _m.lock (326);
    if (_consolidatedSubscriptions.isEmpty()) {
        _m.unlock (326);
        return NULL;
    }
    PtrLList<String> *pRet = _consolidatedSubscriptions.getAllSubscribedGroups();
    const char *pszEnd = ".[od]";
    PtrLList<String> temp = (*pRet);
    for (String *pszCurr = temp.getFirst(); pszCurr != NULL; pszCurr = temp.getNext()) {
        if (pszCurr->endsWith (pszEnd) == 1) {
            String *pDel = pRet->remove (pszCurr);
            if (pDel != NULL) {
                delete pDel;
            }
        }
    }
    _m.unlock (326);
    return pRet;
}
PtrLList<String> * TopologyWorldState::getTargetNodes (DisServiceDataMsg *pDSDMsg)
{
    // Returns a list with the target nodes for pDSDMsg
    _m.lock (145);
    PtrLList<String> *pInterestedNodes = getInterestedRemoteNodes (pDSDMsg);
    PtrLList<String> *pTargetNodes = NULL;
    if (pInterestedNodes) {
        for (String *pSubNode = pInterestedNodes->getFirst(); pSubNode; pSubNode = pInterestedNodes->getNext()) {
            bool bActiveTarget = isActiveNeighbor (pSubNode->c_str());
            const char *pszBestGW = NULL;
            if (bActiveTarget) {
                pszBestGW = pSubNode->c_str();
            } else {
                pszBestGW = getBestGateway (pSubNode->c_str());
            }
            if (pszBestGW) {
                if (pTargetNodes == NULL) {
                    pTargetNodes = new PtrLList<String>();
                }
                if (pTargetNodes->search (new String (pszBestGW)) == NULL) {
                    pTargetNodes->insert (new String (pszBestGW));
                }
            }
        }
    }
    _m.unlock (145);
    return pTargetNodes;
}
Example #16
0
int DSProQueryController::doSQLQueryOnMetadata (const char *pszGroupName, const void *pQuery, unsigned int uiQueryLen,
                                                const char *, InformationStore *pInfoStore,
                                                PtrLList<const char> *pResultsList)
{
    const char *pszMethodName = "DSProQueryController::doSQLQueryOnMetadata";

    String query ((char *) pQuery, uiQueryLen);

    /*const char *pszSqlConstraints = nullptr;

    char *pszTemp = nullptr;
    char *pszToken = nullptr;
    pszToken = strtok_mt (query.c_str(), "WHERE ", &pszTemp);
    if (pszToken == nullptr) {
        checkAndLogMsg (pszMethodName, Logger::L_Warning, "Error in parsing the char * containing the sql query\n");
        return -1;
    }
    pszSqlConstraints = strtok_mt (nullptr, "WHERE ", &pszTemp);
    if (pszSqlConstraints == nullptr) {
        checkAndLogMsg (pszMethodName, Logger::L_Warning, "No where conditions\n");
        return -2;
    }*/

    PtrLList<const char> *ptmp = pInfoStore->getMessageIDs (pszGroupName, query);
    if (ptmp == nullptr) {
        checkAndLogMsg (pszMethodName, Logger::L_Warning, "Error in retrieving the messageIds\n");
        return -3;
    }
    if (pResultsList != nullptr) {
        const char *pszId = ptmp->getFirst();
        for (; pszId != nullptr; pszId = ptmp->getNext()) {
            pResultsList->prepend (pszId);
        }
        ptmp->removeAll (false);
        delete ptmp;
    }

    return 0;
}
Example #17
0
/**
 * The run method. Sits in a while loop checking to 
 * see if any of the events in the list are at a time
 * when they should fire
 */
int Cron::run (void)
{
    while (1) {
        Event *pEvent; // temporary event
        PtrLList<Cron::Event> *pEventList = (PtrLList<Cron::Event>*) _pEventList;
        pEventList->resetGet(); // start at the beginning of the list
        
        // Go through the list
        while ((pEvent = pEventList->getNext()) != NULL) {
            ATime currentTime; // get the current time
            // check to see if the event should happen at this time
            if (pEvent->getStartTime() == currentTime) {
                
                // if the event should happen at this time, check to see whether
                // the event should happen on this day
                int dayOfMonth = currentTime.dayOfMonth();
                int dayOfWeek = currentTime.dayOfWeek();

                if ((pEvent->getDaysOfMonth() & ((0x00000001UL) << (dayOfMonth - 1))) > 0) {
                    if ((pEvent->getDaysOfWeek() & ((0x01) << (dayOfWeek - 1))) > 0) {
                        (*pEvent->getEventCallback()) (pEvent); // call the function
                        // check the count
                        if (pEvent->getCurrentCount() == pEvent->getCount()) {
                            deleteEvent (pEvent->_id); 
                        } else {
                            pEvent->incrCount();
                        }
                        // update the time
                        ATime tmpTime = pEvent->getStartTime() + pEvent->getPeriod();
                        pEvent->setTime(tmpTime);
                    }
                }
            } 
        } 

        sleepForMilliseconds (60000);
      
    }
}
Example #18
0
PtrLList<Chunker::Fragment> * Chunker::fragmentBuffer (const void *pBuf, uint32 ui32Len, Type inputObjectType, uint8 ui8NoOfChunks, Type outputChunkType, uint8 ui8ChunkCompressionQuality)
{
    BMPImage *pBMPImage = NULL;
    if (inputObjectType == Chunker::BMP) {
        BufferReader br (pBuf, ui32Len, false);
        int rc;
        pBMPImage = new BMPImage();
        if (0 != (rc = pBMPImage->readHeaderAndImage (&br))) {
            checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                            "failed to read BMP image; rc = %d\n", rc);
            delete pBMPImage;
            return NULL;
        }
    }
    else if (inputObjectType == Chunker::JPEG) {
        pBMPImage = JPEGLibWrapper::convertJPEGToBMP (pBuf, ui32Len);
        if (pBMPImage == NULL) {
            checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                            "failed to convert JPEG image to BMP\n");
            return NULL;
        }
    }
    else if (inputObjectType == Chunker::JPEG2000) {
        #if defined (ANDROID)
            checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                            "JPEG2000 not supported on the Android platform\n");
            return NULL;
        #else
            BufferReader *pReader = JasperWrapper::convertToBMP (pBuf, ui32Len);
            if (pReader == NULL) {
                checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                "failed to convert JPEG2000 image to BMP\n");
                return NULL;
            }
            int rc;
            pBMPImage = new BMPImage();
            if (0 != (rc = pBMPImage->readHeaderAndImage (pReader))) {
                checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                "failed to read BMP image after converting JPEG2000 to BMP; rc = %d\n", rc);
                delete pBMPImage;
                delete pReader;
                return NULL;
            }
            delete pReader;
        #endif
    }
    else if (inputObjectType == Chunker::PNG) {
        #if defined (ANDROID)
            checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                            "JPEG2000 not supported on the Android platform\n");
            return NULL;
        #else
            pBMPImage = PNGLibWrapper::convertPNGToBMP (pBuf, ui32Len);
            if (pBMPImage == NULL) {
                checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                "failed to convert PNG image to BMP\n");
                return NULL;
            }
        #endif
    }
    else {
        checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                        "unsupported input type %d\n", (int) inputObjectType);
        return NULL;
    }
    PtrLList<Fragment> *pFragments = new PtrLList<Fragment>;
    for (uint8 ui8CurrentChunk = 1; ui8CurrentChunk <= ui8NoOfChunks; ui8CurrentChunk++) {
        BMPImage *pBMPChunk = BMPChunker::fragmentBMP (pBMPImage, ui8CurrentChunk, ui8NoOfChunks);
        if (pBMPChunk == NULL) {
            checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                            "failed to fragment BMP to create chunk %d of %d\n", (int) ui8CurrentChunk, (int) ui8NoOfChunks);
            delete pFragments;
            return NULL;
        }
        int rc;
        Fragment *pFragment = new Fragment();
        pFragment->src_type = inputObjectType;
        if (outputChunkType == Chunker::BMP) {
            BufferWriter bw (pBMPChunk->getTotalSize(), 1024);
            if (0 != (rc = pBMPChunk->writeHeaderAndImage (&bw))) {
                checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                "failed to write chunk BMP into buffer; rc = %d\n", rc);
                delete pFragments;
                return NULL;
            }
            uint32 ui32ChunkLen = bw.getBufferLength();
            pFragment->pReader = new BufferReader (bw.relinquishBuffer(), ui32ChunkLen, true);
            pFragment->ui64FragLen = ui32ChunkLen;
        }
        else if (outputChunkType == Chunker::JPEG) {
            BufferReader *pReader = JPEGLibWrapper::convertBMPToJPEG (pBMPChunk, ui8ChunkCompressionQuality);
            if (pReader == NULL) {
                checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                "failed to encode BMP chunk into JPEG\n");
                delete pFragments;
                return NULL;
            }
            pFragment->pReader = pReader;
            pFragment->ui64FragLen = pReader->getBytesAvailable();
        }
        else if (outputChunkType == Chunker::JPEG2000) {
            #if defined (ANDROID)
                checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                "JPEG2000 not supported on the Android platform\n");
                delete pFragments;
                return NULL;
            #else
                BufferWriter bw (pBMPChunk->getTotalSize(), 1024);
                if (0 != (rc = pBMPChunk->writeHeaderAndImage (&bw))) {
                    checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                    "failed to write chunk BMP into buffer for creating JPEG2000; rc = %d\n", rc);
                    delete pFragments;
                    return NULL;
                }
                BufferReader *pReader = JasperWrapper::convertToJPEG2000 (bw.getBuffer(), bw.getBufferLength());
                if (pReader == NULL) {
                    checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                                    "failed to encode BMP chunk into JPEG2000\n");
                    delete pFragments;
                    return NULL;
                }
                pFragment->pReader = pReader;
                pFragment->ui64FragLen = pReader->getBytesAvailable();
            #endif
        }
        else {
            checkAndLogMsg ("Chunker::fragmentBuffer", Logger::L_MildError,
                            "unsupported output type %d\n", (int) outputChunkType);
            return NULL;
        }
        pFragment->out_type = outputChunkType;
        pFragment->ui8Part = ui8CurrentChunk;
        pFragment->ui8TotParts = ui8NoOfChunks;
        pFragments->append (pFragment);
    }
    return pFragments;
}
PtrLList<Message> * NetworkTrafficMemory::filterRecentlySent (Message *pMsg, int64 i64RequestArrivalTime)
{
    _m.lock (186);
    MessageHeader *pMH = pMsg->getMessageHeader();
    if (pMH == NULL) {
        checkAndLogMsg ("NetworkTrafficMemory::filterRecentlySent", Logger::L_Warning,
                        "Message object does not contain message header.\n");
    }

    PtrLList<Message> *pRet = new PtrLList<Message>();
    if (pRet == NULL) {
        checkAndLogMsg ("NetworkTrafficMemory::filterRecentlySent", memoryExhausted);
        _m.unlock (186);
        return NULL;
    }

    if (_ui16IgnoreRequestTime == 0) {
        pRet->insert (new Message (pMH->clone(), pMsg->getData()));
         _m.unlock (186);
        return pRet;
    }

    MessagesByGroup *pBG = _messageByGroup.get (pMH->getGroupName());
    if (pBG == NULL) {
        pRet->insert (new Message (pMH->clone(), pMsg->getData()));
         _m.unlock (186);
        return pRet;
    }

    MessagesBySender *pMS = pBG->messageBySender.get (pMH->getPublisherNodeId());
    if (pMS == NULL) {
        pRet->insert (new Message (pMH->clone(), pMsg->getData()));
         _m.unlock (186);
        return pRet;
    }

    FragmentedMessageHeader *pFMH = pMS->messageBySeqId.get (pMH->getMsgSeqId());
    if (pFMH == NULL) {
        pRet->insert (new Message (pMH->clone(), pMsg->getData()));
         _m.unlock (186);
        return pRet;
    }

    if (pMH->isChunk()) {
        FragmentedChunk *pFC = ((FragmentedMessage *) pFMH)->chunksByChunkId.get (pMsg->getChunkMsgInfo()->getChunkId());
        if (pFC == NULL) {
            pRet->insert (new Message (pMH->clone(), pMsg->getData()));
            _m.unlock (186);
            return pRet;
        }
        else {
            pFMH = pFC;
        }
    }

    char *psFragToSendData;
    psFragToSendData = (char *) pMsg->getData();
    uint32 ui32FragToSendStart = pMH->getFragmentOffset();
    uint32 ui32FragToSendEnd = ui32FragToSendStart + pMH->getFragmentLength();

    MessageHeader *pTmpMI;

    for (FragmentWrapper *pFW = pFMH->fragments.getFirst(); (pFW != NULL) && (ui32FragToSendEnd > pFW->ui32Offset); pFW = pFMH->fragments.getNext()) {
        if (pFW->overlaps (ui32FragToSendStart, ui32FragToSendEnd)) {
            if ((i64RequestArrivalTime - pFW->i64LastServingTime) < _ui16IgnoreRequestTime) {
                // I need to split
                // write everything before pFW->ui32Offset
                uint32 ui32TmpFragLength = (ui32FragToSendStart < pFW->ui32Offset ? pFW->ui32Offset - ui32FragToSendStart : (uint32) 0);
                if (ui32TmpFragLength > 0) {
                    pTmpMI = pMH->clone();
                    pTmpMI->setFragmentOffset (ui32FragToSendStart);
                    pTmpMI->setFragmentLength (ui32TmpFragLength);
                    pRet->insert (new Message (pTmpMI, psFragToSendData));
                }
                // ui32FragToSendStart = pFW->ui32End;
                // psFragToSendData = psFragToSendData + ui32TmpFragLength;
                ui32TmpFragLength = minimum (pFW->ui32End, ui32FragToSendEnd) - ui32FragToSendStart;
                ui32FragToSendStart += ui32TmpFragLength;
                psFragToSendData += ui32TmpFragLength;
            }
        }
    }
    // Add the remaining part
    if (ui32FragToSendStart < ui32FragToSendEnd) {
        pTmpMI = pMH->clone();
        pTmpMI->setFragmentOffset (ui32FragToSendStart);
        pTmpMI->setFragmentLength (ui32FragToSendEnd - ui32FragToSendStart);
        pRet->insert (new Message (pTmpMI, psFragToSendData));
    }
    _m.unlock (186);
    return pRet;
}
/*
   send ReplicationStartReqMsg
   if send error delete self
   wait for ReplicationStartReplyMsg as follows
      loop until reply is received
          on receive error or peerNode dies, delete self
   get filtered list of messages to replicate
   for all messages to send, loop
       if clear to send
          send next message
          if send error, delete self
       else wait for clear to send (watch out for deadPeer?)
    all done delete self

    note: deleting self signals thread's parent to terminate this thread
          go through the list of successfully send and ack'd?
          and possibly queue up another session?
              
*/
void TargetBasedReplicationController::ReplicationSessionThread::run (void)
{
    int rc;
    const char *pszMethod = "TargetBasedReplicationController::ReplicationSessionThread::run";

    // First - send the ReplicationStartReqMsg
    ReplicationStartReqMsg rsrm (_localNodeID, _targetNodeID, TargetBasedReplicationController::CONTROLLER_TYPE, TargetBasedReplicationController::CONTROLLER_VERSION, _bCheckTargetForMsgs, _bRequireAcks);

    if (_pTBRepCtlr->transmitCtrlToCtrlMessage (&rsrm, "TargetBasedReplicationController: sending ReplicationStartReqMsg") != 0) {
        checkAndLogMsg (pszMethod, Logger::L_MildError,
                        "transmission of ReplicationStartReqMsg failed - terminating replication session\n");
        _pTBRepCtlr->addTerminatingReplicator (this);
        setTerminatingResultCode (-1);
        terminating();
        return;
    }
    else {
        checkAndLogMsg (pszMethod, Logger::L_Info,
                        "transmitted ReplicationStartReqMsg to target node %s\n",
                        (const char*) _targetNodeID);
    }

    // Wait for the ReplicationStartReplyMsg
    _m.lock();
    while (!_bReplicating) {
        if (terminationRequested()) {
            _pTBRepCtlr->addTerminatingReplicator (this);
            setTerminatingResultCode (-2);
            terminating();
            _m.unlock();
            return;
        }
        else if (_bTargetDead) {
            checkAndLogMsg (pszMethod, Logger::L_Warning,
                            "target node %s died while waiting for the ReplicationStartReplyMsg\n",
                            (const char*) _targetNodeID);
            _pTBRepCtlr->addTerminatingReplicator (this);
            setTerminatingResultCode (-3);
            terminating();
            _m.unlock();
            return;
        }
        _cv.wait (1000);
    }
    _m.unlock();

    // Ready to replicate messages at this point
    if ((_pMsgsToExclude != NULL) && (_pMsgsToExclude->getCount() > 0)) {
        checkAndLogMsg (pszMethod, Logger::L_Info,
                        "adding constraint to exclude messages previously received by node %s\n", (const char*) _targetNodeID);
    }

    int64 i64QueryStartTime = getTimeInMilliseconds();
    PtrLList<MessageId> *pMsgIds = _pTBRepCtlr->_pDataCacheInterface->getNotReplicatedMsgList (_targetNodeID, 0, _pMsgsToExclude);
    if (pMsgIds != NULL) {
        checkAndLogMsg (pszMethod, Logger::L_Info,
                        "identified %lu messages to replicate to node %s - query time %lu ms\n",
                        (uint32) pMsgIds->getCount(), (const char*) _targetNodeID, (uint32) (getTimeInMilliseconds() - i64QueryStartTime));
        for (MessageId *pMIdTemp = pMsgIds->getFirst(); pMIdTemp; pMIdTemp = pMsgIds->getNext()) {
            // Check for termination
            if (terminationRequested()) {
                setTerminatingResultCode (-4);
                break;
            }

            // Check Flow Control and target peer death
            int64 i64PauseStartTime = 0;
            while ((!_bTargetDead) && (!_pTBRepCtlr->clearToSendOnAllInterfaces())) {
                if (i64PauseStartTime == 0) {
                    i64PauseStartTime = getTimeInMilliseconds();
                }
                sleepForMilliseconds (100);
            }
            if (_bTargetDead) {
                checkAndLogMsg (pszMethod, Logger::L_Warning,
                                "target %s died while replicating messages\n",
                                (const char*) _targetNodeID);
                setTerminatingResultCode (-5);
                break;
            }
            if (i64PauseStartTime != 0) {
                checkAndLogMsg (pszMethod, Logger::L_Info,
                                "paused replication for %lu ms because it was not clear to send\n",
                                (uint32) (getTimeInMilliseconds() - i64PauseStartTime));
            }

            // Replicate one message
            if (0 != (rc = _pTBRepCtlr->replicateMessage (pMIdTemp->getId(), _targetNodeID, 2000))) {
                checkAndLogMsg (pszMethod, Logger::L_Warning,
                                "replicateMessage() failed for message <%s> to target node <%s>; rc = %d\n",
                                pMIdTemp->getId(), (const char*) _targetNodeID, rc);
            }
            else if (!_bRequireAcks) {
                // Assume that the message has been successfully replicated
                _pTBRepCtlr->_pTransmissionHistory->addMessageTarget (pMIdTemp->getId(), _localNodeID);
                checkAndLogMsg (pszMethod, Logger::L_LowDetailDebug,
                                "replicated message %s to destination node %s\n",
                                pMIdTemp->getId(), (const char *) _localNodeID);
            }
        }
    }
    delete pMsgIds;
    _pTBRepCtlr->releaseQueryResults();

    // Send the ReplicationEnd message
    if (_bTargetDead) {
            checkAndLogMsg (pszMethod, Logger::L_MildError,
                            "not sending replication end message because target node %s is dead\n",
                            (const char *) _targetNodeID);
    }
    else {
        ReplicationEndMsg rem (_localNodeID, _targetNodeID, TargetBasedReplicationController::CONTROLLER_TYPE, TargetBasedReplicationController::CONTROLLER_VERSION);
        if (0 != (rc = _pTBRepCtlr->transmitCtrlToCtrlMessage (&rem, "replication end message"))) {
            checkAndLogMsg (pszMethod, Logger::L_MildError,
                            "failed to send replication end message; rc = %d\n", rc);
        }
        else {
            checkAndLogMsg (pszMethod, Logger::L_Info,
                            "completed replication session with target node %s\n",
                            (const char *) _targetNodeID);
        }
    }

    _pTBRepCtlr->addTerminatingReplicator (this);
    terminating();
}