SimpleGameClient Class Reference

#include <SimpleGameClient.h>

Inheritance diagram for SimpleGameClient:

BaseApp BaseRpc RpcListener

List of all members.


Detailed Description

SimpleGameClient class.

A simple test application, which simulates node movement based on different movement generators.

Definition at line 49 of file SimpleGameClient.h.


Public Member Functions

virtual ~SimpleGameClient ()
virtual void initializeApp (int stage)
 initializes derived class-attributes
virtual void handleTimerEvent (cMessage *msg)
 processes self-messages
virtual void handleLowerMessage (cMessage *msg)
 method to handle non-commonAPI messages from the overlay
virtual void handleReadyMessage (CompReadyMessage *msg)
 method to handle ready messages from the overlay

Protected Member Functions

void updateNeighbors (GameAPIListMessage *sgcMsg)
void updatePosition ()
void handleRealworldPacket (char *buf, uint32_t len)

Protected Attributes

GlobalCoordinator * coordinator
CollisionList CollisionRect
simtime_t movementDelay
double areaDimension
double movementSpeed
double movementRate
double AOIWidth
bool useScenery
bool overlayReady
Vector2D position
MovementGenerator * Generator
NeighborMap Neighbors
NeighborMap::iterator itNeighbors
std::string GeneratorType
timeval startTime
cMessage * move_timer
cMessage * packetNotification
RealtimeScheduler::PacketBuffer packetBuffer
RealtimeScheduler * scheduler
unsigned int mtu
SOCKET appFd
bool doRealworld
bool frozen

Constructor & Destructor Documentation

SimpleGameClient::~SimpleGameClient (  )  [virtual]

Definition at line 411 of file SimpleGameClient.cc.

00412 {
00413     // destroy self timer messages
00414     cancelAndDelete(move_timer);
00415     cancelAndDelete(packetNotification);
00416 }


Member Function Documentation

void SimpleGameClient::initializeApp ( int  stage  )  [virtual]

initializes derived class-attributes

Parameters:
stage the init stage

Reimplemented from BaseApp.

Definition at line 28 of file SimpleGameClient.cc.

