/* Copyright (C) 2016 J.F.Dockes * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #ifndef _CMDTALK_H_INCLUDED_ #define _CMDTALK_H_INCLUDED_ /** * Execute commands and exchange messages with it. * * A simple stream protocol is used for the dialog. HTTP or some kind * of full-blown RPC could have been used, but there was also good * reason to keep it simple (yet powerful), given the limited context * of dialog through a pipe. * * The data is exchanged in TLV fashion, in a way that should be * usable in most script languages. The basic unit of data has one line * with a data type and a count (both ASCII), followed by the data. A * 'message' is made of one or several units or tags and ends with one empty * line. * * Example:(the message begins before 'Filename' and has 'Filename' and * 'Ipath' tags): * Filename: 24 /my/home/mail/somefolderIpath: 2 22 #include #include class CmdTalk { public: CmdTalk(int timeosecs); virtual ~CmdTalk(); // @param env each entry should be of the form name=value. They // augment the subprocess environnement. // @param path replaces the PATH variable when looking for the command. // // Note that cmdtalk.py:main() method is a test routine which // expects data pairs on the command line. If actual parameters // need to be passed, it can't be used by the processor. virtual bool startCmd(const std::string& cmdname, const std::vector& args = std::vector(), const std::vector& env = std::vector(), const std::vector& path = std::vector() ); virtual bool running(); // Single exchange: send and receive data. virtual bool talk(const std::unordered_map& args, std::unordered_map& rep); // Specialized version with special argument used by dispatcher to call // designated method virtual bool callproc( const std::string& proc, const std::unordered_map& args, std::unordered_map& rep); CmdTalk(const CmdTalk&) = delete; CmdTalk &operator=(const CmdTalk &) = delete; private: class Internal; Internal *m{0}; }; #endif /* _CMDTALK_H_INCLUDED_ */