Homepage Demos Overview Downloads Tutorials 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 
00015 //! interfaces with wireless to pass information from the gui to the monitor
00016 int wmmonitorcmd_callback(char *buf, int bytes);
00017 class WMMonitorBehavior;
00018 
00019 //! so wmmonitorcmd_callback knows where to send the input from the GUI
00020 extern WMMonitorBehavior *wmMonitorBehavior;
00021 
00022 class WMitem_base;
00023 
00024 //! Listens to wmmonitor control commands coming in from the command port.
00025 class WMMonitorBehavior : public BehaviorBase {
00026  protected:
00027   //! The input command stream socket
00028   Socket *cmdsock;
00029 
00030  private:
00031   WMMonitorBehavior(const WMMonitorBehavior&); //!< don't call
00032   WMMonitorBehavior operator=(const WMMonitorBehavior&); //!< don't call
00033 
00034  public:
00035   //! constructor
00036   WMMonitorBehavior() :
00037     BehaviorBase(),
00038     cmdsock(NULL)
00039   { wmMonitorBehavior = this; }
00040   //! destructor
00041   virtual ~WMMonitorBehavior() { wmMonitorBehavior = NULL; }
00042 
00043   //! processes input from the GUI
00044   void registerData(const std::string& s);
00045 
00046   //! Finds the item with the corresponding name
00047   WMitem_base* find (std::string& s);
00048 
00049   //! Writes out the passed information to #cmdsock
00050   void report (const std::string& var_type,
00051                const std::string& var_name,
00052                const std::string& value);
00053 
00054   //! Writes out the passed information to #cmdsock
00055   void report (const std::string& var_type,
00056                const std::string& var_name,
00057                const char* value, int value_length);
00058 
00059   //! Writes out the passed information to #cmdsock
00060   void report(const char* var_type, int var_type_length,
00061               const char* var_name, int var_name_length,
00062               const char* value, int value_length);
00063 
00064   virtual void DoStart() {
00065     // Behavior startup
00066     BehaviorBase::DoStart();
00067     // Turn on wireless
00068     cmdsock=wireless->socket(SocketNS::SOCK_STREAM, 2048, 8192);
00069     wireless->setReceiver(cmdsock->sock, wmmonitorcmd_callback);
00070     wireless->setDaemon(cmdsock,true); 
00071     wireless->listen(cmdsock->sock, config->main.wmmonitor_port);
00072     // open gui
00073     // GUI is opened by separate behavior ViewWMVarsBehavior
00074     // until bug with clearing wmvars when restarting this is fixed
00075     erouter->addListener(this,EventBase::wmVarEGID);
00076   }
00077 
00078   virtual void DoStop() {
00079 //    Controller::closeGUI(getGUIType());
00080     erouter->forgetListener(this);
00081     // Close socket; turn wireless off
00082     wireless->setDaemon(cmdsock,false); 
00083     wireless->close(cmdsock);
00084     // Total behavior stop
00085     BehaviorBase::DoStop();
00086   }
00087 
00088   virtual void processEvent(const EventBase& e) {
00089     WMentry * entry = reinterpret_cast<WMentry*>(e.getSourceID());
00090     std::string s(entry->item_name);
00091     WMregistry *temp=entry->registry;
00092     while (temp!=&GlobalWM && temp!=NULL) {
00093       s=temp->name + "." + s;
00094       temp=temp->parent;
00095     }
00096     report(entry->type_name, s, entry->item->toString());
00097   }
00098 
00099   virtual unsigned int getPort() const { return config->main.wmmonitor_port; } //!< returns network port from config
00100 
00101   virtual std::string getName() const { return "Watchable Memory Monitor"; } //!< returns name of behavior
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 
00108 protected:
00109   static const unsigned int packet_wmclass=12; //!< magic id number
00110   //! writes packet type and timestamp
00111   inline static void encodeHeader(char **dst, unsigned int pformat) {
00112     encode(dst, pformat);
00113     encode(dst, get_time());
00114   }
00115   //! writes @a value to @a dst and advances @a dst
00116   template<class T>
00117   inline static void encode(char **dst, const T& value) {
00118     memcpy(*dst, &value, sizeof(T));
00119     // it'd be nice to use network byte order, but we'll save the aibo extra work
00120     //hostToNetwork(*dst, (char *)&value, sizeof(T));
00121     (*dst) += sizeof(T);
00122   }
00123 
00124   //! writes @a length bytes from @a src to @a dst
00125   template<class T>
00126   inline static void encode(char **dst, const T * src, int num) {
00127     memcpy(*dst, src, num*sizeof(T));
00128     (*dst) += num*sizeof(T);
00129   }
00130 
00131 
00132 };
00133 
00134 /*! @file
00135  * @brief Defines WMMonitorBehavior, which listens to commands from the Aibo3D gui and shows current state
00136  * @author alokl (Creator)
00137  *
00138  * $Author: ejt $
00139  * $Name: tekkotsu-2_0 $
00140  * $Revision: 1.6 $
00141  * $State: Exp $
00142  * $Date: 2004/01/19 20:35:31 $
00143  */
00144 
00145 #endif 

Tekkotsu v2.0
Generated Wed Jan 21 03:20:30 2004 by Doxygen 1.3.4