SimpleRobotBrainComponent.C

00001 #include "SimpleRobotBrainComponent.H"
00002 
00003 SimpleRobotBrainComponent::SimpleRobotBrainComponent(char * iceStormIP,
00004  char * myName, Ice::CommunicatorPtr ic) {
00005 
00006          //initialize the communicator (this is auto in normal ice):
00007          itsIc = ic;
00008 
00009          char buf[255];
00010          bool connected = false;
00011          int port = DEFAULT_STARTING_PORT;
00012 
00013          while (!connected) {
00014                 try {
00015                         sprintf(buf, "default -p %i", port);
00016                         itsAdapter = itsIc->createObjectAdapterWithEndpoints(myName, buf);
00017                         connected = true;
00018                 } catch (Ice::SocketException) {
00019                         port++;
00020                 }
00021          }
00022 
00023          itsObjectPrx = itsAdapter->add((Ice::ObjectPtr) this,
00024                                                                          itsIc->stringToIdentity(myName));
00025 
00026          sprintf(buf, "SimEvents/TopicManager:tcp -p 11111 -h %s -t %d",
00027                  iceStormIP, DEFAULT_TIMEOUT);
00028          Ice::ObjectPrx TopicMgrObjPrx = itsIc->stringToProxy(buf);
00029          itsTopicManagerPrx = IceStorm::TopicManagerPrx::checkedCast(TopicMgrObjPrx);
00030 }
00031 
00032 SimpleRobotBrainComponent::~SimpleRobotBrainComponent(){
00033 
00034         try {
00035                 itsIc->waitForShutdown();
00036                 itsIc->destroy();
00037         } catch (const Ice::Exception& e) {}
00038 }
00039 
00040 void SimpleRobotBrainComponent::start() {
00041         itsAdapter->activate();
00042 }
00043 
00044 bool SimpleRobotBrainComponent::registerSubscription(const std::string& MessageTopic) {
00045 
00046         IceStorm::TopicPrx topic;
00047 
00048         IceStorm::QoS qos;
00049 
00050         try {
00051                 topic = itsTopicManagerPrx->retrieve(MessageTopic.c_str());
00052 
00053                 topic->subscribeAndGetPublisher(qos, itsObjectPrx);
00054 
00055                 itsTopicSubscriptions.insert( make_pair(MessageTopic, topic) );
00056         } catch (const IceStorm::AlreadySubscribed&) {
00057                 topic->unsubscribe(itsObjectPrx);
00058 
00059                 topic = itsTopicManagerPrx->retrieve(MessageTopic.c_str());
00060                 topic->subscribeAndGetPublisher(qos, itsObjectPrx);
00061                 itsTopicSubscriptions.insert( make_pair(MessageTopic, topic) );
00062         }
00063         return true;
00064 }
00065 
00066 bool SimpleRobotBrainComponent::registerPublisher(const std::string& MessageTopic) {
00067 
00068         IceStorm::TopicPrx topic;
00069 
00070         try {
00071                 topic = itsTopicManagerPrx->retrieve(MessageTopic.c_str());
00072         } catch (const IceStorm::NoSuchTopic&) {
00073                 topic = itsTopicManagerPrx->create(MessageTopic.c_str());
00074         }
00075 
00076         Ice::ObjectPrx pub = topic->getPublisher()->ice_oneway();
00077         RobotSimEvents::EventsPrx publisher = RobotSimEvents::EventsPrx::uncheckedCast(pub);
00078         itsTopicPublishers.insert( make_pair(MessageTopic, publisher) );
00079 
00080         return true;
00081 }
00082 
00083 bool SimpleRobotBrainComponent::publish (const std::string& MessageTopic,
00084                 RobotSimEvents::EventMessagePtr msg) {
00085 
00086         std::map<std::string, RobotSimEvents::EventsPrx>::iterator iter =
00087                 itsTopicPublishers.find(MessageTopic);
00088 
00089         if (iter == itsTopicPublishers.end())
00090                 return false;
00091 
00092         RobotSimEvents::EventsPrx publisher = iter->second;
00093 
00094         publisher->updateMessage(msg);
00095 
00096         return true;
00097 }
00098 
Generated on Sun May 8 08:41:21 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3