summaryrefslogtreecommitdiff
path: root/src/cli.c
blob: 2c4dde80192186ee6eb39b7566e0eb8b3a2770f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include "cli.h"

#ifndef _WIN32
#include <strings.h>
#endif
#include <ctype.h>
#include <stdarg.h>
#ifndef _WIN32
#include "linenoise.h"
#endif
#ifndef _WIN32
#include "http_lib.h"
#endif
#include "json-c/json.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define HISTORY_FILE ".biruda_history"

#include "coordinator.h"
#include "worker.h"
#include "system.h"

#ifndef _WIN32

typedef enum {
	COMMAND,	// command parsing
	START_WORKER,	// worker commands expect a worker (and
	STOP_WORKER,	// optionally an architecture and os parameter)
	MESSAGES_WORKER,
	WAIT		// wait state, allow autodisp to work, but wait
			// for commands for n seconds (handy in batches
			// and for testing)
} command_state_t;

typedef enum {
	WORKER_ILLEGAL,	// not in a worker related state
	WORKER_NAME,	// name of the worker
	WORKER_OS,	// in case there is more than one worker with that name
	WORKER_ARCH,	// architecture, if still ambiguos
	WORKER_EXECUTE	// all data, execute worker command now
} command_worker_state_t;

static char *commands[] = {
	"help", "quit", "status", "start", "stop",
	"autodisp", "messages", "wait", NULL
};

static bool print_colors = false;

static bool is_interactive = false;

static command_state_t command_state = COMMAND;

static command_worker_state_t command_worker_state = WORKER_ILLEGAL; 

static char *worker_names[MAX_WORKERS];

static int nof_worker_names = 0;

static bool autodisp_toggle = false;

static unsigned int wait_time = 0;

static void cleanup_worker_names( )
{
	for( int i = 0; i < nof_worker_names; i++ ) {
		free( worker_names[i] );
	}
	nof_worker_names = 0;
}

static void print_error( const char *fmt, ... );

static void get_workers( )
{
	char *url = "status";
	char *data = NULL;
	int len;
	http_retcode ret = http_get( url, &data, &len, NULL );
	if( ret == 200 ) {
		cleanup_worker_names( );
		
		char *line = strtok( data, "\n" );
		while( line != NULL ) {
			char *p = strchr( line, ' ' );
			if( p == NULL ) {
				line = strtok( NULL, "\n" );
				continue;
			}
			if( strncmp( line, "worker", p - line ) != 0 ) {
				line = strtok( NULL, "\n" );
				continue;
			}
			char *pp = strchr( p+1, ' ' );
			if( pp == NULL ) {
				line = strtok( NULL, "\n" );
				continue;
			}
			char s = *pp;
			*pp = '\0';
			if( nof_worker_names >= MAX_WORKERS ) {
				fprintf( stderr, "ERROR: Too many workers seen, ignoring rest!\n" );
				free( data );
				return;
			}
			bool duplicate = false;
			for( int j = 0; j < nof_worker_names; j++ ) {
				if( strcmp( worker_names[j], p + 1 ) == 0 ) {
					duplicate = true;
				}
			}
			if( !duplicate ) {
				worker_names[nof_worker_names++] = strdup( p + 1 );
			}
			*pp = s;
			line = strtok( NULL, "\n" );
		}
	} else {
		print_error( "ERROR: HTTP error %d", ret );
	}
	free( data );
}

static void print_workers( )
{
	printf( "available workers: " );
	for( int i = 0; i < nof_worker_names; i++ ) {
		if( i != 0 ) printf( " " );
		printf( worker_names[i] );
	}
	puts( "" );
}

static void completion_func( const char *buf, linenoiseCompletions *lc )
{
	unsigned int i;
	size_t len = strlen( buf );

	switch( command_state ) {
		case COMMAND:
			for (i = 0; commands[i]; ++i) {
				char *cmd = commands[i];
				if( strlen( cmd ) < len ) continue;
				if( strncasecmp( buf, cmd, len ) == 0 ) {
					linenoiseAddCompletion( lc, cmd );
				}
			}
			break;
					
		case START_WORKER:
		case STOP_WORKER:
		case MESSAGES_WORKER:
			get_workers( );
			for( int i = 0; i < nof_worker_names; i++ ) {
				linenoiseAddCompletion( lc, worker_names[i] );
			}
			break;
		
		case WAIT:
			// no completion
			break;
	}		
}

static void print_help( )
{
	puts( "\n"
		"   help     - show this help page\n"
		"   quit     - quit the client\n"
		"   status   - status of the biruda network\n"
		"   start    - start a worker manually\n"
		"   stop     - stop a worker manually\n"
		"   messages - show output of workers\n"
		"   autodisp - automatically show messages (toggle)\n"
		"   wait     - wait n seconds before next command\n"
	);
}

static void print_colored( const char *fmt, int color, va_list ap )
{
	char buf[1024];
	
	(void)vsnprintf( buf, sizeof( buf ), fmt, ap );
	
	if( print_colors ) {
		printf( "%c[9%dm%s%c[0m\n", 27, color, buf, 27 );
	} else {
		puts( buf );
	}
}
	
