Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SpiderMachineBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SpiderMachineBehavior_h_
00003 #define INCLUDED_SpiderMachineBehavior_h_
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include <queue>
00007 
00008 class StateNode;
00009 
00010 //! When active and connected over network socket, outputs structure of requested state machine(s)
00011 /*! The protocol is:
00012  *  - '<tt>list</tt>' - send list of all instantiated StateNodes
00013  *  - '<tt>spider </tt><i>name</i>' - spider the current structure of StateNode named <i>name</i>
00014  *  - '<tt>listen </tt><i>name</i>' - send updates regarding the activation status of <i>name</i> and its subnodes; you can specify a state which is not yet running
00015  *  - '<tt>ignore </tt><i>name</i>' - cancels a previous listen command
00016  *  - '<tt>clear</tt>' - cancels all previous listen commands; should be called at the beginning or end of each connection, preferably both
00017  *  
00018  *  Each of those commands should be terminated with a newline -
00019  *  i.e. one command per line
00020  *
00021  *  After a <tt>list</tt> command, the first line will be the number
00022  *  of StateNodes, followed by that number of lines, one StateNode
00023  *  name per line.
00024  *
00025  *  After a <tt>spider</tt> command, an XML description of the model
00026  *  will be sent.  If no matching StateNode is found, an warning will
00027  *  be displayed on #serr, and an empty model
00028  *  ("<model></model>") returned over the network
00029  *  connection.
00030  *
00031  *  All other commands give no direct response - listen can be
00032  *  executed before the specified StateNode is yet running, and ignore
00033  *  doesn't care whether or not the specified StateNode was actually
00034  *  being listened for.
00035  *
00036  *  The format of the model is:
00037  @verbatim
00038  <!DOCTYPE model [
00039  <!ELEMENT model (state*, transition*)>
00040  <!ELEMENT state (state*, transition*)>
00041  <!ELEMENT transition (source+, dest+)>
00042  <!ELEMENT source (#PCDATA)>
00043  <!ELEMENT dest (#PCDATA)>
00044  
00045  <!ATTLIST state id CDATA #REQUIRED>
00046  <!ATTLIST state class CDATA #REQUIRED>
00047  <!ATTLIST transition id CDATA #REQUIRED>
00048  <!ATTLIST transition class CDATA #REQUIRED>
00049  ]>@endverbatim
00050  *
00051  *  The format of status updates following a listen command is:
00052  @verbatim
00053  <!DOCTYPE event [
00054  <!ELEMENT event (fire*, statestart*, statestop*)>
00055  <!ELEMENT fire (EMPTY)>
00056  <!ELEMENT statestart (EMPTY)>
00057  <!ELEMENT statestop (EMPTY)>
00058 
00059  <!ATTLIST fire id CDATA #REQUIRED>
00060  <!ATTLIST fire time CDATA #REQUIRED>
00061  <!ATTLIST statestart id CDATA #REQUIRED>
00062  <!ATTLIST statestart time CDATA #REQUIRED>
00063  <!ATTLIST statestop id CDATA #REQUIRED>
00064  <!ATTLIST statestop time CDATA #REQUIRED>
00065  ]>@endverbatim
00066 */
00067 class SpiderMachineBehavior : public BehaviorBase {
00068 public: 
00069   //! Points to the one SpiderMachineBehavior object that the input command stream is talking to.
00070   /*! A kludge. Dunno how you're gonna make sure you're not using this uninitialized. */
00071   static SpiderMachineBehavior * theOne;
00072   static unsigned int port; //!< the port to listen on (10081 by default)
00073   static int callback(char *buf, int bytes); //!< called by wireless when there's new data
00074 
00075 public:
00076   //! constructor
00077   SpiderMachineBehavior() : BehaviorBase("SpiderMachineBehavior"), cmdsock(NULL), expected(), listen(), queuedEvents() {}
00078 
00079   virtual void DoStart();
00080   virtual void DoStop();
00081   virtual void processEvent(const EventBase& e);
00082 
00083   //! dumps all of the transitions and subnodes of a given statenode
00084   void spider(const StateNode* n, unsigned int depth=0);
00085 
00086   //! returns true iff @a n or one of its parents is found in #listen
00087   bool isListening(const StateNode* n);
00088 
00089   static std::string getClassDescription() {
00090     char tmp[20];
00091     sprintf(tmp,"%d",port);
00092     return std::string("When active and connected over network socket on port ")+tmp
00093       +std::string(", outputs structure of requested state machine(s)");
00094   }
00095   virtual std::string getDescription() const { return getClassDescription(); }
00096   
00097   //! parses commands sent from callback()
00098   void runCommand(const std::string& s);
00099 
00100 protected:
00101   //!just to prettify the data sent out - probably should make this a null-op to save bandwidth after debugging is done
00102   void indent(unsigned int level);
00103 
00104   //!searches currently instantiated StateNodes to find the one named @a name
00105   const StateNode * find(const std::string& name);
00106 
00107   class Socket *cmdsock; //!< the socket for communication
00108 
00109   typedef std::set<BehaviorBase*> registry_t; //!< the type of the behavior registry (BehaviorBase::registry)
00110   typedef std::multiset<const StateNode*> expected_t; //!< the type of #expected
00111   typedef std::set<std::string> listen_t; //!< the type of #listen
00112   typedef std::queue<EventBase> queuedEvents_t; //!< the type of #queuedEvents
00113 
00114   expected_t expected; //!< a set of behaviors which are involved with an impending transition - their next stateMachineEGID event should be ignored
00115 
00116   listen_t listen; //!< a set of state machine names which should have their subnodes monitored
00117 
00118   queuedEvents_t queuedEvents; //!< used if a transition causes other transitions, those transitions need to be remembered
00119 
00120 private:
00121   SpiderMachineBehavior(const SpiderMachineBehavior&); //!< don't call
00122   SpiderMachineBehavior operator=(const SpiderMachineBehavior&); //!< don't call
00123 };
00124 
00125 /*! @file
00126  * @brief Defines SpiderMachineBehavior, which active and connected over network socket, outputs structure of requested state machine(s)
00127  * @author ejt (Creator)
00128  *
00129  * $Author: ejt $
00130  * $Name: tekkotsu-2_4_1 $
00131  * $Revision: 1.9 $
00132  * $State: Exp $
00133  * $Date: 2005/08/07 04:11:03 $
00134  */
00135 
00136 #endif

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:49 2005 by Doxygen 1.4.4