Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WMMonitorBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_WMMonitorBehavior_h_
00003 #define INCLUDED_WMMonitorBehavior_h_
00004 
00005 #include <iostream>
00006 #include "Wireless/Wireless.h"
00007 #include "Behaviors/BehaviorBase.h"
00008 #include "Motion/MotionManager.h"
00009 #include "Events/EventRouter.h"
00010 #include "Events/EventBase.h"
00011 #include "Shared/RobotInfo.h"
00012 #include "Behaviors/Controller.h"
00013 #include "Shared/WMclass.h"
00014 #include "Shared/Config.h"
00015 
00016 //! interfaces with wireless to pass information from the gui to the monitor
00017 int wmmonitorcmd_callback(char *buf, int bytes);
00018 class WMMonitorBehavior;
00019 
00020 //! so wmmonitorcmd_callback knows where to send the input from the GUI
00021 extern WMMonitorBehavior *wmMonitorBehavior;
00022 
00023 class WMitem_base;
00024 
00025 //! Listens to wmmonitor control commands coming in from the command port.
00026 class WMMonitorBehavior : public BehaviorBase {
00027  protected:
00028   //! The input command stream socket
00029   Socket *cmdsock;
00030 
00031  private:
00032   WMMonitorBehavior(const WMMonitorBehavior&); //!< don't call
00033   WMMonitorBehavior operator=(const WMMonitorBehavior&); //!< don't call
00034 
00035  public:
00036   //! constructor
00037   WMMonitorBehavior() :
00038     BehaviorBase("WMMonitorBehavior"),
00039     cmdsock(NULL)
00040   { wmMonitorBehavior = this; }
00041   //! destructor
00042   virtual ~WMMonitorBehavior() { wmMonitorBehavior = NULL; }
00043 
00044   //! processes input from the GUI
00045   void registerData(const std::string& s);
00046 
00047   //! Finds the item with the corresponding name
00048   WMitem_base* find (std::string& s);
00049 
00050   //! Writes out the passed information to #cmdsock
00051   void report (const std::string& var_type,
00052                const std::string& var_name,
00053                const std::string& value);
00054 
00055   //! Writes out the passed information to #cmdsock
00056   void report (const std::string& var_type,
00057                const std::string& var_name,
00058                const char* value, int value_length);
00059 
00060   //! Writes out the passed information to #cmdsock
00061   void report(const char* var_type, int var_type_length,
00062               const char* var_name, int var_name_length,
00063               const char* value, int value_length);
00064 
00065   virtual void doStart() {
00066     // Behavior startup
00067     BehaviorBase::doStart();
00068     // Turn on wireless
00069     cmdsock=wireless->socket(Socket::SOCK_STREAM, 2048, 8192);
00070     wireless->setReceiver(cmdsock->sock, wmmonitorcmd_callback);
00071     wireless->setDaemon(cmdsock,true); 
00072     wireless->listen(cmdsock->sock, config->main.wmmonitor_port);
00073     // open gui
00074     // GUI is opened by separate behavior ViewWMVarsBehavior
00075     // until bug with clearing wmvars when restarting this is fixed
00076     erouter->addListener(this,EventBase::wmVarEGID);
00077   }
00078 
00079   virtual void doStop() {
00080 //    Controller::closeGUI(getGUIType());
00081     erouter->removeListener(this);
00082     // Close socket; turn wireless off
00083     wireless->setDaemon(cmdsock,false); 
00084     wireless->close(cmdsock);
00085     // Total behavior stop
00086     BehaviorBase::doStop();
00087   }
00088 
00089   virtual void doEvent() {
00090     WMentry * entry = reinterpret_cast<WMentry*>(event->getSourceID());
00091     std::string s(entry->item_name);
00092     WMregistry *temp=entry->registry;
00093     while (temp!=&GlobalWM && temp!=NULL) {
00094       s=temp->name + "." + s;
00095       temp=temp->parent;
00096     }
00097     report(entry->type_name, s, entry->item->toString());
00098   }
00099 
00100   virtual unsigned int getPort() const { return config->main.wmmonitor_port; } //!< returns network port from config
00101 
00102   static std::string getClassDescription() { 
00103     char tmp[20];
00104     sprintf(tmp,"%d",*config->main.wmmonitor_port);
00105     return std::string("Bidirectional control communication with WMMonitor on port ")+tmp;
00106   }
00107   virtual std::string getDescription() const { return getClassDescription(); }
00108 
00109 protected:
00110   static const unsigned int packet_wmclass=14; //!< magic id number, corresponds to Listener.java PACKET_WMCLASS
00111   //! writes packet type and timestamp
00112   inline static void encodeHeader(char **dst, unsigned int pformat) {
00113     encode(dst, pformat);
00114     encode(dst, get_time());
00115   }
00116   //! writes @a value to @a dst and advances @a dst
00117   template<class T>
00118   inline static void encode(char **dst, const T& value) {
00119     memcpy(*dst, &value, sizeof(T));
00120     // it'd be nice to use network byte order, but we'll save the aibo extra work
00121     //hostToNetwork(*dst, (char *)&value, sizeof(T));
00122     (*dst) += sizeof(T);
00123   }
00124 
00125   //! writes @a length bytes from @a src to @a dst
00126   template<class T>
00127   inline static void encode(char **dst, const T * src, int num) {
00128     memcpy(*dst, src, num*sizeof(T));
00129     (*dst) += num*sizeof(T);
00130   }
00131 
00132 
00133 };
00134 
00135 /*! @file
00136  * @brief Defines WMMonitorBehavior, which listens to commands from the Aibo3D gui and shows current state
00137  * @author alokl (Creator)
00138  */
00139 
00140 #endif 

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:53 2016 by Doxygen 1.6.3