[TeamTalk 16]: [553] TeamTalk/Agents: compartmentalized per-robot information into a TeamTalk::robot class
tk@edam.speech.cs.cmu.edu
tk at edam.speech.cs.cmu.edu
Sun Dec 10 22:21:43 EST 2006
An HTML attachment was scrubbed...
URL: http://mailman.srv.cs.cmu.edu/pipermail/teamtalk-developers/attachments/20061210/a01c8f2e/attachment-0001.html
-------------- next part --------------
Modified: TeamTalk/Agents/PrimitiveComm/PrimitiveComm.vcproj
===================================================================
--- TeamTalk/Agents/PrimitiveComm/PrimitiveComm.vcproj 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/PrimitiveComm/PrimitiveComm.vcproj 2006-12-11 03:21:42 UTC (rev 553)
@@ -184,6 +184,10 @@
>
</File>
<File
+ RelativePath=".\robot_class.cpp"
+ >
+ </File>
+ <File
RelativePath=".\robot_client.cpp"
>
</File>
@@ -262,6 +266,10 @@
>
</File>
<File
+ RelativePath=".\robot_class.h"
+ >
+ </File>
+ <File
RelativePath=".\robot_client.hpp"
>
</File>
Added: TeamTalk/Agents/PrimitiveComm/robot_class.cpp
===================================================================
--- TeamTalk/Agents/PrimitiveComm/robot_class.cpp (rev 0)
+++ TeamTalk/Agents/PrimitiveComm/robot_class.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -0,0 +1,42 @@
+#include "robot_class.h"
+#include "utils.h"
+#include "boeing_net.h"
+
+using namespace std;
+using namespace TeamTalk;
+
+Robot::Robot(const std::string &_name, Robot::RobotClass _robotClass, const std::string &_loc,
+ unsigned short _port, const std::string &_voice)
+ : name(_name), robotClass(_robotClass), loc(_loc), port(_port), voice(_voice) {}
+
+Robot::Robot(const std::string &_name, Robot::RobotClass _robotClass, const std::string &_loc,
+ const std::string &_voice)
+ : name(_name), robotClass(_robotClass), loc(_loc), port(Boeing::ROBOT_PORT), voice(_voice) {}
+
+Robot::Robot(const Robot& x) : name(x.name), robotClass(x.robotClass), loc(x.loc), port(x.port), voice(x.voice) {}
+
+Robot::~Robot() {}
+
+istream& operator>>(istream& in, Robot::RobotClass& robotClass) {
+ string s;
+ if((in >> s).fail()) return in;
+ if(s == "safe" || s == "SAFE") robotClass = Robot::SAFE;
+ else if(s == "dangerous" || s == "DANGEROUS") robotClass = Robot::DANGEROUS;
+ else in.setf(ios::failbit);
+ return in;
+}
+
+ostream& operator<<(ostream& out, const Robot::RobotClass& robotClass) {
+ switch(robotClass) {
+ case Robot::SAFE:
+ out << "safe";
+ break;
+ case Robot::DANGEROUS:
+ out << "dangerous";
+ break;
+ default:
+ error << "unknown robot class: " << (int)robotClass << endl;
+ out << (int)robotClass;
+ }
+ return out;
+}
\ No newline at end of file
Added: TeamTalk/Agents/PrimitiveComm/robot_class.h
===================================================================
--- TeamTalk/Agents/PrimitiveComm/robot_class.h (rev 0)
+++ TeamTalk/Agents/PrimitiveComm/robot_class.h 2006-12-11 03:21:42 UTC (rev 553)
@@ -0,0 +1,28 @@
+#ifndef ROBOT_CLASS_H
+#define ROBOT_CLASS_H
+
+#include <string>
+
+using namespace std;
+
+namespace TeamTalk {
+ class Robot {
+ public:
+ string name;
+ enum RobotClass {SAFE, DANGEROUS} robotClass;
+ string loc;
+ unsigned short port;
+ string voice;
+
+ Robot(const string& _name, RobotClass _robotClass, const string& _loc, unsigned short port,
+ const string& voice);
+ Robot(const string& _name, RobotClass _robotClass, const string& _loc, const string& _voice);
+ Robot(const Robot& robot);
+ ~Robot();
+ };
+};
+
+istream& operator>>(istream& in, TeamTalk::Robot::RobotClass& robotClass);
+ostream& operator<<(ostream& out, const TeamTalk::Robot::RobotClass& robotClass);
+
+#endif
\ No newline at end of file
Modified: TeamTalk/Agents/PrimitiveComm/robot_client.cpp
===================================================================
--- TeamTalk/Agents/PrimitiveComm/robot_client.cpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/PrimitiveComm/robot_client.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -28,44 +28,139 @@
using namespace std;
using namespace TeamTalk;
-void RobotClient::connect(const string &loc, const string &voice,
- const string &safeness, unsigned short port) {
- voice_ = voice;
- debug << "opening " << loc << ':' << port << endl;
- open(loc.c_str(), port);
- if(spawnback) (*spawnback)(name_, safeness);
+bool RobotClient::connect()
+{
+ debug << "opening " << robot_.loc << ':' << robot_.port << endl;
+ return open(robot_.loc.c_str(), robot_.port) != 0;
}
+void RobotClient::spawnServer()
+{
+ //spawn server
+#ifdef WIN32
+ serverThread_ = (HANDLE)_beginthread(readMessages, 0, (void*)this);
+ _beginthread(trackbots, 0, (void*)this);
+#else
+ pthread_t trackThread;
+ pthread_create(&serverThread_, pthread_attr_default, readMessages, (void *)this);
+ pthread_create(&trackThread_, pthread_attr_default, trackbots, (void*)this);
+#endif
+}
+
+#ifdef WIN32
+void RobotClient::trackbots(void* thisp)
+#else
+void* RobotClient::trackbots(void* thisp)
+#endif
+{
+ RobotClient* me = (RobotClient*)thisp;
+ debug << "starting trackbots thread" << endl;
+ for(int t=0; true; t++) {
+ Sleep(1000);
+ debug << "sending location request" << endl;
+ if(!me->sendReqLocation()) {
+ //maybe we're not connected anymore, try to connect
+ warn << "we appear to be disconnected...trying to reconnect" << endl;
+ if(me->reopen()) {
+ warn << "ok, reconnected to " << me->getHost() << ':' << me->getPort() << endl;
+ me->sendReqLocation();
+ } else {
+ warn << "couldn't reconnect to " << me->getHost() << ':' << me->getPort() << endl;
+ }
+ }
+ if(!(t%40) && me->hasCamera()) { //send image request every 40 seconds
+ me->sendReqImage();
+ }
+ }
+}
+
+//---------------------------
+// receiving messages
+
+#ifdef WIN32
+void RobotClient::readMessages(void *thisp)
+#else
+void* RobotClient::readMessages(void *thisp)
+#endif
+{
+ RobotClient* me = (RobotClient *)thisp;
+ debug("client") << "About to start reading thread" << endl;
+ for(;;) {
+ bool gotAMessage = false;
+ const Boeing::MsgRobot* mesg = me->getNextMessage();
+ if(mesg == NULL) continue;
+ try {
+ Msg* m = Msg::interpretBoeingPacket(string(mesg->buff, sizeof(Boeing::MsgRobot)), me->getName());
+ if(!m) throw MalformedPacketException("RobotClient::readMessages",
+ "interpretation of message returned NULL");
+ gotAMessage = true;
+
+ //report unless you get a monotonous location message
+ if(dynamic_cast<MsgRobLocation*>(m))
+ debug("client") << "robot " << me->getName() << ": " << m << endl;
+ else
+ info("client") << "robot " << me->getName() << ": " << m << endl;
+ {
+ //[2005-09-22] (tk): don't worry about pings for now
+ //MsgPingPP* ping = dynamic_cast<MsgPingPP*>(m);
+ //if(ping != NULL) {
+ // if(me->addRobot(ping->handle(),
+ // ping->content()->header.sender)) {
+ // if(me->addRobot(ping->handle(), st_remote_addr))
+ // //if this robot is new to us, reply to the ping
+ // me->sendPacket(ping->handle(), MsgPingPP(me->handle_));
+ //}
+ }
+ me->robotCallback_->processRobotMessage(m);
+ delete m;
+ } catch(MalformedPacketException e) {
+ error << e.what();
+ }
+ if(!gotAMessage) Sleep(500);
+ }
+}
+
void (*RobotClient::spawnback)(const string&, const string&) = NULL;
-RobotClient::RobotClient(const string& name) : name_(name) {}
-
RobotClient::RobotClient(const std::string& name, const std::string& loc,
- const std::string& voice, const std::string& safeness, unsigned short port)
- : name_(name) {
- connect(loc, voice, safeness, port);
+ const std::string& voice, const std::string& safeness, unsigned short port, RobotCallback* robotCallback)
+ : robot_(name, Robot::SAFE, loc, port, voice), robotCallback_(robotCallback)
+{
+ if(safeness == "SAFE" || safeness == "safe") robot_.robotClass = Robot::SAFE;
+ else if(safeness == "DANGEROUS" || safeness == "dangerous") robot_.robotClass = Robot::DANGEROUS;
+ else {
+ error << "RobotClient::RobotClient: unknown robot class: " << safeness << endl;
+ return;
+ }
+ if(spawnback) (*spawnback)(name, safeness);
+ connect();
+ spawnServer();
}
-string RobotClient::getName() const {return name_;}
-string RobotClient::getVoice() const {return voice_;}
+string RobotClient::getName() const {return robot_.name;}
+string RobotClient::getVoice() const {return robot_.voice;}
+bool RobotClient::hasCamera() const {return robot_.robotClass == Robot::DANGEROUS;}
-bool RobotClient::operator<(const RobotClient& rhs) const {
- return name_ < rhs.name_;
+void RobotClient::wait() const
+{
+#ifdef WIN32
+ WaitForSingleObject(serverThread_, INFINITE);
+#else
+ pthread_join(serverThread_, NULL);
+#endif
}
-
//const unsigned short RobotsClient::PORT = 32788;
//const unsigned int RobotClient::MAXLINE = 0xfff; //maximum size of received message <4k
//unsigned int RobotClient::KEEPALIVEPERIOD = 900; //time between pings 15min
//char* RobotClient::BROADCASTADDR = "192.168.1.255";
-RobotsClient::RobotsClient(const string& handle, RobotCallback* robotCallback)
- : handle_(handle), robotCallback_(robotCallback)
+RobotsClient::RobotsClient(const string& handle)
+ : handle_(handle)
{
srand ((unsigned int)time(NULL));
Msg::taskIDCounter = rand();
initializeSocketLayer();
getMyAddress();
- serverThread_ = spawnServer();
}
RobotsClient::~RobotsClient()
@@ -77,13 +172,15 @@
#endif
}
-void RobotsClient::initializeSocketLayer() {
+void RobotsClient::initializeSocketLayer()
+{
#ifdef WIN32
initializeSockets();
#endif
}
-void RobotsClient::getMyAddress() {
+void RobotsClient::getMyAddress()
+{
gethostname(hostname, Boeing::SADDR_LENGTH);
struct hostent *Host = gethostbyname(hostname);
in_addr me;
@@ -91,109 +188,30 @@
strncpy(hostname, inet_ntoa(me), Boeing::SADDR_LENGTH);
}
-#ifdef WIN32
-HANDLE RobotsClient::spawnServer() {
-#else
-pthread_t RobotsClient::spawnServer() {
-#endif
- //spawn server
-#ifdef WIN32
- return (HANDLE)_beginthread(readMessages, 0, (void *) this);
-#else
- pthread_t readingThread, keepaliveThread;
- pthread_create(
- &readingThread,
- pthread_attr_default,
- readMessages,
- (void *) this
- );
- return readingThread;
-#endif
-}
-
//returns false if a robot by that name already exists
//else it creates that robot client and inserts it
bool RobotsClient::addRobot(const string& name, const string& loc,
const string& voice, const string& safeness,
- unsigned short port)
+ unsigned short port, RobotCallback *robotCallback)
{
if(find(name)) return false;
- insert(RobotClient(name, loc, voice, safeness, port));
+ push_back(new RobotClient(name, loc, voice, safeness, port, robotCallback));
return true;
}
-//---------------------------
-// receiving messages
-
-#ifdef WIN32
-void RobotsClient::readMessages(void *thisp)
-#else
-void* RobotsClient::readMessages(void *thisp)
-#endif
+vector<string> RobotsClient::getClientList() const
{
- RobotsClient* me = (RobotsClient *) thisp;
- debug("client") << "About to start reading thread" << endl;
- for(;;) {
- bool gotAMessage = false;
- for(iterator i = me->begin(); i != me->end(); i++) {
- const Boeing::MsgRobot* mesg = i->getNextMessage();
- if(mesg == NULL) continue;
- gotAMessage = true;
- try {
- Msg* m = Msg::interpretBoeingPacket(string(mesg->buff, sizeof(Boeing::MsgRobot)), i->getName());
- if(m == NULL) {
- error << "interpretation of message returned NULL" << endl;
- continue;
- }
- //report unless you get a monotonous location message
- if(dynamic_cast<MsgRobLocation*>(m))
- debug("client") << "robot " << i->getName() << ": " << m << endl;
- else
- info("client") << "robot " << i->getName() << ": " << m << endl;
- {
- //[2005-09-22] (tk): don't worry about pings for now
- //MsgPingPP* ping = dynamic_cast<MsgPingPP*>(m);
- //if(ping != NULL) {
- // if(me->addRobot(ping->handle(),
- // ping->content()->header.sender)) {
- // if(me->addRobot(ping->handle(), st_remote_addr))
- // //if this robot is new to us, reply to the ping
- // me->sendPacket(ping->handle(), MsgPingPP(me->handle_));
- //}
- }
- me->robotCallback_->processRobotMessage(m);
- delete m;
- } catch(MalformedPacketException e) {
- error << e.what();
- }
- }
- if(!gotAMessage) Sleep(500);
- }
-}
-
-vector<string> RobotsClient::getClientList() const {
vector<string> ret;
for(const_iterator i = begin(); i != end(); i++) {
- ret.push_back(i->getName());
+ ret.push_back((*i)->getName());
}
return ret;
}
-RobotClient* RobotsClient::find(const string& name) {
- iterator i = set<RobotClient>::find(RobotClient(name));
- if (i == end()) {
- warn << "didn;t find robotclient for " << name << endl;
- return NULL;
- } else {
- warn << "found robotclient " << ((void*)&*i) << " for " << name << endl;
+RobotClient* RobotsClient::find(const string& name)
+{
+ for(const_iterator i = begin(); i != end(); i++) {
+ if((*i)->getName() == name) return *i;
}
- return &*i;
+ return NULL;
}
-
-void RobotsClient::wait() const {
-#ifdef WIN32
- WaitForSingleObject(serverThread_, INFINITE);
-#else
- pthread_join(serverThread_, NULL);
-#endif
-}
Modified: TeamTalk/Agents/PrimitiveComm/robot_client.hpp
===================================================================
--- TeamTalk/Agents/PrimitiveComm/robot_client.hpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/PrimitiveComm/robot_client.hpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -15,8 +15,7 @@
#endif
#include <iostream>
-#include <map>
-#include <vector>
+#include <list>
#include <string>
#include "robot_packet2.h"
#include "udpsocket.h"
@@ -24,6 +23,7 @@
#include <boeing_trader_client.h>
#include <boeing_robot_client.h>
#include <boeing_map_client.h>
+#include <robot_class.h>
using namespace std;
@@ -53,12 +53,21 @@
*/
class RobotClient : public Boeing::RobotClient {
private:
- string name_; //the robot's name
- string voice_; //the name of the robot's voice
+ Robot robot_; //the type of robot
+ RobotCallback* robotCallback_;
protected:
- void connect(const string& loc, const string& voice, const string& safeness,
- unsigned short port);
+ bool connect(); //returns true if connection succeeded
+ void spawnServer();
+#ifdef WIN32
+ static void trackbots(void *thisp);
+ static void readMessages(void *thisp);
+ HANDLE serverThread_;
+#else
+ static void* trackbots(void *thisp);
+ static void* readMessages(void *thisp);
+ pthread_t serverThread_;
+#endif
public:
//spawnback is a hook for robot instantiation
@@ -66,55 +75,38 @@
//and descruction, but may not know everything that needs to be done. By
//default it is set to do nothing.
static void (*spawnback)(const string& name, const string& safeness);
- RobotClient(const string& name);
+
RobotClient(const string& name, const string& loc, const string& voice,
- const string& safeness, unsigned short port=0);
+ const string& safeness, unsigned short port, RobotCallback* robotCallback);
string getName() const;
string getVoice() const;
+ bool hasCamera() const;
- bool operator<(const RobotClient& rhs) const;
+ void wait() const;
};
-class RobotsClient : public set<RobotClient> {
+class RobotsClient : public list<RobotClient*> {
private:
char hostname[Boeing::SADDR_LENGTH]; // our host name
char ip[16]; // our ip address in presentation format
string handle_; //our handle
-#ifdef WIN32
- static void readMessages(void *thisp);
- HANDLE serverThread_;
-#else
- static void* readMessages(void *thisp);
- pthread_t serverThread_;
-#endif
-
- RobotCallback* robotCallback_;
-
protected:
void initializeSocketLayer();
void getMyAddress();
-#ifdef WIN32
- HANDLE spawnServer();
-#else
- pthread_t spawnServer();
-#endif
public:
// fill out any constructor/destructor info you need
- RobotsClient(const string& handle, RobotCallback* robotCallback);
+ RobotsClient(const string& handle);
~RobotsClient();
// returns true if robot wasn't already added
bool addRobot(const string& name, const string& loc, const string& voice,
- const string& safeness, unsigned short port=0);
+ const string& safeness, unsigned short port, RobotCallback* robotCallback);
vector<string> getClientList() const;
RobotClient* find(const string& name);
-
- //wait for thread
- void wait() const;
};
} //namespace TEAMTALK
#endif
Modified: TeamTalk/Agents/PrimitiveComm/robot_packet2.cpp
===================================================================
--- TeamTalk/Agents/PrimitiveComm/robot_packet2.cpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/PrimitiveComm/robot_packet2.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -33,6 +33,8 @@
//instantiation from Boeing packet
Msg::Msg(string sender, double tstamp) : sender_(sender), tstamp_(tstamp) {}
+Msg::Msg(const Msg& m) : sender_(m.sender_), tstamp_(m.tstamp_) {}
+
Msg::~Msg() {}
Msg* Msg::interpretBoeingPacket(const string& m, const string& sender)
Modified: TeamTalk/Agents/PrimitiveComm/robot_packet2.h
===================================================================
--- TeamTalk/Agents/PrimitiveComm/robot_packet2.h 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/PrimitiveComm/robot_packet2.h 2006-12-11 03:21:42 UTC (rev 553)
@@ -54,6 +54,7 @@
Msg(string sender=string());
//instantiation from Boeing packet
Msg(string sender, double tstamp);
+ Msg(const Msg& m);
public:
virtual ~Msg();
Modified: TeamTalk/Agents/TeamTalkBackend/TeamTalkBackend.vcproj
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/TeamTalkBackend.vcproj 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/TeamTalkBackend.vcproj 2006-12-11 03:21:42 UTC (rev 553)
@@ -144,7 +144,7 @@
/>
<Tool
Name="VCLinkerTool"
- AdditionalDependencies="..\..\ExternalLibraries\Galaxy\lib\x86-nt\libgalaxy.lib ws2_32.lib ..\greta\Release\greta.lib"
+ AdditionalDependencies="..\..\ExternalLibraries\Galaxy\lib\x86-nt\libgalaxy.lib ws2_32.lib"
OutputFile="$(OutDir)/TeamTalkBackend.exe"
LinkIncremental="1"
IgnoreAllDefaultLibraries="false"
Modified: TeamTalk/Agents/TeamTalkBackend/backendstub/backendstub.cpp
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/backendstub/backendstub.cpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/backendstub/backendstub.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -109,12 +109,12 @@
string token;
task >> token;
if(token == "search") {
- Robot *dangerousRobot = robots.findFirst(Robot::DANGEROUS);
+ Robot *dangerousRobot = robots.findFirst(TeamTalk::Robot::DANGEROUS);
if(!dangerousRobot) {
warn << "couldn't find a dangerous robot" << endl;
continue;
}
- Robot *safeRobot = robots.findFirst(Robot::SAFE);
+ Robot *safeRobot = robots.findFirst(TeamTalk::Robot::SAFE);
if(!safeRobot) {
warn << "couldn't find a safe robot" << endl;
continue;
@@ -134,7 +134,7 @@
safeRobot->enqueueGoal(trail[i]);
safeRobot->enqueueGoal(Goal(trail[i], msg->msg_task.taskid));
} else if(token == "explore") {
- Robot *safeRobot = robots.findFirst(Robot::SAFE);
+ Robot *safeRobot = robots.findFirst(TeamTalk::Robot::SAFE);
if(!safeRobot) {
warn << "couldn't find a safe robot" << endl;
continue;
@@ -212,8 +212,8 @@
else if(!strcmp(argv[i], "--peerfile"))
peerfile = argv[++i];
else {
- //start a robot, put it on the stack
- robots.push_back(Robot(argv[i], Robot::SAFE, &tserver, atoi(argv[i])));
+ fatal << "main: no robots specified" << endl;
+ exit(1);
}
}
@@ -238,23 +238,24 @@
string voice;
Frobotips >> voice;
debug << "voice is " << voice << endl;
- Robot::RobotClass robotClass;
+ TeamTalk::Robot::RobotClass robotClass;
if((Frobotips >> robotClass).fail())
error << "couldn't get robot class for " << rname << endl;
string::size_type i = rip.find(':');
debug << "bout to add..." << endl;
if(i != string::npos) {
unsigned short port = atoi(string(rip, i+1, rip.size()-i-1).c_str());
- robots.push_back(Robot(rname, robotClass, &tserver, port));
+ robots.push_back(new Robot(TeamTalk::Robot(rname, robotClass, "localhost", port, voice), &tserver));
info << "added " << rname << '@' << port << " class:" << robotClass << endl;
} else {
- robots.push_back(Robot(rname, robotClass, &tserver));
+ robots.push_back(new Robot(TeamTalk::Robot(rname, robotClass, "localhost", voice), &tserver));
info << "added " << rname << " class:" << robotClass << endl;
}
ignoreToEndOfLine(Frobotips);
}
}
- if(robots.empty()) robots.push_back(Robot(name, Robot::SAFE, &tserver));
+ if(robots.empty())
+ robots.push_back(new Robot(TeamTalk::Robot(name, TeamTalk::Robot::SAFE, "localhost", ""), &tserver));
Boeing::MapServer *mserver;
if(need_mapserver) {
@@ -271,7 +272,7 @@
HANDLE* handles = new HANDLE[robots.size()];
int i=0;
for(Robots::const_iterator iter = robots.begin(); iter != robots.end(); i++, iter++) {
- handles[i] = iter->getSimulationHandle();
+ handles[i] = (*iter)->getSimulationHandle();
}
WaitForMultipleObjects((DWORD) robots.size(), handles, true, INFINITE);
return 0;
Modified: TeamTalk/Agents/TeamTalkBackend/backendstub/robot.cpp
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/backendstub/robot.cpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/backendstub/robot.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -181,22 +181,19 @@
_endthread();
}
-Robot::Robot(const string& name, RobotClass robotClass,
- Boeing::TraderServer* tserver, unsigned short port)
-: followee(NULL), action(WAITING), name_(name), robotClass_(robotClass),
-tserver_(tserver) {
+Robot::Robot(const TeamTalk::Robot& robot, Boeing::TraderServer* tserver)
+: followee(NULL), action(WAITING), robot_(robot), tserver_(tserver)
+{
InitializeCriticalSection(&CriticalSection);
if(!image) readImage("pic.jpg");
server_ = new Boeing::RobotServer();
- if(port) server_->open(port);
- else server_->open();
+ server_->open(robot.port);
simulationThread_ = (HANDLE)_beginthread(simulate, 0, (void *) this);
}
-Robot::Robot(const Robot& x)
+Robot::Robot(const ::Robot& x)
: pose(x.pose), action(x.action), followee(x.followee), goal(x.goal),
-goals(x.goals), name_(x.name_), robotClass_(x.robotClass_), tserver_(x.tserver_),
-server_(x.server_)
+goals(x.goals), robot_(x.robot_), tserver_(x.tserver_), server_(x.server_)
{
InitializeCriticalSection(&CriticalSection);
simulationThread_ = (HANDLE)_beginthread(simulate, 0, (void *) this);
@@ -300,34 +297,11 @@
tserver_ = tserver;
}
-string Robot::getName() const {return name_;}
+string Robot::getName() const {return robot_.name;}
-Robot::RobotClass Robot::getClass() const {return robotClass_;}
+TeamTalk::Robot::RobotClass Robot::getClass() const {return robot_.robotClass;}
const Pose<float>& Robot::getPose() const {return pose;}
const Point<float>& Robot::getLocation() const {return pose.loc;}
float Robot::getYaw() const {return pose.yaw;}
-istream& operator>>(istream& in, Robot::RobotClass& robotClass) {
- string s;
- if((in >> s).fail()) return in;
- if(s == "safe" || s == "SAFE") robotClass = Robot::SAFE;
- else if(s == "dangerous" || s == "DANGEROUS") robotClass = Robot::DANGEROUS;
- else in.setf(ios::failbit);
- return in;
-}
-
-ostream& operator<<(ostream& out, const Robot::RobotClass& robotClass) {
- switch(robotClass) {
- case Robot::SAFE:
- out << "safe";
- break;
- case Robot::DANGEROUS:
- out << "dangerous";
- break;
- default:
- error << "unknown robot class: " << (int)robotClass << endl;
- out << (int)robotClass;
- }
- return out;
-}
Modified: TeamTalk/Agents/TeamTalkBackend/backendstub/robot.h
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/backendstub/robot.h 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/backendstub/robot.h 2006-12-11 03:21:42 UTC (rev 553)
@@ -5,6 +5,7 @@
#include <boeing_robot_server.h>
#include <boeing_trader_server.h>
#include <robot_packet2.h>
+#include <robot_class.h>
#include <map>
#include <queue>
@@ -35,9 +36,6 @@
};
class Robot {
-public:
- enum RobotClass {SAFE, DANGEROUS};
-
protected:
//current orientation and state
Pose<float> pose;
@@ -53,8 +51,7 @@
queue<Goal> goals; //queue of goals
//robot characteristics
- string name_;
- RobotClass robotClass_;
+ TeamTalk::Robot robot_;
//parameters common to all robots
static const float velocity; //bots have const velocity when moving
@@ -78,8 +75,7 @@
static void simulate_wander(void *thisp);
public:
- Robot(const string& name, RobotClass robotClass, Boeing::TraderServer* tserver,
- unsigned short port=0);
+ Robot(const TeamTalk::Robot& robot, Boeing::TraderServer* tserver);
Robot(const Robot& robot);
~Robot();
void callback(const Msg* msgp, const sockaddr* st_remote);
@@ -94,13 +90,10 @@
//getters
string getName() const;
- RobotClass getClass() const;
+ TeamTalk::Robot::RobotClass getClass() const;
const Pose<float>& getPose() const;
const Point<float>& getLocation() const;
float getYaw() const;
};
-istream& operator>>(istream& in, Robot::RobotClass& robotClass);
-ostream& operator<<(ostream& out, const Robot::RobotClass& robotClass);
-
#endif
\ No newline at end of file
Modified: TeamTalk/Agents/TeamTalkBackend/backendstub/robots.cpp
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/backendstub/robots.cpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/backendstub/robots.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -2,12 +2,12 @@
using namespace std;
-Robots::Robots() : vector<Robot>() {}
+Robots::Robots() : list<Robot*>() {}
const Robot* Robots::find(const string& name) const
{
for(const_iterator i = begin(); i != end(); i++) {
- if(i->getName() == name) return &*i;
+ if((*i)->getName() == name) return *i;
}
return NULL;
}
@@ -15,32 +15,32 @@
Robot* Robots::find(const string& name)
{
for(iterator i = begin(); i != end(); i++) {
- if(i->getName() == name) return &*i;
+ if((*i)->getName() == name) return *i;
}
return NULL;
}
-Robots Robots::find(Robot::RobotClass robotClass) const
+Robots Robots::find(TeamTalk::Robot::RobotClass robotClass) const
{
Robots classRobots;
for(const_iterator i = begin(); i != end(); i++) {
- if(i->getClass() == robotClass) classRobots.push_back(*i);
+ if((*i)->getClass() == robotClass) classRobots.push_back(*i);
}
return classRobots;
}
-const Robot* Robots::findFirst(Robot::RobotClass robotClass) const
+const Robot* Robots::findFirst(TeamTalk::Robot::RobotClass robotClass) const
{
for(const_iterator i = begin(); i != end(); i++) {
- if(i->getClass() == robotClass) return &*i;
+ if((*i)->getClass() == robotClass) return *i;
}
return NULL;
}
-Robot* Robots::findFirst(Robot::RobotClass robotClass)
+Robot* Robots::findFirst(TeamTalk::Robot::RobotClass robotClass)
{
for(iterator i = begin(); i != end(); i++) {
- if(i->getClass() == robotClass) return &*i;
+ if((*i)->getClass() == robotClass) return *i;
}
return NULL;
}
Modified: TeamTalk/Agents/TeamTalkBackend/backendstub/robots.h
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/backendstub/robots.h 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/backendstub/robots.h 2006-12-11 03:21:42 UTC (rev 553)
@@ -7,17 +7,19 @@
#include "robot.h"
+#include <list>
+
using namespace std;
-class Robots : public vector<Robot> {
+class Robots : public list<Robot*> {
public:
Robots();
const Robot* find(const string& name) const;
Robot* find(const string& name);
- Robots find(Robot::RobotClass robotClass) const;
- const Robot* findFirst(Robot::RobotClass robotClass) const;
- Robot* findFirst(Robot::RobotClass robotClass);
+ Robots find(TeamTalk::Robot::RobotClass robotClass) const;
+ const Robot* findFirst(TeamTalk::Robot::RobotClass robotClass) const;
+ Robot* findFirst(TeamTalk::Robot::RobotClass robotClass);
};
#endif
\ No newline at end of file
Modified: TeamTalk/Agents/TeamTalkBackend/robot-galaxy_adapter.cpp
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/robot-galaxy_adapter.cpp 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/robot-galaxy_adapter.cpp 2006-12-11 03:21:42 UTC (rev 553)
@@ -38,7 +38,7 @@
Frobotips >> voice >> safeness;
ignoreToEndOfLine(Frobotips);
info << "adding robot@" << rip << ':' << port << ' ' << voice << ' ' << safeness << endl;
- if(!r_client->addRobot(rname, rip, voice, safeness, port)) {
+ if(!r_client->addRobot(rname, rip, voice, safeness, port, this)) {
error << "couldn't add " << rname << endl;
}
}
@@ -154,10 +154,9 @@
{
GalaxyRobots* me = (GalaxyRobots*)p;
Boeing::MapClient* m_client = me->m_client;
- for(;;) {
+ for(int t=0;;) {
const Boeing::MsgMapClient* msg = m_client->getNextMessage();
if(!msg) {
- debug << "msgmapclient is null" << endl;
Sleep(1000);
continue;
}
@@ -189,37 +188,16 @@
}
}
-void GalaxyRobots::trackbots(void *p)
+void GalaxyRobots::trackmap(void *thisp)
{
- GalaxyRobots* me = (GalaxyRobots*)p;
- TeamTalk::RobotsClient* r_client = me->r_client;
+ GalaxyRobots* me = (GalaxyRobots*)thisp;
Boeing::MapClient* m_client = me->m_client;
- debug << "starting trackbots thread" << endl;
+ debug << "starting trackmap thread" << endl;
for(int t=0; true; t++) {
Sleep(1000);
- for(TeamTalk::RobotsClient::iterator i = r_client->begin();
- i != r_client->end(); i++) {
- debug << "sending location request" << endl;
- if(!i->sendReqLocation()) {
- //maybe we're not connected anymore, try to connect
- warn << "we appear to be disconnected...trying to reconnect" << endl;
- if(i->reopen()) {
- warn << "ok, reconnected to "
- << i->getHost() << ':' << i->getPort() << endl;
- i->sendReqLocation();
- } else {
- warn << "couldn't reconnect to "
- << i->getHost() << ':' << i->getPort() << endl;
- }
- }
- }
if(!(t%3)) { //send map keepalive every 3 seconds
m_client->sendKeepAlive(0);
}
- if(!(t%40) && !r_client->empty()) { //send image request every 40 seconds
- r_client->begin()->sendReqImage();
- //p_client->find("FESTO")->sendReqImage();
- }
}
}
@@ -247,50 +225,42 @@
{
skeleton_comm_ = NULL;
- {
- static char* *const OAS = {NULL};
- int i;
- //if (!GalUtil_OACheckUsage(argc, argv, OAS, &i)) exit(1);
- }
-
iTraderTaskId = rand();
- debug << "about to get the p_client" << endl;
- try {
+ debug << "about to get the p_client" << endl;
+ try {
//startup trader client
t_client = new Boeing::TraderClient();
- if(!t_client->isConnected()) {
- fatal << "optraderserver not found" << endl;
- } else {
- debug << "connected to trader" << endl;
- }
//startup map client
- m_client = new Boeing::MapClient();
+ m_client = new Boeing::MapClient();
//startup robot client
TeamTalk::RobotClient::spawnback = &Agent::spawnMinorGalaxy;
- r_client = new TeamTalk::RobotsClient("tester", this);
+ r_client = new TeamTalk::RobotsClient("tester");
processPeerfile("peerfile.txt");
- if(!testLastConfig("peerfile.txt",
+ if(!testLastConfig("peerfile.txt",
"..\\..\\Resources\\DecoderConfig\\LanguageModel\\zap2LM.arpa"))
- addRobotNamesToGrammar();
- if(!m_client->isConnected()) {
- fatal << "mapserver not found" << endl;
+ addRobotNamesToGrammar();
+ if(!m_client->isConnected()) {
+ fatal << "mapserver not found" << endl;
} else if(!m_client->sendSubscribe(0)) {
- fatal << "couldn't send subscribe message to map server" << endl;
- }
-
- //start listening threads
- _beginthread(traderlistener, 0, (void*) this);
- _beginthread(maplistener, 0, (void*) this);
-
- //start thread to track bots
- _beginthread(trackbots, 0, (void*) this);
+ fatal << "couldn't send subscribe message to map server" << endl;
+ } else {
+ _beginthread(maplistener, 0, (void*) this);
+ _beginthread(trackmap, 0, (void*) this);
+ }
+ if(!t_client->isConnected()) {
+ fatal << "optraderserver not found" << endl;
+ } else {
+ debug << "connected to trader" << endl;
+ //start listening threads
+ _beginthread(traderlistener, 0, (void*) this);
+ }
} catch(exception e) {
- fatal << "couldn't start GalaxyRobots: " << e.what() << ": " << endl;
+ fatal << "couldn't start GalaxyRobots: " << e.what() << ": " << endl;
throw e;
- }
+ }
}
GalaxyRobots::~GalaxyRobots() {
Modified: TeamTalk/Agents/TeamTalkBackend/robot-galaxy_adapter.h
===================================================================
--- TeamTalk/Agents/TeamTalkBackend/robot-galaxy_adapter.h 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/TeamTalkBackend/robot-galaxy_adapter.h 2006-12-11 03:21:42 UTC (rev 553)
@@ -21,9 +21,9 @@
//static bool testLastConfig(const string& source, const string& target);
- static void traderlistener(void *p);
- static void maplistener(void *p);
- static void trackbots(void *p);
+ static void traderlistener(void *thisp);
+ static void maplistener(void *thisp);
+ static void trackmap(void *thisp);
public:
GalaxyRobots(Gal_Server *s, int argc, char **argv);
Modified: TeamTalk/Agents/boeingLib/boeing/boeing_map_client.cc
===================================================================
--- TeamTalk/Agents/boeingLib/boeing/boeing_map_client.cc 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/boeingLib/boeing/boeing_map_client.cc 2006-12-11 03:21:42 UTC (rev 553)
@@ -151,7 +151,7 @@
debug("Got message rv %i, type %x len %u\n",rv,
rxdata.hdr.type,rxdata.hdr.len);
- if (rv!=rxdata.hdr.len) {
+ if (rv>0 && rv!=rxdata.hdr.len) {
printf("WARNING: size mismatch! %i, but packet says %i\n",
rv,rxdata.hdr.len);
} else {
Modified: TeamTalk/Agents/boeingLib/boeing/boeing_map_server.cc
===================================================================
--- TeamTalk/Agents/boeingLib/boeing/boeing_map_server.cc 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/boeingLib/boeing/boeing_map_server.cc 2006-12-11 03:21:42 UTC (rev 553)
@@ -76,7 +76,7 @@
int rv = sock->recv(rxdata.buff,sizeof(rxdata));
//printf("mapserver got %d bytes\n", rv);
- if (rv!=rxdata.hdr.len) {
+ if (rv>0 && rv!=rxdata.hdr.len) {
printf("WARNING: size mismatch! %i, but packet says %i\n",
rv,rxdata.hdr.len);
}
Modified: TeamTalk/Agents/boeingLib/boeing/boeing_robot_client.cc
===================================================================
--- TeamTalk/Agents/boeingLib/boeing/boeing_robot_client.cc 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/boeingLib/boeing/boeing_robot_client.cc 2006-12-11 03:21:42 UTC (rev 553)
@@ -274,7 +274,7 @@
const MsgRobot* RobotClient::getNextMessage()
{
// read your socket if there is something available
- // process it
+ // process it *NON-BLOCKING*
if (!isConnected()) {
printf("WARNING: Not connected\n");
@@ -292,7 +292,7 @@
printf("MEssage was too long!!! Playing it safe and ignoring it\n");
return NULL;
}
- if (rv!=rxdata.hdr.len) {
+ if (rv>0 && rv!=rxdata.hdr.len) {
printf("WARNING: size mismatch! %i, but packet says %i\n",
rv,rxdata.hdr.len);
}
Modified: TeamTalk/Agents/boeingLib/boeing/boeing_trader_client.cc
===================================================================
--- TeamTalk/Agents/boeingLib/boeing/boeing_trader_client.cc 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/boeingLib/boeing/boeing_trader_client.cc 2006-12-11 03:21:42 UTC (rev 553)
@@ -134,7 +134,7 @@
debug("Got message rv %i, type %x len %u\n",rv,
rxdata.hdr.type,rxdata.hdr.len);
- if (rv!=rxdata.hdr.len) {
+ if (rv>0 && rv!=rxdata.hdr.len) {
printf("WARNING: size mismatch! %i, but packet says %i\n",
rv,rxdata.hdr.len);
}
Modified: TeamTalk/Agents/boeingLib/boeing/boeing_trader_server.cc
===================================================================
--- TeamTalk/Agents/boeingLib/boeing/boeing_trader_server.cc 2006-12-08 18:00:28 UTC (rev 552)
+++ TeamTalk/Agents/boeingLib/boeing/boeing_trader_server.cc 2006-12-11 03:21:42 UTC (rev 553)
@@ -76,7 +76,7 @@
int rv = sock->recv(rxdata.buff,sizeof(rxdata));
//printf("traderserver got %d bytes\n", rv);
- if (rv!=rxdata.hdr.len) {
+ if (rv > 0 && rv!=rxdata.hdr.len) {
printf("WARNING: size mismatch! %i, but packet says %i\n",
rv,rxdata.hdr.len);
}
More information about the TeamTalk-developers
mailing list