summaryrefslogtreecommitdiff
path: root/src/webserver.c
blob: 908c2a78abf4fd2d3223aed24ba0e062f6c4b17c (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
#include "webserver.h"
#include "master.h"

#include <microhttpd.h>

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

static struct MHD_Daemon *d;

static int handle_request( void *cls, struct MHD_Connection *connection,
	const char *url, const char *method, const char *version,
	const char *upload_data, size_t *upload_data_size, void **ptr )
{
	struct MHD_Response *response;
	int ret;
	
	if( strcmp( method, "GET" ) != 0 ) {
		return MHD_NO;
	}
	
	fprintf( stderr, "http request: %s\n", url );

	char biruda_msg[2048];
	biruda_msg[0] = '\0';
	if( strcmp( url, "/status" ) == 0 ) {
		for( int pos = 0; pos < MAX_COORDINATORS; pos++ ) {
			coordinator_t *c = &coordinator[pos];
			if( c->used ) {
				char part[256];
				snprintf( part, 256, "%s %s %s %d %s %lld (%d) %d\n",
					c->host, c->os, c->arch, c->cpus,
					( c->alive ? "alive" : "dead" ),
					(long long)c->lastAlive, pos,
					c->nof_workers );
				strncat( biruda_msg, part, 2048 );
			}
		}
	} else {
		snprintf( biruda_msg, 2048, "Welcome to biruda! Please state your wish..\n" );
	}

	response = MHD_create_response_from_buffer( strlen( biruda_msg ),
		(void *)biruda_msg, MHD_RESPMEM_MUST_COPY );
        ret = MHD_queue_response( connection, MHD_HTTP_OK, response );
	MHD_destroy_response( response );
	
	return ret;
}

int webserver_init( unsigned int port )
{
	d = MHD_start_daemon(
		MHD_USE_SELECT_INTERNALLY, port, 
		NULL,
		NULL,
		&handle_request,
		NULL,
		MHD_OPTION_END );
	if( d == 0 ) {
		return 1;
	}

	puts( "http daemon started" );
	
	return 0;
}

void webserver_terminate( )
{
	MHD_stop_daemon( d );
	puts( "http daemon stopped" );
}

int webserver_free( )
{
	return 0;
}