Statistics
| Branch: | Tag: | Revision:

dvbd / src / clientconnection.cpp @ 59be6a47

History | View | Annotate | Download (5.49 KB)

1
/*
2
  Copyright 2003 John Knottenbelt
3
  
4
  This program is free software; you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation; either version 2 of the License, or
7
  (at your option) any later version.
8
 
9
  This program is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
 
14
  You should have received a copy of the GNU General Public License
15
  along with this program; if not, write to the Free Software
16
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17
*/
18

    
19
#include "clientconnection.h"
20
#include "unixclientsocket.h"
21
#include "stringutil.h"
22
#include "utils.h"
23
#include <iostream>
24

    
25
class SetBlocking {
26
public:
27
  SetBlocking(UnixClientSocket &client, bool desiredState)
28
    : client_(client), savedState_(client.isBlocking())
29
  {
30
    client_.setBlocking(desiredState);
31
  }
32

    
33
  ~SetBlocking()
34
  {
35
    client_.setBlocking(savedState_);
36
  }
37

    
38
private:
39
  UnixClientSocket &client_;
40
  bool savedState_;
41
};
42

    
43
ClientConnection::ClientConnection( const std::string &socketFile )
44
 : control_(socketFile), data_(socketFile), good_(true)
45
{
46
  if (control_ && data_) {
47
    control_.write("CONTROL\n");
48
    std::string key;
49
    if (!control_.readLine(key)) {
50
      good_ = false;
51
      return;
52
    }
53
    key = stripTrailingWS(key);
54
    data_.write("DATA " + key + "\n");
55
    std::string welcome;
56
    if (!control_.readLine(welcome)) {
57
      good_ = false;
58
      return;
59
    }
60
  }
61
}
62

    
63
ClientConnection::operator bool () const
64
{
65
  return good_ && control_ && data_;
66
}
67

    
68
std::string ClientConnection::getError() const
69
{
70
  return error_;
71
}
72

    
73
bool ClientConnection::getChannels(const std::string &type, int priority, StringList &channels)
74
{
75
  SetBlocking blocking(control_, true);
76

    
77
  // Check if the command is to list the channels
78
  control_.write("LIST " + type + " " + toString(priority) + "\n");
79

    
80
  do {
81
    std::string reply;
82
    if (!control_.readLine(reply)) {
83
      error_ = "failed to read channel from control socket";
84
      return false;
85
    }
86

    
87
    if (reply.substr(0, 5) == "ERROR") {
88
      return false;
89
    }
90
    else if (reply == "OK\n") 
91
      return true;
92
    else 
93
      channels.push_back( stripTrailingWS(reply.substr(1)) );
94

    
95
  } while (true);
96

    
97
}
98

    
99
UnixClientSocket& ClientConnection::control()
100
{
101
  return control_;
102
}
103

    
104
UnixClientSocket& ClientConnection::data()
105
{
106
  return data_;
107
}
108

    
109
int ClientConnection::schedule(const std::string &type, const std::string &channel,
110
                               bool convertToPS, time_t when, unsigned duration, int priority,
111
                               const std::string &outputFile)
112
{
113
  SetBlocking saveBlocking(control_, true);
114
  // Convert the time to the right format
115
  char data[256];
116
  strftime(data, sizeof(data), "%Y-%m-%d %H:%M:%S", localtime(&when));
117
  std::string dateTime = data;
118

    
119
  std::string command = 
120
    "SCHEDULE " + dateTime + " " + type + " " + escapeWS(channel) + " " + 
121
    toString(convertToPS) + " " + toString(priority) + " " + 
122
    toString(duration * 60) + " " + outputFile + "\n";
123
  std::cerr << command;
124

    
125
  control_.write(command);
126
    
127
  std::string reply;
128
  control_.readLine(reply);
129
  
130
  if (reply.substr(0, 3) == "JOB") {
131
    int job = toInt(reply.substr(4));
132
    control_.readLine(reply);        // Ok
133
    return job;
134
  }
135
  error_ = stripTrailingWS(reply);
136
  return -1;
137
}
138

    
139
void ClientConnection::checkSchedule(StringList &messages)
140
{
141
  SetBlocking saveBlocking(control_, true);
142
  control_.write("CHECK\n");
143
  do {
144
    std::string reply;
145
    control_.readLine(reply);
146
    if (reply == "OK\n")
147
      break;
148
    messages.push_back(stripTrailingWS(reply));
149
  } while (true);
150
}
151

    
152
bool ClientConnection::listSchedule(StringList &s)
153
{
154
  SetBlocking saveBlocking(control_, true);
155
  control_.write("LISTSCHEDULE\n");
156
  do {
157
    std::string reply;
158
    control_.readLine(reply);
159
    if (reply == "OK\n")
160
      break;
161
    s.push_back(stripTrailingWS(reply));
162
  } while (true);
163
  return true;
164
}
165

    
166
bool ClientConnection::removeJob(int job)
167
{
168
  SetBlocking saveBlocking(control_, true);
169
  control_.write("REMOVEJOB " + toString(job) + "\n");
170
  std::string reply;
171
  control_.readLine(reply);
172
  if (reply == "OK\n")
173
    return true;
174
  error_ = stripTrailingWS(reply);
175
  return false;
176
}
177

    
178
bool ClientConnection::tune(const std::string &type, const std::string &channel, 
179
                            bool convertToPS, int priority)
180
{
181
  SetBlocking saveBlocking(control_, true);
182
  control_.write("TUNE " + type + " " + escapeWS(channel) + " " + 
183
                 toString(convertToPS) + " " + toString(priority) + "\n");
184
  std::string reply;
185
  control_.readLine(reply);
186
  if (reply == "OK\n")
187
    return true;
188
  error_ = stripTrailingWS(reply);
189
  return false;
190
}
191

    
192
bool ClientConnection::getNextJobTime(time_t &nextJobTime)
193
{
194
  SetBlocking saveBlocking(control_, true);
195
  control_.write("NEXTJOBTIME\n");
196

    
197
  std::string reply;
198
  control_.readLine(reply);
199
  if (reply.substr(0, 5) == "ERROR") {
200
    error_ = stripTrailingWS(reply);
201
    return false;
202
  }
203
  struct tm tm;
204
  if (strptime(reply.c_str(), "%Y-%m-%d %H:%M:%S", &tm) == NULL) {
205
    error_ = "Invalid date/time: " + stripTrailingWS(reply);
206
    return false;
207
  }
208
  nextJobTime = mktime(&tm);
209
  return true;
210
}
211

    
212
bool ClientConnection::listConnections(StringList &s)
213
{
214
  SetBlocking saveBlocking(control_, true);
215
  control_.write("LISTCONNECTIONS\n");
216
  do {
217
    std::string reply;
218
    control_.readLine(reply);
219
    if (reply == "OK\n")
220
      break;
221
    s.push_back(stripTrailingWS(reply));
222
  } while (true);
223
  return true;
224
}
225