00001 
00010 #include <EDDSourceLib/inc/EDDDataSource.h>
00011 #include <EDDSourceLib/inc/EDDMsgLoop.h>
00012 #include <proto/EDDSourceMsg.h>
00013 #include <string.h>
00014 
00015 tcEDDDataSource::tcEDDDataSource(void)
00016 : mnSourceId(-1)
00017 , mnBytesSent(0)
00018 {
00019 }
00020 
00021 tcEDDDataSource::~tcEDDDataSource(void)
00022 {
00023 }
00024     
00025 void 
00026 tcEDDDataSource::RegisterWithMsgLoop(void)
00027 {
00028     mnSourceId = tcEDDMsgLoop::GetInstance()->AddDataSource(this);
00029 }
00030 
00031 void 
00032 tcEDDDataSource::UnRegisterWithMsgLoop(void)
00033 {
00034     if (0 <= mnSourceId)
00035     {
00036         tcEDDMsgLoop::GetInstance()->RemoveDataSource(mnSourceId);
00037     }
00038 }
00039 
00040 void
00041 tcEDDDataSource::SendSourceDescMsg(char** apFields, char** apItems, int NumItems)
00042 {
00043         char* buffer;
00044     tsEDDSourceDescMsg* lpMsg;
00045     int desclen = 0;
00046     int bufsize;
00047     for(int index = 0; index < NumItems; index++)
00048     {
00049         desclen += strlen(apItems[index]);
00050         desclen += strlen(apFields[index]);
00051         desclen += 2;
00052     }
00053         desclen++; 
00054     if (desclen < 3) desclen = 0;
00055     bufsize = sizeof(tsEDDSourceDescMsg)+desclen-3;
00056         buffer = new char[bufsize];
00057     lpMsg = (tsEDDSourceDescMsg*) buffer;
00058     lpMsg->maDescriptionData[0] = 0;
00059     for (int index = 0; index < NumItems; index++)
00060     {
00061         strcat(&lpMsg->maDescriptionData[0],apFields[index]);
00062         strcat(&lpMsg->maDescriptionData[0],"=");
00063         strcat(&lpMsg->maDescriptionData[0],apItems[index]);
00064         strcat(&lpMsg->maDescriptionData[0],";");
00065     }
00066         desclen = strlen(&lpMsg->maDescriptionData[0]);
00067     lpMsg->mnDelimiter = ';';
00068     BuildHeader(&lpMsg->msHeader, EDDMSGID_SOURCEDESCMSG, bufsize);
00069     tcEDDMsgLoop::GetInstance()->SendMsg(&lpMsg->msHeader,true);
00070     delete [] buffer;
00071 }
00072 
00073 bool 
00074 tcEDDDataSource::IsSubscribed(void)
00075 {
00076     return tcEDDMsgLoop::GetInstance()->IsSubscribed(mnSourceId);
00077 }
00078 
00079 void
00080 tcEDDDataSource::BuildHeader(tsEDDMsgHdr* apMsgHdr, uint16_t MsgID, uint16_t len)
00081 {
00082     apMsgHdr->mnSyncWord   = EDD_MSG_SYNC;
00083     apMsgHdr->mnMsgId      = MsgID;
00084     apMsgHdr->mnMsgLen     = len;
00085     apMsgHdr->mnVersion    = EDD_CURRENT_VER;
00086     apMsgHdr->mnDataSource = mnSourceId;
00087     apMsgHdr->mnAllSources = 0;    
00088     mnBytesSent += len;
00089 }