summaryrefslogtreecommitdiff
path: root/smarttvfactory.c
blob: 3478e9dd74691672b47a7a677efb6b1a462693bb (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
/*
 * smarttvfactory.h: VDR on Smart TV plugin
 *
 * Copyright (C) 2012 T. Lohmar
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 * Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 *
 */

#ifndef STANDALONE
#include <vdr/recording.h>
#include <vdr/videodir.h>
#endif

#include <iostream>

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>

#include <iostream>
#include <fstream>


#include "smarttvfactory.h"

#ifndef STANDALONE
#define PORT 8000
#else
#define PORT 9000
#endif

#define OKAY 0
#define ERROR (-1)

#define DEBUG


using namespace std;

void SmartTvServerStartThread(void* arg) {
  SmartTvServer* m = (SmartTvServer*)arg;
  m->threadLoop();
  delete m;
  pthread_exit(NULL);
}

SmartTvServer::SmartTvServer(): mRequestCount(0), isInited(false), serverPort(PORT), mServerFd(-1),
  mSegmentDuration(10), mHasMinBufferTime(40),  mLiveChannels(20), 
  clientList(), mActiveSessions(0), mConfig(NULL) {
}


SmartTvServer::~SmartTvServer() {

  if (mConfig != NULL)
    delete mConfig;
}

void SmartTvServer::cleanUp() {
  // close listening ports
  for (uint idx= 0; idx < clientList.size(); idx++) {
    if (clientList[idx] != NULL) {
      close(idx);
      delete clientList[idx];
      clientList[idx] = NULL;
    }
  }

  // close server port
  close(mServerFd);

  // Leave thread
  pthread_cancel(mThreadId);
  pthread_join(mThreadId, NULL);

  mLog.shutdown();
}

int SmartTvServer::runAsThread() {
  int res = pthread_create(&mThreadId, NULL, (void*(*)(void*))SmartTvServerStartThread, (void *)this);
  if (res != 0) {
    *(mLog.log()) << " Error creating thread. res= " << res 
		  << endl;
    return 0;
  } 
  return 1;
}

void SmartTvServer::threadLoop() {
  *(mLog.log()) << " SmartTvServer Thread Started " << endl;

  loop();

  *(mLog.log()) << " SmartTvServer Thread Stopped "  << endl;
}


void SmartTvServer::loop() {
  socklen_t addr_size = 0;
  int rfd;
  sockaddr_in sadr;
  int req_id = 0;
  int ret = 0;
  struct timeval timeout;

  int maxfd;

  fd_set read_set;
  fd_set write_set;

  FD_ZERO(&read_set);
  FD_ZERO(&write_set);

  FD_ZERO(&mReadState);
  FD_ZERO(&mWriteState);

  FD_SET(mServerFd, &mReadState);
  maxfd = mServerFd;

  *(mLog.log()) << "mServerFd= " << mServerFd << endl;

  int handeled_fds = 0;

  for (;;) {                    
    FD_ZERO(&read_set);
    FD_ZERO(&write_set);
    read_set = mReadState;
    write_set = mWriteState;

    if (ret != handeled_fds) {
      // Only ok, when the server has closed a handing HTTP connection
      *(mLog.log()) << "WARNING: Select-ret= " << ret 
		    << " != handeled_fds= " << handeled_fds << endl;
      /*      FD_ZERO(&mReadState);
      FD_ZERO(&mWriteState);
      FD_SET(mServerFd, &mReadState);
      maxfd = mServerFd;

      read_set = mReadState;
      write_set = mWriteState;
      for (uint idx= 0; idx < clientList.size(); idx++) {
	if (clientList[idx] != NULL) {
	  close(idx);
	  delete clientList[idx];
	  clientList[idx] = NULL;
	}
      }
      mActiveSessions = 0;
*/
    }

    handeled_fds = 0;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;

    ret = select(maxfd + 1, &read_set, &write_set, NULL, &timeout);
    
    if (ret == 0) {
      // timeout: Check for dead TCP connections
      for (uint idx= 0; idx < clientList.size(); idx++) {
	if (clientList[idx] != NULL)
	  if (clientList[idx]->checkStatus() == ERROR) {
	    close(idx);
	    delete clientList[idx];
	    clientList[idx] = NULL;
	    mActiveSessions--;
	    FD_CLR(idx, &mReadState);      /* dead client */
	    FD_CLR(idx, &mWriteState);
	    *(mLog.log()) << "WARNING: Timeout - Dead Client fd=" <<  idx  << endl;
	  }
      } 
      continue;
    } // timeout

    if (ret < 0){
      *(mLog.log()) << "ERROR: select error " <<  errno << endl;
      continue;
    } // Error

    // new accept
    if (FD_ISSET(mServerFd, &read_set)) {
      handeled_fds ++;
      if((rfd = accept(mServerFd, (sockaddr*)&sadr, &addr_size))!= -1){
	req_id ++;

#ifndef DEBUG
	*(mLog.log()) << "fd= " << rfd
		      << " --------------------- Received connection ---------------------" << endl;
#endif

	FD_SET(rfd, &mReadState);       
	FD_SET(rfd, &mWriteState);      

	if (rfd > maxfd) {
	  maxfd = rfd;
	}

	if (clientList.size() < (rfd+1)) {
	  clientList.resize(rfd+1, NULL); // Check.
	}
	clientList[rfd] = new cHttpResource(rfd, req_id, serverPort, this);
	mActiveSessions ++;
	*(mLog.log()) << " + mActiveSessions= " << mActiveSessions << endl;
      }
      else{
	*(mLog.log()) << "Error accepting " <<  errno << endl;
      }    
    }

    // Check for data on already accepted connections
    for (rfd = 0; rfd < clientList.size(); rfd++) {
      if (clientList[rfd] == NULL)
	continue;
      if (FD_ISSET(rfd, &read_set)) {
	handeled_fds ++;
	// HandleRead
	if (clientList[rfd] == NULL) {
	  *(mLog.log()) << "ERROR in Check Read: oops - no cHttpResource anymore fd= " << rfd << endl;
	  close(rfd);
          FD_CLR(rfd, &mReadState);      /* remove dead client */
	  FD_CLR(rfd, &mWriteState);
	  continue;
	}
	if ( clientList[rfd]->handleRead() < 0){
#ifndef DEBUG
	  *(mLog.log()) << "fd= " << rfd << " --------------------- Check Read: Closing ---------------------" << endl;
#endif
	  close(rfd);
	  delete clientList[rfd];
	  clientList[rfd] = NULL;
	  mActiveSessions--;
	  *(mLog.log()) << " - Check Read: mActiveSessions= " << mActiveSessions << endl;
          FD_CLR(rfd, &mReadState);      /* dead client */
	  FD_CLR(rfd, &mWriteState);
	}
      }
    }

    // Check for write
    for (rfd = 0; rfd < clientList.size(); rfd++) {
      if (clientList[rfd] == NULL)
	continue;
      if (FD_ISSET(rfd, &write_set)) {
	handeled_fds++;
	// HandleWrite
	if (clientList[rfd] == NULL) {
	  close(rfd);
          FD_CLR(rfd, &mReadState);     
	  FD_CLR(rfd, &mWriteState);
	  continue;
	}
	if ( clientList[rfd]->handleWrite() < 0){
#ifndef DEBUG
	  *(mLog.log()) << "fd= " << rfd << " --------------------- Check Write: Closing ---------------------" << endl;
#endif
	  close(rfd);
	  delete clientList[rfd];
	  clientList[rfd] = NULL;
	  mActiveSessions--;
	  *(mLog.log()) << " - Check Write: mActiveSessions= " << mActiveSessions << endl;
          FD_CLR(rfd, &mReadState);     
	  FD_CLR(rfd, &mWriteState);
	}
      }
    }

    // selfcheck
    /*    *(mLog.log()) << "Select Summary: ret= " << ret
      		  << " handeled_fds=" << handeled_fds 
		  << " mActiveSessions= " << mActiveSessions
		  << " clientList.size()= " << clientList.size() 
		  << endl;
*/
    //Check for active sessions
    /*
    *(mLog.log()) << "checking number of active sessions clientList.size()= " << clientList.size() << endl;

    int act_ses = 0;
    for (uint idx= 0; idx < clientList.size(); idx++) {
      if (clientList[idx] != NULL)
	act_ses++;
    } 
    if (act_ses != mActiveSessions) {
      *(mLog.log()) << "ERROR: Lost somewhere a session: "
		    << "mActiveSessions= " << mActiveSessions
		    << "act_ses= " << act_ses
		    << endl;
      mActiveSessions = act_ses;
    }
    *(mLog.log()) << "checking number of active sessions - done mActiveSessions= " << mActiveSessions << endl;
*/
  } // for (;;) 
} // org bracket

int SmartTvServer::isServing() {
  return (mActiveSessions != 0 ? true : false);
}

void SmartTvServer::initServer(string dir) {
  /* This function initialtes the listening socket for the server
   * and sets isInited to true
   */
  mConfigDir = dir;
  int ret;
  struct sockaddr_in sock;
  int yes = 1;


#ifndef STANDALONE
  mConfig = new cSmartTvConfig(dir); 
  mLog.init(mConfig->getLogFile());
  esyslog("SmartTvWeb: Logfile created");
  
  *(mLog.log()) << mConfig->getLogFile() << endl;

#else
  mConfig = new cSmartTvConfig("."); 
  mLog.init(mConfig->getLogFile());
  cout << "SmartTvWeb: Logfile created" << endl;
  cout << "SmartTvWeb: Listening on port= " << PORT << endl;

#endif
  
  //  mConfig->printConfig();

  mSegmentDuration= mConfig->getSegmentDuration();
  mHasMinBufferTime= mConfig->getHasMinBufferTime();
  mLiveChannels = mConfig->getLiveChannels();

  *(mLog.log()) <<"HTTP server listening on port " <<  serverPort << endl;

  mServerFd = socket(PF_INET, SOCK_STREAM, 0);
  if (mServerFd <0) {
    *(mLog.log()) << "Error: Cannot create serving socket, exit" << endl;
    exit(1); 
  }

  ret = setsockopt(mServerFd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
  if (ret <0) {
    *(mLog.log()) << "Error: Cannot set sockopts on serving socket, exit" << endl;
    exit(1); 
  }

  memset((char *) &sock, 0, sizeof(sock));
  sock.sin_family = AF_INET;

  if (mConfig->getServerAddress() == "")
    sock.sin_addr.s_addr = htonl(INADDR_ANY);
  else {
    *(mLog.log()) << "Binding Server to " << mConfig->getServerAddress() << endl;
    sock.sin_addr.s_addr = inet_addr(mConfig->getServerAddress().c_str());
  }
  sock.sin_port = htons(serverPort);

  ret = bind(mServerFd, (struct sockaddr *) &sock, sizeof(sock));
  if (ret !=0) {
    *(mLog.log()) << "Error: Cannot bind serving socket, exit" << endl;
    exit(1); 
  }
  
  /*
  struct ifreq ifr;

  ifr.ifr_addr.sa_family = AF_INET;
  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
  ioctl(mServerFd, SIOCGIFADDR, &ifr);
  string own_ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
  *(mLog.log()) << " own if ip= " << own_ip << endl; 
*/

  ret = listen(mServerFd, 5);
  if (ret <0) {
    *(mLog.log()) << "Error: Cannot set listening on serving socket, exit" << endl;
    exit(1); 
  }

  isInited = true;
}