EventProxy.cc
Go to the documentation of this file.00001 #include "Events/EventProxy.h"
00002 #include "Shared/WorldState.h"
00003 #include "Events/RemoteRouter.h"
00004 #include "Events/EventRouter.h"
00005
00006 #include "Shared/debuget.h"
00007
00008 using namespace std;
00009
00010 EventProxy::EventProxy(int port) : RemoteEvents(), listening(true) {
00011 sck = wireless->socket(Socket::SOCK_STREAM, 4096, 4096);
00012 wireless->setReceiver(sck, this);
00013 wireless->setDaemon(sck, true);
00014 wireless->listen(sck, port);
00015 cout << "Adding timer" << endl;
00016 }
00017
00018 EventProxy::~EventProxy() {
00019 if (isConnected())
00020 wireless->close(sck);
00021 }
00022
00023 bool EventProxy::isActive() {
00024 return listening || isConnected();
00025 }
00026
00027 int EventProxy::processData(char *data, int bytes) {
00028 listening = false;
00029 while (bytes) {
00030 if (bufType == Invalid) {
00031
00032 if (!readType(data, bytes)) {
00033 cout << "Error reading buffer type" << endl;
00034 }
00035 } else if (!sizeLeft) {
00036
00037 if (!readSize(data, bytes)) {
00038 cout << "Error reading buffer size" << endl;
00039 }
00040 } else {
00041
00042 if (readData(data, bytes)) {
00043
00044 switch(bufType) {
00045 case RequestData:
00046
00047 handleRemoteRequest((RemoteRequest *)&vecbuf[0]);
00048 break;
00049 case Invalid:
00050 cout << "Error: invalid data. This should never happen." << endl;
00051 return -1;
00052 default:
00053 cout << "Error: data came in that wasn't expected" << endl;
00054 return -1;
00055 }
00056 bufType = Invalid;
00057 }
00058 }
00059 }
00060
00061 return 0;
00062 }
00063
00064
00065 void EventProxy::processEvent(const EventBase &event) {
00066
00067 if (event.getGeneratorID() != EventBase::timerEGID) {
00068 if (!isConnected()) {
00069 cout << "Got an event but not connected!" << endl;
00070 return;
00071 }
00072
00073
00074 if (event.getHostID() != -1)
00075 return;
00076
00077
00078
00079 int esize = 0;
00080 byte *ebuf = new byte[defaultBufferSize];
00081
00082 if ( (esize = event.saveBinaryBuffer((char *)ebuf, defaultBufferSize)) ) {
00083 NetworkBuffer nBuf;
00084 nBuf.addItem(EventData);
00085 nBuf.addBuffer(ebuf, esize);
00086
00087 if (!nBuf.send(sck)) {
00088 cout << "Error sending event to remote robot." << endl;
00089 return;
00090 }
00091 } else {
00092 cout << "Unable to save event to a buffer, aborting transmission." << endl;
00093 }
00094
00095 delete[] ebuf;
00096 } else {
00097
00098 sendState((RemoteState::StateType)event.getSourceID());
00099 }
00100 }
00101
00102 void EventProxy::handleRemoteRequest(RemoteRequest *info) {
00103
00104 switch (info->type) {
00105 case EventListenerRequest:
00106 cout << "Adding remote event listener request: " << info->numElements
00107 << " elements for host " << remoteIPString() << endl;
00108
00109 switch (info->numElements) {
00110 case 1:
00111 erouter->addListener(this, info->egid);
00112 break;
00113
00114 case 2:
00115 erouter->addListener(this, info->egid, info->sid);
00116 break;
00117
00118 case 3:
00119 erouter->addListener(this, info->egid, info->sid, info->etid);
00120 break;
00121
00122 default:
00123 cout << "Invalid number of elements (" << info->numElements << ") in event listener request." << endl;
00124 cout << *(float*)(NULL);
00125 break;
00126 }
00127 break;
00128
00129
00130 case RemoveEventListenerRequest:
00131 cout << "Removing remote event listener: " << info->numElements
00132 << " for host " << remoteIPString() << endl;
00133
00134 switch (info->numElements) {
00135 case 1:
00136 erouter->removeListener(this, info->egid);
00137 break;
00138
00139 case 2:
00140 erouter->removeListener(this, info->egid, info->sid);
00141 break;
00142
00143 case 3:
00144 erouter->removeListener(this, info->egid, info->sid, info->etid);
00145 break;
00146
00147 default:
00148 cout << "Invalid number of elements (" << info->numElements << ") in event listener request." << endl;
00149 break;
00150 }
00151 break;
00152
00153
00154 case StateUpdateRequest:
00155 cout << "Adding remote state update request for host "
00156 << remoteIPString() << endl;
00157
00158 erouter->addTimer(this, info->sType, info->interval, true);
00159 break;
00160
00161 case StopStateUpdateRequest:
00162 cout << "Removing remote state update request" << endl;
00163
00164 erouter->removeTimer(this, info->sType);
00165 break;
00166
00167 }
00168 }
00169
00170
00171 void EventProxy::sendState(RemoteState::StateType stype) {
00172 if (!isConnected()) {
00173 cout << "Got a request to send state data but not connected!" << endl;
00174 return;
00175 }
00176
00177 float *src = NULL;
00178 int size = RemoteState::sizes[stype];
00179
00180
00181 switch (stype) {
00182 case RemoteState::OutputState:
00183 src = state->outputs;
00184 break;
00185
00186 case RemoteState::ButtonState:
00187 src = state->buttons;
00188 break;
00189
00190 case RemoteState::SensorState:
00191 src = state->sensors;
00192 break;
00193
00194 default:
00195 cout << "Unrecognized state type, aborting" << endl;
00196 return;
00197 }
00198
00199 NetworkBuffer nBuf;
00200 nBuf.addItem(StateData);
00201 nBuf.addItem(size + 2*sizeof(int));
00202 nBuf.addItem(stype);
00203 nBuf.addBuffer((byte *)src, size);
00204
00205 if (!nBuf.send(sck)) {
00206 cout << "Error sending state buffer" << endl;
00207 }
00208 }