00029 {
00030     // all initialization is done in the first stage
00031     if (stage != MIN_STAGE_APP) {
00032         return;
00033     }
00034 
00035     // fetch parameters
00036     areaDimension = par("areaDimension");
00037     useScenery = par("useScenery");
00038     movementRate = par("movementRate");
00039     movementSpeed = par("movementSpeed");
00040     movementDelay= 1.0 / movementRate;
00041     AOIWidth = 0.0;
00042     int groupSize = (int)par("groupSize");
00043     if(groupSize < 1) {
00044         groupSize = 1;
00045     }
00046     GeneratorType = par("movementGenerator").stdstringValue();
00047 
00048     WATCH_MAP(Neighbors);
00049     WATCH_LIST(CollisionRect);
00050     WATCH(position);
00051     WATCH(GeneratorType);
00052     WATCH(overlayReady);
00053 
00054     doRealworld = false;
00055     coordinator = check_and_cast<GlobalCoordinator*>(simulation.getModuleByPath("globalObserver.globalFunctions[0].coordinator"));
00056     scheduler = NULL;
00057     packetNotification = NULL;
00058 
00059     frozen = false;
00060 
00061     CollisionList* listPtr = NULL;
00062     if(useScenery == true) {
00063         listPtr = &CollisionRect;
00064     }
00065 
00066     double speed = SIMTIME_DBL(movementDelay) * movementSpeed;
00067     if(strcmp(GeneratorType.c_str(), "greatGathering") == 0) {
00068         if(debugOutput) {
00069             EV << "[SimpleGameClient::initializeApp() @ " << overlay->getThisNode().ip
00070                << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00071                << "    SIMPLECLIENT: Movement generator: greatGathering"
00072                << endl;
00073         }
00074         Generator = new greatGathering(areaDimension, speed, &Neighbors, coordinator, listPtr);
00075     }
00076     else if(strcmp(GeneratorType.c_str(), "groupRoaming") == 0) {
00077         if(debugOutput) {
00078             EV << "[SimpleGameClient::initializeApp() @ " << overlay->getThisNode().ip
00079                << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00080                << "    SIMPLECLIENT: Movement generator: groupRoaming"
00081                << endl;
00082         }
00083         Generator = new groupRoaming(areaDimension, speed, &Neighbors, coordinator, listPtr, groupSize);
00084     }
00085     else if(strcmp(GeneratorType.c_str(), "realWorldRoaming") == 0) {
00086         if(debugOutput) {
00087             EV << "[SimpleGameClient::initializeApp() @ " << overlay->getThisNode().ip
00088                << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00089                << "    SIMPLECLIENT: Movement generator: realWorldRoaming"
00090                << endl;
00091         }
00092         Generator = new realWorldRoaming(areaDimension, movementSpeed, &Neighbors, coordinator, listPtr);
00093         mtu = par("mtu");
00094         packetNotification = new cMessage("packetNotification");
00095         scheduler = check_and_cast<RealtimeScheduler*>(simulation.getScheduler());
00096         scheduler->setInterfaceModule(this, packetNotification, &packetBuffer, mtu, true);
00097         appFd = INVALID_SOCKET;
00098     }
00099     else {
00100         // debug output
00101         if(debugOutput) {
00102             if(strcmp(GeneratorType.c_str(), "randomRoaming") == 0) {
00103                 EV << "[SimpleGameClient::initializeApp() @ " << overlay->getThisNode().ip
00104                    << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00105                    << "    SIMPLECLIENT: Movement generator: randomRoaming"
00106                    << endl;
00107             }
00108             else {
00109                 EV << "[SimpleGameClient::initializeApp() @ " << overlay->getThisNode().ip
00110                    << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00111                    << "    SIMPLECLIENT: Movement generator: <unknown>. Defaulting to: randomRoaming"
00112                    << endl;
00113             }
00114         }
00115         Generator = new randomRoaming(areaDimension, speed, &Neighbors, coordinator, listPtr);
00116     }
00117 
00118     // self-messages
00119     move_timer = new cMessage("move_timer");
00120     overlayReady = false;
00121 }

void SimpleGameClient::handleTimerEvent ( cMessage *  msg  )  [virtual]

processes self-messages

method to handle self-messages should be overwritten in derived application if needed

Parameters:
msg self-message

Reimplemented from BaseApp.

Definition at line 123 of file SimpleGameClient.cc.

00124 {
00125     if(msg->isName("move_timer")) {
00126         //reset timer
00127         cancelEvent(move_timer);
00128         if(overlayReady) {
00129             scheduleAt(simTime() + movementDelay, msg);
00130         }
00131         // handle event
00132         updatePosition();
00133     }
00134     else if(msg->isName("packetNotification")) {
00135         while(packetBuffer.size() > 0) {
00136             // get packet from buffer and parse it
00137             RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin());
00138             packetBuffer.pop_front();
00139             switch (packet.func) {
00140                 case RealtimeScheduler::PacketBufferEntry::PACKET_DATA: {
00141                     if(packet.fd != appFd) {
00142                         error("SimpleClient::handleMessage(): Received packet from unknown socket!");
00143                     }
00144                     handleRealworldPacket(packet.data, packet.length);
00145                 } break;
00146                 case RealtimeScheduler::PacketBufferEntry::PACKET_FD_NEW: {
00147                     if(appFd != INVALID_SOCKET) {
00148                         error("SimpleClient::handleMessage(): Multiple connections not supported yet!");
00149                     }
00150                     if (packet.addr != NULL) {
00151                         delete packet.addr;
00152                         packet.addr = NULL;
00153                     }
00154                     appFd = packet.fd;
00155                 } break;
00156                 case RealtimeScheduler::PacketBufferEntry::PACKET_FD_CLOSE: {
00157                     if(packet.fd != appFd) {
00158                         error("SimpleClient::handleMessage(): Trying to close unknown connection!");
00159                     }
00160                     appFd = INVALID_SOCKET;
00161                 } break;
00162             }
00163             delete packet.data;
00164         }
00165     }
00166     else if(msg->isName("Snowball landing")) {
00167         SCSnowTimer* snowTimer = check_and_cast<SCSnowTimer*>(msg);
00168         if(position.distanceSqr(snowTimer->getPosition() / 32 ) < 2) {
00169             // freeze me!
00170             frozen = true;
00171             cMessage* unfreeze = new cMessage("unfreeze me");
00172             scheduleAt(simTime() + 5, unfreeze);
00173 
00174             timeval now;
00175             gettimeofday(&now, NULL);
00176             GameAPIFrozenMessage* freeze = new GameAPIFrozenMessage("I'm frozen");
00177             freeze->setCommand(GAMEEVENT_FROZEN);
00178             freeze->setSrc(overlay->getThisNode());
00179             freeze->setTimeSec(now.tv_sec + 5);
00180             freeze->setTimeUsec(now.tv_usec);
00181             sendMessageToLowerTier(freeze);
00182 
00183             if(doRealworld) {
00184                 SCFrozenPacket scfreeze;
00185                 scfreeze.packetType = SC_EV_FROZEN;
00186                 scfreeze.ip = overlay->getThisNode().ip.get4().getInt();
00187                 scfreeze.time_sec = now.tv_sec + 5;
00188                 scfreeze.time_usec = now.tv_usec;
00189                 int packetlen = sizeof(scfreeze);
00190                 scheduler->sendBytes((const char*)&packetlen, sizeof(int), 0, 0, true, appFd);
00191                 scheduler->sendBytes((const char*)&scfreeze, packetlen, 0, 0, true, appFd);
00192             }
00193         }
00194         delete msg;
00195     }
00196     else if(msg->isName("unfreeze me")) {
00197         frozen = false;
00198         delete msg;
00199     }
00200 }

