Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

EventBase.cc

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

Tekkotsu v5.1CVS
Generated Fri Mar 16 05:26:37 2012 by Doxygen 1.6.3