Switch to unified view

a/src/cdplugins/cmdtalk.h b/src/cdplugins/cmdtalk.h
1
/home/dockes/projets/docklib/cmdtalk/cmdtalk.h
1
/* Copyright (C) 2016 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#ifndef _CMDTALK_H_INCLUDED_
18
#define _CMDTALK_H_INCLUDED_
19
20
/** 
21
 * Execute commands and exchange messages with it.
22
 *
23
 * A simple stream protocol is used for the dialog. HTTP or some kind
24
 * of full-blown RPC could have been used, but there was also good
25
 * reason to keep it simple (yet powerful), given the limited context
26
 * of dialog through a pipe.
27
 *
28
 * The data is exchanged in TLV fashion, in a way that should be
29
 * usable in most script languages. The basic unit of data has one line 
30
 * with a data type and a count (both ASCII), followed by the data. A
31
 * 'message' is made of one or several units or tags and ends with one empty
32
 * line. 
33
 * 
34
 * Example:(the message begins before 'Filename' and has 'Filename' and 
35
 * 'Ipath' tags):
36
 * 
37
Filename: 24
38
/my/home/mail/somefolderIpath: 2
39
22
40
41
<Message ends here: because of the empty line after '22'
42
43
 * 
44
 * Example answer, with 'Mimetype' and 'Data' tags
45
 * 
46
Mimetype: 10
47
text/plainData: 10
48
0123456789
49
50
<Message ends here because of empty line
51
52
 *        
53
 * This format is both extensible and reasonably easy to parse. 
54
 * While it's more fitted for python or perl on the script side, it
55
 * should even be sort of usable from the shell (e.g.: use dd to read
56
 * the counted data). Most alternatives would need data encoding in
57
 * some cases.
58
 *
59
 * Higher level dialog:
60
 * The C++ program is the master and sends request messages to the script. 
61
 * Both sides of the communication should be prepared to receive and discard 
62
 * unknown tags.
63
 */
64
65
#include <string>
66
#include <vector>
67
#include <unordered_map>
68
69
class CmdTalk {
70
 public:
71
    CmdTalk();
72
    virtual ~CmdTalk();
73
74
    // @param env each entry should be of the form name=value. They
75
    //   augment the subprocess environnement.
76
    // @param path replaces the PATH variable when looking for the command.
77
    virtual bool startCmd(const std::string& cmdname,
78
            const std::vector<std::string>& args =
79
            std::vector<std::string>(),
80
            const std::vector<std::string>& env =
81
            std::vector<std::string>(),
82
            const std::vector<std::string>& path =
83
            std::vector<std::string>()
84
  );
85
    virtual bool running();
86
    
87
    // Single exchange: send and receive data.
88
    virtual bool talk(const std::unordered_map<std::string, std::string>& args,
89
            std::unordered_map<std::string, std::string>& rep);
90
91
    // Specialized version with special argument used by dispatcher to call
92
    // designated method
93
    virtual bool callproc(
94
  const std::string& proc,
95
  const std::unordered_map<std::string, std::string>& args,
96
  std::unordered_map<std::string, std::string>& rep);
97
    
98
private:
99
    class Internal;
100
    Internal *m;
101
};
102
103
#endif /* _CMDTALK_H_INCLUDED_ */