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.

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 (TierReadyMessage *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

GlobalCoordinatorcoordinator
double movementDelay
double areaDimension
double movementSpeed
double movementRate
double AOIWidth
bool overlayReady
Vector2D position
MovementGeneratorGenerator
NeighborMap Neighbors
NeighborMap::iterator itNeighbors
std::string GeneratorType
cMessage * move_timer
cMessage * packetNotification
RealtimeScheduler::PacketBuffer packetBuffer
RealtimeSchedulerscheduler
unsigned int mtu
int appFd
bool doRealworld


Constructor & Destructor Documentation

SimpleGameClient::~SimpleGameClient (  )  [virtual]

00249 {
00250     // destroy self timer messages
00251     cancelAndDelete(move_timer);
00252     cancelAndDelete(packetNotification);
00253 }


Member Function Documentation

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

initializes derived class-attributes

Parameters:
stage the init stage

Reimplemented from BaseApp.

00029 {
00030     // all initialization is done in the first stage
00031     if (stage != MIN_STAGE_APP)
00032         return;
00033 
00034     // fetch parameters
00035     areaDimension = par("areaDimension");
00036     movementRate = par("movementRate");
00037     movementSpeed = par("movementSpeed");
00038     movementDelay= 1.0 / movementRate;
00039     AOIWidth = par("AOIWidth");
00040     int groupSize = (int)par("groupSize");
00041     GeneratorType = (std::string)par("movementGenerator");
00042 
00043     WATCH_MAP(Neighbors);
00044     WATCH(position);
00045     WATCH(GeneratorType);
00046     WATCH(overlayReady);
00047 
00048     doRealworld = false;
00049     coordinator = NULL;
00050     scheduler = NULL;
00051     packetNotification = NULL;
00052 
00053 
00054 
00055     double speed = movementDelay * movementSpeed;
00056     if(strcmp(GeneratorType.c_str(), "greatGathering") == 0) {
00057         if(debugOutput) ev << "SIMPLECLIENT: Movement generator: greatGathering" << endl;
00058         coordinator = check_and_cast<GlobalCoordinator*>(simulation.moduleByPath("globalObserver.globalFunctions[0].coordinator"));
00059         Generator = new greatGathering(areaDimension, speed, &Neighbors, coordinator);
00060     }
00061     else if(strcmp(GeneratorType.c_str(), "groupRoaming") == 0) {
00062         if(debugOutput) ev << "SIMPLECLIENT: Movement generator: groupRoaming" << endl;
00063         coordinator = check_and_cast<GlobalCoordinator*>(simulation.moduleByPath("globalObserver.globalFunctions[0].coordinator"));
00064         Generator = new groupRoaming(areaDimension, speed, &Neighbors, coordinator, groupSize);
00065     }
00066     else if(strcmp(GeneratorType.c_str(), "realWorldRoaming") == 0) {
00067         if(debugOutput) ev << "SIMPLECLIENT: Movement generator: realWorldRoaming" << endl;
00068         Generator = new realWorldRoaming(areaDimension, movementSpeed, &Neighbors);
00069         mtu = par("mtu");
00070         packetNotification = new cMessage("packetNotification");
00071         scheduler = check_and_cast<RealtimeScheduler*>(simulation.scheduler());
00072         scheduler->setInterfaceModule(this, packetNotification, &packetBuffer, mtu, true);
00073         appFd = -1;
00074     }
00075     else {
00076         // debug output
00077         if(debugOutput) {
00078             if(strcmp(GeneratorType.c_str(), "randomRoaming") == 0) ev << "SIMPLECLIENT: Movement generator: randomRoaming" << endl;
00079             else ev << "SIMPLECLIENT: Movement generator: <unknown>. Defaulting to: randomRoaming" << endl;
00080         }
00081         Generator = new randomRoaming(areaDimension, speed, &Neighbors);
00082     }
00083 
00084     // self-messages
00085     move_timer = new cMessage("move_timer");
00086     overlayReady = false;
00087 }

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.

00090 {
00091     if(msg->isName("move_timer")) {
00092         //reset timer
00093         cancelEvent(move_timer);
00094         if(overlayReady)
00095             scheduleAt(simulation.simTime() + movementDelay, msg);
00096         // handle event
00097         updatePosition();
00098     }
00099     if(msg->isName("packetNotification")) {
00100         while(packetBuffer.size() > 0) {
00101             // get packet from buffer and parse it
00102             RealtimeScheduler::PacketBufferEntry packet = *(packetBuffer.begin());
00103             packetBuffer.pop_front();
00104             switch (packet.func) {
00105                 case RealtimeScheduler::PacketBufferEntry::DATA: {
00106                     if(packet.fd != appFd) {
00107                         error("SimpleClient::handleMessage(): Received packet from unknown socket!");
00108                     }
00109                     handleRealworldPacket(packet.data, packet.length);
00110                 } break;
00111                 case RealtimeScheduler::PacketBufferEntry::FD_NEW: {
00112                     if(appFd != -1) {
00113                         error("SimpleClient::handleMessage(): Multiple connections not supported yet!");
00114                     }
00115                     appFd = packet.fd;
00116                 } break;
00117                 case RealtimeScheduler::PacketBufferEntry::FD_CLOSE: {
00118                     if(packet.fd != appFd) {
00119                         error("SimpleClient::handleMessage(): Trying to close unknown connection!");
00120                     }
00121                     appFd = -1;
00122                 }
00123             }
00124             delete packet.data;
00125         }
00126     }
00127 }

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

method to handle non-commonAPI messages from the overlay

Parameters:
msg message to handle

Reimplemented from BaseApp.

00130 {
00131     if(dynamic_cast<GameAPIMessage*>(msg)) {
00132         GameAPIMessage* gameAPIMsg = check_and_cast<GameAPIMessage*>(msg);
00133         switch(gameAPIMsg->getCommand()) {
00134             case MOVEMENT_REQUEST: {
00135                 updatePosition();
00136             } break;
00137             case NEIGHBOR_UPDATE: {
00138                 GameAPIListMessage* gameAPIListMsg = check_and_cast<GameAPIListMessage*>(msg);
00139                 updateNeighbors(gameAPIListMsg);
00140             }
00141         }
00142     }
00143     delete msg;
00144 }

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

method to handle ready messages from the overlay

Parameters:
msg message to handle

Reimplemented from BaseApp.

00147 {
00148     if(msg->getReady()) {
00149         overlayReady = true;
00150         scheduleAt(simulation.simTime() + movementDelay, move_timer);
00151     }
00152     else
00153         overlayReady = false;
00154 
00155     delete msg;
00156 }

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

00187 {
00188     unsigned int i;
00189 
00190     if(doRealworld) {
00191         int packetSize;
00192 
00193         SCRemovePacket removePacket;
00194         removePacket.packetType = SC_REMOVE_NEIGHBOR;
00195         packetSize = sizeof(removePacket);
00196 
00197         for(i=0; i<sgcMsg->getRemoveNeighborArraySize(); ++i) {
00198             removePacket.ip = sgcMsg->getRemoveNeighbor(i).ip.get4().getInt();
00199             scheduler->sendBytes((const char*)&packetSize, sizeof(int), 0, 0, true, appFd);
00200             scheduler->sendBytes((const char*)&removePacket, packetSize, 0, 0, true, appFd);
00201         }
00202 
00203         SCAddPacket addPacket;
00204         addPacket.packetType = SC_ADD_NEIGHBOR;
00205         packetSize = sizeof(addPacket);
00206 
00207         for(i=0; i<sgcMsg->getAddNeighborArraySize(); ++i) {
00208             addPacket.ip = sgcMsg->getAddNeighbor(i).ip.get4().getInt();
00209             addPacket.posX = sgcMsg->getNeighborPosition(i).x;
00210             addPacket.posY = sgcMsg->getNeighborPosition(i).y;
00211             scheduler->sendBytes((const char*)&packetSize, sizeof(int), 0, 0, true, appFd);
00212             scheduler->sendBytes((const char*)&addPacket, packetSize, 0, 0, true, appFd);
00213         }
00214     }
00215     else {
00216         for(i=0; i<sgcMsg->getRemoveNeighborArraySize(); ++i)
00217             Neighbors.erase(sgcMsg->getRemoveNeighbor(i));
00218 
00219         for(i=0; i<sgcMsg->getAddNeighborArraySize(); ++i) {
00220             Vector2D newPos;
00221             newPos = sgcMsg->getNeighborPosition(i);
00222             itNeighbors = Neighbors.find(sgcMsg->getAddNeighbor(i));
00223             if(itNeighbors != Neighbors.end()) {
00224                 Vector2D temp = newPos - itNeighbors->second.position;
00225                 temp.normalize();
00226                 itNeighbors->second.direction = temp;
00227                 itNeighbors->second.position = newPos;
00228             }
00229             else {
00230                 NeighborMapEntry entry;
00231                 entry.position = newPos;
00232                 Neighbors.insert(std::make_pair(sgcMsg->getAddNeighbor(i), entry));
00233             }
00234         }
00235     }
00236 }

void SimpleGameClient::updatePosition (  )  [protected]

00239 {
00240     Generator->move();
00241     position = Generator->getPosition();
00242     GameAPIPositionMessage *posMsg = new GameAPIPositionMessage("MOVEMENT_INDICATION");
00243     posMsg->setCommand(MOVEMENT_INDICATION);
00244     posMsg->setPosition(position);
00245     sendMessageToOverlay(posMsg);
00246 }

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

00159 {
00160     SCBasePacket *type = (SCBasePacket*)buf;
00161     switch(type->packetType) {
00162         case SC_PARAM_REQUEST: {
00163             SCParamPacket packet;
00164             packet.packetType = SC_PARAM_RESPONSE;
00165             packet.speed = movementSpeed;
00166             packet.dimension = areaDimension;
00167             packet.AOI = AOIWidth;
00168             packet.delay = movementDelay;
00169             packet.startX = position.x;
00170             packet.startY = position.y;
00171             int packetSize = sizeof(packet);
00172             scheduler->sendBytes((const char*)&packetSize, sizeof(int), 0, 0, true, appFd);
00173             scheduler->sendBytes((const char*)&packet, packetSize, 0, 0, true, appFd);
00174             doRealworld = true;
00175         } break;
00176         case SC_MOVE_INDICATION: {
00177             SCMovePacket *packet = (SCMovePacket*)type;
00178             Vector2D temp;
00179             temp.x = packet->posX;
00180             temp.y = packet->posY;
00181             dynamic_cast<realWorldRoaming*>(Generator)->setPosition(temp);
00182         }
00183     }
00184 }


Member Data Documentation

GlobalCoordinator* SimpleGameClient::coordinator [protected]

double SimpleGameClient::movementDelay [protected]

double SimpleGameClient::areaDimension [protected]

double SimpleGameClient::movementSpeed [protected]

double SimpleGameClient::movementRate [protected]

double SimpleGameClient::AOIWidth [protected]

bool SimpleGameClient::overlayReady [protected]

Vector2D SimpleGameClient::position [protected]

MovementGenerator* SimpleGameClient::Generator [protected]

NeighborMap SimpleGameClient::Neighbors [protected]

NeighborMap::iterator SimpleGameClient::itNeighbors [protected]

std::string SimpleGameClient::GeneratorType [protected]

cMessage* SimpleGameClient::move_timer [protected]

cMessage* SimpleGameClient::packetNotification [protected]

RealtimeScheduler::PacketBuffer SimpleGameClient::packetBuffer [protected]

RealtimeScheduler* SimpleGameClient::scheduler [protected]

unsigned int SimpleGameClient::mtu [protected]

int SimpleGameClient::appFd [protected]

bool SimpleGameClient::doRealworld [protected]


The documentation for this class was generated from the following files:
Generated on Thu Apr 17 13:19:30 2008 for ITM OverSim by  doxygen 1.5.3