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

#include <inttypes.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/select.h>

#include <microhttpd.h>

/* embedded web pages and JS code */
#include "index.h"
#include "jquery-ui-css.h"
#include "jquery-js.h"
#include "jquery-ui-js.h"
 
static struct MHD_Daemon *d;

#if MHD_VERSION < 0x00090500
enum MHD_ResponseMemoryMode {
	MHD_RESPMEM_PERSISTENT,
	MHD_RESPMEM_MUST_FREE,	
	MHD_RESPMEM_MUST_COPY	
};

#define MHD_create_response_from_buffer wrap_response

static struct MHD_Response *wrap_response( size_t size,	void *buffer, enum MHD_ResponseMemoryMode mode)
{
	return MHD_create_response_from_data( size, buffer,
		( mode | MHD_RESPMEM_MUST_FREE ) == MHD_RESPMEM_MUST_FREE,
		( mode | MHD_RESPMEM_MUST_COPY ) == MHD_RESPMEM_MUST_COPY );
}

#endif
	                                                    
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 = malloc( 512 * 1024 );
		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, strlen( part ) );
					
					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, strlen( part ) );
					}
				}
			}			
		} else if( strcmp( url, "/worker" ) == 0 ) {

			const char *op = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "op" );

			printf( "Got GET operation '%s'\n", op );
			
			if( op == NULL ) {
				return MHD_NO;
			}
			
			if( strcmp( op, "output" ) == 0 ) {
				
				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 if( strcmp( url, "/web" ) == 0 ) {
			strncpy( biruda_msg, (char *)index_html, index_html_len );
		} else if( strcmp( url, "/web/jquery-ui.css" ) == 0 ) {
			strncpy( biruda_msg, (char *)jquery_ui_css, jquery_ui_css_len );
		} else if( strcmp( url, "/web/jquery.js" ) == 0 ) {
			strncpy( biruda_msg, (char *)jquery_js, jquery_js_len );
		} else if( strcmp( url, "/web/jquery-ui.js" ) == 0 ) {
			strncpy( biruda_msg, (char *)jquery_ui_js, jquery_ui_js_len );
		} else {
			strcat( biruda_msg, "<html><body><p>This is biruda, the REST api. You can access the web interface <a href=\"/web\">here</a>.</p></body></html>" );
		}

		response = MHD_create_response_from_buffer( strlen( biruda_msg ),
			(void *)biruda_msg, MHD_RESPMEM_MUST_COPY );
		free( biruda_msg );
		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 ) {
				return MHD_NO;
			}
			
			if( 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 if( strcmp( op, "stop" ) == 0 ) {
				
				const char *name = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "name" );
				
				if( name != NULL ) {
				
					printf( "Got POST parameter for stopping a worker with name '%s'\n", name );
					
					int res = master_stop_worker( name );
					
					if( res < 0 ) {
						snprintf( biruda_msg, sizeof( biruda_msg ), "Queueing stop request message failed\n" );
					} else {
						snprintf( biruda_msg, sizeof( biruda_msg ), "Queued stop 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;
	}

	printf( "http daemon started on port %d\n", port );
	
	return 0;
}

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

int webserver_free( )
{
	return 0;
}