Merge ../mlt
[melted] / src / modules / valerie / consumer_valerie.c
1 /*
2 * consumer_valerie.c -- pushes a service via valerie
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@telenet.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <valerie/valerie.h>
22 #include <valerie/valerie_remote.h>
23 #include <framework/mlt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <unistd.h>
29
30 static int consumer_is_stopped( mlt_consumer this );
31 static int consumer_start( mlt_consumer this );
32
33 /** This is what will be called by the factory
34 */
35
36 mlt_consumer consumer_valerie_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
37 {
38 // Create the consumer object
39 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
40
41 // If no malloc'd and consumer init ok
42 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
43 {
44 if ( arg != NULL && strchr( arg, ':' ) )
45 {
46 char *temp = NULL;
47 int port = atoi( strchr( arg, ':' ) + 1 );
48 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "server", arg );
49 temp = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "server" );
50 *( strchr( temp, ':' ) ) = '\0';
51 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "port", port );
52 }
53 else
54 {
55 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "server", arg == NULL ? "localhost" : arg );
56 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "port", 5250 );
57 }
58
59 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "unit", 0 );
60 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "command", "append" );
61
62 // Allow thread to be started/stopped
63 this->start = consumer_start;
64 this->is_stopped = consumer_is_stopped;
65
66 // Return the consumer produced
67 return this;
68 }
69
70 // malloc or consumer init failed
71 free( this );
72
73 // Indicate failure
74 return NULL;
75 }
76
77 static int consumer_start( mlt_consumer this )
78 {
79 // Get the producer service
80 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
81
82 // Get the properties object
83 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
84
85 // Get all the properties now
86 char *server = mlt_properties_get( properties, "server" );
87 int port = mlt_properties_get_int( properties, "port" );
88 char *cmd = mlt_properties_get( properties, "command" );
89 int unit = mlt_properties_get_int( properties, "unit" );
90 char *title = mlt_properties_get( properties, "title" );
91 char command[ 2048 ];
92
93 // If this is a reuse, then a valerie object will exist
94 valerie connection = mlt_properties_get_data( properties, "connection", NULL );
95
96 // Special case - we can get a doc too...
97 char *doc = mlt_properties_get( properties, "westley" );
98
99 // Set the title if provided
100 if ( service != NULL )
101 {
102 if ( title != NULL )
103 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", title );
104 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
105 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
106 title = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" );
107 }
108
109 strcpy( command, cmd == NULL ? "" : cmd );
110 if ( strstr( command, "title=" ) == NULL && title != NULL )
111 {
112 strcat( command, " title=\"" );
113 strcat( command, title );
114 strcat( command, "\"" );
115 }
116
117 if ( service != NULL || doc != NULL )
118 {
119 // Initiate the connection if required
120 if ( connection == NULL )
121 {
122 valerie_parser parser = valerie_parser_init_remote( server, port );
123 connection = valerie_init( parser );
124 if ( valerie_connect( connection ) == valerie_ok )
125 {
126 mlt_properties_set_data( properties, "connection", connection, 0, ( mlt_destructor )valerie_close, NULL );
127 mlt_properties_set_data( properties, "parser", parser, 0, ( mlt_destructor )valerie_parser_close, NULL );
128 }
129 else
130 {
131 fprintf( stderr, "Unable to connect to the server at %s:%d\n", server, port );
132 mlt_properties_set_int( properties, "_error", 1 );
133 valerie_close( connection );
134 valerie_parser_close( parser );
135 connection = NULL;
136 }
137 }
138
139 // If we have connection, push the service over
140 if ( connection != NULL )
141 {
142 if ( doc == NULL )
143 {
144 int error;
145
146 // Push the service
147 error = valerie_unit_push( connection, unit, command, service );
148
149 // Report error
150 if ( error != valerie_ok )
151 fprintf( stderr, "Push failed on %s:%d %s u%d (%d)\n", server, port, command, unit, error );
152 }
153 else
154 {
155 // Push the service
156 int error = valerie_unit_receive( connection, unit, command, doc );
157
158 // Report error
159 if ( error != valerie_ok )
160 fprintf( stderr, "Send failed on %s:%d %s u%d (%d)\n", server, port, command, unit, error );
161 }
162 }
163 }
164
165 mlt_consumer_stop( this );
166 mlt_consumer_stopped( this );
167
168 return 0;
169 }
170
171 static int consumer_is_stopped( mlt_consumer this )
172 {
173 return 1;
174 }