Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WMMonitorBehavior.cc

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

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