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)
00030       wmreg=&GlobalWM;
00031     else {
00032       WMitem<WMregistry>* wmitem=static_cast<WMitem<WMregistry> *> (find (var));
00033       if (wmitem!=NULL)
00034         wmreg=&wmitem->get_value();
00035       else
00036         serr->printf("WMMonitorBehavior: Could not find '%s'\n",var.c_str());
00037     }
00038     if (wmreg==NULL)
00039       serr->printf("WMMonitorBehavior: wmreg is NULL\n");
00040     else {
00041       //sout->printf("Reporting:\n");
00042       for (std::vector<WMentry*>::const_iterator it = wmreg->entries.begin(); it != wmreg->entries.end(); it++) {
00043         WMentry* entry=*it;
00044         std::string sn(entry->item_name);
00045         WMregistry *temp=entry->registry;
00046         while (temp!=&GlobalWM && temp!=NULL) {
00047           sn=temp->name + "." + sn;
00048           temp=temp->parent;
00049         }
00050         //sout->printf("Reporting %s %s %s\n",entry->type_name.c_str(),sn.c_str(),entry->item->toString().c_str());
00051         report(entry->type_name, sn, entry->item->toString());
00052       }
00053     }
00054   } else if (cmd[0]=='d') {    // set debug mode (blocking/nonblocking)
00055     // implement within this class
00056   }
00057 }
00058 
00059 
00060 WMitem_base*
00061 WMMonitorBehavior::find (std::string& s) {
00062   WMregistry* wmreg=&GlobalWM;
00063   unsigned int pos=s.find('.');
00064   while (pos!=std::string::npos) {
00065     bool changed=false;
00066     std::string subreg=s.substr(0, pos);
00067     s=s.substr(pos+1);
00068     for (std::vector<WMentry*>::const_iterator it = wmreg->entries.begin(); it != wmreg->entries.end(); it++)
00069       if ( (*it)->item_name == subreg) {
00070         WMitem<WMregistry> const* wmitem=static_cast<WMitem<WMregistry> const*>((*it)->item);
00071         wmreg=&(wmitem->get_value());
00072         changed=true;
00073         break;
00074       }
00075     if (!changed) return NULL;
00076 
00077     pos=s.find('.');
00078   }
00079 
00080   for (std::vector<WMentry*>::const_iterator it = wmreg->entries.begin(); it != wmreg->entries.end(); it++)
00081     if ( (*it)->item_name == s)
00082       return (*it)->item;
00083   return NULL;
00084 }
00085 
00086 void
00087 WMMonitorBehavior::report (const std::string& var_type,
00088                            const std::string& var_name,
00089                            const std::string& value) {
00090   report (var_type.c_str(), var_type.length(),
00091           var_name.c_str(), var_name.length(),
00092           value.c_str(), value.length());
00093 }
00094 
00095 void
00096 WMMonitorBehavior::report (const std::string& var_type,
00097                            const std::string& var_name,
00098                            const char* value, int value_length) {
00099   report (var_type.c_str(), var_type.length(),
00100           var_name.c_str(), var_name.length(),
00101           value, value_length);
00102 }
00103 
00104 void
00105 WMMonitorBehavior::report (const char* var_type, int var_type_length,
00106                            const char* var_name, int var_name_length,
00107                            const char* value, int value_length) {
00108   char *buf=(char*)cmdsock->getWriteBuffer(5*sizeof(int)+var_type_length+var_name_length+value_length);
00109   if (buf) {
00110     encodeHeader(&buf, packet_wmclass);
00111     encode(&buf, var_type_length);
00112     encode(&buf, var_type, var_type_length);
00113     encode(&buf, var_name_length);
00114     encode(&buf, var_name, var_name_length);
00115     encode(&buf, value_length);
00116     encode(&buf, value, value_length);
00117     cmdsock->write(5*sizeof(int)+var_type_length+var_name_length+value_length);
00118   } else
00119     serr->printf("WMMonitorBehavior: Failed to get write buffer\n");
00120 }
00121 
00122 int wmmonitorcmd_callback(char *buf, int bytes) {
00123   std::string s(buf, bytes);
00124   if (wmMonitorBehavior==NULL) return 0;
00125   static std::string incomplete;
00126                                                                                 
00127   //pass a line at a time to the controller
00128   while(s.size()>0) {
00129     unsigned int endline=s.find('\n');
00130     if(endline==std::string::npos) {
00131       incomplete+=s;
00132       return 0;
00133     }
00134     incomplete+=s.substr(0,endline);
00135     //is now complete:
00136     wmMonitorBehavior->registerData(incomplete);
00137     incomplete.erase();
00138     s=s.substr(endline+1);
00139   }
00140                                                                                 
00141   return 0;
00142 }
00143 
00144 /*! @file
00145  * @brief Defines WMMonitorBehavior, which listens to commands from the Aibo3D gui and shows current state
00146  * @author alokl (Creator)
00147  *
00148  * $Author: ejt $
00149  * $Name: tekkotsu-2_2_2 $
00150  * $Revision: 1.3 $
00151  * $State: Exp $
00152  * $Date: 2004/04/16 20:17:22 $
00153  */

Tekkotsu v2.2.2
Generated Tue Jan 4 15:43:16 2005 by Doxygen 1.4.0