adopt to winsock
[melted] / src / melted++ / MltMelted.cpp
1 /**
2 * MltMelted.cpp - MLT Melted Wrapper
3 * Copyright (C) 2004-2009 Charles Yates
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 Lesser General Public License as published
8 * by 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 #include "MltMelted.h"
22 #include "MltService.h"
23 #include "MltResponse.h"
24 using namespace Mlt;
25
26 #include <time.h>
27
28 static mvcp_response mlt_melted_execute( void *arg, char *command )
29 {
30 Melted *melted = ( Melted * )arg;
31 if ( melted != NULL )
32 {
33 Response *response = melted->execute( command );
34 mvcp_response real = mvcp_response_clone( response->get_response( ) );
35 delete response;
36 return real;
37 }
38 else
39 {
40 mvcp_response response = mvcp_response_init( );
41 mvcp_response_set_error( response, 500, "Invalid server" );
42 return response;
43 }
44 }
45
46 static mvcp_response mlt_melted_received( void *arg, char *command, char *doc )
47 {
48 Melted *melted = ( Melted * )arg;
49 if ( melted != NULL )
50 {
51 Response *response = melted->received( command, doc );
52 if ( response != NULL )
53 {
54 mvcp_response real = mvcp_response_clone( response->get_response( ) );
55 delete response;
56 return real;
57 }
58 return NULL;
59 }
60 else
61 {
62 mvcp_response response = mvcp_response_init( );
63 mvcp_response_set_error( response, 500, "Invalid server" );
64 return response;
65 }
66 }
67
68 static mvcp_response mlt_melted_push( void *arg, char *command, mlt_service service )
69 {
70 Melted *melted = ( Melted * )arg;
71 if ( melted != NULL )
72 {
73 Service input( service );
74 Response *response = melted->push( command, &input );
75 mvcp_response real = mvcp_response_clone( response->get_response( ) );
76 delete response;
77 return real;
78 }
79 else
80 {
81 mvcp_response response = mvcp_response_init( );
82 mvcp_response_set_error( response, 500, "Invalid server" );
83 return response;
84 }
85 }
86
87 Melted::Melted( char *name, int port, char *config ) :
88 Properties( false )
89 {
90 server = melted_server_init( name );
91 melted_server_set_port( server, port );
92 melted_server_set_config( server, config );
93 }
94
95 Melted::~Melted( )
96 {
97 melted_server_close( server );
98 }
99
100 mlt_properties Melted::get_properties( )
101 {
102 return &server->parent;
103 }
104
105 bool Melted::start( )
106 {
107 if ( melted_server_execute( server ) == 0 )
108 {
109 _real = server->parser->real;
110 _execute = server->parser->execute;
111 _received = server->parser->received;
112 _push = server->parser->push;
113 server->parser->real = this;
114 server->parser->execute = mlt_melted_execute;
115 server->parser->received = mlt_melted_received;
116 server->parser->push = mlt_melted_push;
117 }
118 return server->shutdown == 0;
119 }
120
121 bool Melted::is_running( )
122 {
123 return server->shutdown == 0;
124 }
125
126 Response *Melted::execute( char *command )
127 {
128 return new Response( _execute( _real, command ) );
129 }
130
131 Response *Melted::received( char *command, char *doc )
132 {
133 return new Response( _received( _real, command, doc ) );
134 }
135
136 Response *Melted::push( char *command, Service *service )
137 {
138 return new Response( _push( _real, command, service->get_service( ) ) );
139 }
140
141 void Melted::wait_for_shutdown( )
142 {
143 struct timespec tm = { 1, 0 };
144 while ( !server->shutdown )
145 nanosleep( &tm, NULL );
146 }
147
148 void Melted::log_level( int threshold )
149 {
150 melted_log_init( log_stderr, threshold );
151 }
152
153 Properties *Melted::unit( int index )
154 {
155 mlt_properties properties = melted_server_fetch_unit( server, index );
156 return properties != NULL ? new Properties( properties ) : NULL;
157 }