void SimpleGameClient::handleLowerMessage ( cMessage *  msg  )  [virtual]

method to handle non-commonAPI messages from the overlay

Parameters:
msg message to handle

Reimplemented from BaseApp.

Definition at line 202 of file SimpleGameClient.cc.

00203 {
00204     if(dynamic_cast<GameAPIMessage*>(msg)) {
00205         GameAPIMessage* gameAPIMsg = check_and_cast<GameAPIMessage*>(msg);
00206         if(gameAPIMsg->getCommand() == MOVEMENT_REQUEST) {
00207             updatePosition();
00208         }
00209         else if(gameAPIMsg->getCommand() == NEIGHBOR_UPDATE) {
00210             GameAPIListMessage* gameAPIListMsg = check_and_cast<GameAPIListMessage*>(msg);
00211             updateNeighbors(gameAPIListMsg);
00212         }
00213         else if(gameAPIMsg->getCommand() == RESIZE_AOI) {
00214             GameAPIResizeAOIMessage* gameAPIResizeMsg = check_and_cast<GameAPIResizeAOIMessage*>(msg);
00215             AOIWidth = gameAPIResizeMsg->getAOIsize();
00216             if(doRealworld) {
00217                 SCAOIPacket packet;
00218                 packet.packetType = SC_RESIZE_AOI;
00219                 packet.AOI = AOIWidth;
00220                 int packetlen = sizeof(SCAOIPacket);
00221                 scheduler->sendBytes((const char*)&packetlen, sizeof(int), 0, 0, true, appFd);
00222                 scheduler->sendBytes((const char*)&packet, packetlen, 0, 0, true, appFd);
00223             }
00224         }
00225         else if(gameAPIMsg->getCommand() == GAMEEVENT_CHAT) {
00226             if(doRealworld) {
00227                 GameAPIChatMessage* chatMsg = check_and_cast<GameAPIChatMessage*>(msg);
00228                 const char* msg = chatMsg->getMsg();
00229                 int packetlen = sizeof(SCChatPacket) + strlen(msg) + 1;
00230                 SCChatPacket* pack = (SCChatPacket*)malloc(packetlen);
00231                 pack->packetType = SC_EV_CHAT;
00232                 pack->ip = chatMsg->getSrc().ip.get4().getInt();
00233                 strcpy(pack->msg, msg);
00234                 scheduler->sendBytes((const char*)&packetlen, sizeof(int), 0, 0, true, appFd);
00235                 scheduler->sendBytes((const char*)pack, packetlen, 0, 0, true, appFd);
00236                 free(pack);
00237             }
00238         }
00239         else if(gameAPIMsg->getCommand() == GAMEEVENT_SNOW) {
00240             GameAPISnowMessage* snowMsg = check_and_cast<GameAPISnowMessage*>(gameAPIMsg);
00241             if(doRealworld) {
00242                 SCSnowPacket packet;
00243                 packet.packetType = SC_EV_SNOWBALL;
00244                 packet.ip = snowMsg->getSrc().ip.get4().getInt();
00245                 packet.startX = snowMsg->getStart().x;
00246                 packet.startY = snowMsg->getStart().y;
00247                 packet.endX = snowMsg->getEnd().x;
00248                 packet.endY = snowMsg->getEnd().y;
00249                 packet.time_sec = snowMsg->getTimeSec();
00250                 packet.time_usec = snowMsg->getTimeUsec();
00251                 int packetlen = sizeof(SCSnowPacket);
00252                 scheduler->sendBytes((const char*)&packetlen, sizeof(int), 0, 0, true, appFd);
00253                 scheduler->sendBytes((const char*)&packet, packetlen, 0, 0, true, appFd);
00254             }
00255             SCSnowTimer* snowTimer = new SCSnowTimer("Snowball landing");
00256             snowTimer->setPosition(snowMsg->getEnd());
00257             timeval snowTime;
00258             snowTime.tv_sec = snowMsg->getTimeSec();
00259             snowTime.tv_usec = snowMsg->getTimeUsec();
00260             snowTime = timeval_substract(snowTime, startTime);
00261             simtime_t snow_t = snowTime.tv_sec + snowTime.tv_usec / 1000000.0;
00262             if(snow_t < simTime()) {
00263                 snow_t = simTime();
00264             }
00265             scheduleAt(snow_t, snowTimer);
00266         }
00267         else if(gameAPIMsg->getCommand() == GAMEEVENT_FROZEN) {
00268             if(doRealworld) {
00269                 GameAPIFrozenMessage* frozen = check_and_cast<GameAPIFrozenMessage*>(gameAPIMsg);
00270                 SCFrozenPacket scfreeze;
00271                 scfreeze.packetType = SC_EV_FROZEN;
00272                 scfreeze.ip = frozen->getSrc().ip.get4().getInt();
00273                 scfreeze.time_sec = frozen->getTimeSec();
00274                 scfreeze.time_usec = frozen->getTimeUsec();
00275                 int packetlen = sizeof(scfreeze);
00276                 scheduler->sendBytes((const char*)&packetlen, sizeof(int), 0, 0, true, appFd);
00277                 scheduler->sendBytes((const char*)&scfreeze, packetlen, 0, 0, true, appFd);
00278             }
00279         }
00280     }
00281     delete msg;
00282 }

