Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

KoduDiscover.cc

Go to the documentation of this file.
00001 
00002 #include "Events/EventBase.h"
00003 #include "Events/EventListener.h"
00004 #include "Events/EventRouter.h"
00005 #include "Wireless/Wireless.h"
00006 #include "Kodu/KoduDiscover.h"
00007 #include <iostream>
00008 #include <sys/socket.h>
00009 #include <netinet/in.h>
00010 #include <arpa/inet.h>
00011 
00012 map<int, player_identity> KoduDiscover::players;
00013 player_identity KoduDiscover::my_identity;
00014 Socket * KoduDiscover::discover_sock;
00015 Socket * KoduDiscover::broadcast_sock;
00016 ident_callback KoduDiscover::new_robot;
00017 void* KoduDiscover::callback_arg;
00018 
00019 string KoduType::strs[] = {
00020     "invalid",
00021     "kodu",
00022     "cycle",
00023     "octopus",
00024     "turtle"
00025 };
00026 
00027 KoduDiscover::KoduDiscover(player_identity& ident, const string& iface) {
00028     ident.hostAddr = wireless->getIFAddress((const char*)iface.c_str());
00029     std::cout << "Got address " << ident.hostAddr << " for interface " << iface.c_str() << std::endl;
00030     my_identity = ident;
00031     broadcast_sock = wireless->socket(Socket::SOCK_DGRAM, 1024, 1024);
00032     discover_sock = wireless->socket(Socket::SOCK_DGRAM, 1024, 1024);
00033     wireless->setReceiver(discover_sock->sock, &discover_rcv);
00034     wireless->listen(discover_sock->sock, 4567); //FIXME configuration variable
00035     new_robot = NULL;
00036     callback_arg = NULL;
00037     //Add timer callback for sending discover messages
00038     erouter->addTimer(this, 1, 2000, true);
00039 }
00040 
00041 KoduDiscover::~KoduDiscover() {
00042     erouter->removeTimer(this);
00043     wireless->setReceiver(discover_sock->sock, (int (*)(char*,int))NULL);
00044     wireless->close(broadcast_sock);
00045     wireless->close(discover_sock);
00046 }
00047 
00048 void KoduDiscover::setNewRobotCallback(ident_callback new_robot_cb, void *cb_arg) {
00049     new_robot = new_robot_cb;
00050     callback_arg = cb_arg;
00051 }
00052 
00053 int KoduDiscover::discover_rcv(char* buf, int size) {
00054     if (size != sizeof(player_identity)) {
00055         cerr << "Badly sized discover packet" << endl;
00056     }
00057     player_identity *ident = (player_identity*)buf;
00058     if (players.find(ident->hostAddr) == players.end()) {
00059         if (new_robot != NULL && ident->hostAddr != my_identity.hostAddr) {
00060             new_robot(ident, callback_arg);
00061         }
00062     }
00063     players[ident->hostAddr] = *ident;
00064     cout << "Got discover packet from " << ident->hostAddr << " of type " << ident->type << endl;
00065 
00066     return 0;
00067 }
00068 
00069 void KoduDiscover::processEvent(const EventBase& event) {
00070     if (event.getGeneratorID() == EventBase::timerEGID) {
00071         wireless->connect(broadcast_sock->sock, "255.255.255.255", 4567);
00072         broadcast_sock->write((const byte*)&my_identity, sizeof(player_identity));
00073 
00074     }
00075 }

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:43 2016 by Doxygen 1.6.3