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