summaryrefslogtreecommitdiff
path: root/comthread.c
blob: 266135a7ad5911ba575c815e642d05f0fbfba69f (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//***************************************************************************
// Group VDR/GraphTFT
// File comthread.c
// Date 28.10.06
// This code is distributed under the terms and conditions of the
// GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
// (c) 2006-2013 Jörg Wendel
//--------------------------------------------------------------------------
// Class ComThread
//***************************************************************************

#include "common.h"
#include "comthread.h"
#include "display.h"

//***************************************************************************
// Object
//***************************************************************************

ComThread::ComThread(void* aDisplay, int width, int height)
   : cRemote("graphtft-fe")
{
   display = aDisplay;
   themeWidth = width;
   themeHeight = height;

   bufferSize = maxBuf;

   buffer = new char[bufferSize+TB];

   running = false;
   timeout = 5;
   checkTime = 60;
   port = na;
   *host = 0;
   pid = 0;
   jpgQuality = 60;
   width = 720;
   height = 576;

   listener = new TcpChannel(timeout);
}

ComThread::~ComThread()
{
   if (display)
      ((cGraphTFTDisplay*)display)->clearComThread();

   Stop();

   // close all client connections

   while (clients.size())
   {
      clients[0].channel->close();
      delete clients[0].channel;
      clients.erase(clients.begin());
   }

   listener->close();

   delete listener;
   delete[] buffer;
}

//***************************************************************************
// Init
//***************************************************************************

int ComThread::init(Renderer* aRenderer, unsigned int aPort, const char* aHost)
{
   renderer = aRenderer;

   if (aHost)
      setHost(aHost);

   if (aPort)
      setPort(aPort);

   if (listener->open(port) != 0)
   {
      tell(0, "Error: Can't establish listener");
      return fail;
   }

   tell(0, "Listener established!");

   return success;
}

int ComThread::close(TcpClient* client, int status, const char* message)
{
   cMutexLock lock(&_mutex);

   vector<TcpClient>::iterator it;

   for (it = clients.begin(); it < clients.end(); it++)
   {
      TcpClient* c = &(*it);

      if (c == client)
      {
         if (message)
            tell(0, "(%d) %s", status, message);

         c->channel->close();
         delete c->channel;
         c->channel = 0;
         clients.erase(it);

         break;
      }
   }

   return done;
}

//***************************************************************************
// Stop
//***************************************************************************

void ComThread::Stop()
{
   if (running) 
   {
      isyslog("GraphTFT plugin try to stop communication thread");
      running = false;
      Cancel(3);
   }
}

//***************************************************************************
// Run
//***************************************************************************

void ComThread::Action()
{
   TcpClient client;
   struct timeval tv;
   fd_set readSet;
   int maxFD;                               // the highest-numbered descriptor
   int status;

   pid = getpid();
   tell(0, "TCP communication thread started (pid=%d)", pid);

   running = true;

   while (running)
   {
      if (!(time(0) % 120))
         tell(2, "still running, %ld clients connected", clients.size());

      // check client connections
      //    perform select on all connections
      
      FD_ZERO(&readSet);
      FD_SET(listener->getHandle(), &readSet);  // file descriptor of listener
      maxFD = listener->getHandle();
      
      _mutex.Lock();

      for (unsigned int i = 0; i < clients.size(); i++)
      {
         FD_SET(clients[i].channel->getHandle(), &readSet);
         maxFD = max(maxFD, clients[i].channel->getHandle());
      }

      _mutex.Unlock();
      
      tv.tv_sec  = 1;
      tv.tv_usec = 0;
      
      // call select and wait up to 1 second
      
      if ((status = select(maxFD+1, &readSet, 0, 0, &tv)) < 0)
      {
         tell(0, "Error: Select failed, error was %s", strerror(errno));
         continue;
      }
         
      if (status == 0)
         continue;                                          // no data pending
      
      // at least one tcp message pending
      
      read(&readSet);

      // check for new connection

      if (FD_ISSET(listener->getHandle(), &readSet))
      {
         FD_CLR(listener->getHandle(), &readSet);

         if (listener->listen(client.channel) == success)
         {
            tell(0, "Client connection accepted, now "
                 "%ld clients connected", clients.size()+1);
            
            client.lastCheck = time(0);
            client.jpgQuality = 30;   // speed up first draw on slow connections
            client.channel->write(cGraphTftComService::cmdWelcome);
            
            // initial refresh         
            
            if (refresh(&client) == success)
            {
               client.jpgQuality = jpgQuality;
               clients.push_back(client);
            }
            else
               close(&client, 0, "initial refresh failed");
         }
      }
   }

   isyslog("GraphTFT plugin tcp communication thread ended (pid=%d)", pid);
}

//***************************************************************************
// read
//***************************************************************************

int ComThread::read(fd_set* readSet)
{
   cMutexLock lock(&_mutex);
   int status;

   for (unsigned int i = 0; i < clients.size(); i++)
   {
      if (FD_ISSET(clients[i].channel->getHandle(), readSet))
      {
         FD_CLR(clients[i].channel->getHandle(), readSet);
         
         if ((status = read(&clients[i])) != success && status != TcpChannel::wrnTimeout)
         {
            close(&clients[i], status, "Error: Communication problems, read failed, closing line!");
            continue;
         }
      }

      if (time(0) > clients[i].lastCheck + checkTime)
         close(&clients[i], 0, "Error: Missing check command on tcp connection, closing line!");
   }

   return done;
}

//***************************************************************************
// Read
//***************************************************************************

int ComThread::read(TcpClient* client)
{
   int status;
   TcpChannel::Header tmp;
   TcpChannel::Header header;

   // es stehen Daten an, erst einmal den Header abholen ..

   if ((status = client->channel->read((char*)&tmp, sizeof(TcpChannel::Header))) == 0)
   {
      header.command = ntohl(tmp.command);
      header.size = ntohl(tmp.size);

      tell(3, "Got command %d with %d data bytes", header.command, header.size);

      switch (header.command)
      {
         case cGraphTftComService::cmdWelcome:
         {
            tell(1, "Got welcome");
            break;
         }

         case cGraphTftComService::cmdLogout:
         {
            tell(1, "Got logout from client, closing line");
            close(client, 0, "Closing connection due to client logout");

            break;
         }

         case cGraphTftComService::cmdData:
         {
            tell(7, "Got data");
            status = client->channel->read(buffer, header.size);
            break;
         }

         case cGraphTftComService::cmdJpegQuality:
         {
            int quality;

            status = client->channel->read((char*)&quality, header.size);
            quality = ntohl(quality);

            tell(0, "Got JPEG quality (%d)", quality);

            if (quality > 0 && quality <= 100)
               client->jpgQuality = quality;

            break;
         }

         case cGraphTftComService::cmdMouseEvent:
         {
            GraphTftTouchEvent ev;

            status = client->channel->read((char*)&ev, header.size);
           
            ev.x = ntohl(ev.x);
            ev.y = ntohl(ev.y);
            ev.button = ntohl(ev.button);
            ev.flag = ntohl(ev.flag);
            ev.data = ntohl(ev.data);

            tell(0, "Got mouse event, button (%d/%d) at (%d/%d)", ev.button, ev.flag, ev.x, ev.y); 

            if (ev.flag & ComThread::efKeyboard)
               Put(ev.button);
            else
               ((cGraphTFTDisplay*)display)->mouseEvent(ev.x, ev.y, ev.button, 
                                                        ev.flag, ev.data);

            break;
         }

         case cGraphTftComService::cmdStartCalibration:
         {
            ((cGraphTFTDisplay*)display)->setCalibrate(true);

            break;
         }

         case cGraphTftComService::cmdStopCalibration:
         {
            ((cGraphTFTDisplay*)display)->setCalibrate(false);

            break;
         }

         case cGraphTftComService::cmdCheck:
         {
            client->lastCheck = time(0);

            break;
         }

         default:
         {
            tell(0, "Got unexpected protocol (%d/%d), aborting", 
                 header.command, header.size); 
            status = fail;

            break;
         }
      }
   }
   
   return status;
}

//***************************************************************************
// Put Key Code
//***************************************************************************

bool ComThread::Put(uint64_t Code, bool Repeat, bool Release)
{ 
   tell(5, "Put key action (%ld)", Code);

   return cRemote::Put(Code, Repeat, Release); 
}

//***************************************************************************
// Refresh
//***************************************************************************

int ComThread::refresh()
{
   cMutexLock lock(&_mutex);
   int status;

   for (unsigned int i = 0; i < clients.size(); i++)
   {
      if ((status = refresh(&clients[i])) != success)
         close(&clients[i], status, "Refresh failed, closing connection");
   }

   return done;
}

int ComThread::refresh(TcpClient* client)
{
   long size;
   unsigned char* jpeg = 0;
   int status = success;

   if (!client->channel || !client->channel->isConnected())
      return fail;

   LogDuration ld("ComThread::refresh()", 2);
   
   if ((size = renderer->toJpeg(jpeg, client->jpgQuality)) > 0)
   {
      tell(7, "Info: Write %ld kb to %d", size/1024, client->channel->getHandle());
      
      status = client->channel->write(cGraphTftComService::cmdData, (char*)jpeg, size);

      free(jpeg);
   }

   return status;
}