00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __GLOVE_COMM_COMMANDPROCESSOR_HPP_
00028 #define __GLOVE_COMM_COMMANDPROCESSOR_HPP_
00029
00030 #include "../config.hpp"
00031
00032 #include <string>
00033
00034 #include <boost/exception.hpp>
00035 #include <boost/bind.hpp>
00036 #include <boost/config/warning_disable.hpp>
00037 #include <boost/spirit/include/qi.hpp>
00038 #include <boost/spirit/include/phoenix.hpp>
00039
00040 #include "CommandInterface.hpp"
00041 #include "msg.hpp"
00042
00043 #define DEBUG
00044
00045 #ifdef DEBUG
00046 #include <iostream>
00047 #endif
00048
00049 namespace glove {
00050 namespace comm {
00051
00073 class API CommandProcessor {
00081 template<typename Iterator>
00082 struct CommandGrammar: boost::spirit::qi::grammar<Iterator, CommandInterface::result_type()> {
00083 boost::spirit::qi::rule<Iterator, CommandInterface::result_type()> start;
00084 boost::spirit::qi::rule<Iterator, bool()> bool_;
00085
00090 CommandGrammar(CommandInterface& ci) :
00091 CommandGrammar::base_type(start), cmdInterface(ci) {
00092
00093 using boost::spirit::eps;
00094 using boost::spirit::lit;
00095 using boost::spirit::int_;
00096 using boost::phoenix::bind;
00097 using boost::phoenix::construct;
00098 namespace arg = boost::spirit::arg_names;
00099
00100 bool_ = (lit("true") [arg::_val = true] | lit("false)") [arg::_val = false]);
00101
00102 CommandInterface::result_type v;
00103 start = eps [arg::_val = construct<CommandInterface::result_type>()] >>
00104 (
00105 lit("VTI ")
00106 ) | (
00107 lit(MAGIC) >>
00108 (
00109 lit("requestDeviceList()")
00110 [bind(&CommandInterface::requestDeviceList, &cmdInterface, arg::_val)]
00111 | (lit("connect(") >> int_ >> ')')
00112 [bind(&CommandInterface::connect, &cmdInterface, arg::_val, arg::_1)]
00113 | (lit("disconnect(") >> int_ >> ')')
00114 [bind(&CommandInterface::disconnect, &cmdInterface, arg::_val, arg::_1)]
00115 | (lit("isConnected(") >> int_ >> ')')
00116 [bind(&CommandInterface::isConnected, &cmdInterface, arg::_val, arg::_1)]
00117
00118 | (lit("numSensors(") >> int_ >> ')')
00119 [bind(&CommandInterface::numSensors, &cmdInterface, arg::_val, arg::_1)]
00120 | (lit("isRightHanded(") >> int_ >> ')')
00121 [bind(&CommandInterface::isRightHanded, &cmdInterface, arg::_val, arg::_1)]
00122 | (lit("isLeftHanded(") >> int_ >> ')')
00123 [bind(&CommandInterface::isLeftHanded, &cmdInterface, arg::_val, arg::_1)]
00124
00125 | (lit("getValue(") >> int_ >> ',' >> int_ >> ')')
00126 [bind(&CommandInterface::getValue, &cmdInterface, arg::_val, arg::_1, arg::_2)]
00127 | (lit("getValues(") >> int_ >> ')')
00128 [bind(&CommandInterface::getValues, &cmdInterface, arg::_val, arg::_1)]
00129
00130 | (lit("info(") >> int_ >> ')')
00131 [bind(&CommandInterface::info, &cmdInterface, arg::_val, arg::_1)]
00132 | (lit("version(") >> int_ >> ')')
00133 [bind(&CommandInterface::version, &cmdInterface, arg::_val, arg::_1)]
00134 )
00135 >> lit(MAGIC_END)
00136 );
00137 }
00138
00139 private:
00140 CommandInterface& cmdInterface;
00141
00142 };
00143 template<typename Iterator> friend struct CommandGrammar;
00144
00145 CommandGrammar<std::string::const_iterator>* cmdGrammar;
00146
00147 public:
00148
00153 CommandProcessor(CommandInterface& ci);
00157 virtual ~CommandProcessor();
00158
00166 CommandInterface::result_type process(const std::string& msg);
00167 };
00168
00169 }
00170 }
00171
00172 #endif