Parent: [4b5ddb] (diff)

Download this file

cmdtalk.cpp    244 lines (208 with data), 6.1 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* 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.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "cmdtalk.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <mutex>
#include "smallut.h"
#include "execmd.h"
#ifdef MDU_INCLUDE_LOG
#include MDU_INCLUDE_LOG
#else
#include "log.h"
#endif
using namespace std;
class TimeoutExcept {};
class Canceler : public ExecCmdAdvise {
public:
Canceler(int tmsecs)
: m_timeosecs(tmsecs) {}
virtual void newData(int cnt) {
if (m_starttime && (time(0) - m_starttime) > m_timeosecs) {
throw TimeoutExcept();
}
}
void reset() {
m_starttime = time(0);
}
int m_timeosecs;
time_t m_starttime{0};
};
class CmdTalk::Internal {
public:
Internal(int timeosecs)
: m_cancel(timeosecs) {}
~Internal() {
delete cmd;
}
bool readDataElement(string& name, string &data);
bool talk(const pair<string, string>& arg0,
const unordered_map<string, string>& args,
unordered_map<string, string>& rep);
ExecCmd *cmd{0};
Canceler m_cancel;
std::mutex mmutex;
};
CmdTalk::CmdTalk(int timeosecs)
{
m = new Internal(timeosecs);
}
CmdTalk::~CmdTalk()
{
delete m;
}
bool CmdTalk::startCmd(const string& cmdname,
const vector<string>& args,
const vector<string>& env,
const vector<string>& path)
{
LOGDEB("CmdTalk::startCmd\n" );
delete m->cmd;
m->cmd = new ExecCmd;
m->cmd->setAdvise(&m->m_cancel);
for (const auto& it : env) {
m->cmd->putenv(it);
}
string acmdname(cmdname);
if (!path.empty()) {
string colonpath;
for (const auto& it: path) {
colonpath += it + ":";
}
if (!colonpath.empty()) {
colonpath.erase(colonpath.size()-1);
}
LOGDEB("CmdTalk::startCmd: PATH: [" << colonpath << "]\n");
ExecCmd::which(cmdname, acmdname, colonpath.c_str());
}
if (m->cmd->startExec(acmdname, args, 1, 1) < 0) {
return false;
}
return true;
}
// Messages are made of data elements. Each element is like:
// name: len\ndata
// An empty line signals the end of the message, so the whole thing
// would look like:
// Name1: Len1\nData1Name2: Len2\nData2\n
bool CmdTalk::Internal::readDataElement(string& name, string &data)
{
string ibuf;
m_cancel.reset();
try {
// Read name and length
if (cmd->getline(ibuf) <= 0) {
LOGERR("CmdTalk: getline error\n" );
return false;
}
} catch (TimeoutExcept) {
LOGINF("CmdTalk:readDataElement: fatal timeout (" <<
m_cancel.m_timeosecs << " S)\n");
return false;
}
LOGDEB1("CmdTalk:rde: line [" << (ibuf) << "]\n" );
// Empty line (end of message) ?
if (!ibuf.compare("\n")) {
LOGDEB("CmdTalk: Got empty line\n" );
return true;
}
// We're expecting something like Name: len\n
vector<string> tokens;
stringToTokens(ibuf, tokens);
if (tokens.size() != 2) {
LOGERR("CmdTalk: bad line in filter output: [" << (ibuf) << "]\n" );
return false;
}
vector<string>::iterator it = tokens.begin();
name = *it++;
string& slen = *it;
int len;
if (sscanf(slen.c_str(), "%d", &len) != 1) {
LOGERR("CmdTalk: bad line in filter output: [" << (ibuf) << "]\n" );
return false;
}
// Read element data
data.erase();
if (len > 0 && cmd->receive(data, len) != len) {
LOGERR("CmdTalk: expected " << len << " bytes of data, got " <<
data.length() << "\n");
return false;
}
LOGDEB1("CmdTalk:rde: got: name [" << name << "] len " << len <<"value ["<<
(data.size() > 100 ? (data.substr(0, 100) + " ...") : data)<< endl);
return true;
}
bool CmdTalk::Internal::talk(const pair<string, string>& arg0,
const unordered_map<string, string>& args,
unordered_map<string, string>& rep)
{
std::unique_lock<std::mutex> lock(mmutex);
if (cmd->getChildPid() <= 0) {
LOGERR("CmdTalk::talk: no process\n");
return false;
}
ostringstream obuf;
if (!arg0.first.empty()) {
obuf << arg0.first << ": " << arg0.second.size() << "\n" << arg0.second;
}
for (const auto& it : args) {
obuf << it.first << ": " << it.second.size() << "\n" << it.second;
}
obuf << "\n";
if (cmd->send(obuf.str()) < 0) {
cmd->zapChild();
LOGERR("CmdTalk: send error\n" );
return false;
}
// Read answer (multiple elements)
LOGDEB1("CmdTalk: reading answer\n" );
for (;;) {
string name, data;
if (!readDataElement(name, data)) {
cmd->zapChild();
return false;
}
if (name.empty()) {
break;
}
trimstring(name, ":");
LOGDEB1("CmdTalk: got [" << name << "] -> [" << data << "]\n");
rep[name] = data;
}
if (rep.find("cmdtalkstatus") != rep.end()) {
return false;
} else {
return true;
}
}
bool CmdTalk::running()
{
return m && m->cmd && m->cmd->getChildPid() > 0;
}
bool CmdTalk::talk(const unordered_map<string, string>& args,
unordered_map<string, string>& rep)
{
return m->talk({"",""}, args, rep);
}
bool CmdTalk::callproc(
const string& proc,
const unordered_map<std::string, std::string>& args,
unordered_map<std::string, std::string>& rep)
{
return m->talk({"cmdtalk:proc", proc}, args, rep);
}