Commit 17a89393 authored by Simon Latapie's avatar Simon Latapie

* vlm.c,vlm.h, telnet.c: new vlm message system, independant from

   telnet interface (will be useful for http vlm interface).
parent d756feeb
......@@ -125,7 +125,8 @@ typedef struct
int i_tel_cmd; // for specific telnet commands
} telnet_client_t;
static void Write_message( telnet_client_t * , char * , int );
static char* MessageToString( vlm_message_t* , int );
static void Write_message( telnet_client_t * , vlm_message_t* , char * , int );
static int SocketListen( intf_thread_t * , int );
struct intf_sys_t
......@@ -242,7 +243,7 @@ static void Run( intf_thread_t *p_intf )
cl->fd = fd;
cl->buffer_write = NULL;
cl->p_buffer_write = cl->buffer_write;
Write_message( cl, "Password:\xff\xfb\x01", WRITE_MODE_PWD );
Write_message( cl, NULL, "Password:\xff\xfb\x01", WRITE_MODE_PWD );
TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
}
......@@ -350,7 +351,7 @@ static void Run( intf_thread_t *p_intf )
if( cl->p_buffer_read - cl->buffer_read == 999 ) // too long !
{
Write_message( cl , "Line too long\n" , cl->i_mode + 2 );
Write_message( cl , NULL, "Line too long\n" , cl->i_mode + 2 );
}
}
}
......@@ -369,12 +370,12 @@ static void Run( intf_thread_t *p_intf )
*cl->p_buffer_read = '\0';
if( strcmp( s_password, cl->buffer_read ) == 0 )
{
Write_message( cl , "\xff\xfc\x01\nWelcome, Master\n> ", WRITE_MODE_CMD );
Write_message( cl , NULL, "\xff\xfc\x01\nWelcome, Master\n> ", WRITE_MODE_CMD );
}
else
{
/* wrong password */
Write_message( cl , "\n\rTry again, you polio:\n" , WRITE_MODE_PWD );
Write_message( cl , NULL, "\n\rTry again, you polio:\n" , WRITE_MODE_PWD );
}
}
else if( cl->i_mode == READ_MODE_CMD && *cl->p_buffer_read == '\n' )
......@@ -395,32 +396,48 @@ static void Run( intf_thread_t *p_intf )
}
else
{
char *message;
vlm_message_t *message;
/* create a standard string */
*cl->p_buffer_read = '\0';
vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read , &message);
Write_message( cl , message , WRITE_MODE_CMD );
Write_message( cl , message, NULL , WRITE_MODE_CMD );
free( message );
vlm_MessageDelete( message );
}
}
}
}
}
static void Write_message( telnet_client_t * client, char * message, int i_mode )
static void Write_message( telnet_client_t * client, vlm_message_t * message, char * string_message, int i_mode )
{
char *psz_message;
client->p_buffer_read = client->buffer_read;
(client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
if( client->buffer_write ) free( client->buffer_write );
client->buffer_write = malloc( strlen( message ) + 1 );
strcpy( client->buffer_write , message );
/* generate the psz_message string */
if( message != NULL ) /* ok, look for vlm_message_t */
{
psz_message = MessageToString( message , 0 );
psz_message = realloc( psz_message , strlen( psz_message ) + strlen( "\n> " ) + 1 );
strcat( psz_message , "\n> " );
}
else /* it is a basic string_message */
{
psz_message = strdup( string_message );
}
client->buffer_write = malloc( strlen( psz_message ) + 1 );
strcpy( client->buffer_write , psz_message );
client->p_buffer_write = client->buffer_write;
client->i_buffer_write = strlen( message );
client->i_buffer_write = strlen( psz_message );
client->i_mode = i_mode;
free( psz_message );
}
/* Does what we want except select and accept */
......@@ -498,3 +515,48 @@ socket_failed:
return -1;
}
/* we need the level of the message to put a beautiful indentation.
first level is 0 */
static char* MessageToString( vlm_message_t* message , int i_level )
{
int i;
char *psz_message;
if( message == NULL )
{
return strdup( "" );
}
else if( i_level == 0 && message->i_child == 0 && message->psz_value == NULL ) /* a command is successful */
{
/* don't write anything */
return strdup( "" );
}
else
{
psz_message = strdup( "" );
psz_message = realloc( psz_message , strlen( psz_message ) + strlen( message->psz_name ) + i_level + 1 );
for( i = 0 ; i < i_level ; i++ )
{
strcat( psz_message , " " );
}
strcat( psz_message , message->psz_name );
if( message->psz_value )
{
psz_message = realloc( psz_message , strlen( psz_message ) + strlen( message->psz_value ) + 3 + 1 );
strcat( psz_message , " : " );
strcat( psz_message , message->psz_value );
}
for( i = 0 ; i < message->i_child ; i++ )
{
char *child_message = MessageToString( message->child[i] , i_level + 1 );
psz_message = realloc( psz_message , strlen( psz_message ) + strlen( child_message ) + 1 + 1 );
strcat( psz_message, "\n" );
strcat( psz_message, child_message );
free( child_message );
}
return psz_message;
}
}
......@@ -48,15 +48,18 @@ static int vlm_MediaDelete( vlm_t *, vlm_media_t *, char * );
static vlm_media_t *vlm_MediaSearch( vlm_t *, char * );
static int vlm_MediaSetup( vlm_media_t *, char *, char * );
static int vlm_MediaControl( vlm_t *, vlm_media_t *, char *, char * );
static char *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
static char *vlm_Help( vlm_t *, char * );
static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
static vlm_message_t *vlm_Help( vlm_t *, char * );
static vlm_message_t* vlm_MessageNew( char * , char * );
static vlm_message_t* vlm_MessageAdd( vlm_message_t*, vlm_message_t* );
static vlm_schedule_t *vlm_ScheduleNew( vlm_t *, char *);
static int vlm_ScheduleDelete( vlm_t *, vlm_schedule_t *, char *);
static int vlm_ScheduleSetup( vlm_schedule_t *, char *, char *);
static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char *);
static int ExecuteCommand( vlm_t *, char * , char **);
static int ExecuteCommand( vlm_t *, char * , vlm_message_t **);
static int Manage( vlc_object_t* );
......@@ -180,7 +183,7 @@ static char *FindEndCommand( char *psz )
#endif
int vlm_ExecuteCommand( vlm_t *vlm, char *command, char **message)
int vlm_ExecuteCommand( vlm_t *vlm, char *command, vlm_message_t **message)
{
int result;
......@@ -192,13 +195,14 @@ int vlm_ExecuteCommand( vlm_t *vlm, char *command, char **message)
}
/* Execute a command which ends by '\0' (string) */
int ExecuteCommand( vlm_t *vlm, char *command , char **message)
int ExecuteCommand( vlm_t *vlm, char *command , vlm_message_t **p_message)
{
int i_return = 0;
int i_command = 0;
char **p_command = NULL;
char *cmd = command;
int i;
vlm_message_t * message = NULL;
/* First, parse the line and cut it */
while( *cmd != '\0' )
......@@ -237,7 +241,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( i_command == 0 )
{
*message = strdup( "" );
message = vlm_MessageNew( "" , NULL );
i_return = 0;
goto end_seq;
}
......@@ -260,8 +264,10 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
/* new vlm_schedule */
if( vlm_ScheduleSearch( vlm , p_command[1] ) != NULL || strcmp(p_command[1] , "schedule") == 0 )
{
*message = malloc( (strlen(p_command[1]) + 18) * sizeof(char) );
sprintf( *message , "%s is already used\n" , p_command[1] );
char *error_message = malloc( strlen(p_command[1]) + strlen(" is already used") + 1 );
sprintf( error_message, "%s is already used" , p_command[1] );
message = vlm_MessageNew( "new" , error_message );
free( error_message );
i_return = 1;
goto end_seq;
}
......@@ -306,19 +312,19 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
else
{
vlm_ScheduleDelete( vlm, schedule, NULL );
*message = strdup( "Wrong properties syntax\n" );
message = vlm_MessageNew( p_command[i], "Wrong properties syntax" );
i_return = 1;
goto end_seq;
}
}
}
*message = strdup( "" );
message = vlm_MessageNew( "new" , NULL );
i_return = 0;
goto end_seq;
}
*message = strdup( "" );
message = vlm_MessageNew( "new" , NULL );
i_return = 0;
goto end_seq;
}
......@@ -333,7 +339,10 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
}
else
{
*message = strdup( "Choose between vod or broadcast\n" );
char *error_message = malloc( strlen(p_command[1]) + strlen(": Choose between vod or broadcast") + 1 );
sprintf( error_message, "%s: Choose between vod or broadcast" , p_command[1] );
message = vlm_MessageNew( "new" , error_message );
free( error_message );
i_return = 1;
goto end_seq;
}
......@@ -341,8 +350,10 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
/* new vlm_media */
if( vlm_MediaSearch( vlm , p_command[1] ) != NULL || strcmp(p_command[1] , "media") == 0 )
{
*message = malloc( (strlen(p_command[1]) + 18) * sizeof(char) );
sprintf( *message , "%s is already used\n" , p_command[1] );
char *error_message = malloc( strlen(p_command[1]) + strlen(" is already used") + 1 );
sprintf( error_message, "%s is already used" , p_command[1] );
message = vlm_MessageNew( "new" , error_message );
free( error_message );
i_return = 1;
goto end_seq;
}
......@@ -367,25 +378,25 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
else
{
vlm_MediaDelete( vlm, media, NULL );
*message = strdup( "Wrong properties syntax\n" );
message = vlm_MessageNew( p_command[i] , "Wrong properties syntax" );
i_return = 1;
goto end_seq;
}
}
}
*message = strdup( "" );
message = vlm_MessageNew( "new" , NULL );
i_return = 0;
goto end_seq;
}
*message = strdup( "" );
message = vlm_MessageNew( "new" , NULL );
i_return = 0;
goto end_seq;
}
else
{
*message = strdup( "Wrong new command syntax\n" );
message = vlm_MessageNew( "new" , "Wrong command syntax" );
i_return = 1;
goto end_seq;
}
......@@ -403,14 +414,14 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( schedule != NULL )
{
vlm_ScheduleDelete( vlm, schedule, NULL );
*message = strdup( "" );
message = vlm_MessageNew( "del" , NULL );
i_return = 0;
goto end_seq;
}
else if( media != NULL )
{
vlm_MediaDelete( vlm, media, NULL );
*message = strdup( "" );
message = vlm_MessageNew( "del" , NULL );
i_return = 0;
goto end_seq;
}
......@@ -442,15 +453,17 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
}
else
{
*message = malloc( (strlen(p_command[1]) + 16) * sizeof(char) );
sprintf( *message , "%s media unknown\n" , p_command[1] );
char *error_message = malloc( strlen(p_command[1]) + strlen(": media unknown") + 1 );
sprintf( error_message, "%s: media unknown" , p_command[1] );
message = vlm_MessageNew( "del" , error_message );
free( error_message );
i_return = 1;
goto end_seq;
}
}
else
{
*message = strdup( "Wrong setup command syntax\n" );
message = vlm_MessageNew( "setup" , "Wrong command syntax" );
i_return = 1;
goto end_seq;
}
......@@ -459,7 +472,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
{
if( i_command == 1 )
{
*message = vlm_Show( vlm, NULL , NULL, NULL );
message = vlm_Show( vlm, NULL , NULL, NULL );
i_return = 0;
goto end_seq;
}
......@@ -473,15 +486,15 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( schedule != NULL )
{
*message = vlm_Show( vlm, NULL, schedule, NULL );
message = vlm_Show( vlm, NULL, schedule, NULL );
}
else if( media != NULL )
{
*message = vlm_Show( vlm, media, NULL, NULL );
message = vlm_Show( vlm, media, NULL, NULL );
}
else
{
*message = vlm_Show( vlm, NULL, NULL, p_command[1] );
message = vlm_Show( vlm, NULL, NULL, p_command[1] );
}
i_return = 0;
......@@ -489,7 +502,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
}
else
{
*message = strdup( "Wrong show command syntax\n" );
message = vlm_MessageNew( "show" , "Wrong command syntax" );
i_return = 1;
goto end_seq;
}
......@@ -498,13 +511,13 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
{
if( i_command == 1 )
{
*message = vlm_Help( vlm, NULL );
message = vlm_Help( vlm, NULL );
i_return = 0;
goto end_seq;
}
else
{
*message = strdup( "Wrong help command syntax\n" );
message = vlm_MessageNew( "help" , "Wrong command syntax" );
i_return = 1;
goto end_seq;
}
......@@ -556,14 +569,14 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
else
{
vlm_ScheduleDelete( vlm, schedule, NULL );
*message = strdup( "Wrong properties syntax\n" );
message = vlm_MessageNew( "setup" , "Wrong properties syntax" );
i_return = 1;
goto end_seq;
}
}
}
*message = strdup( "" );
message = vlm_MessageNew( "setup" , NULL );
i_return = 0;
goto end_seq;
}
......@@ -579,7 +592,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
{
if( media->i_type != BROADCAST_TYPE )
{
*message = strdup( "loop is only available for broadcast\n" );
message = vlm_MessageNew( "setup" , "lool only available for broadcast" );
i_return = 1;
goto end_seq;
}
......@@ -598,21 +611,23 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
else
{
vlm_MediaDelete( vlm, media, NULL );
*message = strdup( "Wrong properties syntax\n" );
message = vlm_MessageNew( "setup" , "Wrong properties syntax" );
i_return = 1;
goto end_seq;
}
}
}
*message = strdup( "" );
message = vlm_MessageNew( "setup" , NULL );
i_return = 0;
goto end_seq;
}
else
{
*message = malloc( (strlen(p_command[1]) + 28) * sizeof(char) );
sprintf( *message , "%s media or schedule unknown\n" , p_command[1] );
char *error_message = malloc( strlen(p_command[1]) + strlen(" unknown") + 1 );
sprintf( error_message, "%s unknown" , p_command[1] );
message = vlm_MessageNew( "setup" , error_message );
free( error_message );
i_return = 1;
goto end_seq;
}
......@@ -620,7 +635,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
}
else
{
*message = strdup( "Wrong setup command syntax\n" );
message = vlm_MessageNew( "setup" , "Wrong command syntax" );
i_return = 1;
goto end_seq;
}
......@@ -636,8 +651,10 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( media == NULL )
{
*message = malloc( (strlen(p_command[1]) + 16) * sizeof(char) );
sprintf( *message , "%s media unknown\n" , p_command[1] );
char *error_message = malloc( strlen(p_command[1]) + strlen(": media unknown") + 1 );
sprintf( error_message, "%s: media unknown" , p_command[1] );
message = vlm_MessageNew( "control" , error_message );
free( error_message );
i_return = 1;
goto end_seq;
}
......@@ -656,14 +673,14 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
/* for now */
vlm_MediaControl( vlm, media, p_command[2], psz_args );
*message = strdup( "" );
message = vlm_MessageNew( "control" , NULL );
i_return = 0;
goto end_seq;
}
}
else
{
*message = strdup( "Wrong control command syntax\n" );
message = vlm_MessageNew( "control" , "Wrong command syntax" );
i_return = 1;
goto end_seq;
}
......@@ -679,7 +696,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( file == NULL )
{
*message = strdup( "Unable to save file\n" );
message = vlm_MessageNew( "save" , "Unable to save file" );
i_return = 1;
goto end_seq;
}
......@@ -692,14 +709,14 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
fwrite( save, strlen( save ) , 1 , file );
fclose( file );
free( save );
*message = strdup( "" );
message = vlm_MessageNew( "save" , NULL );
i_return = 0;
goto end_seq;
}
}
else
{
*message = strdup( "Wrong save command\n" );
message = vlm_MessageNew( "save" , "Wrong command" );
i_return = 1;
goto end_seq;
}
......@@ -715,7 +732,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( file == NULL )
{
*message = strdup( "Unable to load file\n" );
message = vlm_MessageNew( "load" , "Unable to load file" );
i_return = 1;
goto end_seq;
}
......@@ -734,7 +751,7 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
if( vlm_Load( vlm, buffer ) )
{
free( buffer );
*message = strdup( "error while loading file\n" );
message = vlm_MessageNew( "load" , "error while loading file" );
i_return = 1;
goto end_seq;
}
......@@ -742,26 +759,26 @@ int ExecuteCommand( vlm_t *vlm, char *command , char **message)
}
else
{
*message = strdup( "read file error\n" );
message = vlm_MessageNew( "load" , "read file error" );
i_return = 1;
goto end_seq;
}
fclose( file );
*message = strdup( "" );
message = vlm_MessageNew( "load" , NULL );
i_return = 0;
goto end_seq;
}
}
else
{
*message = strdup( "Wrong save command\n" );
message = vlm_MessageNew( "load" , "Wrong command" );
i_return = 1;
goto end_seq;
}
}
else
{
*message = strdup( "Unknown comand\n" );
message = vlm_MessageNew( p_command[0] , "Unknown comand" );
i_return = 1;
goto end_seq;
}
......@@ -773,15 +790,12 @@ end_seq:
free( p_command[i] );
}
/* Add the prompt */
*message = realloc( *message , strlen(*message) + 4 );
sprintf( *message , "%s\n> " , *message );
*p_message = message;
return i_return;
}
static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
{
int i;
......@@ -1045,77 +1059,61 @@ static int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_name, cha
return 1;
}
static char *vlm_Show( vlm_t *vlm, vlm_media_t *media, vlm_schedule_t *schedule, char *psz_filter )
static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media, vlm_schedule_t *schedule, char *psz_filter )
{
if( media != NULL )
{
char *show;
int i;
vlm_message_t *message;
vlm_message_t *message_media;
vlm_message_t *message_child;
show = malloc( strlen( media->psz_name ) + 1 + 1 );
sprintf( show, "\n%s" , media->psz_name );
message = vlm_MessageNew( "show" , NULL );
message_media = vlm_MessageAdd( message , vlm_MessageNew( media->psz_name , NULL ) );
if( media->i_type == VOD_TYPE )
{
show = realloc( show, strlen( show ) + 4 + 1 ); // don't forget the '\0'
strcat( show , "\nvod" );
vlm_MessageAdd( message_media , vlm_MessageNew( "type" , "vod" ) );
}
else
{
show = realloc( show, strlen( show ) + 10 + 1 ); // don't forget the '\0'
strcat( show , "\nbroadcast" );
vlm_MessageAdd( message_media , vlm_MessageNew( "type" , "broadcast" ) );
}
if( media->b_enabled == VLC_TRUE )
{
show = realloc( show, strlen( show ) + 8 + 1 ); // don't forget the '\0'
strcat( show , "\nenabled" );
vlm_MessageAdd( message_media , vlm_MessageNew( "enabled" , "yes" ) );
}
else
{
show = realloc( show, strlen( show ) + 9 + 1 ); // don't forget the '\0'
strcat( show , "\ndisabled" );
vlm_MessageAdd( message_media , vlm_MessageNew( "enabled" , "no" ) );
}
if( media->b_loop == VLC_TRUE )
{
show = realloc( show, strlen( show ) + 5 + 1 ); // don't forget the '\0'
strcat( show , "\nloop" );
vlm_MessageAdd( message_media , vlm_MessageNew( "loop" , "yes" ) );
}
show = realloc( show, strlen( show ) + 9 + 1 ); // don't forget the '\0'
strcat( show , "\ninputs: " );
message_child = vlm_MessageAdd( message_media , vlm_MessageNew( "inputs" , NULL ) );
for( i=0 ; i < (media->i_input) ; i++ )
{
show = realloc( show, strlen( show ) + strlen( media->input[i] ) + 2 + 1 ); // don't forget the '\0' and '\n'
strcat( show , "\n " );
strcat( show , media->input[i] );
vlm_MessageAdd( message_child , vlm_MessageNew( media->input[i] , NULL ) );
}
show = realloc( show, strlen( show ) + 10 + 1 ); // don't forget the '\0'
strcat( show , "\noutput: " );
if( media->psz_output != NULL )
{
show = realloc( show, strlen( show ) + strlen( media->psz_output ) + 1 ); // don't forget the '\0' and '\n'