e47358484cb2c94c46b955d4717f3600386de9ae
[melted] / src / modules / valerie / consumer_valerie.c
1 /*
2 * consumer_westley.c -- a libxml2 serialiser of mlt service networks
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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 #include "consumer_valerie.h"
22 #include <valerie/valerie.h>
23 #include <valerie/valerie_remote.h>
24 #include <framework/mlt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <pthread.h>
29 #include <unistd.h>
30
31 static int consumer_is_stopped( mlt_consumer this );
32 static int consumer_start( mlt_consumer this );
33
34 /** This is what will be called by the factory
35 */
36
37 mlt_consumer consumer_valerie_init( char *arg )
38 {
39 // Create the consumer object
40 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
41
42 // If no malloc'd and consumer init ok
43 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
44 {
45 // Allow thread to be started/stopped
46 this->start = consumer_start;
47 this->is_stopped = consumer_is_stopped;
48
49 mlt_properties_set( mlt_consumer_properties( this ), "server", arg == NULL ? "localhost" : arg );
50 mlt_properties_set_int( mlt_consumer_properties( this ), "port", 5250 );
51 mlt_properties_set_int( mlt_consumer_properties( this ), "unit", 0 );
52 mlt_properties_set( mlt_consumer_properties( this ), "command", "append" );
53
54 // Return the consumer produced
55 return this;
56 }
57
58 // malloc or consumer init failed
59 free( this );
60
61 // Indicate failure
62 return NULL;
63 }
64
65 static int consumer_start( mlt_consumer this )
66 {
67 // Get the producer service
68 mlt_service service = mlt_service_producer( mlt_consumer_service( this ) );
69
70 // Get the properties object
71 mlt_properties properties = mlt_consumer_properties( this );
72
73 // Get all the properties now
74 char *server = mlt_properties_get( properties, "server" );
75 int port = mlt_properties_get_int( properties, "port" );
76 char *command = mlt_properties_get( properties, "command" );
77 int unit = mlt_properties_get_int( properties, "unit" );
78 char *title = mlt_properties_get( properties, "title" );
79
80 // If this is a reuse, then a valerie object will exist
81 valerie connection = mlt_properties_get_data( properties, "connection", NULL );
82
83 if ( service != NULL )
84 {
85 // Initiate the connection if required
86 if ( connection == NULL )
87 {
88 valerie_parser parser = valerie_parser_init_remote( server, port );
89 connection = valerie_init( parser );
90 if ( valerie_connect( connection ) == valerie_ok )
91 {
92 mlt_properties_set_data( properties, "connection", connection, 0, ( mlt_destructor )valerie_close, NULL );
93 mlt_properties_set_data( properties, "parser", parser, 0, ( mlt_destructor )valerie_parser_close, NULL );
94 }
95 else
96 {
97 fprintf( stderr, "Unable to connect to the server at %s:%d\n", server, port );
98 valerie_close( connection );
99 valerie_parser_close( parser );
100 connection = NULL;
101 }
102 }
103
104 // If we have connection, push the service over
105 if ( connection != NULL )
106 {
107 int error;
108
109 // Set the title if provided
110 if ( title != NULL )
111 mlt_properties_set( mlt_service_properties( service ), "title", title );
112 else if ( mlt_properties_get( mlt_service_properties( service ), "title" ) == NULL )
113 mlt_properties_set( mlt_service_properties( service ), "title", "Anonymous Submission" );
114
115 // Push the service
116 error = valerie_unit_push( connection, unit, command, service );
117
118 // Report error
119 if ( error != valerie_ok )
120 fprintf( stderr, "Push failed on %s:%d %s u%d (%d)\n", server, port, command, unit, error );
121 }
122 }
123
124 mlt_consumer_stop( this );
125 mlt_consumer_stopped( this );
126
127 return 0;
128 }
129
130 static int consumer_is_stopped( mlt_consumer this )
131 {
132 return 1;
133 }