WMMonitorBehavior.h
Go to the documentation of this file.00001
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
00017 int wmmonitorcmd_callback(char *buf, int bytes);
00018 class WMMonitorBehavior;
00019
00020
00021 extern WMMonitorBehavior *wmMonitorBehavior;
00022
00023 class WMitem_base;
00024
00025
00026 class WMMonitorBehavior : public BehaviorBase {
00027 protected:
00028
00029 Socket *cmdsock;
00030
00031 private:
00032 WMMonitorBehavior(const WMMonitorBehavior&);
00033 WMMonitorBehavior operator=(const WMMonitorBehavior&);
00034
00035 public:
00036
00037 WMMonitorBehavior() :
00038 BehaviorBase("WMMonitorBehavior"),
00039 cmdsock(NULL)
00040 { wmMonitorBehavior = this; }
00041
00042 virtual ~WMMonitorBehavior() { wmMonitorBehavior = NULL; }
00043
00044
00045 void registerData(const std::string& s);
00046
00047
00048 WMitem_base* find (std::string& s);
00049
00050
00051 void report (const std::string& var_type,
00052 const std::string& var_name,
00053 const std::string& value);
00054
00055
00056 void report (const std::string& var_type,
00057 const std::string& var_name,
00058 const char* value, int value_length);
00059
00060
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
00067 BehaviorBase::doStart();
00068
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
00074
00075
00076 erouter->addListener(this,EventBase::wmVarEGID);
00077 }
00078
00079 virtual void doStop() {
00080
00081 erouter->removeListener(this);
00082
00083 wireless->setDaemon(cmdsock,false);
00084 wireless->close(cmdsock);
00085
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; }
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;
00111
00112 inline static void encodeHeader(char **dst, unsigned int pformat) {
00113 encode(dst, pformat);
00114 encode(dst, get_time());
00115 }
00116
00117 template<class T>
00118 inline static void encode(char **dst, const T& value) {
00119 memcpy(*dst, &value, sizeof(T));
00120
00121
00122 (*dst) += sizeof(T);
00123 }
00124
00125
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
00136
00137
00138
00139
00140 #endif