00001 #include "EventTranslator.h"
00002 #include "Events/LocomotionEvent.h"
00003 #include "Events/VisionEvent.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 VisionEvent*>(&event)!=NULL) {
00022 type=VisionEvent_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");
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))
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 VisionEvent_ID: {
00063 VisionEvent * vision=new VisionEvent;
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
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094