]> git.vomp.tv Git - vompserver.git/blob - mvpserver.c
New structure
[vompserver.git] / mvpserver.c
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "mvpserver.h"
22
23 MVPServer::MVPServer()
24 {
25 }
26
27 MVPServer::~MVPServer()
28 {
29   stop();
30 }
31
32 int MVPServer::stop()
33 {
34   if (threadIsActive()) threadCancel();
35   close(listeningSocket);
36
37   udpr.shutdown();
38
39   log.log("Main", Log::INFO, "Stopped main server thread");
40   log.shutdown();
41
42   config.shutdown();
43
44   return 1;
45 }
46
47 int MVPServer::run()
48 {
49   if (threadIsActive()) return 1;
50
51   // Start config
52   const char* configDir = cPlugin::ConfigDirectory();
53   if (!configDir)
54   {
55     dsyslog("VOMP: Could not get config dir from VDR");
56   }
57   else
58   {
59     char configFileName[PATH_MAX];
60     snprintf(configFileName, PATH_MAX, "%s/vomp.conf", configDir);
61     if (config.init(configFileName))
62     {
63       dsyslog("VOMP: Config file found");
64     }
65     else
66     {
67       dsyslog("VOMP: Config file not found");
68     }
69   }
70
71   // Start logging
72
73   char* cfgLogFilename = config.getValueString("General", "Log file");
74   if (cfgLogFilename)
75   {
76     log.init(Log::DEBUG, cfgLogFilename);
77     delete[] cfgLogFilename;
78   }
79
80   // Work out a name for this server
81
82   char* serverName;
83
84   // Try to get from vomp.conf
85   serverName = config.getValueString("General", "Server name");
86   if (!serverName) // If not, get the hostname
87   {
88     serverName = new char[1024];
89     if (gethostname(serverName, 1024)) // if not, just use "-"
90     {
91       strcpy(serverName, "-");
92     }
93   }
94
95   int udpSuccess = udpr.run(serverName);
96
97   delete[] serverName;
98
99   if (!udpSuccess)
100   {
101     log.log("Main", Log::CRIT, "Could not start UDP replier");
102     stop();
103     return 0;
104   }
105
106   // start thread here
107   if (!threadStart())
108   {
109     log.log("Main", Log::CRIT, "Could not start MVPServer thread");
110     stop();
111     return 0;
112   }
113
114   log.log("Main", Log::DEBUG, "MVPServer run success");
115   return 1;
116 }
117
118 void MVPServer::threadMethod()
119 {
120   // I want to die as soon as I am cancelled because I'll be in accept()
121   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
122   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
123
124   struct sockaddr_in address;
125   address.sin_family = AF_INET;
126   address.sin_port = htons(3024);
127   address.sin_addr.s_addr = INADDR_ANY;
128   socklen_t length = sizeof(address);
129
130   listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
131   if (listeningSocket < 0)
132   {
133     log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
134     return;
135   }
136
137   int value=1;
138   setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
139
140   if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
141   {
142     log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
143     close(listeningSocket);
144     return;
145   }
146
147   listen(listeningSocket, 5);
148
149   int clientSocket;
150
151   while(1)
152   {
153     clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
154     MVPClient* m = new MVPClient(clientSocket);
155     m->run();
156   }
157 }