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");
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   switch(type) {
00056   case LocomotionEvent_ID: {
00057     LocomotionEvent * loco=new LocomotionEvent;
00058     unsigned int err=loco->LoadBuffer(buf,size);
00059     ASSERT(err!=0,"bad load");
00060     erouter->postEvent(loco);
00061   } break;
00062   case VisionObjectEvent_ID: {
00063     VisionObjectEvent * vision=new VisionObjectEvent;
00064     unsigned int err=vision->LoadBuffer(buf,size);
00065     ASSERT(err!=0,"bad load");
00066     erouter->postEvent(vision);
00067   } break;
00068   case TextMsgEvent_ID: {
00069     TextMsgEvent * text=new TextMsgEvent;
00070     unsigned int err=text->LoadBuffer(buf,size);
00071     ASSERT(err!=0,"bad load");
00072     erouter->postEvent(text);
00073   } break;
00074   case EventBase_ID: {
00075     EventBase * event=new EventBase;
00076     unsigned int err=event->LoadBuffer(buf,size);
00077     ASSERT(err!=0,"bad load");
00078     erouter->postEvent(event);
00079   } break;
00080   }
00081 }
00082 
00083 
00084 /*! @file
00085  * @brief Implements EventTranslator, which receives events from EventRouters in non-Main processes and adds them into a SharedQueue for Main to pick up
00086  * @author ejt (Creator)
00087  *
00088  * $Author: ejt $
00089  * $Name: tekkotsu-2_2 $
00090  * $Revision: 1.7 $
00091  * $State: Exp $
00092  * $Date: 2004/10/14 23:02:35 $
00093  */
00094 

Tekkotsu v2.2
Generated Tue Oct 19 14:19:14 2004 by Doxygen 1.3.9.1