00001 // File: HawkAgent.C 00002 // Author: Josh Villbrandt <josh.villbrandt@usc.edu> 00003 // Date: April 2010 00004 00005 #ifndef HAWKAGENT_H 00006 #define HAWKAGENT_H 00007 00008 #include <string> 00009 #include <sstream> 00010 #include <map> 00011 #include <algorithm> 00012 #include <Ice/Ice.h> 00013 #include <IceStorm/IceStorm.h> 00014 #include "Util/Semaphore.H" 00015 #include "Robots/BeoHawk/core/HawkMessages.ice.H" 00016 #include "Ice/ImageIce.ice.H" // this is a hack to force ImageIce to link correctly for HawkMessages.ice 00017 00018 #define DEFAULT_STARTING_PORT 4295 00019 #define DEFAULT_TIMEOUT 1000 00020 00021 class HawkAgent : public HawkMessages::MessageAgent 00022 { 00023 public: 00024 HawkAgent(std::string myName, int argc, char* argv[]); 00025 ~HawkAgent(); 00026 bool start(); 00027 bool stop(); 00028 void wakeUp(); 00029 00030 // Implement these 00031 virtual bool scheduler() { return false; }; 00032 virtual void registerTopics() {}; 00033 virtual void catchMessage(const HawkMessages::MessagePtr& msg, const Ice::Current&) {}; 00034 00035 protected: 00036 // Use these 00037 std::string itsName; 00038 bool registerSubscription(std::string MessageTopic); 00039 bool registerPublisher(std::string MessageTopic); 00040 bool publish(std::string MessageTopic, HawkMessages::MessagePtr msg); 00041 00042 // Parameter stuff 00043 class Parameter { 00044 public: 00045 void setValue(std::string set) { 00046 value = set; 00047 } 00048 int getIntValue() { 00049 std::istringstream buffer(value); 00050 int temp; 00051 buffer >> temp; 00052 return temp; 00053 } 00054 bool getBoolValue() { 00055 if(value == "1" || value == "TRUE" || value == "true") 00056 return true; 00057 else return false; 00058 } 00059 double getDoubleValue() { 00060 std::istringstream buffer(value); 00061 double temp; 00062 buffer >> temp; 00063 return temp; 00064 } 00065 std::string getStringValue() { 00066 return value; 00067 } 00068 private: 00069 std::string value; 00070 }; 00071 std::map<std::string, Parameter> parameterDefaults; 00072 int loadIntParameter(std::string name, int defaultValue); 00073 bool loadBoolParameter(std::string name, bool defaultValue); 00074 double loadDoubleParameter(std::string name, double defaultValue); 00075 std::string loadStringParameter(std::string name, std::string defaultValue); 00076 bool parameterExists(std::string name); 00077 00078 // Help stuff 00079 std::string helpTitle; 00080 std::string helpDescription; 00081 std::vector<std::string> helpOptions; 00082 bool helpParameter(); 00083 00084 private: 00085 bool killAgent; 00086 bool firstRun; 00087 bool helpFlag; 00088 void help(); 00089 Semaphore itsLock; 00090 Ice::CommunicatorPtr itsIc; 00091 Ice::ObjectAdapterPtr itsAdapter; 00092 Ice::ObjectPrx itsObjectPrx; 00093 IceStorm::TopicManagerPrx itsTopicManagerPrx; 00094 std::map<std::string, IceStorm::TopicPrx> itsTopicSubscriptions; 00095 std::map<std::string, HawkMessages::MessageAgentPrx> itsTopicPublishers; 00096 }; 00097 00098 #endif