void SimpleGameClient::handleReadyMessage ( CompReadyMessage *  msg  )  [virtual]

method to handle ready messages from the overlay

Parameters:
msg message to handle

Reimplemented from BaseApp.

Definition at line 284 of file SimpleGameClient.cc.

00285 {
00286     if(msg->getReady()) {
00287         overlayReady = true;
00288         if(!move_timer->isScheduled()) {
00289             scheduleAt(simTime() + movementDelay, move_timer);
00290         }
00291         gettimeofday(&startTime, NULL);
00292         startTime = timeval_substract(startTime, SIMTIME_DBL(simTime()));
00293     }
00294     else {
00295         overlayReady = false;
00296     }
00297 
00298     delete msg;
00299 }

void SimpleGameClient::updateNeighbors ( GameAPIListMessage *  sgcMsg  )  [protected]

Definition at line 348 of file SimpleGameClient.cc.

Referenced by handleLowerMessage().

00349 {
00350     unsigned int i;
00351 
00352     if(doRealworld) {
00353         int packetSize;
00354 
00355         SCRemovePacket removePacket;
00356         removePacket.packetType = SC_REMOVE_NEIGHBOR;
00357         packetSize = sizeof(removePacket);
00358 
00359         for(i=0; i<sgcMsg->getRemoveNeighborArraySize(); ++i) {
00360             removePacket.ip = sgcMsg->getRemoveNeighbor(i).ip.get4().getInt();
00361             scheduler->sendBytes((const char*)&packetSize, sizeof(int), 0, 0, true, appFd);
00362             scheduler->sendBytes((const char*)&removePacket, packetSize, 0, 0, true, appFd);
00363         }
00364 
00365         SCAddPacket addPacket;
00366         addPacket.packetType = SC_ADD_NEIGHBOR;
00367         packetSize = sizeof(addPacket);
00368 
00369         for(i=0; i<sgcMsg->getAddNeighborArraySize(); ++i) {
00370             addPacket.ip = sgcMsg->getAddNeighbor(i).ip.get4().getInt();
00371             if( addPacket.ip == thisNode.ip.get4().getInt() ) continue;
00372             addPacket.posX = sgcMsg->getNeighborPosition(i).x;
00373             addPacket.posY = sgcMsg->getNeighborPosition(i).y;
00374             scheduler->sendBytes((const char*)&packetSize, sizeof(int), 0, 0, true, appFd);
00375             scheduler->sendBytes((const char*)&addPacket, packetSize, 0, 0, true, appFd);
00376         }
00377     }
00378     for(i=0; i<sgcMsg->getRemoveNeighborArraySize(); ++i)
00379         Neighbors.erase(sgcMsg->getRemoveNeighbor(i));
00380 
00381     for(i=0; i<sgcMsg->getAddNeighborArraySize(); ++i) {
00382         Vector2D newPos;
00383         newPos = sgcMsg->getNeighborPosition(i);
00384         itNeighbors = Neighbors.find(sgcMsg->getAddNeighbor(i));
00385         if(itNeighbors != Neighbors.end()) {
00386             Vector2D temp = newPos - itNeighbors->second.position;
00387             temp.normalize();
00388             itNeighbors->second.direction = temp;
00389             itNeighbors->second.position = newPos;
00390         }
00391         else {
00392             NeighborMapEntry entry;
00393             entry.position = newPos;
00394             Neighbors.insert(std::make_pair(sgcMsg->getAddNeighbor(i), entry));
00395         }
00396     }
00397 }

