RobotBrainComponent.H

00001 #ifndef ROBOTBRAINCOMPONENT_H
00002 #define ROBOTBRAINCOMPONENT_H
00003 
00004 #include "Component/ModelComponent.H"
00005 #include "Component/ModelParam.H"
00006 #include "Component/ModelManager.H"
00007 #include "Component/OptionManager.H"
00008 
00009 #include "Ice/RobotBrainObjects.ice.H"
00010 #include "Ice/RobotSimEvents.ice.H"
00011 
00012 #include <Ice/Ice.h>
00013 #include <IceUtil/Thread.h>
00014 #include <IceStorm/IceStorm.h>
00015 
00016 #include <string>
00017 #include <map>
00018 
00019 class RobotBrainComponent : public ModelComponent, public RobotSimEvents::Events, public IceUtil::Thread
00020 {
00021   public:
00022     //! Robot Brain Component Constructor: Simply calls the ModelComponent constructor.
00023     //! Make sure that you include this constructor in your inherited class' initialization list
00024     //! Also, make sure that you use a unique tagName for each object that you want to add
00025     RobotBrainComponent(OptionManager& mgr,
00026         const std::string& descrName = "GenericRobotBrainComponent",
00027         const std::string& tagName = "GenericRobotBrainComponent");
00028 
00029     virtual ~RobotBrainComponent(){};
00030 
00031     //! Init: Sets up all of the Ice runtime details, and ends with a call to registerTopics
00032     void init(Ice::CommunicatorPtr ic, Ice::ObjectAdapterPtr adapter);
00033 
00034     //! Register Subscription: Register this object as a subscriber to a specified topic
00035     bool registerSubscription(const std::string& MessageTopic);
00036 
00037     //! Register Publisher: Register this object as a publisher of a specified topic
00038     void registerPublisher(const std::string& MessageTopic);
00039 
00040     //! Publish: Send out a message to all subscribers
00041     bool publish(const::std::string& MessageTopic, RobotSimEvents::EventMessagePtr msg);
00042 
00043     //! Start2: Overload of the ModelComponent start2 - don't mess with this
00044     void start2();
00045 
00046     //! Stop1: Overload of the ModelComponent stop1 - don't mess with this
00047     void stop1();
00048 
00049     //! Run: This function gets forked into a new thread once the ModelManager is started. By default, it calls the evolve function.
00050     //! It will likely become non-virtual in the future, so please put all run-time functionality into evolve
00051     virtual void run();
00052 
00053                 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
00054                 //
00055                 // USERS: Below are the functions you should overload to make your own RobotBrainComponents.
00056                 //
00057                 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
00058 
00059     //! Register Topics: This function is called at the end of init, and is virtual so that is can be overloaded
00060     //!                                                                  by chid classes. In your overloaded version you should put all of the registerSubsciption
00061                 //!                                                             and registerPublisher calls that you would like to happen automatically after init.
00062     virtual void registerTopics() {};
00063 
00064     //! Evolve: Gets called by run in a while(1) loop. Anything that you want to happen continuously should go in here
00065     virtual void evolve() {};
00066 
00067     //! Update Message: Gets called by the Ice runtime when a publisher of a message that your object subscribes
00068                 //!                                                                        to publishes a message.
00069     virtual void updateMessage(const RobotSimEvents::EventMessagePtr& eMsg,
00070         const Ice::Current&) {};
00071 
00072                 //! start3: Gets called immediately after start2, which is after all of the subcomponents have been started.
00073                 //!         If you have any code that needs to run only once at the beginning before the general run/evolve
00074                 //!                                        loop, but after the constructor, then you can put it here.
00075                 virtual void start3() {};
00076 
00077   protected:
00078                 //A unique name for this instance of RobotBrainComponent
00079     OModelParam<std::string>              itsIceIdentity;
00080                 //The IP of the IceStorm server
00081     OModelParam<std::string>              itsIcestormIP;
00082                 //Time to wait before giving up trying to connect to IceStorm
00083     OModelParam<int>                      itsConnectionTimeout;
00084                 //The amount of microseconds that run() will wait between subsequent evolve() calls.
00085     int itsEvolveSleepTime;
00086 
00087   private:
00088                 //Ice connection details
00089     Ice::CommunicatorPtr                  itsIcPtr;
00090     Ice::ObjectAdapterPtr                 itsAdapterPtr;
00091     Ice::ObjectPrx                        itsObjectPrx;
00092     IceStorm::TopicManagerPrx             itsTopicManagerPrx;
00093 
00094     //A map to hold all of our topic subscriptions
00095     std::map<std::string, IceStorm::TopicPrx>  itsTopicSubscriptions;
00096 
00097     //A map to hold all of our topic publishers
00098     std::map<std::string, RobotSimEvents::EventsPrx>  itsTopicPublishers;
00099 
00100     bool itsAlive;
00101 
00102   std::string itsTagName;
00103 
00104 
00105 };
00106 
00107 #endif
00108 
Generated on Sun May 8 08:41:32 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3