Homepage Demos Overview Downloads Tutorials Reference
Credits

WMMonitorBehavior.cc

Go to the documentation of this file.
00001 #include "WMMonitorBehavior.h"
00002 #include "Shared/WMclass.h"
00003 
00004 //! so wmmonitorcmd_callback knows where to send the input from the GUI
00005 WMMonitorBehavior *wmMonitorBehavior = NULL;
00006 
00007 void
00008 WMMonitorBehavior::registerData(const std::string& s) {
00009   if (s.size()==0) return;
00010   unsigned int pos;
00011 
00012   pos=s.find(' ');
00013   std::string cmd=s.substr(0,pos);
00014   std::string var=s.substr(pos+1, s.length());
00015 
00016   if (cmd[0]=='w') {           // enable watch on WMitem
00017     WMitem_base* wmitem=find (var);
00018     if (wmitem!=NULL) wmitem->watch();
00019   } else if (cmd[0]=='s') {    // disable watch on WMitem
00020     WMitem_base* wmitem=find (var);
00021     if (wmitem!=NULL) wmitem->unwatch();
00022   } else if (cmd[0]=='x') {    // examing a WMitem
00023     WMitem_base* wmitem=find (var);
00024     if (wmitem!=NULL)
00025       report(wmitem->entry->type_name, wmitem->entry->item_name,
00026           wmitem->toString());
00027   } else if (cmd[0]=='r') {
00028     WMregistry* wmreg=NULL;
00029     if (var.length()==0) wmreg=&GlobalWM;
00030     else {
00031       WMitem<WMregistry>* wmitem=static_cast<WMitem<WMregistry> *> (find (var));
00032       if (wmitem!=NULL)
00033         wmreg=&wmitem->get_value();
00034     }
00035     if (wmreg!=NULL)
00036       for (std::vector<WMentry*>::const_iterator it = wmreg->entries.begin();
00037           it != wmreg->entries.end(); it++) {
00038         WMentry* entry=*it;
00039         std::string sn(entry->item_name);
00040         WMregistry *temp=entry->registry;
00041         while (temp!=&GlobalWM && temp!=NULL) {
00042           sn=temp->name + "." + sn;
00043           temp=temp->parent;
00044         }
00045         report(entry->type_name, sn, entry->item->toString());
00046       }
00047   } else if (cmd[0]=='d') {    // set debug mode (blocking/nonblocking)
00048     // implement within this class
00049   }
00050 }
00051 
00052 
00053 WMitem_base*
00054 WMMonitorBehavior::find (std::string& s) {
00055   WMregistry* wmreg=&GlobalWM;
00056   unsigned int pos=s.find('.');
00057   while (pos!=std::string::npos) {
00058     bool changed=false;
00059     std::string subreg=s.substr(0, pos);
00060     s=s.substr(pos+1);
00061     for (std::vector<WMentry*>::const_iterator it = wmreg->entries.begin(); it != wmreg->entries.end(); it++)
00062       if ( (*it)->item_name == subreg) {
00063         WMitem<WMregistry> const* wmitem=static_cast<WMitem<WMregistry> const*>((*it)->item);
00064         wmreg=&(wmitem->get_value());
00065         changed=true;
00066         break;
00067       }
00068     if (!changed) return NULL;
00069 
00070     pos=s.find('.');
00071   }
00072 
00073   for (std::vector<WMentry*>::const_iterator it = wmreg->entries.begin(); it != wmreg->entries.end(); it++)
00074     if ( (*it)->item_name == s)
00075       return (*it)->item;
00076   return NULL;
00077 }
00078 
00079 void
00080 WMMonitorBehavior::report (const std::string& var_type,
00081                            const std::string& var_name,
00082                            const std::string& value) {
00083   report (var_type.c_str(), var_type.length(),
00084           var_name.c_str(), var_name.length(),
00085           value.c_str(), value.length());
00086 }
00087 
00088 void
00089 WMMonitorBehavior::report (const std::string& var_type,
00090                            const std::string& var_name,
00091                            const char* value, int value_length) {
00092   report (var_type.c_str(), var_type.length(),
00093           var_name.c_str(), var_name.length(),
00094           value, value_length);
00095 }
00096 
00097 void
00098 WMMonitorBehavior::report (const char* var_type, int var_type_length,
00099                            const char* var_name, int var_name_length,
00100                            const char* value, int value_length) {
00101   char *buf=(char*)cmdsock->getWriteBuffer(5+ var_type_length+
00102                                               var_name_length+
00103                                               value_length);
00104   if (buf) {
00105     encodeHeader(&buf, packet_wmclass);
00106     encode(&buf, var_type_length);
00107     encode(&buf, var_type, var_type_length);
00108     encode(&buf, var_name_length);
00109     encode(&buf, var_name, var_name_length);
00110     encode(&buf, value_length);
00111     encode(&buf, value, value_length);
00112     cmdsock->write(5*sizeof(int)+var_type_length+var_name_length+value_length);
00113   }
00114 }
00115 
00116 int wmmonitorcmd_callback(char *buf, int bytes) {
00117   std::string s(buf, bytes);
00118   if (wmMonitorBehavior==NULL) return 0;
00119   static std::string incomplete;
00120                                                                                 
00121   //pass a line at a time to the controller
00122   while(s.size()>0) {
00123     unsigned int endline=s.find('\n');
00124     if(endline==std::string::npos) {
00125       incomplete+=s;
00126       return 0;
00127     }
00128     incomplete+=s.substr(0,endline);
00129     //is now complete:
00130     wmMonitorBehavior->registerData(incomplete);
00131     incomplete.erase();
00132     s=s.substr(endline+1);
00133   }
00134                                                                                 
00135   return 0;
00136 }
00137 
00138 /*! @file
00139  * @brief Defines WMMonitorBehavior, which listens to commands from the Aibo3D gui and shows current state
00140  * @author alokl (Creator)
00141  *
00142  * $Author: ejt $
00143  * $Name: tekkotsu-2_0 $
00144  * $Revision: 1.2 $
00145  * $State: Exp $
00146  * $Date: 2004/01/19 20:35:31 $
00147  */

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