Homepage
Demos
Overview
Downloads
Tutorials
Reference
Credits

EventTranslator.cc

Go to the documentation of this file.
00001 #include "EventTranslator.h"
00002 #include "Events/LocomotionEvent.h"
00003 #include "Events/VisionObjectEvent.h"
00004 #include "Events/TextMsgEvent.h"
00005 #include "Events/EventRouter.h"
00006 #include "Shared/debuget.h"
00007 #include <iostream>
00008 
00009 bool
00010 EventTranslator::trapEvent(const EventBase& event) {
00011   enqueue(event,queue);
00012   return true;
00013 }
00014 
00015 void
00016 EventTranslator::enqueue(const EventBase& event, Queue_t * q) {
00017   unsigned int len=event.getBinSize();
00018   TypeID_t type;
00019   if(dynamic_cast<const LocomotionEvent*>(&event)!=NULL) {
00020     type=LocomotionEvent_ID;
00021   } else if(dynamic_cast<const VisionObjectEvent*>(&event)!=NULL) {
00022     type=VisionObjectEvent_ID;
00023   } else if(dynamic_cast<const TextMsgEvent*>(&event)!=NULL) {
00024     type=TextMsgEvent_ID;
00025   } else {
00026     ASSERT(dynamic_cast<const EventBase*>(&event)!=NULL,"stupid OS/compiler/linker/whatever");
00027     type=EventBase_ID;
00028   }
00029   void* buf=q->reserve(sizeof(TypeID_t)+len);
00030   if(buf==NULL) {
00031     ASSERT(false,"Queue overflow "<<type<<' '<<len);
00032   } else {
00033     *reinterpret_cast<TypeID_t*>(buf)=type;
00034     reinterpret_cast<char*>(buf)+=sizeof(TypeID_t);
00035     unsigned int err=event.SaveBuffer(reinterpret_cast<char*>(buf),len);
00036     ASSERT(err!=0,"bad save");
00037   }
00038   q->done();
00039 }
00040   
00041 void
00042 EventTranslator::translateEvents() {
00043   unsigned int i;
00044   for(i=0; i<queue->size(); i++)
00045     sendEvent(queue->data(i),queue->size(i));
00046   while(!queue->clear(i)) //needed so if another was added while we were processing last one we will get it
00047     sendEvent(queue->data(i++),queue->size(i));
00048 }
00049 
00050 void
00051 EventTranslator::sendEvent(const void * entry, unsigned int size) {
00052   TypeID_t type=*reinterpret_cast<const TypeID_t*>(entry);
00053   const char* buf=reinterpret_cast<const char*>(entry)+sizeof(TypeID_t);
00054   size-=sizeof(TypeID_t);
00055   EventBase * evt=NULL;
00056   switch(type) {
00057   case LocomotionEvent_ID: {
00058     evt=new LocomotionEvent;
00059   } break;
00060   case VisionObjectEvent_ID: {
00061     evt=new VisionObjectEvent;
00062   } break;
00063   case TextMsgEvent_ID: {
00064     evt=new TextMsgEvent;
00065   } break;
00066   case EventBase_ID: {
00067     evt=new EventBase;
00068   } break;
00069   //no default case because we want
00070   //a compiler warning if we miss one
00071   }
00072   ASSERTRET(evt!=NULL,"Unrecognized entry type");
00073   unsigned int err=evt->LoadBuffer(buf,size);
00074   if(err==0) {
00075     std::cout<<"ERROR: type="<<type<< " len="<<size<<std::endl;
00076     for(unsigned int i=0; i<size; i++) {
00077       printf("%hx",buf[i]);
00078       if(i%10==0)
00079         printf("\n");
00080       else if(i%4==0)
00081         printf(" ");
00082     }
00083     std::cout << "This is a rare bug we are trying to track down.\n"
00084       "Please email a brief message containing the above\n"
00085       "hex dump to info@tekkotsu.org.  Thank you!" << std::endl;
00086   }
00087   ASSERTRET(err!=0,"bad load");
00088   ASSERTRET(evt->getGeneratorID()<EventBase::numEGIDs && evt->getTypeID()<EventBase::numETIDs,
00089             "invalid EGID "<<evt->getGeneratorID()<<" or ETID "<<evt->getTypeID());
00090   erouter->postEvent(evt);
00091 }
00092 
00093 
00094 /*! @file
00095  * @brief Implements EventTranslator, which receives events from EventRouters in non-Main processes and adds them into a SharedQueue for Main to pick up
00096  * @author ejt (Creator)
00097  *
00098  * $Author: ejt $
00099  * $Name: tekkotsu-2_3 $
00100  * $Revision: 1.8 $
00101  * $State: Exp $
00102  * $Date: 2005/01/29 00:02:55 $
00103  */
00104 

Tekkotsu v2.3
Generated Sat Jan 29 02:25:21 2005 by Doxygen 1.4.0