static void print_error( const char *fmt, ... )
{
	va_list ap;
	va_start( ap, fmt );
	print_colored( fmt, 1, ap );
	va_end( ap );
}

static void print_answer( const char *fmt, ... )
{
	va_list ap;
	va_start( ap, fmt );
	print_colored( fmt, 2, ap );
	va_end( ap );
}

static void print_message( const char *fmt, ... )
{
	va_list ap;
	va_start( ap, fmt );
	print_colored( fmt, 4, ap );
	va_end( ap );
}

static void print_status( )
{
	char *url = "status";
	char *data = NULL;
	int len;
	http_retcode ret = http_get( url, &data, &len, NULL );
	if( ret == 200 ) {
		if( strlen( data ) > 0 && data[len-1] == '\n' ) {
			data[len-1] = '\0';
			len--;
		}
		if( strlen( data ) > 0 && data[len-1] == '\r' ) {
			data[len-1] = '\0';
		}
		print_answer( data );
	} else {
		print_error( "ERROR: HTTP error %d", ret );
	}
	free( data );
}

static void print_worker_output( const char *worker_name )
{
	char url[128];
	snprintf( url, sizeof( url ), "worker?op=output&name=%s", worker_name );
	char *data = NULL;
	int len;
	http_retcode ret = http_get( url, &data, &len, NULL );
	if( ret == 200 ) {
		if( strlen( data ) > 0 && data[len-1] == '\n' ) {
			data[len-1] = '\0';
			len--;
		}
		if( strlen( data ) > 0 && data[len-1] == '\r' ) {
			data[len-1] = '\0';
			len--;
		}
		if( len > 0 ) {
			print_message( "output %s:", worker_name );
			print_answer( data );
		}
	} else if( ret == ERRNOLG ) {
		// ignore empty result, worker has no output currently
	} else {
		print_error( "ERROR: HTTP error %d", ret );
	}
	free( data );
}

static void print_messages( )
{
	get_workers( );
	for( int i = 0; i < nof_worker_names; i++ ) {
		print_worker_output( worker_names[i] );
	}
}

static void start_worker( const char *worker_name )
{
	char url[128];
	snprintf( url, sizeof( url ), "worker?op=start&name=%s", worker_name );
	http_retcode ret = http_post( url, "", 0, "Content-Type: text/plain" );
	if( ret == 200 ) {
		print_answer( "Request queued" );
	} else {
		print_error( "ERROR: HTTP error %d", ret );
	}
}

static void stop_worker( const char *worker_name )
{
	char url[128];
	snprintf( url, sizeof( url ), "worker?op=stop&name=%s", worker_name );
	http_retcode ret = http_post( url, "", 0, "Content-Type: text/plain" );
	if( ret == 200 ) {
		print_answer( "Request queued" );
	} else {
		print_error( "ERROR: HTTP error %d", ret );
	}
}

static void get_worker_data( )
{
	switch( command_worker_state ) {		
		case WORKER_NAME:
			get_workers( );
			print_workers( );
			command_worker_state = WORKER_EXECUTE;
			break;
		
		case WORKER_EXECUTE:
		case WORKER_ILLEGAL:
		case WORKER_ARCH:
		case WORKER_OS:
			/* later */
			print_error( "ERROR: internal worker state error" );
			break;
	}	
}			

#endif

#ifdef _WIN32
int start_interactive( bool colors, const char *host, unsigned short port, FILE *in )
{
	fprintf( stderr, "ERROR: Not implemented on Windows!\n" );
	return EXIT_FAILURE;
}

