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

#include "pthread.h"

#include "nanomsg/nn.h"
#include "nanomsg/survey.h"

#include "json-c/json.h"

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

#ifdef _WIN32
#define WIN32_MEAN_AND_LEAN
#include <Winsock2.h>
#else
#include <unistd.h>
#endif

#include "system.h"

static pthread_t coordinator_thread;
static int coordinator_sock;
static int coordinator_must_terminate = 0;

int nof_workers = 0;
worker_t worker[MAX_WORKERS];

static char *create_discover_answer( )
{
	json_object *obj = json_object_new_object( );
	json_object *op = json_object_new_string( "register" );
	json_object_object_add( obj, "op", op );

	/* we are a coordinator */
	json_object *role = json_object_new_string( "coordinator" );
	json_object_object_add( obj, "role", role );
	
	/* return hostname for unique identification (maybe better
	 * or additionally the MAC of the first interface or hostid?)
	 */
	char hostname[100];
	gethostname( hostname, sizeof( hostname ) );
	json_object *host = json_object_new_string( hostname );
	json_object_object_add( obj, "host", host );
	
	/* number of CPUs, important so we don't overload the
	 * server (TODO: should maybe be configurable how many
	 * CPUs we want to use in absolute, percentage use or 
	 * leave free 
	 */
	unsigned int nofCpus = system_available_cpus( );
	json_object *cpus = json_object_new_int( nofCpus );
	json_object_object_add( obj, "cpus", cpus );
	
	/* Operating system running on the coordinator, this is
	 * important so we know what we can run on the system
	 * directly
	 */
	char os_name[100];
	system_os( os_name, sizeof( os_name ) );
	json_object *os = json_object_new_string( os_name );
	json_object_object_add( obj, "os", os );

	/* system architecture, important whether we can run chrooted
	 * environments as workers directly without an virtualizer
	 */
	char machine_arch[100];
	system_arch( machine_arch, sizeof( machine_arch ) );
	json_object *arch = json_object_new_string( machine_arch );
	json_object_object_add( obj, "arch", arch );	
	
	json_object *worker_array_obj = json_object_new_array( );
	for( int i = 0; i < nof_workers; i++ ) {
		worker_t *w = &worker[i];
		json_object *worker_obj = json_object_new_object( );
		
		json_object *name_obj = json_object_new_string( w->name );
		json_object_object_add( worker_obj, "name", name_obj );

		json_object *mode_obj = json_object_new_string( worker_exection_mode_str( w->mode ) );
		json_object_object_add( worker_obj, "mode", mode_obj );

		if( w->mode != WORKER_EXECUTION_DISABLED ) {
			json_object *command_obj = json_object_new_string( w->command );
			json_object_object_add( worker_obj, "command", command_obj );
		}
		
		json_object_array_add( worker_array_obj, worker_obj );
	}
	json_object_object_add( obj, "workers", worker_array_obj );
	
	/* produce message as string, caller must free it */
	const char *msg = json_object_to_json_string( obj );
	char *res = strdup( msg );
	
	json_object_put( obj );
	
	return res;
}

static void *coordinator_func( void *thread_data )
{
	char *control = (char *)thread_data;
	
	coordinator_sock = nn_socket( AF_SP, NN_RESPONDENT );
	
	(void)nn_connect( coordinator_sock, control );

	printf( "coordinator connected to %s\n", control );

	while( !coordinator_must_terminate ) {		
		printf( "coordinator idle: %d\n", coordinator_must_terminate );
		
		char *answer = NULL;
		int bytes = nn_recv( coordinator_sock, &answer, NN_MSG, 0 );
		if( coordinator_must_terminate ) continue;
		if( bytes >= 0 ) {
			json_object *recv_obj = json_tokener_parse( answer );
			const char *recv_obj_str = json_object_to_json_string( recv_obj );
			printf( "coordinator received: %s\n", recv_obj_str );
			json_object *op_obj;
			json_object_object_get_ex( recv_obj, "op", &op_obj );
			const char *op = json_object_get_string( op_obj );

			if( strcmp( op, "discover" ) == 0 ) {
				char *msg = create_discover_answer( );
				size_t msg_size = strlen( msg ) + 1;
				
				printf( "coordinator send: %s\n", msg );
				bytes = nn_send( coordinator_sock, msg, msg_size, 0 );
				if( bytes < 0 ) {
					if( errno == ETERM ) {
						coordinator_must_terminate = 1;
						continue;
					} else {
						fprintf( stderr, "ERROR: nn_send returned %d: %s (%d)\n",
							bytes, nn_strerror( errno ), errno );
					}
				}
				if( bytes != msg_size ) {
					fprintf( stderr, "ERROR: truncated message!" );
				}
				
				free( msg );
			}

			json_object_put( recv_obj );
			nn_freemsg( answer );
		}
		if( bytes < 0 ) {
			if( errno == EAGAIN || errno == EINTR ) {
				continue;
			} else if( errno == ETERM ) {
				coordinator_must_terminate = 1;
				continue;
			} else {
				fprintf( stderr, "ERROR: nn_recv returned %d: %s (%d)\n",
					bytes, nn_strerror( errno ), errno );
			}
		}
	}
	
	(void)nn_shutdown( coordinator_sock, 0 );

	puts( "coordinator disconnected" );
	
	return NULL;
}

int coordinator_init( const char *control )
{
	pthread_attr_t attr;
	int res;
	
	coordinator_must_terminate = 0;
	nof_workers = 0;

	res = pthread_attr_init( &attr );
	if( res != 0 ) {
		return 1;
	}
	
	res = pthread_create( &coordinator_thread, &attr, coordinator_func, (void *)control );
	if( res != 0 ) {
		return 1;
	}
		
	return 0;
}

void coordinator_terminate( )
{
	coordinator_must_terminate = 1;
	
	nn_term( );
}

static void free_workers( )
{
	for( int i = 0; i < nof_workers; i++ ) {
		worker_t *w = &worker[i];
		
		free( w->name );
		free( w->command );
	}
}

int coordinator_free( )
{
	void *result;
	int res;
	
	res = pthread_join( coordinator_thread, &result );
	if( res != 0 ) {
		return 1;
	}
	
	free_workers( );
	
	return 0;
}

int coordinator_add_worker( const char *name, worker_execution_mode_t mode, const char *command )
{
	if( nof_workers >= MAX_WORKERS ) {
		fprintf( stderr, "Can't define more workers, limit reached!\n" );		
		return -1;
	}
	
	worker_t *w = &worker[nof_workers];
	w->name = strdup( name );
	w->mode = mode;
	if( command != NULL ) {
		w->command = strdup( command );
	} else {
		w->mode = WORKER_EXECUTION_DISABLED;
	}
	
	nof_workers++;
	
	return 0;
}