void SimpleGameClient::updatePosition (  )  [protected]

Definition at line 399 of file SimpleGameClient.cc.

Referenced by handleLowerMessage(), and handleTimerEvent().

00400 {
00401     if(!frozen) {
00402         Generator->move();
00403     }
00404     position = Generator->getPosition();
00405     GameAPIPositionMessage *posMsg = new GameAPIPositionMessage("MOVEMENT_INDICATION");
00406     posMsg->setCommand(MOVEMENT_INDICATION);
00407     posMsg->setPosition(position);
00408     sendMessageToLowerTier(posMsg);
00409 }

void SimpleGameClient::handleRealworldPacket ( char *  buf,
uint32_t  len 
) [protected]

Definition at line 301 of file SimpleGameClient.cc.

Referenced by handleTimerEvent().

00302 {
00303     SCBasePacket *type = (SCBasePacket*)buf;
00304     if(type->packetType == SC_PARAM_REQUEST) {
00305         SCParamPacket packet;
00306         packet.packetType = SC_PARAM_RESPONSE;
00307         packet.speed = movementSpeed;
00308         packet.dimension = areaDimension;
00309         packet.AOI = AOIWidth;
00310         packet.delay = SIMTIME_DBL(movementDelay);
00311         packet.startX = position.x;
00312         packet.startY = position.y;
00313         packet.ip = thisNode.ip.get4().getInt();
00314         packet.seed = coordinator->getSeed();
00315         int packetSize = sizeof(packet);
00316         scheduler->sendBytes((const char*)&packetSize, sizeof(int), 0, 0, true, appFd);
00317         scheduler->sendBytes((const char*)&packet, packetSize, 0, 0, true, appFd);
00318         doRealworld = true;
00319     }
00320     else if(type->packetType == SC_MOVE_INDICATION) {
00321         SCMovePacket *packet = (SCMovePacket*)type;
00322         Vector2D temp;
00323         temp.x = packet->posX;
00324         temp.y = packet->posY;
00325         dynamic_cast<realWorldRoaming*>(Generator)->setPosition(temp);
00326     }
00327     else if(type->packetType == SC_EV_CHAT) {
00328         SCChatPacket* packet = (SCChatPacket*)type;
00329         GameAPIChatMessage* chatMsg = new GameAPIChatMessage("ChatMsg");
00330         chatMsg->setCommand(GAMEEVENT_CHAT);
00331         chatMsg->setSrc(thisNode);
00332         chatMsg->setMsg(packet->msg);
00333         sendMessageToLowerTier(chatMsg);
00334     }
00335     else if(type->packetType == SC_EV_SNOWBALL) {
00336         SCSnowPacket* packet = (SCSnowPacket*)type;
00337         GameAPISnowMessage* snowMsg = new GameAPISnowMessage("Throw Snowball");
00338         snowMsg->setCommand(GAMEEVENT_SNOW);
00339         snowMsg->setSrc(thisNode);
00340         snowMsg->setStart(Vector2D(packet->startX, packet->startY));
00341         snowMsg->setEnd(Vector2D(packet->endX, packet->endY));
00342         snowMsg->setTimeSec(packet->time_sec);
00343         snowMsg->setTimeUsec(packet->time_usec);
00344         sendMessageToLowerTier(snowMsg);
00345     }
00346 }


