command.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  command.cpp - Processes client commands
4  -------------------
5  begin : Wed Aug 9 2006
6  copyright : (C) 2006 Bas Kemper
7  email : kst@ <my name> .be
8 
9  ***************************************************************************
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU Lesser General Public *
12  * License as published by the Free Software Foundation; either *
13  * version 2.1 of the License, or (at your option) any later version. *
14  * *
15  * This library is distributed in the hope that it will be useful, *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
18  * Lesser General Public License for more details. *
19  * *
20  * You should have received a copy of the GNU Lesser General Public *
21  * License along with this library; if not, write to the Free Software *
22  * Foundation, Inc., 59 Temple Place, *
23  * Suite 330, Boston, MA 02111-1307 USA *
24  * *
25  ***************************************************************************/
26 
27 #include <algorithm> /* std::transform is needed for upper-case conversion */
28 #include <string>
29 #include <cstdlib> /* strtoull */
30 #include <cctype>
31 #include <cerrno>
32 #include <rtt/Property.hpp>
33 #include "NiceHeaderMarshaller.hpp"
34 #include "TcpReporting.hpp"
35 #include "command.hpp"
36 #include "socket.hpp"
37 #include "datasender.hpp"
38 #include "socketmarshaller.hpp"
39 
41 using OCL::TCP::Socket;
43 
44 namespace
45 {
49  class prefixbuf : public std::streambuf
50  {
51  private:
52  std::streambuf* _opt;
53  bool newline;
54 
55  public:
56  prefixbuf( std::streambuf* opt ) : _opt(opt)
57  {
58  setp(0, 0);
59  setg(0, 0, 0);
60  newline = true;
61  }
62 
63  ~prefixbuf()
64  {
65  sync();
66  }
67 
68  protected:
69  int overflow(int c)
70  {
71  if( c != EOF )
72  {
73  if( newline )
74  {
75  if( _opt->sputn("300 ", 4) != 4 )
76  {
77  return EOF;
78  }
79  newline = false;
80  }
81  int ret = _opt->sputc(c);
82  newline = ( c == '\n' );
83  return ret;
84  }
85  return 0;
86  }
87 
88  int sync()
89  {
90  return _opt->pubsync();
91  }
92  };
93 
97  class prefixstr : public std::ostream
98  {
99  public:
100  prefixstr(std::ostream& opt)
101  : std::ostream( new prefixbuf( opt.rdbuf() ) )
102  {
103  }
104 
105  ~prefixstr()
106  {
107  delete rdbuf();
108  }
109  };
110 
111  /* std::toupper is implemented as a macro and cannot be used by
112  std::transform without a wrapper function. */
113  char to_upper (const char c)
114  {
115  return toupper(c);
116  }
117 
121  class HeaderCommand : public RealCommand
122  {
123  protected:
124  void maincode( int, std::string* )
125  {
126  std::vector<std::string> list = _parent->getConnection()->getMarshaller()->getReporter()->getReport()->list();
127  for(unsigned int i=0;i<list.size();i++)
128  socket()<<"305 "<<list[i]<<std::endl;
129  socket() << "306 End of list" << std::endl;
130  }
131 
132  public:
133  HeaderCommand(TcpReportingInterpreter* parent)
134  : RealCommand( "HEADERS", parent )
135  {
136  }
137 
138  ~HeaderCommand()
139  {}
140 
141  void manualExecute()
142  {
143  maincode(0,0);
144  }
145  };
146 
147  class HelpCommand : public RealCommand
148  {
149  protected:
153  void printCommands()
154  {
155  const std::vector<Command*> &cmds = _parent->giveCommands();
156  socket() << "Use HELP <command>" << std::endl;
157  for( unsigned int i = 0; i < cmds.size(); i++ )
158  {
159  if( cmds[i] == cmds[i]->getRealCommand( cmds ) )
160  {
161  socket() << cmds[i]->getName() << '\n';
162  }
163  }
164  socket() << '.' << std::endl;
165  }
166 
170  void printHelpFor( const std::string& name, const RealCommand* command )
171  {
172  socket() << "Name: " << name << std::endl;
173  socket() << "Usage: " << name;
174  if( command->getSyntax() )
175  {
176  socket() << " " << command->getSyntax();
177  }
178  socket() << std::endl;
179  }
180 
184  void printHelpFor( const std::string& cmd )
185  {
186  const std::vector<Command*> &cmds = _parent->giveCommands();
187  for( unsigned int i = 0; i < cmds.size(); i++ )
188  {
189  if( cmds[i]->getName() == cmd )
190  {
191  printHelpFor( cmd, cmds[i]->getRealCommand( cmds ) );
192  return;
193  }
194  }
195  printCommands();
196  }
197 
198  void maincode( int argc, std::string* params )
199  {
200  if( argc == 0 )
201  {
202  printCommands();
203  } else {
204  printHelpFor(params[0]);
205  }
206  }
207  public:
208  HelpCommand(TcpReportingInterpreter* parent)
209  : RealCommand( "HELP", parent, 0, 1, "[nothing | <command name>]" )
210  {
211  }
212  };
213 
214  class ListCommand : public RealCommand
215  {
216  protected:
217  void maincode( int, std::string* )
218  {
219  socket() << "103 none" << std::endl;
220  }
221 
222  public:
223  ListCommand(TcpReportingInterpreter* parent)
224  : RealCommand( "LISTEXTENSIONS", parent )
225  {
226  }
227  };
228 
229  class QuitCommand : public RealCommand
230  {
231  protected:
232  void maincode( int, std::string* )
233  {
234  // The main marshaller is not notified about the connection
235  // being closed but will detect it and delete the DataSender
236  // in the next serialize() run because DataSender is a
237  // thread and this method is (indirectly) called by
238  // DataSender::loop.
239  socket().close();
240  }
241 
242  public:
243  QuitCommand(TcpReportingInterpreter* parent)
244  : RealCommand( "QUIT", parent )
245  {
246  }
247  };
248 
249  class SetLimitCommand : public RealCommand
250  {
251  protected:
252  void maincode( int, std::string* args )
253  {
254  int olderr = errno;
255  char* tailptr;
256  unsigned long long limit = strtoull( args[0].c_str(), &tailptr, 10 );
257  if( *tailptr != '\0' || ( errno != olderr && errno == ERANGE ) )
258  {
259  sendError102();
260  } else {
261  _parent->getConnection()->setLimit(limit);
262  sendOK();
263  }
264  }
265 
266  public:
267  SetLimitCommand(TcpReportingInterpreter* parent)
268  : RealCommand( "SETLIMIT", parent, 1, 1, "<frameid>" )
269  {
270  }
271  };
272 
276  class SilenceCommand : public RealCommand
277  {
278  protected:
279  void maincode( int, std::string* args )
280  {
281  toupper( args, 0 );
282  if( args[0] == "ON" )
283  {
284  _parent->getConnection()->silence(true);
285  } else if( args[0] == "OFF") {
286  _parent->getConnection()->silence(false);
287  } else {
288  sendError102();
289  return;
290  }
291  socket() << "107 Silence " << args[0] << std::endl;
292  }
293 
294  public:
295  SilenceCommand(TcpReportingInterpreter* parent)
296  : RealCommand( "SILENCE", parent, 1, 1, "[ON | OFF]" )
297  {
298  }
299  };
300 
304  class SubscribeCommand : public RealCommand
305  {
306  // TODO: id's elimneren voor subscribe command
307  // TODO: id's elimneren voor unsubscribe command
308  // TODO: id's elimneren
309  protected:
310  void maincode( int, std::string* args )
311  {
312  if( _parent->getConnection()->addSubscription(args[0]) )
313  {
314  socket() << "302 " << args[0] << std::endl;
315  } else {
316  socket() << "301 " << args[0] << std::endl;
317  }
318  }
319 
320  public:
321  SubscribeCommand(TcpReportingInterpreter* parent)
322  : RealCommand( "SUBSCRIBE", parent, 1, 1, "<source name>" )
323  {
324  }
325  };
326 
327  class SubscriptionsCommand : public RealCommand
328  {
329  protected:
330  void maincode( int, std::string* )
331  {
332  _parent->getConnection()->listSubscriptions();
333  }
334 
335  public:
336  SubscriptionsCommand(TcpReportingInterpreter* parent)
337  : RealCommand( "SUBS", parent )
338  {
339  }
340  };
341 
342  class UnsubscribeCommand : public RealCommand
343  {
344  protected:
345  void maincode( int, std::string* args )
346  {
347  if( _parent->getConnection()->removeSubscription(args[0]) )
348  {
349  socket() << "303 " << args[0] << std::endl;
350  } else {
351  socket() << "304 " << args[0] << std::endl;
352  }
353  }
354 
355  public:
356  UnsubscribeCommand(TcpReportingInterpreter* parent)
357  : RealCommand( "UNSUBSCRIBE", parent, 1, 1, "<source name>" )
358  {
359  }
360  };
361 
362  class VersionCommand : public RealCommand
363  {
364  protected:
365  void maincode( int, std::string* args )
366  {
367  if( args[0] == "1.0" )
368  {
369  _parent->setVersion10();
370  sendOK();
371  } else {
372  socket() << "106 Not supported" << std::endl;
373  }
374  }
375 
376  public:
377  VersionCommand(TcpReportingInterpreter* parent)
378  : RealCommand( "VERSION", parent, 1, 1, "1.0" )
379  {
380  }
381  };
382 }
383 
384 namespace OCL
385 {
386 namespace TCP
387 {
388  /*
389  * The default Orocos Command objects are not used since we
390  * do not use Data Sources here.
391  */
392  class RealCommand;
393 
394  Command::Command( std::string name )
395  : _name( name )
396  {
397  }
398 
400  {
401  }
402 
403  Command* Command::find(const std::vector<Command*>& cmds, const std::string& cmp)
404  {
405  for( unsigned int j = 0; j < cmds.size(); j++ )
406  {
407  if( *cmds[j] == cmp )
408  {
409  return cmds[j];
410  }
411  }
412  return 0;
413  }
414 
415  const std::string& Command::getName() const
416  {
417  return _name;
418  }
419 
420  bool Command::is(std::string& cmd) const
421  {
422  return cmd == _name;
423  }
424 
425  bool Command::operator==(const std::string& cmp) const
426  {
427  return cmp == _name;
428  }
429 
430  bool Command::operator!=(const std::string& cmp) const
431  {
432  return cmp != _name;
433  }
434 
435  bool Command::operator<( const Command& cmp ) const
436  {
437  return _name < cmp.getName();
438  }
439 
440  AliasCommand::AliasCommand( std::string name, std::string alias )
441  : Command( name ), _alias( alias )
442  {
443  }
444 
445  RealCommand* AliasCommand::getRealCommand(const std::vector<Command*>& cmds) const
446  {
447  Command* ret = Command::find( cmds, _alias );
448  if( !ret )
449  {
450  return 0;
451  }
452  return ret->getRealCommand(cmds);
453  }
454 
455  RealCommand::RealCommand( std::string name, TcpReportingInterpreter* parent, unsigned int minargs,
456  unsigned int maxargs, const char* syntax )
457  : Command( name ), _parent( parent ), _minargs( minargs ), _maxargs( maxargs ), _syntax( syntax )
458  {
459  }
460 
462  {
463  }
464 
465  const char* RealCommand::getSyntax() const
466  {
467  return _syntax;
468  }
469 
471  {
472  if( _syntax )
473  {
474  socket() << "102 Syntax: " << _name << ' ' << _syntax << std::endl;
475  } else {
476  socket() << "102 Syntax: " << _name << std::endl;
477  }
478  return false;
479  }
480 
481  bool RealCommand::sendOK() const
482  {
483  socket() << "101 OK" << std::endl;
484  return true;
485  }
486 
487  bool RealCommand::correctSyntax( unsigned int argc, std::string* )
488  {
489  if( argc < _minargs || argc > _maxargs )
490  {
491  return sendError102();
492  }
493  return true;
494  }
495 
496  RealCommand* RealCommand::getRealCommand(const std::vector<Command*>& cmds) const
497  {
498  return const_cast<RealCommand*>(this);
499  }
500 
501  void RealCommand::execute( int argc, std::string* args )
502  {
503  if( correctSyntax( argc, args ) )
504  {
505  maincode( argc, args );
506  }
507  }
508 
509  void RealCommand::toupper( std::string* args, int i ) const
510  {
511  std::transform( args[i].begin(), args[i].end(), args[i].begin(), to_upper );
512  }
513 
514  void RealCommand::toupper( std::string* args, int start, int stop ) const
515  {
516  for( int i = start; i <= stop; i++ )
517  {
518  toupper( args, i );
519  }
520  }
521 
523  {
524  return _parent->getConnection()->getSocket();
525  }
526 
528  : _parent( parent )
529  {
530  addCommand( new VersionCommand(this) );
531  addCommand( new HelpCommand(this) );
532  addCommand( new QuitCommand(this) );
533  addCommand( new AliasCommand( "EXIT", "QUIT" ) );
534  }
535 
537  {
538  for( std::vector<Command*>::iterator i = cmds.begin();
539  i != cmds.end();
540  i++ )
541  {
542  delete *i;
543  }
544  }
545 
547  {
548  // this method has a complexity of O(n) because we want
549  // the list to be sorted.
550  commands.lock();
551  std::vector<Command*>::iterator i = cmds.begin();
552  while( i != cmds.end() && *command < **i ) {
553  i++;
554  }
555 
556  // avoid duplicates
557  if( i != cmds.end() && *command == (*i)->getName() )
558  {
559  return;
560  }
561  cmds.insert( i, command );
562  commands.unlock();
563  }
564 
565  const std::vector<Command*>& TcpReportingInterpreter::giveCommands() const
566  {
567  return cmds;
568  }
569 
571  {
572  return _parent;
573  }
574 
576  {
577  std::string ipt = getConnection()->getSocket().readLine();
578 
579  if( ipt.empty() )
580  {
581  return;
582  }
583 
584  /* parseParameters returns data by reference */
585  std::string cmd;
586  std::string* params;
587 
588  unsigned int argc = parseParameters( ipt, cmd, &params );
589 
590  std::transform( cmd.begin(), cmd.end(), cmd.begin(), to_upper );
591 
592  /* search the command to be executed */
593  bool correct = false;
594  commands.lock();
595  Command* obj = Command::find( cmds, cmd );
596  if( obj ) /* command found */
597  {
598  RealCommand* rcommand = obj->getRealCommand(cmds);
599  if( rcommand ) /* alias is correct */
600  {
601  rcommand->execute( argc, params );
602  correct = true;
603  }
604  } else {
605  Logger::log() << Logger::Error << "Invalid command: " << ipt << Logger::endl;
606  }
607  commands.unlock();
608 
609  if( !correct )
610  {
611  getConnection()->getSocket() << "105 Command not found" << std::endl;
612  }
613  }
614 
616  std::string& ipt, std::string& cmd, std::string** params )
617  {
618  unsigned int argc = 0;
619  std::string::size_type pos = ipt.find_first_of("\t ", 0);
620  while( pos != std::string::npos )
621  {
622  pos = ipt.find_first_of("\t ", pos + 1);
623  argc++;
624  }
625  if( argc > 0 )
626  {
627  *params = new std::string[argc];
628  pos = ipt.find_first_of("\t ", 0);
629  cmd = ipt.substr(0, pos);
630  unsigned int npos;
631  for( unsigned int i = 0; i < argc; i++ )
632  {
633  npos = ipt.find_first_of("\t ", pos + 1);
634  (*params)[i] = ipt.substr(pos+1,npos - pos - 1);
635  pos = npos;
636  }
637  } else {
638  cmd = ipt;
639  *params = 0;
640  }
641  return argc;
642  }
643 
645  {
646  commands.lock();
647  std::vector<Command*>::iterator i = cmds.begin();
648  while( i != cmds.end() && **i != ipt ) {
649  i++;
650  }
651  if( i == cmds.end() )
652  {
653  Logger::log() << Logger::Error << "TcpReportingInterpreter::removeCommand: removing unknown command" << ipt << Logger::endl;
654  } else {
655  Command* todel = *i;
656  cmds.erase(i);
657  delete todel;
658  }
659  commands.unlock();
660  }
661 
663  {
664  commands.lock();
665  removeCommand( "VERSION" );
666  addCommand( new ListCommand(this) );
667  addCommand( new HeaderCommand(this) );
668  addCommand( new SilenceCommand(this) );
669  addCommand( new SetLimitCommand(this) );
670  addCommand( new SubscribeCommand(this) );
671  addCommand( new UnsubscribeCommand(this) );
672  addCommand( new SubscriptionsCommand(this) );
673  commands.unlock();
674  _parent->silence( false );
675  }
676 }
677 }
678 
Variable opBinary s not applicable to args
Definition: rtt.cpp:757
void toupper(std::string *args, int index) const
Definition: command.cpp:509
virtual ~Command()
Definition: command.cpp:399
virtual RealCommand * getRealCommand(const std::vector< Command * > &cmds) const =0
void execute(int argc, std::string *args)
Definition: command.cpp:501
Socket & socket() const
Definition: command.cpp:522
bool operator==(const std::string &cmp) const
Definition: command.cpp:425
const char * _syntax
Definition: command.hpp:150
unsigned int parseParameters(std::string &ipt, std::string &cmd, std::string **params)
Definition: command.cpp:615
virtual void maincode(int argc, std::string *args)=0
Datasender * getConnection() const
Definition: command.cpp:570
const char * getSyntax() const
Definition: command.cpp:465
bool operator!=(const std::string &cmp) const
Definition: command.cpp:430
TcpReportingInterpreter(Datasender *parent)
Definition: command.cpp:527
virtual RealCommand * getRealCommand(const std::vector< Command * > &cmds) const
Definition: command.cpp:496
AliasCommand(std::string name, std::string alias)
Definition: command.cpp:440
virtual bool correctSyntax(unsigned int argc, std::string *args)
Definition: command.cpp:487
std::string readLine()
Definition: socket.cpp:222
virtual void unlock()
std::vector< Command * > cmds
Definition: command.hpp:48
static Command * find(const std::vector< Command * > &cmds, const std::string &cmp)
Definition: command.cpp:403
virtual bool is(std::string &cmd) const
Definition: command.cpp:420
Socket & getSocket() const
Definition: datasender.cpp:82
bool operator<(const Command &cmp) const
Definition: command.cpp:435
const std::string & getName() const
Definition: command.cpp:415
virtual RealCommand * getRealCommand(const std::vector< Command * > &cmds) const
Definition: command.cpp:445
bool sendOK() const
Definition: command.cpp:481
bool sendError102() const
Definition: command.cpp:470
static std::ostream & endl(std::ostream &__os)
unsigned int _maxargs
Definition: command.hpp:149
const std::vector< Command * > & giveCommands() const
Definition: command.cpp:565
virtual ~RealCommand()
Definition: command.cpp:461
void addCommand(Command *command)
Definition: command.cpp:546
static Logger & log()
RealCommand(std::string name, TcpReportingInterpreter *parent, unsigned int minargs=0, unsigned int maxargs=0, const char *syntax=0)
Definition: command.cpp:455
std::string _name
Definition: command.hpp:94
std::string _alias
Definition: command.hpp:133
void removeCommand(const char *name)
Definition: command.cpp:644
void silence(bool newstate)
Definition: datasender.cpp:197
TcpReportingInterpreter * _parent
Definition: command.hpp:147
RTT::os::MutexRecursive commands
Definition: command.hpp:49


ocl
Author(s): OCL Development Team
autogenerated on Mon Mar 23 2020 04:47:19