Switch to unified view

a/src/mediaserver/cdplugins/cmdtalk.cpp b/src/mediaserver/cdplugins/cmdtalk.cpp
...
...
26
#include "execmd.h"
26
#include "execmd.h"
27
#include "log.h"
27
#include "log.h"
28
28
29
using namespace std;
29
using namespace std;
30
30
31
class TimeoutExcept {};
32
33
class Canceler : public ExecCmdAdvise {
34
public:
35
    Canceler(int tmsecs) 
36
        : m_timeosecs(tmsecs) {}
37
38
    virtual void newData(int cnt) {
39
        if (m_starttime && (time(0) - m_starttime) > m_timeosecs) {
40
            throw TimeoutExcept();
41
        }
42
    }
43
44
    void reset() {
45
        m_starttime = time(0);
46
    }
47
    int m_timeosecs;
48
    time_t m_starttime{0};
49
};
50
31
class CmdTalk::Internal {
51
class CmdTalk::Internal {
32
public:
52
public:
33
    Internal()
53
    Internal(int timeosecs)
34
  : cmd(0) {
54
  : m_cancel(timeosecs) {}
35
    }
55
36
    ~Internal() {
56
    ~Internal() {
37
    delete cmd;
57
    delete cmd;
38
    }
58
    }
59
39
    bool readDataElement(string& name, string &data);
60
    bool readDataElement(string& name, string &data);
40
61
41
    bool talk(const pair<string, string>& arg0,
62
    bool talk(const pair<string, string>& arg0,
42
          const unordered_map<string, string>& args,
63
          const unordered_map<string, string>& args,
43
          unordered_map<string, string>& rep);
64
          unordered_map<string, string>& rep);
65
44
    ExecCmd *cmd;
66
    ExecCmd *cmd{0};
67
    Canceler m_cancel;
45
    std::mutex mmutex;
68
    std::mutex mmutex;
46
};
69
};
47
70
48
CmdTalk::CmdTalk()
71
CmdTalk::CmdTalk(int timeosecs)
49
{
72
{
50
    m = new Internal;
73
    m = new Internal(timeosecs);
51
}
74
}
52
CmdTalk::~CmdTalk()
75
CmdTalk::~CmdTalk()
53
{
76
{
54
    delete m;
77
    delete m;
55
}
78
}
...
...
61
{
84
{
62
    LOGDEB("CmdTalk::startCmd\n" );
85
    LOGDEB("CmdTalk::startCmd\n" );
63
86
64
    delete m->cmd;
87
    delete m->cmd;
65
    m->cmd = new ExecCmd;
88
    m->cmd = new ExecCmd;
66
    
89
    m->cmd->setAdvise(&m->m_cancel);
90
67
    for (const auto& it : env) {
91
    for (const auto& it : env) {
68
    m->cmd->putenv(it);
92
    m->cmd->putenv(it);
69
    }
93
    }
70
94
71
    string acmdname(cmdname);
95
    string acmdname(cmdname);
...
...
94
// Name1: Len1\nData1Name2: Len2\nData2\n
118
// Name1: Len1\nData1Name2: Len2\nData2\n
95
bool CmdTalk::Internal::readDataElement(string& name, string &data)
119
bool CmdTalk::Internal::readDataElement(string& name, string &data)
96
{
120
{
97
    string ibuf;
121
    string ibuf;
98
122
123
    m_cancel.reset();
124
    try {
99
    // Read name and length
125
        // Read name and length
100
    if (cmd->getline(ibuf) <= 0) {
126
        if (cmd->getline(ibuf) <= 0) {
101
        LOGERR("CmdTalk: getline error\n" );
127
            LOGERR("CmdTalk: getline error\n" );
128
            return false;
129
        }
130
    } catch (TimeoutExcept) {
131
        LOGINF("CmdTalk:readDataElement: fatal timeout (" <<
132
               m_cancel.m_timeosecs << " S)\n");
102
        return false;
133
        return false;
103
    }
134
    }
104
    
135
    
105
    LOGDEB1("CmdTalk:rde: line ["  << (ibuf) << "]\n" );
136
    LOGDEB1("CmdTalk:rde: line ["  << (ibuf) << "]\n" );
106
137