]> git.vomp.tv Git - vompserver.git/blob - mvpserver.c
Replacement transceiver and use of new thread class (from client!)
[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   if (threadIsActive()) stop();
30 }
31
32 int MVPServer::stop()
33 {
34   if (!threadIsActive()) return 0;
35
36   threadCancel();
37   log.log("MVPServer", Log::INFO, "Stopped MVPServer thread");
38
39   udpr.stop();
40   log.shutdown();
41
42   close(listeningSocket);
43
44   return 1;
45 }
46
47 int MVPServer::run()
48 {
49   if (threadIsActive()) return 1;
50
51   log.init(Log::DEBUG, "/tmp/vompserver.log", 1);
52   if (!udpr.run())
53   {
54     log.log("MVPServer", Log::CRIT, "Could not start UDP replier");
55     log.shutdown();
56     return 0;
57   }
58
59   // start thread here
60   if (!threadStart())
61   {
62     log.log("MVPServer", Log::CRIT, "Could not start MVPServer thread");
63     udpr.stop();
64     log.shutdown();
65     return 0;
66   }
67
68   log.log("MVPServer", Log::DEBUG, "MVPServer run success");
69   return 1;
70 }
71
72 void MVPServer::threadMethod()
73 {
74   // I want to die as soon as I am cancelled because I'll be in accept()
75   pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
76   pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
77
78   struct sockaddr_in address;
79   address.sin_family = AF_INET;
80   address.sin_port = htons(3024);
81   address.sin_addr.s_addr = INADDR_ANY;
82   socklen_t length = sizeof(address);
83
84   listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
85   if (listeningSocket < 0)
86   {
87     log.log("MVPServer", Log::CRIT, "Could not get TCP socket in vompserver");
88     return;
89   }
90
91   int value=1;
92   setsockopt(listeningSocket,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));
93
94   if (bind(listeningSocket,(struct sockaddr *)&address,sizeof(address)) < 0)
95   {
96     log.log("MVPServer", Log::CRIT, "Could not bind to socket in vompserver");
97     close(listeningSocket);
98     return;
99   }
100
101   listen(listeningSocket, 5);
102
103   int clientSocket;
104
105   while(1)
106   {
107     clientSocket = accept(listeningSocket,(struct sockaddr *)&address, &length);
108     MVPClient* m = new MVPClient(clientSocket);
109     m->run();
110   }
111 }
112
113
114 ULLONG ntohll(ULLONG a)
115 {
116   return htonll(a);
117 }
118
119 ULLONG htonll(ULLONG a)
120 {
121   #if BYTE_ORDER == BIG_ENDIAN
122     return a;
123   #else
124     ULLONG b = 0;
125
126     b = ((a << 56) & 0xFF00000000000000ULL)
127       | ((a << 40) & 0x00FF000000000000ULL)
128       | ((a << 24) & 0x0000FF0000000000ULL)
129       | ((a <<  8) & 0x000000FF00000000ULL)
130       | ((a >>  8) & 0x00000000FF000000ULL)
131       | ((a >> 24) & 0x0000000000FF0000ULL)
132       | ((a >> 40) & 0x000000000000FF00ULL)
133       | ((a >> 56) & 0x00000000000000FFULL) ;
134
135     return b;
136   #endif
137 }