00001 #include "EventBase.h"
00002 #include "EventRouter.h"
00003 #include <stdio.h>
00004 #include <sstream>
00005 #include <libxml/tree.h>
00006 #include "Shared/debuget.h"
00007 #include "Events/RemoteRouter.h"
00008
00009
00010 const EventBase::classTypeID_t EventBase::autoRegisterEventBase=getTypeRegistry().registerType<EventBase>(makeClassTypeID("BASE"));
00011 #include "FilterBankEvent.h"
00012 const EventBase::classTypeID_t FilterBankEvent::autoRegisterFilterBankEvent=getTypeRegistry().registerType<FilterBankEvent>(makeClassTypeID("FBKE"));
00013 #include "SegmentedColorFilterBankEvent.h"
00014 const EventBase::classTypeID_t SegmentedColorFilterBankEvent::autoRegisterSegmentedColorFilterBankEvent=getTypeRegistry().registerType<SegmentedColorFilterBankEvent>(makeClassTypeID("SFBK"));
00015
00016 const char* const EventBase::EventGeneratorNames[numEGIDs] = {
00017 "unknownEGID",
00018 "aiEGID",
00019 "audioEGID",
00020 "buttonEGID",
00021 "cameraResolutionEGID",
00022 "erouterEGID",
00023 "estopEGID",
00024 "locomotionEGID",
00025 "lookoutEGID",
00026 "mapbuilderEGID",
00027 "micOSndEGID",
00028 "micRawEGID",
00029 "micFFTEGID",
00030 "micPitchEGID",
00031 "motmanEGID",
00032 "pilotEGID",
00033 "powerEGID",
00034 "sensorEGID",
00035 "stateMachineEGID",
00036 "stateSignalEGID",
00037 "stateTransitionEGID",
00038 "textmsgEGID",
00039 "timerEGID",
00040 "visOFbkEGID",
00041 "visRawCameraEGID",
00042 "visInterleaveEGID",
00043 "visJPEGEGID",
00044 "visPNGEGID",
00045 "visSegmentEGID",
00046 "visRLEEGID",
00047 "visRegionEGID",
00048 "visObjEGID",
00049 "wmVarEGID",
00050 "worldModelEGID",
00051 "remoteStateEGID"
00052 };
00053
00054 const char* const EventBase::EventTypeNames[numETIDs] = {
00055 "activate",
00056 "status",
00057 "deactivate"
00058 };
00059
00060 const char* const EventBase::EventTypeAbbr[numETIDs] = { "A", "S", "D" };
00061
00062 EventBase::registry_t& EventBase::getTypeRegistry() {
00063 static registry_t typeRegistry;
00064 return typeRegistry;
00065 }
00066
00067 EventBase::EventBase()
00068 : XMLLoadSave(), stim_id(), magnitude(0), timestamp(get_time()), saveFormat(XML),
00069 nameisgen(true), genID(unknownEGID), typeID(statusETID), sourceID((size_t)-1),
00070 hostID(-1), duration(0)
00071 {
00072 genName();
00073 }
00074
00075 EventBase::EventBase(EventGeneratorID_t gid, size_t sid, EventTypeID_t tid, unsigned int dur)
00076 : XMLLoadSave(), stim_id(), magnitude(0), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(gid), typeID(tid), sourceID(sid), hostID(-1), duration(dur)
00077 {
00078 genName();
00079 if(tid==deactivateETID)
00080 setMagnitude(0.0);
00081 else
00082 setMagnitude(1.0);
00083 }
00084
00085 EventBase::EventBase(EventGeneratorID_t gid, size_t sid, EventTypeID_t tid, unsigned int dur, const std::string& n)
00086 : XMLLoadSave(), stim_id(), magnitude(0), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(gid), typeID(tid), sourceID(sid), hostID(-1), duration(dur)
00087 {
00088 setName(n);
00089 if(tid==deactivateETID)
00090 setMagnitude(0.0);
00091 else
00092 setMagnitude(1.0);
00093 }
00094
00095 EventBase::EventBase(EventGeneratorID_t gid, size_t sid, EventTypeID_t tid, unsigned int dur, const std::string& n, float mag)
00096 : XMLLoadSave(), stim_id(), magnitude(mag), timestamp(get_time()), saveFormat(XML), nameisgen(true), genID(gid), typeID(tid), sourceID(sid), hostID(-1), duration(dur)
00097 {
00098 setName(n);
00099 }
00100
00101 EventBase&
00102 EventBase::setName(const std::string& sourcename) {
00103 stim_id='(';
00104 if(genID<numEGIDs) {
00105 stim_id+=EventGeneratorNames[genID];
00106 } else {
00107 stim_id+=std::string("InvalidGen");
00108 char tmp2[16];
00109 snprintf(tmp2,16,"(%d)",genID);
00110 stim_id+=tmp2;
00111 }
00112 stim_id+=',';
00113 stim_id+=sourcename;
00114 stim_id+=',';
00115 stim_id+=EventTypeAbbr[getTypeID()];
00116
00117 if (hostID != -1) {
00118 stim_id += ',';
00119 stim_id += EventRouter::intToStringIP(hostID);
00120 }
00121
00122 stim_id+=')';
00123 nameisgen=false;
00124 return *this;
00125 }
00126
00127 std::string
00128 EventBase::getDescription(bool , unsigned int verbosity) const {
00129 std::ostringstream logdata;
00130 logdata << getName();
00131 if(verbosity>=1)
00132 logdata << '\t' << getGeneratorID() << '\t' << getSourceID() << '\t' << getTypeID();
00133 if(verbosity>=2)
00134 logdata << '\t' << getDuration() << '\t' << getTimeStamp();
00135 if(verbosity>=3)
00136 logdata << '\t' << getMagnitude();
00137 return logdata.str();
00138 }
00139
00140 unsigned int
00141 EventBase::getBinSize() const {
00142 if(saveFormat==XML)
00143 return XMLLoadSave::getBinSize();
00144 unsigned int used=0;
00145 used+=creatorSize("EventBase");
00146 used+=getSerializedSize(stim_id);
00147 used+=getSerializedSize(magnitude);
00148 used+=getSerializedSize(timestamp);
00149 used+=getSerializedSize(nameisgen);
00150 used+=getSerializedSize<char>();
00151 used+=getSerializedSize<char>();
00152 used+=getSerializedSize(sourceID);
00153 used+=getSerializedSize(hostID);
00154 used+=getSerializedSize(duration);
00155 return used;
00156 }
00157
00158 unsigned int
00159 EventBase::loadBinaryBuffer(const char buf[], unsigned int len) {
00160 unsigned int origlen=len;
00161 if(!checkCreatorInc("EventBase",buf,len,true)) return 0;
00162 if(!decodeInc(stim_id,buf,len)) return 0;
00163 if(!decodeInc(magnitude,buf,len)) return 0;
00164 if(!decodeInc(timestamp,buf,len)) return 0;
00165 if(!decodeInc(nameisgen,buf,len)) return 0;
00166 char tmp;
00167 if(!decodeInc(tmp,buf,len)) return 0;
00168 genID=(EventGeneratorID_t)tmp;
00169 if(!decodeInc(tmp,buf,len)) return 0;
00170 typeID=(EventTypeID_t)tmp;
00171 if(!decodeInc(sourceID,buf,len)) return 0;
00172 if(!decodeInc(hostID,buf,len)) return 0;
00173 if(!decodeInc(duration,buf,len)) return 0;
00174
00175 genName();
00176 return origlen-len;
00177 }
00178
00179 unsigned int
00180 EventBase::saveBinaryBuffer(char buf[], unsigned int len) const {
00181 unsigned int origlen=len;
00182 if(!saveCreatorInc("EventBase",buf,len)) return 0;
00183 if(!encodeInc(stim_id,buf,len)) return 0;
00184 if(!encodeInc(magnitude,buf,len)) return 0;
00185 if(!encodeInc(timestamp,buf,len)) return 0;
00186 if(!encodeInc(nameisgen,buf,len)) return 0;
00187 if(!encodeInc((char)genID,buf,len)) return 0;
00188 if(!encodeInc((char)typeID,buf,len)) return 0;
00189 if(!encodeInc(sourceID,buf,len)) return 0;
00190 if(!encodeInc(hostID,buf,len)) return 0;
00191 if(!encodeInc(duration,buf,len)) return 0;
00192 return origlen-len;
00193 }
00194
00195 void EventBase::loadXML(xmlNode* node) {
00196 if(xmlStrcmp(node->name, (const xmlChar *)"event"))
00197 throw bad_format(node,"Load of the wrong type -- expecting 'event' node");
00198 unsigned int i;
00199 xmlChar* str = xmlGetProp(node,(const xmlChar*)"egid");
00200 if(str==NULL)
00201 throw bad_format(node,"missing generator id");
00202 for(i=0; i<numEGIDs; i++) {
00203 if(xmlStrcmp(str,(const xmlChar*)EventGeneratorNames[i])==0)
00204 break;
00205 }
00206 xmlFree(str);
00207 if(i==numEGIDs)
00208 throw bad_format(node,"bad event generator name");
00209 genID=static_cast<EventGeneratorID_t>(i);
00210
00211 str = xmlGetProp(node,(const xmlChar*)"sid");
00212 if(str==NULL)
00213 throw bad_format(node,"missing source id");
00214 std::stringstream((const char*)str) >> sourceID;
00215 xmlFree(str);
00216
00217 str = xmlGetProp(node,(const xmlChar*)"hid");
00218 if(str==NULL)
00219 throw bad_format(node,"missing host id");
00220 hostID=atoi((const char*)str);
00221 xmlFree(str);
00222
00223 str = xmlGetProp(node,(const xmlChar*)"etid");
00224 if(str==NULL)
00225 throw bad_format(node,"missing type id");
00226 for(i=0; i<numETIDs; i++) {
00227 if(xmlStrcmp(str,(const xmlChar*)EventTypeAbbr[i])==0)
00228 break;
00229 if(xmlStrcmp(str,(const xmlChar*)EventTypeNames[i])==0)
00230 break;
00231 }
00232 xmlFree(str);
00233 if(i==numETIDs)
00234 throw bad_format(node,"bad event type name");
00235 typeID=static_cast<EventTypeID_t>(i);
00236 str = xmlGetProp(node,(const xmlChar*)"time");
00237 if(str==NULL)
00238 throw bad_format(node,"missing timestamp");
00239 timestamp=atoi((const char*)str);
00240 xmlFree(str);
00241 str = xmlGetProp(node,(const xmlChar*)"duration");
00242 if(str==NULL)
00243 throw bad_format(node,"missing duration");
00244 duration=atoi((const char*)str);
00245 xmlFree(str);
00246 str = xmlGetProp(node,(const xmlChar*)"magnitude");
00247 if(str==NULL)
00248 throw bad_format(node,"missing magnitude");
00249 magnitude=atoi((const char*)str);
00250 xmlFree(str);
00251 str = xmlGetProp(node,(const xmlChar*)"custom_name");
00252 if(str!=NULL) {
00253 setName((const char*)str);
00254 xmlFree(str);
00255 } else
00256 genName();
00257 }
00258 void EventBase::saveXML(xmlNode * node) const {
00259 xmlNodeSetName(node,(const xmlChar*)"event");
00260 xmlSetProp(node,(const xmlChar*)"egid",(const xmlChar*)EventGeneratorNames[genID]);
00261 char buf[20];
00262
00263 snprintf(buf,20,"%lu",static_cast<unsigned long>(sourceID));
00264 xmlSetProp(node,(const xmlChar*)"sid",(const xmlChar*)buf);
00265
00266 snprintf(buf,20,"%lu",static_cast<long>(hostID));
00267 xmlSetProp(node,(const xmlChar*)"hid",(const xmlChar*)buf);
00268
00269 xmlSetProp(node,(const xmlChar*)"etid",(const xmlChar*)EventTypeAbbr[typeID]);
00270 snprintf(buf,20,"%u",timestamp);
00271 xmlSetProp(node,(const xmlChar*)"time",(const xmlChar*)buf);
00272 snprintf(buf,20,"%u",duration);
00273 xmlSetProp(node,(const xmlChar*)"duration",(const xmlChar*)buf);
00274 snprintf(buf,20,"%g",magnitude);
00275 xmlSetProp(node,(const xmlChar*)"magnitude",(const xmlChar*)buf);
00276 if(!nameisgen) {
00277
00278 size_t b=stim_id.find(',');
00279 ASSERTRET(b!=std::string::npos,"malformed event name! (no comma)");
00280 size_t e=stim_id.find(',',++b);
00281 ASSERTRET(e!=std::string::npos,"malformed event name! (no 2nd comma)");
00282 xmlSetProp(node,(const xmlChar*)"custom_name",(const xmlChar*)stim_id.substr(b,e-b).c_str());
00283 }
00284 }
00285
00286 void
00287 EventBase::genName() {
00288 if(!nameisgen)
00289 return;
00290 char tmp[16];
00291 snprintf(tmp,16,"%lu",static_cast<unsigned long>(sourceID));
00292 setName(tmp);
00293 nameisgen=true;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306