Member Data Documentation

Definition at line 60 of file SimpleGameClient.h.

Referenced by handleRealworldPacket(), and initializeApp().

Definition at line 61 of file SimpleGameClient.h.

Referenced by initializeApp().

simtime_t SimpleGameClient::movementDelay [protected]

double SimpleGameClient::areaDimension [protected]

Definition at line 68 of file SimpleGameClient.h.

Referenced by handleRealworldPacket(), and initializeApp().

double SimpleGameClient::movementSpeed [protected]

Definition at line 68 of file SimpleGameClient.h.

Referenced by handleRealworldPacket(), and initializeApp().

double SimpleGameClient::movementRate [protected]

Definition at line 68 of file SimpleGameClient.h.

Referenced by initializeApp().

double SimpleGameClient::AOIWidth [protected]

Definition at line 68 of file SimpleGameClient.h.

Referenced by handleLowerMessage(), handleRealworldPacket(), and initializeApp().

bool SimpleGameClient::useScenery [protected]

Definition at line 69 of file SimpleGameClient.h.

Referenced by initializeApp().

Definition at line 70 of file SimpleGameClient.h.

Referenced by handleReadyMessage(), handleTimerEvent(), and initializeApp().

Definition at line 72 of file SimpleGameClient.h.

Referenced by handleRealworldPacket(), initializeApp(), and updatePosition().

Definition at line 74 of file SimpleGameClient.h.

Referenced by initializeApp(), and updateNeighbors().

NeighborMap::iterator SimpleGameClient::itNeighbors [protected]

Definition at line 75 of file SimpleGameClient.h.

Referenced by updateNeighbors().

std::string SimpleGameClient::GeneratorType [protected]

Definition at line 76 of file SimpleGameClient.h.

Referenced by initializeApp().

timeval SimpleGameClient::startTime [protected]

Definition at line 78 of file SimpleGameClient.h.

Referenced by handleLowerMessage(), and handleReadyMessage().

cMessage* SimpleGameClient::move_timer [protected]

cMessage* SimpleGameClient::packetNotification [protected]

Definition at line 85 of file SimpleGameClient.h.

Referenced by initializeApp(), and ~SimpleGameClient().

Definition at line 86 of file SimpleGameClient.h.

Referenced by handleTimerEvent(), and initializeApp().

unsigned int SimpleGameClient::mtu [protected]

Definition at line 88 of file SimpleGameClient.h.

Referenced by initializeApp().

SOCKET SimpleGameClient::appFd [protected]

bool SimpleGameClient::frozen [protected]


The documentation for this class was generated from the following files:

Generated on Wed Mar 25 16:32:54 2009 for OverSim by  doxygen 1.5.5