summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-12-21 13:39:06 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2014-12-21 13:39:06 +0100
commite8d7e3401051dd9d8e2992ae0ed0d5149a6bdcc5 (patch)
treeeaeab3af96f568dd04e27d31be3471b7fe9dc9ea
parent4ed9394c0c2ba0ae65a20861fa998d7b701f8640 (diff)
downloadbiruda-e8d7e3401051dd9d8e2992ae0ed0d5149a6bdcc5.tar.gz
biruda-e8d7e3401051dd9d8e2992ae0ed0d5149a6bdcc5.tar.bz2
added a tail of worker output messages (needed for a web client showing the last lines of output)
-rw-r--r--src/master.c50
-rw-r--r--src/master.h1
-rw-r--r--src/webserver.c11
3 files changed, 59 insertions, 3 deletions
diff --git a/src/master.c b/src/master.c
index d22a489..ad266e2 100644
--- a/src/master.c
+++ b/src/master.c
@@ -451,7 +451,7 @@ static int master_output_write_terminated( const char *spool_dir, const char *na
return 0;
}
-void master_output_tail( const char *name, char *s, size_t len )
+void master_output_outstanding_messages( const char *name, char *s, size_t len )
{
worker_t *w = worker_by_name( name );
@@ -476,7 +476,53 @@ void master_output_tail( const char *name, char *s, size_t len )
fgets( line, sizeof( line ), wed->spool_file );
cur += snprintf( cur, end - cur, "%s", line );
wed->read_pos += strlen( line );
- fprintf( stderr, "%lu %lu %s", wed->read_pos, end_pos, line );
+ }
+}
+
+void master_output_tail( const char *name, char *s, size_t len )
+{
+ worker_t *w = worker_by_name( name );
+
+ *s = '\0';
+
+ if( w == NULL ) return;
+
+ execution_worker_data_t *wed = (execution_worker_data_t *)w->execution_data;
+
+ if( wed->spool_file == NULL ) return;
+
+ long end_pos = ftell( wed->spool_file );
+ fseek( wed->spool_file, 0, SEEK_SET );
+ long pos = 0;
+
+ char *last_lines[25];
+ memset( last_lines, 0, sizeof( last_lines ) );
+ int last_lines_pos = 0;
+ char line[100];
+ while( pos < end_pos ) {
+ fgets( line, sizeof( line ), wed->spool_file );
+ if( last_lines[last_lines_pos] != NULL ) {
+ free( last_lines[last_lines_pos] );
+ }
+ last_lines[last_lines_pos] = strdup( line );
+ last_lines_pos++;
+ if( last_lines_pos >= 25 ) {
+ last_lines_pos = 0;
+ }
+ pos += strlen( line );
+ }
+
+ char *cur = s;
+ char *end = s + len;
+ for( int i = last_lines_pos; i < 25; i++ ) {
+ if( last_lines[i] != NULL ) {
+ cur += snprintf( cur, end - cur, "%s", last_lines[i] );
+ }
+ }
+ for( int i = 0; i < last_lines_pos; i++ ) {
+ if( last_lines[i] != NULL ) {
+ cur += snprintf( cur, end - cur, "%s", last_lines[i] );
+ }
}
}
diff --git a/src/master.h b/src/master.h
index 77ca3f5..b135b35 100644
--- a/src/master.h
+++ b/src/master.h
@@ -11,6 +11,7 @@ int master_free( );
int master_start_worker( const char *name );
int master_stop_worker( const char *name );
+void master_output_outstanding_messages( const char *name, char *s, size_t len );
void master_output_tail( const char *name, char *s, size_t len );
#define MAX_COORDINATORS 128
diff --git a/src/webserver.c b/src/webserver.c
index f30a811..9b62f0e 100644
--- a/src/webserver.c
+++ b/src/webserver.c
@@ -60,11 +60,20 @@ static int handle_request( void *cls, struct MHD_Connection *connection,
const char *name = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "name" );
if( name != NULL ) {
+ master_output_outstanding_messages( name, biruda_msg, sizeof( biruda_msg ) );
+ } else {
+ return MHD_NO;
+ }
+ } else if( strcmp( op, "tail" ) == 0 ) {
+
+ const char *name = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "name" );
+
+ if( name != NULL ) {
master_output_tail( name, biruda_msg, sizeof( biruda_msg ) );
} else {
return MHD_NO;
}
- }
+ }
} else {
snprintf( biruda_msg, sizeof( biruda_msg ), "Welcome to biruda! Please state your wish..\n" );