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
|
#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;
fprintf( stderr, "%s http request: %s\n", method, url );
if( strcmp( method, "GET" ) == 0 ) {
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, sizeof( part ),
"coordinator %s %s %s %d %s %lld (%d)\n",
c->host, c->os, c->arch, c->cpus,
( c->alive ? "alive" : "dead" ),
(long long)c->lastAlive, pos );
strncat( biruda_msg, part, sizeof( biruda_msg ) );
for( int i = 0; i < c->nof_workers; i++ ) {
worker_t *w = &c->worker[i];
snprintf( part, sizeof( part ),
"worker %s %s %s %s (%d)\n",
w->name,
worker_state_str( w->state ),
worker_exection_mode_str( w->mode ),
( w->command == NULL ? "" : w->command ),
i );
strncat( biruda_msg, part, sizeof( biruda_msg ) );
}
}
}
} else {
snprintf( biruda_msg, sizeof( biruda_msg ), "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 );
} else if( strcmp( method, "POST" ) == 0 ) {
if( strcmp( url, "/worker" ) == 0 ) {
char biruda_msg[2048];
biruda_msg[0] = '\0';
const char *op = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "op" );
printf( "Got POST operation '%s'\n", op );
if( op != NULL && strcmp( op, "start" ) == 0 ) {
const char *name = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "name" );
if( name != NULL ) {
printf( "Got POST parameter for starting a worker with name '%s'\n", name );
int res = master_start_worker( name );
if( res < 0 ) {
snprintf( biruda_msg, sizeof( biruda_msg ), "Queueing start request message failed\n" );
} else {
snprintf( biruda_msg, sizeof( biruda_msg ), "Queued start worker request\n" );
}
response = MHD_create_response_from_buffer( strlen( biruda_msg ),
(void *)biruda_msg, MHD_RESPMEM_MUST_COPY );
ret = MHD_queue_response( connection, ( res == 0 ) ? MHD_HTTP_OK : MHD_HTTP_INTERNAL_SERVER_ERROR, response );
MHD_destroy_response( response );
}
} else {
return MHD_NO;
}
} else {
return MHD_NO;
}
} else {
return MHD_NO;
}
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;
}
|