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 <valerie/valerie.h>
22 #include <valerie/valerie_remote.h>
23 #include <framework/mlt.h>
30 static int consumer_is_stopped( mlt_consumer
this );
31 static int consumer_start( mlt_consumer
this );
33 /** This is what will be called by the factory
36 mlt_consumer
consumer_valerie_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *arg
)
38 // Create the consumer object
39 mlt_consumer
this = calloc( sizeof( struct mlt_consumer_s
), 1 );
41 // If no malloc'd and consumer init ok
42 if ( this != NULL
&& mlt_consumer_init( this, NULL
, profile
) == 0 )
44 if ( arg
!= NULL
&& strchr( arg
, ':' ) )
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
);
55 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "server", arg
== NULL ?
"localhost" : arg
);
56 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "port", 5250 );
59 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "unit", 0 );
60 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "command", "append" );
62 // Allow thread to be started/stopped
63 this->start
= consumer_start
;
64 this->is_stopped
= consumer_is_stopped
;
66 // Return the consumer produced
70 // malloc or consumer init failed
77 static int consumer_start( mlt_consumer
this )
79 // Get the producer service
80 mlt_service service
= mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
82 // Get the properties object
83 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
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" );
93 // If this is a reuse, then a valerie object will exist
94 valerie connection
= mlt_properties_get_data( properties
, "connection", NULL
);
96 // Special case - we can get a doc too...
97 char *doc
= mlt_properties_get( properties
, "westley" );
99 // Set the title if provided
100 if ( service
!= 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" );
109 strcpy( command
, cmd
== NULL ?
"" : cmd
);
110 if ( strstr( command
, "title=" ) == NULL
&& title
!= NULL
)
112 strcat( command
, " title=\"" );
113 strcat( command
, title
);
114 strcat( command
, "\"" );
117 if ( service
!= NULL
|| doc
!= NULL
)
119 // Initiate the connection if required
120 if ( connection
== NULL
)
122 valerie_parser parser
= valerie_parser_init_remote( server
, port
);
123 connection
= valerie_init( parser
);
124 if ( valerie_connect( connection
) == valerie_ok
)
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
);
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
);
139 // If we have connection, push the service over
140 if ( connection
!= NULL
)
147 error
= valerie_unit_push( connection
, unit
, command
, service
);
150 if ( error
!= valerie_ok
)
151 fprintf( stderr
, "Push failed on %s:%d %s u%d (%d)\n", server
, port
, command
, unit
, error
);
156 int error
= valerie_unit_receive( connection
, unit
, command
, doc
);
159 if ( error
!= valerie_ok
)
160 fprintf( stderr
, "Send failed on %s:%d %s u%d (%d)\n", server
, port
, command
, unit
, error
);
165 mlt_consumer_stop( this );
166 mlt_consumer_stopped( this );
171 static int consumer_is_stopped( mlt_consumer
this )