Parent: [ab4f72] (diff)

Download this file

cmdtalk.h    106 lines (93 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
/* 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 General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
<Message ends here: because of the empty line after '22'
*
* Example answer, with 'Mimetype' and 'Data' tags
*
Mimetype: 10
text/plainData: 10
0123456789
<Message ends here because of empty line
*
* This format is both extensible and reasonably easy to parse.
* While it's more fitted for python or perl on the script side, it
* should even be sort of usable from the shell (e.g.: use dd to read
* the counted data). Most alternatives would need data encoding in
* some cases.
*
* Higher level dialog:
* The C++ program is the master and sends request messages to the script.
* Both sides of the communication should be prepared to receive and discard
* unknown tags.
*/
#include <string>
#include <vector>
#include <unordered_map>
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.
virtual bool startCmd(const std::string& cmdname,
const std::vector<std::string>& args =
std::vector<std::string>(),
const std::vector<std::string>& env =
std::vector<std::string>(),
const std::vector<std::string>& path =
std::vector<std::string>()
);
virtual bool running();
// Single exchange: send and receive data.
virtual bool talk(const std::unordered_map<std::string, std::string>& args,
std::unordered_map<std::string, std::string>& rep);
// Specialized version with special argument used by dispatcher to call
// designated method
virtual bool callproc(
const std::string& proc,
const std::unordered_map<std::string, std::string>& args,
std::unordered_map<std::string, std::string>& rep);
CmdTalk(const CmdTalk&) = delete;
CmdTalk &operator=(const CmdTalk &) = delete;
private:
class Internal;
Internal *m{0};
};
#endif /* _CMDTALK_H_INCLUDED_ */