HawkAgent.C

00001 // File: HawkAgent.C
00002 // Author: Josh Villbrandt <josh.villbrandt@usc.edu>
00003 // Date: April 2010
00004 
00005 #ifndef HAWKAGENT_C
00006 #define HAWKAGENT_C
00007 
00008 #include "Robots/BeoHawk/computer/HawkAgent.H"
00009 
00010 // ######################################################################
00011 HawkAgent::HawkAgent(std::string myName, int argc, char* argv[]) {
00012         // Help stuff
00013         helpTitle = "Generic HawkAgent";
00014         helpDescription = "A generic HawkAgent carries out tasks as a part of a network of agents that communicate through the ICE protocol.";
00015         helpOptions.push_back("\t--icestorm-ip\t(127.0.0.1) The IP of the IceStorm server.");
00016         helpOptions.push_back("\t--help\t\tLaunches this help section.");
00017         
00018         // Variable stuff
00019         itsName = myName;
00020         char * iceStormIP = (char *)"127.0.0.1";
00021         helpFlag = false;
00022         killAgent = false;
00023         firstRun = true;
00024         
00025         // Parse command line args
00026     for(int i = 0; i < argc && !helpFlag; i++) {
00027         if(strstr(argv[i], "--help") != NULL) {
00028                 helpFlag = true;
00029         }
00030         else if(argv[i][0] == '-' && argv[i][1] == '-' && (i+1) < argc) {
00031                 // Check for Ice Storm IP
00032                 if(strstr(argv[i], "--icestorm-ip") != NULL) iceStormIP = argv[i+1];
00033                 
00034                 // Load into parameterDefaults
00035                 std::string name = argv[i];
00036                 name = name.substr(2, name.size()-2);
00037                 Parameter p;
00038                 p.setValue(argv[i+1]);
00039                 parameterDefaults.insert(std::pair<std::string, Parameter>(name, p));
00040         }
00041     }
00042     
00043     if(!helpFlag) {
00044                 // Create Ice communicator
00045                 itsIc = Ice::initialize(argc, argv);
00046 
00047                 // Connect Ice communicator to open port
00048                 char buf[255];
00049                 bool connected = false;
00050                 int port = DEFAULT_STARTING_PORT;
00051                 while (!connected) {
00052                         try {
00053                                 sprintf(buf, "default -p %i", port);
00054                                 itsAdapter = itsIc->createObjectAdapterWithEndpoints(itsName, buf);
00055                                 connected = true;
00056                         } catch (Ice::SocketException) {
00057                                 port++;
00058                         }
00059                 }
00060 
00061                 // Store port
00062                 std::cout << "using port: " << port << std::endl;
00063                 std::stringstream ss;
00064                 ss << port;
00065                 itsName += ":" + ss.str();
00066         
00067                 // Connect to Ice Storm
00068                 itsObjectPrx = itsAdapter->add((Ice::ObjectPtr) this, itsIc->stringToIdentity(itsName));
00069                 sprintf(buf, "SimEvents/TopicManager:tcp -p 11111 -h %s -t %d", iceStormIP, DEFAULT_TIMEOUT);
00070                 Ice::ObjectPrx TopicMgrObjPrx = itsIc->stringToProxy(buf);
00071                 itsTopicManagerPrx = IceStorm::TopicManagerPrx::checkedCast(TopicMgrObjPrx);
00072         }
00073 }
00074 
00075 // ######################################################################
00076 HawkAgent::~HawkAgent() {
00077     /*if(itsIc) {
00078                 try {
00079                         std::cout << "waitForShutdown" << std::endl;
00080                     itsIc->waitForShutdown();
00081                         std::cout << "destroy" << std::endl;
00082                     itsIc->destroy();
00083                 } catch (const Ice::Exception& e) {}
00084     }*/
00085 }
00086 
00087 // ######################################################################
00088 bool HawkAgent::helpParameter() {
00089         return helpFlag;
00090 }
00091 
00092 // ######################################################################
00093 void HawkAgent::help() {
00094         std::cout << std::endl << "HELP: " << helpTitle << std::endl;
00095         std::cout << helpDescription << std::endl;
00096         
00097         std::cout << std::endl << "Options:" << std::endl;
00098         std::sort(helpOptions.begin(), helpOptions.end());
00099         for(int i = 0; i < (int)helpOptions.size(); i++) {
00100                 std::cout << helpOptions[i] << std::endl;
00101         }
00102         std::cout << std::endl;
00103 }
00104 
00105 // ######################################################################
00106 int HawkAgent::loadIntParameter(std::string name, int defaultValue) {
00107         std::map<std::string, Parameter>::iterator it;
00108         
00109         it = parameterDefaults.find(name);
00110         if(it != parameterDefaults.end()) return parameterDefaults[name].getIntValue();
00111         else return defaultValue;
00112 }
00113 
00114 // ######################################################################
00115 bool HawkAgent::loadBoolParameter(std::string name, bool defaultValue) {
00116         std::map<std::string, HawkAgent::Parameter>::iterator it;
00117         
00118         it = parameterDefaults.find(name);
00119         if(it != parameterDefaults.end()) return parameterDefaults[name].getBoolValue();
00120         else return defaultValue;
00121 }
00122 
00123 // ######################################################################
00124 double HawkAgent::loadDoubleParameter(std::string name, double defaultValue) {
00125         std::map<std::string, HawkAgent::Parameter>::iterator it;
00126         
00127         it = parameterDefaults.find(name);
00128         if(it != parameterDefaults.end()) return parameterDefaults[name].getDoubleValue();
00129         else return defaultValue;
00130 }
00131 
00132 // ######################################################################
00133 std::string HawkAgent::loadStringParameter(std::string name, std::string defaultValue) {
00134         std::map<std::string, Parameter>::iterator it;
00135         
00136         it = parameterDefaults.find(name);
00137         if(it != parameterDefaults.end()) return parameterDefaults[name].getStringValue();
00138         else return defaultValue;
00139 }
00140 
00141 // ######################################################################
00142 bool HawkAgent::parameterExists(std::string name) {
00143         std::map<std::string, Parameter>::iterator it;
00144         
00145         it = parameterDefaults.find(name);
00146         if(it != parameterDefaults.end()) return true;
00147         else return false;
00148 }
00149 
00150 // ######################################################################
00151 void HawkAgent::wakeUp() {
00152         //std::cout << "unlocking..." << std::endl;
00153         itsLock.post();
00154 }
00155 
00156 // ######################################################################
00157 bool HawkAgent::start() {
00158         if(helpFlag) {
00159                 help();
00160                 return true;
00161         }
00162         else {
00163                 try {
00164                         if(firstRun) {
00165                                 itsAdapter->activate();
00166                                 registerTopics();
00167                         }
00168         
00169                         while(!killAgent) {
00170                                 if(!firstRun) {
00171                                         //std::cout << "locking..." << std::endl;
00172                                         itsLock.wait();
00173                                 }
00174                 
00175                                 while(!killAgent && scheduler());
00176                         
00177                                 if(firstRun) firstRun = false;
00178                         }
00179                 
00180                         return true;
00181                 }
00182                 catch(IceUtil::NullHandleException) {
00183                         std::cout << "Error: agent cannot start because an error occured during initialization" << std::endl;
00184                         return false;
00185                 }
00186         }
00187 }
00188 
00189 // ######################################################################
00190 bool HawkAgent::stop() {
00191         if(!killAgent) {
00192                 killAgent = true;
00193                 return true;
00194         }
00195         else return false;
00196 }
00197 
00198 // ######################################################################
00199 bool HawkAgent::registerSubscription(std::string MessageTopic) {
00200         MessageTopic += "Topic";
00201     IceStorm::TopicPrx topic;
00202     IceStorm::QoS qos;
00203 
00204     try {
00205         topic = itsTopicManagerPrx->retrieve(MessageTopic.c_str());
00206 
00207         topic->subscribeAndGetPublisher(qos, itsObjectPrx);
00208 
00209         itsTopicSubscriptions.insert( make_pair(MessageTopic, topic) );
00210     } catch (const IceStorm::AlreadySubscribed&) {
00211         topic->unsubscribe(itsObjectPrx);
00212 
00213         topic = itsTopicManagerPrx->retrieve(MessageTopic.c_str());
00214         topic->subscribeAndGetPublisher(qos, itsObjectPrx);
00215         itsTopicSubscriptions.insert( make_pair(MessageTopic, topic) );
00216     }
00217     return true;
00218 }
00219 
00220 // ######################################################################
00221 bool HawkAgent::registerPublisher(std::string MessageTopic) {
00222         MessageTopic += "Topic";
00223     IceStorm::TopicPrx topic;
00224 
00225     try {
00226         topic = itsTopicManagerPrx->retrieve(MessageTopic.c_str());
00227     } catch (const IceStorm::NoSuchTopic&) {
00228         topic = itsTopicManagerPrx->create(MessageTopic.c_str());
00229     }
00230 
00231     Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00232     HawkMessages::MessageAgentPrx publisher = HawkMessages::MessageAgentPrx::uncheckedCast(pub);
00233     itsTopicPublishers.insert( make_pair(MessageTopic, publisher) );
00234 
00235     return true;
00236 }
00237 
00238 // ######################################################################
00239 bool HawkAgent::publish(std::string MessageTopic, HawkMessages::MessagePtr msg) {
00240         MessageTopic += "Topic";
00241     std::map<std::string, HawkMessages::MessageAgentPrx>::iterator iter = itsTopicPublishers.find(MessageTopic);
00242 
00243     if (iter == itsTopicPublishers.end()) return false;
00244 
00245     HawkMessages::MessageAgentPrx publisher = iter->second;
00246 
00247     publisher->catchMessage(msg);
00248 
00249     return true;
00250 }
00251 
00252 #endif
Generated on Sun May 8 08:41:20 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3