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>
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.
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.
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
21 #include "consumer_valerie.h"
22 #include <valerie/valerie.h>
23 #include <valerie/valerie_remote.h>
24 #include <framework/mlt.h>
31 static int consumer_is_stopped( mlt_consumer
this );
32 static int consumer_start( mlt_consumer
this );
34 /** This is what will be called by the factory
37 mlt_consumer
consumer_valerie_init( char *arg
)
39 // Create the consumer object
40 mlt_consumer
this = calloc( sizeof( struct mlt_consumer_s
), 1 );
42 // If no malloc'd and consumer init ok
43 if ( this != NULL
&& mlt_consumer_init( this, NULL
) == 0 )
45 if ( arg
!= NULL
&& strchr( arg
, ':' ) )
48 int port
= atoi( strchr( arg
, ':' ) + 1 );
49 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "server", arg
);
50 temp
= mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "server" );
51 *( strchr( temp
, ':' ) ) = '\0';
52 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "port", port
);
56 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "server", arg
== NULL ?
"localhost" : arg
);
57 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "port", 5250 );
60 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "unit", 0 );
61 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "command", "append" );
63 // Allow thread to be started/stopped
64 this->start
= consumer_start
;
65 this->is_stopped
= consumer_is_stopped
;
67 // Return the consumer produced
71 // malloc or consumer init failed
78 static int consumer_start( mlt_consumer
this )
80 // Get the producer service
81 mlt_service service
= mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
83 // Get the properties object
84 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
86 // Get all the properties now
87 char *server
= mlt_properties_get( properties
, "server" );
88 int port
= mlt_properties_get_int( properties
, "port" );
89 char *cmd
= mlt_properties_get( properties
, "command" );
90 int unit
= mlt_properties_get_int( properties
, "unit" );
91 char *title
= mlt_properties_get( properties
, "title" );
94 // If this is a reuse, then a valerie object will exist
95 valerie connection
= mlt_properties_get_data( properties
, "connection", NULL
);
97 // Special case - we can get a doc too...
98 char *doc
= mlt_properties_get( properties
, "westley" );
100 // Set the title if provided
101 if ( service
!= NULL
)
104 mlt_properties_set( MLT_SERVICE_PROPERTIES( service
), "title", title
);
105 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service
), "title" ) == NULL
)
106 mlt_properties_set( MLT_SERVICE_PROPERTIES( service
), "title", "Anonymous Submission" );
107 title
= mlt_properties_get( MLT_SERVICE_PROPERTIES( service
), "title" );
110 strcpy( command
, cmd
== NULL ?
"" : cmd
);
111 if ( strstr( command
, "title=" ) == NULL
&& title
!= NULL
)
113 strcat( command
, " title=\"" );
114 strcat( command
, title
);
115 strcat( command
, "\"" );
118 if ( service
!= NULL
|| doc
!= NULL
)
120 // Initiate the connection if required
121 if ( connection
== NULL
)
123 valerie_parser parser
= valerie_parser_init_remote( server
, port
);
124 connection
= valerie_init( parser
);
125 if ( valerie_connect( connection
) == valerie_ok
)
127 mlt_properties_set_data( properties
, "connection", connection
, 0, ( mlt_destructor
)valerie_close
, NULL
);
128 mlt_properties_set_data( properties
, "parser", parser
, 0, ( mlt_destructor
)valerie_parser_close
, NULL
);
132 fprintf( stderr
, "Unable to connect to the server at %s:%d\n", server
, port
);
133 mlt_properties_set_int( properties
, "_error", 1 );
134 valerie_close( connection
);
135 valerie_parser_close( parser
);
140 // If we have connection, push the service over
141 if ( connection
!= NULL
)
148 error
= valerie_unit_push( connection
, unit
, command
, service
);
151 if ( error
!= valerie_ok
)
152 fprintf( stderr
, "Push failed on %s:%d %s u%d (%d)\n", server
, port
, command
, unit
, error
);
157 int error
= valerie_unit_receive( connection
, unit
, command
, doc
);
160 if ( error
!= valerie_ok
)
161 fprintf( stderr
, "Send failed on %s:%d %s u%d (%d)\n", server
, port
, command
, unit
, error
);
166 mlt_consumer_stop( this );
167 mlt_consumer_stopped( this );
172 static int consumer_is_stopped( mlt_consumer
this )