bool CMOOSCommClient::Peek(MOOSMSG_LIST & MsgList, int nIDRequired,bool bClear) { MsgList.clear(); m_InLock.Lock(); MOOSMSG_LIST::iterator p,q; p=m_InBox.begin(); while(p!=m_InBox.end()) { if(!p->IsType(MOOS_NULL_MSG)) { //only give client non NULL Msgs if(p->m_nID==nIDRequired) { //this is the correct ID! MsgList.push_front(*p); q=p++; m_InBox.erase(q); continue; } } p++; } //conditionally (ex MIT suggestion 2006) remove all elements if(bClear) m_InBox.clear(); m_InLock.UnLock(); return !MsgList.empty(); }
//Suggested addition by MIT users 2006 - shorter version of OnProcessSummary bool CMOOSDB::OnVarSummaryRequested(CMOOSMsg &Msg, MOOSMSG_LIST &MsgTxList) { std::string TheVars; DBVAR_MAP::iterator p; for(p = m_VarMap.begin(); p != m_VarMap.end(); p++) { //look to a comma if(p!=m_VarMap.begin()) TheVars += ","; TheVars += p->first; } CMOOSMsg Reply; //so the client knows the query result correspondences Reply.m_nID = Msg.m_nID; Reply.m_cMsgType = MOOS_NOTIFY; Reply.m_cDataType = MOOS_STRING; Reply.m_dfTime = MOOSTime(); Reply.m_sSrc = m_sDBName; Reply.m_sKey = "VAR_SUMMARY"; Reply.m_sVal = TheVars; Reply.m_dfVal = -1; MsgTxList.push_front(Reply); return true; }
bool CMOOSDB::OnProcessSummaryRequested(CMOOSMsg &Msg, MOOSMSG_LIST &MsgTxList) { DBVAR_MAP::iterator p; STRING_LIST::iterator q; STRING_LIST Clients; m_pCommServer->GetClientNames(Clients); for(q=Clients.begin();q!=Clients.end();q++) { string sWho = *q; string sPublished= "PUBLISHED="; string sSubscribed = "SUBSCRIBED="; for(p=m_VarMap.begin();p!=m_VarMap.end();p++) { CMOOSDBVar & rVar = p->second; if(rVar.m_Writers.find(sWho)!=rVar.m_Writers.end()) { if(!sPublished.empty()) { sPublished+=","; } sPublished+=rVar.m_sName; } if(rVar.m_Subscribers.find(sWho)!=rVar.m_Subscribers.end()) { if(!sSubscribed.empty()) { sSubscribed+=","; } sSubscribed+=rVar.m_sName; } } CMOOSMsg MsgReply; MsgReply.m_nID = Msg.m_nID; MsgReply.m_cMsgType = MOOS_NOTIFY; MsgReply.m_cDataType = MOOS_STRING; MsgReply.m_dfTime = MOOSTime()-m_dfStartTime; //for display MsgReply.m_sSrc = m_sDBName; MsgReply.m_sKey = "PROC_SUMMARY"; MsgReply.m_sVal = sWho+":"+sSubscribed+","+sPublished; MsgReply.m_dfVal = -1; MsgTxList.push_front(MsgReply); } return true; }
bool CMOOSCommObject::SendMsg(XPCTcpSocket *pSocket,CMOOSMsg &Msg) { MOOSMSG_LIST MsgList; MsgList.push_front(Msg); CMOOSCommPkt Pkt; Pkt.Serialize(MsgList,true); return SendPkt(pSocket,Pkt); }
bool CMOOSDB::OnServerAllRequested(CMOOSMsg &Msg, MOOSMSG_LIST &MsgTxList) { DBVAR_MAP::iterator p; for(p=m_VarMap.begin();p!=m_VarMap.end();p++) { CMOOSDBVar & rVar = p->second; CMOOSMsg MsgVar; MsgVar.m_nID = Msg.m_nID; MsgVar.m_cMsgType = MOOS_NOTIFY; MsgVar.m_cDataType = rVar.m_cDataType; MsgVar.m_dfTime = rVar.m_dfWrittenTime-m_dfStartTime; //for display MsgVar.m_sSrc = rVar.m_sWhoChangedMe; MsgVar.m_sKey = rVar.m_sName; MsgVar.m_sVal = rVar.m_sVal; MsgVar.m_sSrcAux = rVar.m_sSrcAux; MsgVar.m_dfVal = rVar.m_dfVal; MsgVar.m_dfVal2 = rVar.m_dfWriteFreq; MsgVar.m_sOriginatingCommunity = rVar.m_sOriginatingCommunity; if(MsgVar.m_dfTime<0) { MsgVar.m_dfTime =-1; } MsgTxList.push_front(MsgVar); } return true; }
/** This function stuffs messages in/from a packet */ bool CMOOSCommPkt::Serialize(MOOSMSG_LIST &List, bool bToStream, bool bNoNULL, double * pdfPktTime) { if(bToStream) { m_nMsgLen=0; m_pNextData = m_pStream; m_nByteCount = 0; //we need to leave space at the start of the packet for total //length and number of include messages m_pNextData+=2*sizeof(int); m_nByteCount+= 2* sizeof(int); #define PKT_TMP_BUFFER_SIZE 40000 unsigned char TmpBuffer[PKT_TMP_BUFFER_SIZE]; unsigned char * pTmpBuffer = TmpBuffer; MOOSMSG_LIST::iterator p; int nCount = 0; for(p = List.begin();p!=List.end();p++,nCount++) { //MOOSTrace("Sending %s \n",p->m_sKey.c_str()); int nTmpSize = PKT_TMP_BUFFER_SIZE; int nCopied = (*p).Serialize(pTmpBuffer,nTmpSize); if(nCopied!=-1) { //now copy to our stream... CopyToStream(pTmpBuffer,nCopied); } else { return false; } } //finally write how many bytes we have written at the start //look for need to swap byte order if required m_pNextData = m_pStream; int nBC = IsLittleEndian() ? m_nByteCount : SwapByteOrder<int>(m_nByteCount); memcpy((void*)m_pNextData,(void*)(&nBC),sizeof(m_nByteCount)); m_pNextData+=sizeof(m_nByteCount); //and then how many messages are included //look for need to swap byte order if required int nMessages = List.size(); nMessages = IsLittleEndian() ? nMessages : SwapByteOrder<int>(nMessages); memcpy((void*)m_pNextData,(void*)(&nMessages),sizeof(nMessages)); m_pNextData+=sizeof(nMessages); } else { m_pNextData = m_pStream; m_nMsgLen = 0; m_nByteCount = 0; //first figure out the length of the message //look to swap byte order as required memcpy((void*)(&m_nMsgLen),(void*)m_pNextData,sizeof(m_nMsgLen)); m_nMsgLen = IsLittleEndian() ? m_nMsgLen : SwapByteOrder<int>(m_nMsgLen); m_pNextData+=sizeof(m_nMsgLen); m_nByteCount+=sizeof(m_nMsgLen); int nSpaceFree=m_nMsgLen - sizeof(m_nMsgLen); //no figure out how many messages are packed in this packet //look to swap byte order as required int nMessages = 0; memcpy((void*)(&nMessages),(void*)m_pNextData,sizeof(nMessages)); nMessages = IsLittleEndian() ? nMessages : SwapByteOrder<int>(nMessages); m_pNextData+=sizeof(nMessages); nSpaceFree-=sizeof(nMessages); m_nByteCount+=sizeof(nMessages); for(int i = 0; i<nMessages;i++) { CMOOSMsg Msg; int nUsed = Msg.Serialize(m_pNextData,nSpaceFree,false); if(nUsed!=-1) { //allows us to not store NULL messages bool bOmit = bNoNULL && (Msg.m_cMsgType==MOOS_NULL_MSG); if(Msg.m_cMsgType==MOOS_NULL_MSG && pdfPktTime!=NULL && i == 0 ) { *pdfPktTime = Msg.GetDouble(); } if(!bOmit) { List.push_front(Msg); } m_pNextData+=nUsed; m_nByteCount+=nUsed; nSpaceFree-=nUsed; } else { //bad news... break; } } } //here at the last moment we can fill in our totalm length for safe keeping m_nMsgLen = m_nByteCount; return true; }
bool CMOOSPlayBackV2::Iterate(MOOSMSG_LIST &Output) { if(IsEOF()) return false; double dfStopTime = m_dfLastMessageTime+m_dfTickTime; bool bDone = false; while(!bDone && !IsEOF() ) { CMOOSMsg NewMsg; double dfTNext = m_ALog.GetEntryTime(m_nCurrentLine); //are we in a mode in which we are slaved to a client //via its publishing of MOOS_CHOKE? m_dfClientLagTime = MOOSTime() - m_dfLastClientProcessedTime; if (m_dfLastClientProcessedTime != -1 && m_dfClientLagTime > MAX_CHOKE_TIME) { m_bWaitingForClientCatchup = true; bDone = true; dfStopTime = dfTNext; continue; } else { //normal sequential processing under our own steam m_bWaitingForClientCatchup = false; if(dfTNext<=dfStopTime) { if(MessageFromLine(m_ALog.GetLine(m_nCurrentLine),NewMsg)) { if(!IsFiltered(NewMsg.GetSource())) { Output.push_front(NewMsg); } } // arh moved this out of the loop above, because a failed // call to MessageFromLine would make uPlayback hang in // an infinite loop m_nCurrentLine++; } else { bDone = true; } } } //post a playback time if(!Output.empty()) { CMOOSMsg timeMsg( MOOS_NOTIFY, "PLAYBACK_DB_TIME", dfStopTime ); timeMsg.m_sSrc = "uPlayback"; Output.push_front( timeMsg ); } m_dfLastMessageTime = dfStopTime; return true; }