#else
int start_interactive( bool colors, const char *host, unsigned short port, FILE *in )
{
	char history_filename[1024];

	// for http_tidy, tell it where to issue requests to
	http_server = (char *)host;
	http_port = port;
	
	is_interactive = isatty( fileno( in ) );
	print_colors = is_interactive ? colors : false;
	autodisp_toggle = false;

	if( is_interactive ) {
		char *home = getenv( "HOME" );
		if( home != NULL ) {
			snprintf( history_filename, sizeof( history_filename ), "%s/%s", home, HISTORY_FILE );
			linenoiseHistoryLoad( history_filename );
			linenoiseSetCompletionCallback( completion_func );
		}
	}
	
	char *context = "biruda";
	for( ;; ) {
		char prompt[128];
		char buf[1024];
		char *line;
		
		switch( command_state ) {
			case COMMAND:
				context = "biruda";
				break;
			
			case START_WORKER:
			case STOP_WORKER:
			case MESSAGES_WORKER:
				context = "worker";
				break;
			
			case WAIT:
				context = "wait";
				break;
		}

		if( autodisp_toggle ) {
			print_messages( );
		}
		
		if( command_state == WAIT && wait_time > 0 ) {
			wait_time--;
			if( wait_time == 0 ) {
				command_state = COMMAND;
			} else {
				sleep( 1 );
				continue;
			}
		}
		
		if( is_interactive ) {
			snprintf( prompt, sizeof( prompt ), "%s> ", context );
			if( ( line = linenoise( prompt ) ) == NULL ) {
				switch( command_state ) {
					case COMMAND:
						cleanup_worker_names( );
						free( line );
						return EXIT_SUCCESS;
					
					case START_WORKER:
					case STOP_WORKER:
					case MESSAGES_WORKER:
					case WAIT:
						command_state = COMMAND;
						command_worker_state = WORKER_ILLEGAL;
						continue;
				}						
				break;
			}
			strncpy( buf, line, sizeof( buf ) );
			free( line );
			line = buf;
		} else {
			if( fgets( buf, sizeof( buf ), in ) == NULL ) {
				cleanup_worker_names( );
				return EXIT_SUCCESS;
			}
			line = buf;
		}
		
		if( strlen( line ) > 1 ) {
			if( line[strlen( line )-1] == '\n' ) {
				line[strlen( line )-1] = '\0';
			}
		}
		
		if( is_interactive ) {
			linenoiseHistoryAdd( line );
		}
		
		switch( command_state ) {
			case COMMAND:
				if( strncasecmp( line, "quit", 4 ) == 0 ) {
					if( is_interactive ) {
						linenoiseHistorySave( history_filename );
					}
					cleanup_worker_names( );
					return EXIT_SUCCESS;
				} else if( strncasecmp( line, "help", 4 ) == 0 ) {
					print_help( );
				} else if( strncasecmp( line, "status", 6 ) == 0 ) {
					print_status( );
				} else if( strncasecmp( line, "start", 5 ) == 0 ) {
					command_state = START_WORKER;
					command_worker_state = WORKER_NAME;
					get_worker_data( );				
				} else if( strncasecmp( line, "stop", 5 ) == 0 ) {
					command_state = STOP_WORKER;
					command_worker_state = WORKER_NAME;
					get_worker_data( );				
				} else if( strncasecmp( line, "messages", 8 ) == 0 ) {
					command_state = MESSAGES_WORKER;
					command_worker_state = WORKER_NAME;
					get_worker_data( );				
				} else if( strncasecmp( line, "autodisp", 8 ) == 0 ) {
					if( is_interactive ) {
						autodisp_toggle = !autodisp_toggle;
						print_message( "Autodisplay toggle is %s.", autodisp_toggle ? "on" : "off" );
					} else {
						print_error( "'autodisp' makes sense only in interactive mode." );
					}
				} else if( strncasecmp( line, "wait", 4 ) == 0 ) {
					if( is_interactive ) {
						puts( "Enter sleep time (in seconds):") ;
					}
					command_state = WAIT;
				} else if( strcmp( line, "" ) == 0 ) {
					// skip, handy if autodisp is on to show more messages
				} else {
					print_error( "Bad command '%s'.", line );
				}
				break;

			case START_WORKER:
				if( command_worker_state == WORKER_EXECUTE ) {
					start_worker( line );
					command_state = COMMAND;
					command_worker_state = WORKER_ILLEGAL;
				} else {
					get_worker_data( );
				}
				break;
			
			case STOP_WORKER:
				if( command_worker_state == WORKER_EXECUTE ) {
					stop_worker( line );
					command_state = COMMAND;
					command_worker_state = WORKER_ILLEGAL;
				} else {
					get_worker_data( );
				}
				break;
			
			case MESSAGES_WORKER:
				if( command_worker_state == WORKER_EXECUTE ) {
					print_messages( );
					command_state = COMMAND;
					command_worker_state = WORKER_ILLEGAL;
				} else {
					get_worker_data( );
				}
				break;
			
			case WAIT:
				wait_time = atoi( line );
				printf( "Sleeping for %d seconds\n", wait_time );
				break;
				
		}					
	}

	return EXIT_SUCCESS;
}

#endif

void print_guessed_env( bool human_readable )
{	
	char hostname[100];
	system_hostname( hostname, sizeof( hostname ) );
	unsigned int nofCpus = system_available_cpus( );
	unsigned int nofPhysMem = system_phys_memory( );	
	char os_name[100];
	system_os( os_name, sizeof( os_name ) );
	char machine_arch[100];
	system_arch( machine_arch, sizeof( machine_arch ) );

	if( human_readable)  {
		printf( "Architecture: %s\n", machine_arch );
		printf( "Operating system: %s\n", os_name );
		printf( "Hostname: %s\n", hostname );
		printf( "Number of CPUs: %d\n", nofCpus );
		printf( "Physical memory in kB: %d\n", nofPhysMem );
	} else {
		json_object *obj = json_object_new_object( );

		json_object *arch = json_object_new_string( machine_arch );
		json_object_object_add( obj, "arch", arch );	
		json_object *os = json_object_new_string( os_name );
		json_object_object_add( obj, "os", os );
		json_object *host = json_object_new_string( hostname );
		json_object_object_add( obj, "host", host );
		json_object *cpus = json_object_new_int( nofCpus );
		json_object_object_add( obj, "cpus", cpus );
		json_object *physMem = json_object_new_int( nofPhysMem );
		json_object_object_add( obj, "physical_memory", physMem );

		const char *msg = json_object_to_json_string( obj );
		char *res = strdup( msg );
		
		puts( res );
		
		free( res );
		json_object_put( obj );
	}
}