Fix melted on OS X.
[melted] / src / melted / melted_server.c
1 /*
2 * melted_server.c
3 * Copyright (C) 2002-20039 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program 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 * This program 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 this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 /* System header files */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #include <string.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
36 #include <errno.h>
37 #include <arpa/inet.h>
38
39 /* Application header files */
40 #include "melted_server.h"
41 #include "melted_connection.h"
42 #include "melted_local.h"
43 #include "melted_log.h"
44 #include "melted_commands.h"
45 #include <mvcp/mvcp_remote.h>
46 #include <mvcp/mvcp_tokeniser.h>
47
48 static void melted_command_received( mlt_listener listener, mlt_properties owner, melted_server this, void **args )
49 {
50 if ( listener != NULL )
51 listener( owner, this, ( mvcp_response ** )args[ 0 ], ( char * )args[ 1 ] );
52 }
53
54 static void melted_doc_received( mlt_listener listener, mlt_properties owner, melted_server this, void **args )
55 {
56 if ( listener != NULL )
57 listener( owner, this, ( mvcp_response ** )args[ 0 ], ( char * )args[ 1 ], ( char * )args[ 2 ] );
58 }
59
60 static void melted_push_received( mlt_listener listener, mlt_properties owner, melted_server this, void **args )
61 {
62 if ( listener != NULL )
63 listener( owner, this, ( mvcp_response ** )args[ 0 ], ( char * )args[ 1 ], ( mlt_service )args[ 2 ] );
64 }
65
66 /** Initialise a server structure.
67 */
68
69 melted_server melted_server_init( char *id )
70 {
71 melted_server server = malloc( sizeof( melted_server_t ) );
72 if ( server != NULL )
73 memset( server, 0, sizeof( melted_server_t ) );
74 if ( server != NULL && mlt_properties_init( &server->parent, server ) == 0 )
75 {
76 server->id = id;
77 server->port = DEFAULT_TCP_PORT;
78 server->socket = -1;
79 server->shutdown = 1;
80 mlt_events_init( &server->parent );
81 mlt_events_register( &server->parent, "command-received", ( mlt_transmitter )melted_command_received );
82 mlt_events_register( &server->parent, "doc-received", ( mlt_transmitter )melted_doc_received );
83 mlt_events_register( &server->parent, "push-received", ( mlt_transmitter )melted_push_received );
84 }
85 return server;
86 }
87
88 const char *melted_server_id( melted_server server )
89 {
90 return server != NULL && server->id != NULL ? server->id : "melted";
91 }
92
93 void melted_server_set_config( melted_server server, const char *config )
94 {
95 if ( server != NULL )
96 {
97 free( server->config );
98 server->config = config != NULL ? strdup( config ) : NULL;
99 }
100 }
101
102 /** Set the port of the server.
103 */
104
105 void melted_server_set_port( melted_server server, int port )
106 {
107 server->port = port;
108 }
109
110 void melted_server_set_proxy( melted_server server, char *proxy )
111 {
112 mvcp_tokeniser tokeniser = mvcp_tokeniser_init( );
113 server->proxy = 1;
114 server->remote_port = DEFAULT_TCP_PORT;
115 mvcp_tokeniser_parse_new( tokeniser, proxy, ":" );
116 strcpy( server->remote_server, mvcp_tokeniser_get_string( tokeniser, 0 ) );
117 if ( mvcp_tokeniser_count( tokeniser ) == 2 )
118 server->remote_port = atoi( mvcp_tokeniser_get_string( tokeniser, 1 ) );
119 mvcp_tokeniser_close( tokeniser );
120 }
121
122 /** Wait for a connection.
123 */
124
125 static int melted_server_wait_for_connect( melted_server server )
126 {
127 struct timeval tv;
128 fd_set rfds;
129
130 /* Wait for a 1 second. */
131 tv.tv_sec = 1;
132 tv.tv_usec = 0;
133
134 FD_ZERO( &rfds );
135 FD_SET( server->socket, &rfds );
136
137 return select( server->socket + 1, &rfds, NULL, NULL, &tv);
138 }
139
140 /** Run the server thread.
141 */
142
143 static void *melted_server_run( void *arg )
144 {
145 melted_server server = arg;
146 pthread_t cmd_parse_info;
147 connection_t *tmp = NULL;
148 pthread_attr_t thread_attributes;
149 socklen_t socksize;
150
151 socksize = sizeof( struct sockaddr );
152
153 melted_log( LOG_NOTICE, "%s version %s listening on port %i", server->id, VERSION, server->port );
154
155 /* Create the initial thread. We want all threads to be created detached so
156 their resources get freed automatically. (CY: ... hmmph...) */
157 pthread_attr_init( &thread_attributes );
158 pthread_attr_setdetachstate( &thread_attributes, PTHREAD_CREATE_DETACHED );
159
160 while ( !server->shutdown )
161 {
162 /* Wait for a new connection. */
163 if ( melted_server_wait_for_connect( server ) )
164 {
165 /* Create a new block of data to hold a copy of the incoming connection for
166 our server thread. The thread should free this when it terminates. */
167
168 tmp = (connection_t*) malloc( sizeof(connection_t) );
169 tmp->owner = &server->parent;
170 tmp->parser = server->parser;
171 tmp->fd = accept( server->socket, (struct sockaddr*) &(tmp->sin), &socksize );
172
173 /* Pass the connection to a parser thread :-/ */
174 if ( tmp->fd != -1 )
175 pthread_create( &cmd_parse_info, &thread_attributes, parser_thread, tmp );
176 }
177 }
178
179 melted_log( LOG_NOTICE, "%s version %s server terminated.", server->id, VERSION );
180
181 return NULL;
182 }
183
184 /** Execute the server thread.
185 */
186
187 int melted_server_execute( melted_server server )
188 {
189 int error = 0;
190 mvcp_response response = NULL;
191 int index = 0;
192 struct sockaddr_in ServerAddr;
193 int flag = 1;
194
195 server->shutdown = 0;
196
197 ServerAddr.sin_family = AF_INET;
198 ServerAddr.sin_port = htons( server->port );
199 ServerAddr.sin_addr.s_addr = INADDR_ANY;
200
201 /* Create socket, and bind to port. Listen there. Backlog = 5
202 should be sufficient for listen (). */
203 server->socket = socket( AF_INET, SOCK_STREAM, 0 );
204
205 if ( server->socket == -1 )
206 {
207 server->shutdown = 1;
208 perror( "socket" );
209 melted_log( LOG_ERR, "%s unable to create socket.", server->id );
210 return -1;
211 }
212
213 setsockopt( server->socket, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, sizeof( int ) );
214
215 if ( bind( server->socket, (struct sockaddr *) &ServerAddr, sizeof (ServerAddr) ) != 0 )
216 {
217 server->shutdown = 1;
218 perror( "bind" );
219 melted_log( LOG_ERR, "%s unable to bind to port %d.", server->id, server->port );
220 return -1;
221 }
222
223 if ( listen( server->socket, 5 ) != 0 )
224 {
225 server->shutdown = 1;
226 perror( "listen" );
227 melted_log( LOG_ERR, "%s unable to listen on port %d.", server->id, server->port );
228 return -1;
229 }
230
231 #ifndef __DARWIN__
232 fcntl( server->socket, F_SETFL, O_NONBLOCK );
233 #endif
234
235 if ( !server->proxy )
236 {
237 melted_log( LOG_NOTICE, "Starting server on %d.", server->port );
238 server->parser = melted_parser_init_local( );
239 }
240 else
241 {
242 melted_log( LOG_NOTICE, "Starting proxy for %s:%d on %d.", server->remote_server, server->remote_port, server->port );
243 server->parser = mvcp_parser_init_remote( server->remote_server, server->remote_port );
244 }
245
246 response = mvcp_parser_connect( server->parser );
247
248 if ( response != NULL && mvcp_response_get_error_code( response ) == 100 )
249 {
250 /* read configuration file */
251 if ( response != NULL && !server->proxy && server->config != NULL )
252 {
253 mvcp_response_close( response );
254 response = mvcp_parser_run( server->parser, server->config );
255
256 if ( mvcp_response_count( response ) > 1 )
257 {
258 if ( mvcp_response_get_error_code( response ) > 299 )
259 melted_log( LOG_ERR, "Error evaluating server configuration. Processing stopped." );
260 for ( index = 0; index < mvcp_response_count( response ); index ++ )
261 melted_log( LOG_DEBUG, "%4d: %s", index, mvcp_response_get_line( response, index ) );
262 }
263 }
264
265 if ( response != NULL )
266 {
267 int result;
268 mvcp_response_close( response );
269 result = pthread_create( &server->thread, NULL, melted_server_run, server );
270 if ( result )
271 {
272 melted_log( LOG_CRIT, "Failed to launch TCP listener thread" );
273 error = -1;
274 }
275 }
276 }
277 else
278 {
279 melted_log( LOG_ERR, "Error connecting to parser. Processing stopped." );
280 server->shutdown = 1;
281 error = -1;
282 }
283
284 return error;
285 }
286
287 /** Fetch a units properties
288 */
289
290 mlt_properties melted_server_fetch_unit( melted_server server, int index )
291 {
292 melted_unit unit = melted_get_unit( index );
293 return unit != NULL ? unit->properties : NULL;
294 }
295
296 /** Shutdown the server.
297 */
298
299 void melted_server_shutdown( melted_server server )
300 {
301 if ( server != NULL && !server->shutdown )
302 {
303 server->shutdown = 1;
304 pthread_join( server->thread, NULL );
305 melted_server_set_config( server, NULL );
306 mvcp_parser_close( server->parser );
307 server->parser = NULL;
308 close( server->socket );
309 }
310 }
311
312 /** Close the server.
313 */
314
315 void melted_server_close( melted_server server )
316 {
317 if ( server != NULL && mlt_properties_dec_ref( &server->parent ) <= 0 )
318 {
319 mlt_properties_close( &server->parent );
320 melted_server_shutdown( server );
321 free( server );
322 }
323 }