Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Platform/Aperios1.3.2/DDPHandler.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file  Platform/Aperios1.3.2/DDPHandler.cpp
    00003 *
    00004 * Handles the Dog Discovery Protocol (DDP) so that the robot
    00005 * is able to find out the ip's of his teammates.
    00006 *
    00007 * @author <A href=mailto:robocup@m-wachter.de>Michael Wachter</A>
    00008 *
    00009 */
    00010
    00011 #include "DDPHandler.h"
    00012
    00013 #include <stdio.h>
    00014 #include <iostream.h>
    00015
    00016 #include <tools/debugging/debugging.h>
    00017 #include <tools/debugging/watchdog.h>
    00018 #include "NetConfig.h"
    00019
    00020 DDPHandler::DDPHandler() :
    00021 UDPEndpoint(2048,2048)
    00022 {
    00023   for(int i=0; i< Player::numOfPlayerNumbers - 1; i++)
    00024   {
    00025     list[i].ip   = IP_ADDR_ANY;
    00026     list[i].port = 0;
    00027     list[i].timeLastSeen = 0;
    00028   }
    00029   timeLastPackage = 0;
    00030 }
    00031
    00032
    00033 void DDPHandler::updateDDPList()
    00034 {
    00035
    00036 }
    00037
    00038 void DDPHandler::initDDPHandler()
    00039 {
    00040   bind(DDPPORT);
    00041 }
    00042
    00043 IPAddress DDPHandler::getIPAddress(int number)
    00044 {
    00045   // Schaue nach alter der IP-Addresse auf position number
    00046   // Zu alt (>10s) : liefere 0.0.0.0 zurück
    00047   // sonst liefere die IP zurück
    00048   return(IP_ADDR_ANY);
    00049 }
    00050
    00051 void DDPHandler::onReceive(void* data, int size)
    00052 {
    00053   watchdog.enter(1000);
    00054   ASSERT(size == sizeof(DDPPackage));
    00055
    00056   DDPPackage* package = static_cast<DDPPackage*>((void*)data);
    00057
    00058   INFO(sendDDPInfo,idText,text,"Incoming DDP package from " << ipOfLastPackage <<
    00059     " " << package->teamIdentifier << " " << package->teamColor <<
    00060     " " << package->playerNumber );
    00061
    00062   // Is the package from the same team and color ?
    00063   if ( (strcmp(package->teamIdentifier,getPlayer().getTeamName()) == 0)  &&
    00064     (package->teamColor == getPlayer().getTeamColor()) &&
    00065     (package->playerNumber < 5) &&
    00066     !ipOfLastPackage.InLoop())
    00067   {
    00068     // cout << "Incoming DDP package is from own team \n" << flush;
    00069     int fieldNumberToUpdate = -1;
    00070
    00071     int ownPlayerNumber = (int)getPlayer().getPlayerNumber();
    00072     if (package->playerNumber < ownPlayerNumber)
    00073     {
    00074       fieldNumberToUpdate = package->playerNumber;
    00075     }
    00076     else
    00077     {
    00078       fieldNumberToUpdate = package->playerNumber-1;
    00079     }
    00080
    00081     cout << "package:" << package->playerNumber << " own:" << ownPlayerNumber << " update: " << fieldNumberToUpdate << " \n";
    00082
    00083     // Update the field
    00084     list[fieldNumberToUpdate].ip   = ipOfLastPackage;
    00085     list[fieldNumberToUpdate].port = 0xA1BC;
    00086     list[fieldNumberToUpdate].timeLastSeen = SystemCall::getCurrentSystemTime();
    00087
    00088     INFO(sendDDPInfo,idText,text,"Known Dogs: "
    00089       << list[0].ip << "(" << SystemCall::getTimeSince(list[0].timeLastSeen) << "), "
    00090       << list[1].ip << "(" << SystemCall::getTimeSince(list[1].timeLastSeen) << "), "
    00091       << list[2].ip << "(" << SystemCall::getTimeSince(list[2].timeLastSeen) << ")"
    00092
    00093       );
    00094   }
    00095   watchdog.leave(1000);
    00096 }
    00097
    00098 void DDPHandler::sendDDPPackage()
    00099 {
    00100   if (SystemCall::getTimeSince(timeLastPackage) > 2000)
    00101   {
    00102     // Prepare package
    00103     strcpy(thePackage.teamIdentifier, getPlayer().getTeamName());
    00104     thePackage.teamColor = getPlayer().getTeamColor();
    00105     thePackage.playerNumber = getPlayer().getPlayerNumber();
    00106 #ifdef DDPwithBATTERIESTATUS
    00107     thePackage.batterieStatus = SystemCall::getRemainingPower();
    00108 #endif
    00109 #ifdef NETWORK_useEnhencedDebug
    00110     cout << "NON-Encrypted DDP Package sent." << endl << flush;
    00111 #endif
    00112
    00113     send(&thePackage,sizeof(thePackage),IP_ADDR_BROADCAST,DDPPORT);
    00114     timeLastPackage = SystemCall::getCurrentSystemTime();
    00115   }
    00116 }
    00117
    00118 bool DDPHandler::activeTeamMembers()
    00119 {
    00120   for (int i=0; i<Player::numOfPlayerNumbers-1; i++)
    00121   {
    00122     if(SystemCall::getTimeSince(list[i].timeLastSeen) < 15000)
    00123     {
    00124       return(true);
    00125     }
    00126   }
    00127   return(false);
    00128 }