File: cmdtalk.h

package info (click to toggle)
recoll 1.43.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,952 kB
  • sloc: cpp: 104,785; python: 9,933; xml: 7,314; ansic: 6,447; sh: 1,252; perl: 166; makefile: 72
file content (109 lines) | stat: -rw-r--r-- 3,863 bytes parent folder | download | duplicates (3)
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
106
107
108
109
/* 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

<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();
    CmdTalk(const CmdTalk&) = delete;
    CmdTalk& operator=(const CmdTalk&) = delete;

    // @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<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);

private:
    class Internal;
    Internal *m{0};
};

#endif /* _CMDTALK_H_INCLUDED_ */