forked from tryphon/rivendell2-debian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdcatch_connect.cpp
364 lines (297 loc) · 7.27 KB
/
rdcatch_connect.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// catch_connect.cpp
//
// Connect to the Rivendell Netcatcher Daemon.
//
// (C) Copyright 2002-2004 Fred Gleason <[email protected]>
//
// $Id: rdcatch_connect.cpp,v 1.26 2010/07/29 19:32:33 cvs Exp $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
#include <ctype.h>
#include <qobject.h>
#include <rdcatch_connect.h>
RDCatchConnect::RDCatchConnect(int serial,QObject *parent,const char *name)
: QObject(parent,name)
{
cc_serial=serial;
cc_connected=false;
argnum=0;
argptr=0;
for(int i=0;i<MAX_DECKS;i++) {
cc_monitor_state[i]=false;
}
//
// TCP Connection
//
cc_socket=new QSocket(this,"cc_socket");
connect(cc_socket,SIGNAL(connected()),this,SLOT(connectedData()));
connect(cc_socket,SIGNAL(error(int)),this,SLOT(errorData(int)));
connect(cc_socket,SIGNAL(readyRead()),this,SLOT(readyData()));
//
// Start the heartbeat timer
//
cc_heartbeat_timer=new QTimer(this,"cc_heartbeat_timer");
connect(cc_heartbeat_timer,SIGNAL(timeout()),
this,SLOT(heartbeatTimeoutData()));
cc_heartbeat_timer->start(CC_HEARTBEAT_INTERVAL,true);
cc_heartbeat_valid=true;
}
RDCatchConnect::~RDCatchConnect()
{
// printf("Destroying RDCatchConnect\n");
}
void RDCatchConnect::connectHost(QString hostname,Q_UINT16 hostport,
QString password)
{
cc_password=password;
cc_socket->connectToHost(hostname,hostport);
}
RDDeck::Status RDCatchConnect::status(unsigned chan) const
{
if(chan<=MAX_DECKS) {
return cc_record_deck_status[chan-1];
}
return cc_play_deck_status[chan-128];
}
int RDCatchConnect::currentId(unsigned chan) const
{
if(chan<=MAX_DECKS) {
return cc_record_id[chan-1];
}
return cc_play_id[chan-129];
}
void RDCatchConnect::enableMetering(bool state)
{
SendCommand(QString().sprintf("RM %d!",state));
}
void RDCatchConnect::reloadHeartbeat()
{
SendCommand("RH!");
}
void RDCatchConnect::reloadDropboxes()
{
SendCommand("RX!");
}
void RDCatchConnect::addEvent(int id)
{
SendCommand(QString().sprintf("RA %d!",id));
}
void RDCatchConnect::removeEvent(int id)
{
SendCommand(QString().sprintf("RR %d!",id));
}
void RDCatchConnect::updateEvent(int id)
{
SendCommand(QString().sprintf("RU %d!",id));
}
void RDCatchConnect::reset()
{
SendCommand("RS!");
}
void RDCatchConnect::reload()
{
SendCommand("RD!");
}
void RDCatchConnect::refresh()
{
SendCommand("RE 0!");
}
void RDCatchConnect::reloadOffset()
{
SendCommand("RO!");
}
void RDCatchConnect::stop(int deck)
{
SendCommand(QString().sprintf("SR %d!",deck));
}
void RDCatchConnect::monitor(int deck,bool state)
{
SendCommand(QString().sprintf("MN %d %d!",deck,state));
}
void RDCatchConnect::toggleMonitor(int deck)
{
if(cc_monitor_state[deck-1]) {
SendCommand(QString().sprintf("MN %d 0!",deck));
}
else {
SendCommand(QString().sprintf("MN %d 1!",deck));
}
}
void RDCatchConnect::setExitCode(int id,RDRecording::ExitCode code,
const QString &msg)
{
SendCommand(QString().sprintf("SC %d %d %s!",id,code,
(const char *)msg.simplifyWhiteSpace()));
}
void RDCatchConnect::connectedData()
{
SendCommand(QString().sprintf("PW %s!",(const char *)cc_password));
}
void RDCatchConnect::errorData(int errorcode)
{
fprintf(stderr,"RDCatchConnect: socket error %d\n",errorcode);
}
void RDCatchConnect::readyData()
{
char buf[1024];
int c;
while((c=cc_socket->readBlock(buf,254))>0) {
buf[c]=0;
// printf("readyData: %s\n",buf);
for(int i=0;i<c;i++) {
if(buf[i]==' ') {
if(argnum<CC_MAX_ARGS) {
args[argnum][argptr]=0;
argnum++;
argptr=0;
}
else {
if(debug) {
printf("Argument list truncated!\n");
}
}
}
if(buf[i]=='!') {
args[argnum++][argptr]=0;
DispatchCommand();
argnum=0;
argptr=0;
if(cc_socket==NULL) {
return;
}
}
if((isgraph(buf[i]))&&(buf[i]!='!')) {
if(argptr<CC_MAX_LENGTH) {
args[argnum][argptr]=buf[i];
argptr++;
}
else {
if(debug) {
printf("WARNING: argument truncated!\n");
}
}
}
}
}
}
void RDCatchConnect::heartbeatTimeoutData()
{
if(cc_heartbeat_valid) {
emit heartbeatFailed(cc_serial);
cc_heartbeat_valid=false;
}
}
void RDCatchConnect::SendCommand(QString cmd)
{
// printf("SendCommand(%s)\n",(const char *)cmd);
cc_socket->writeBlock((const char *)cmd,cmd.length());
}
void RDCatchConnect::DispatchCommand()
{
int deck;
int channel;
int level;
unsigned chan;
int status;
int id;
if(!strcmp(args[0],"PW")) { // Password Response
if(args[1][0]=='+') {
emit connected(cc_serial,true);
SendCommand("RE 0!");
}
else {
emit connected(cc_serial,false);
}
}
if(!strcmp(args[0],"RE")) { // Channel Status
if(sscanf(args[1],"%u",&chan)!=1){
return;
}
if((chan<0)||((chan>(MAX_DECKS+1))&&(chan<(129)))||
(chan>(MAX_DECKS+129))) {
return;
}
if(sscanf(args[2],"%d",&status)!=1) {
return;
}
if(sscanf(args[3],"%d",&id)!=1) {
return;
}
if(chan==0) {
emit statusChanged(cc_serial,chan,(RDDeck::Status)status,id,"");
return;
}
chan--;
if(chan<=MAX_DECKS) {
if((status!=cc_record_deck_status[chan])||(id!=cc_record_id[chan])) {
cc_record_deck_status[chan]=(RDDeck::Status)status;
cc_record_id[chan]=id;
emit statusChanged(cc_serial,chan+1,cc_record_deck_status[chan],
cc_record_id[chan],args[4]);
}
}
if((chan>=128)&&(chan<MAX_DECKS+128)) {
if((status!=cc_play_deck_status[chan-128])||(id!=cc_play_id[chan-128])) {
cc_play_deck_status[chan-128]=(RDDeck::Status)status;
cc_play_id[chan-128]=id;
emit statusChanged(cc_serial,chan+1,cc_play_deck_status[chan-128],
cc_play_id[chan-128],args[4]);
}
}
return;
}
if(!strcmp(args[0],"RM")) { // Meter Level
if(sscanf(args[1],"%d",&deck)!=1) {
return;
}
if(sscanf(args[2],"%d",&channel)!=1) {
return;
}
if(sscanf(args[3],"%d",&level)!=1) {
return;
}
emit meterLevel(cc_serial,deck,channel,level);
return;
}
if(!strcmp(args[0],"RU")) { // Update Event
if(sscanf(args[1],"%d",&id)!=1) {
return;
}
emit eventUpdated(id);
}
if(!strcmp(args[0],"PE")) { // Purge Event
if(sscanf(args[1],"%d",&id)!=1) {
return;
}
emit eventPurged(id);
}
if(!strcmp(args[0],"HB")) { // Heartbeat
cc_heartbeat_timer->stop();
cc_heartbeat_timer->start(CC_HEARTBEAT_INTERVAL,true);
}
if(!strcmp(args[0],"MN")) { // Monitor State
if(sscanf(args[1],"%d",&deck)!=1) {
return;
}
if(args[2][0]=='1') {
cc_monitor_state[deck-1]=true;
emit monitorChanged(cc_serial,deck,true);
}
else {
cc_monitor_state[deck-1]=false;
emit monitorChanged(cc_serial,deck,false);
}
}
}