summaryrefslogtreecommitdiffstats
path: root/debian/opensync/opensync-0.22/osplugin/osplugin.c
blob: 974ba68e4a7c04bd6417770fc1dd212101800fcf (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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#include <opensync/opensync.h>
#include <glib.h>
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>

#include "opensync/opensync_internals.h"

typedef struct PluginProcess {
	OSyncEnv *env;
	OSyncMember *member;
	OSyncQueue *incoming;
	OSyncQueue *outgoing;

	/** Does osync_member_initialized() run successfully? */
	osync_bool is_initialized;
} PluginProcess;

typedef struct context {
	PluginProcess *pp;
	OSyncMessage *message;

	/** The change being commited, for commit_change() */
	OSyncChange *change;

	/** A function that may be used to set method-specific data in the reply,
	 *  such as the UID in the in the commit_change reply
	 */
	osync_bool (*add_reply_data)(OSyncMessage*, struct context*, OSyncError**);
} context;


static osync_bool add_commit_change_reply_data(OSyncMessage *reply, context *ctx, OSyncError **error);
static osync_bool add_connect_reply_data(OSyncMessage *reply, context *ctx, OSyncError **error);
static osync_bool add_get_changedata_reply_data(OSyncMessage *reply, context *ctx, OSyncError **error);

void message_handler(OSyncMessage*, void*);
void message_callback(OSyncMember*, context*, OSyncError**);

void process_free(PluginProcess *pp)
{
	if (pp->incoming) {
		osync_queue_disconnect(pp->incoming, NULL);
		osync_queue_remove(pp->incoming, NULL);
		osync_queue_free(pp->incoming);
	}

	if (pp->outgoing) {
		osync_queue_disconnect(pp->incoming, NULL);
		osync_queue_free(pp->outgoing);
	}

	if (pp->env)
		osync_env_free(pp->env);

	g_free(pp);
}

void process_error_shutdown(PluginProcess *pp, OSyncError **error)
{
	osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, pp, error);

	OSyncMessage *message = osync_message_new(OSYNC_MESSAGE_ERROR, 0, NULL);
	if (!message)
		goto error;

	osync_marshal_error(message, *error);

	if (!osync_queue_send_message(pp->outgoing, NULL, message, NULL))
		goto error_free_message;

	sleep(1);

	process_free(pp);
	osync_trace(TRACE_EXIT, "%s", __func__);
	exit(1);

error_free_message:
	osync_message_unref(message);
error:
	osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error));
	exit(2);
}

void osync_client_sync_alert_sink(OSyncMember *member)
{
	osync_trace(TRACE_ENTRY, "%s(%p)", __func__, member);
	PluginProcess *pp = (PluginProcess*)osync_member_get_data(member);

	OSyncError *error = NULL;

	OSyncMessage *message = osync_message_new(OSYNC_MESSAGE_SYNC_ALERT, 0, &error);
	if (!message)
		process_error_shutdown(pp, &error);

	if (!osync_queue_send_message(pp->outgoing, NULL, message, &error))
		process_error_shutdown(pp, &error);

	osync_trace(TRACE_EXIT, "%s", __func__);
}

void osync_client_changes_sink(OSyncMember *member, OSyncChange *change, void *user_data)
{
	osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, member, change, user_data);
	context *ctx = (context *)user_data;
	PluginProcess *pp = ctx->pp;
	OSyncMessage *orig = ctx->message;

	OSyncError *error = NULL;

	if (osync_message_is_answered(orig)) {
		osync_change_free(change);
		osync_trace(TRACE_EXIT, "%s", __func__);
		return;
	}

	OSyncMessage *message = osync_message_new(OSYNC_MESSAGE_NEW_CHANGE, 0, &error);
	if (!message)
		process_error_shutdown(pp, &error);

	osync_marshal_change(message, change);

	osync_message_write_long_long_int(message, osync_member_get_id(member));

	if (!osync_queue_send_message(pp->outgoing, NULL, message, &error))
		process_error_shutdown(pp, &error);

	osync_trace(TRACE_EXIT, "%s", __func__);
}

static void usage (char *name)
{
  fprintf (stderr, "\nUsage: %s <group path> <memberid>\n\n", name);
  fprintf (stderr, "<group path> is the path to the directory\n");
  fprintf (stderr, "\tof the group to synchronize\n");
  fprintf (stderr, "<memberid> is the id of the member to debug\n\n");
  fprintf (stderr, "Example: %s /home/joe/.opensync/group1 1\n", name);
  exit (1);
}

int main( int argc, char **argv )
{
	osync_trace(TRACE_ENTRY, "%s(%i, %p)", __func__, argc, argv);
	GMainLoop *syncloop;
	GMainContext *context;
	OSyncError *error = NULL;
	PluginProcess pp;
	
	if (argc != 3)
		usage(argv[0]);

	memset(&pp, 0, sizeof(pp));

	char *group_path = argv[ 1 ];
	int member_id = atoi( argv[ 2 ] );

	context = g_main_context_new();
	syncloop = g_main_loop_new(context, TRUE);

	/** Create environment **/
	OSyncEnv *env = osync_env_new();
	/* Don't load groups. We will load the group manually using osync_group_load() */
	osync_env_set_option(env, "LOAD_GROUPS", "no");

	/* Don't load plugins automatically if OSYNC_MODULE_LIST is set */
	char *module_list = getenv("OSYNC_MODULE_LIST");

	if (module_list) {
		osync_env_set_option(env, "LOAD_PLUGINS", "no");

		osync_trace(TRACE_INTERNAL, "OSYNC_MODULE_LIST variable: %s", module_list);

		char *str, *saveptr;
		for (str = module_list; ; str = NULL) {
			char *path = strtok_r(str, ":", &saveptr);
			if (!path)
				break;

			osync_trace(TRACE_INTERNAL, "Module to be loaded: %s", path);
			if (!osync_module_load(env, path, &error)) {
				fprintf(stderr, "Unable to load plugin %s: %s\n", path, osync_error_print(&error));
				osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error));
				return 1;
			}
		}
	}

	if (!osync_env_initialize(env, &error)) {
		fprintf(stderr, "Unable to initialize environment: %s\n", osync_error_print(&error));
		osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error));
		osync_error_free(&error);
		return 1;
	}

	/** Find group **/
	OSyncGroup *group = osync_group_load(env, group_path, &error);
	if (!group) {
		fprintf(stderr, "Unable to load group from path: %s\n", group_path);
		osync_trace(TRACE_EXIT_ERROR, "%s: Unable to load group from path: %s", __func__, group_path);
		return 2;
	}

	/** Find member **/
	int i;
	for ( i = 0; i < osync_group_num_members(group); ++i ) {
		pp.member = osync_group_nth_member(group, i);
		if (member_id == osync_member_get_id(pp.member))
			break;
		else
			pp.member = NULL;
	}
	if ( !pp.member ) {
		fprintf(stderr, "Unable to find member with id %d\n", member_id);
		osync_trace(TRACE_EXIT_ERROR, "%s: Unable to find member with id %d", __func__, member_id);
		return 3;
	}
	osync_trace(TRACE_INTERNAL, "+++++++++ This is the client #%d (%s plugin) of group %s", member_id, pp.member->pluginname, osync_group_get_name(group));

	/** Create connection pipes **/
	char *pipe_path = g_strdup_printf( "%s/pluginpipe", osync_member_get_configdir( pp.member ) );
	pp.incoming = osync_queue_new( pipe_path, &error );
	pp.outgoing = NULL;
	g_free( pipe_path );

	osync_queue_create( pp.incoming, &error );
	if ( osync_error_is_set( &error ) )
		osync_error_free( &error );

	/** Idle until the syncengine connects to (and reads from) our pipe **/
	if (!osync_queue_connect( pp.incoming, OSYNC_QUEUE_RECEIVER, 0 )) {
		fprintf(stderr, "Unable to connect\n");
		osync_trace(TRACE_EXIT_ERROR, "%s: Unable to connect", __func__);
		exit(1);
	}

	
	osync_member_set_data(pp.member, &pp);

	/** Set callback functions **/
	OSyncMemberFunctions *functions = osync_member_get_memberfunctions(pp.member);
	functions->rf_change = osync_client_changes_sink;
	//functions->rf_message = osync_client_message_sink;
	functions->rf_sync_alert = osync_client_sync_alert_sink;

	/** Start loop **/
	osync_trace(TRACE_INTERNAL, "plugin setting up mainloop");
	osync_queue_set_message_handler(pp.incoming, message_handler, &pp);
	osync_queue_setup_with_gmainloop(pp.incoming, context);
	osync_member_set_loop(pp.member, context);

	osync_trace(TRACE_INTERNAL, "running loop");
	g_main_loop_run(syncloop);

	osync_trace(TRACE_EXIT, "%s", __func__);
	return 0;
}

void message_handler(OSyncMessage *message, void *user_data)
{
	osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, message, user_data);
	PluginProcess *pp = user_data;

	OSyncMessage *reply = NULL;
	OSyncError *error = NULL;
	//OSyncChange *change = 0;
	OSyncMember *member = pp->member;
	char *enginepipe = NULL;
   	context *ctx = NULL;

	osync_trace(TRACE_INTERNAL, "plugin received command %i", osync_message_get_command( message ));

	switch ( osync_message_get_command( message ) ) {
	case OSYNC_MESSAGE_NOOP:
		break;

	case OSYNC_MESSAGE_INITIALIZE:
		osync_trace(TRACE_INTERNAL, "init.");
		osync_message_read_string(message, &enginepipe);

		osync_trace(TRACE_INTERNAL, "enginepipe %s", enginepipe);
		pp->outgoing = osync_queue_new(enginepipe, NULL);
		if (!pp->outgoing) {
			fprintf(stderr, "Unable to make new queue\n");
			osync_trace(TRACE_EXIT_ERROR, "%s: Unable to make new queue", __func__);
			exit(1);
		}
		osync_trace(TRACE_INTERNAL, "connecting to engine");
		if (!osync_queue_connect(pp->outgoing, OSYNC_QUEUE_SENDER, 0 )) {
			fprintf(stderr, "Unable to connect queue\n");
			osync_trace(TRACE_EXIT_ERROR, "%s: Unable to connect queue", __func__);
			exit(1);
		}

		osync_trace(TRACE_INTERNAL, "done connecting to engine");
		/** Instanciate plugin **/
		if (!osync_member_instance_default_plugin(pp->member, &error))
			goto error;

		/** Initialize plugin **/
		if (!osync_member_initialize(pp->member, &error))
			goto error;

		pp->is_initialized = TRUE;

		osync_trace(TRACE_INTERNAL, "sending reply to engine");
		reply = osync_message_new_reply(message, NULL);
		if (!reply) {
			fprintf(stderr, "Unable to make new reply\n");
			osync_trace(TRACE_EXIT_ERROR, "%s: Unable to make new reply", __func__);
			exit(1);
		}

		if (!osync_queue_send_message(pp->outgoing, NULL, reply, NULL)) {
			fprintf(stderr, "Unable to send reply\n");
			osync_trace(TRACE_EXIT_ERROR, "%s: Unable to send reply", __func__);
			exit(1);
		}

		osync_trace(TRACE_INTERNAL, "done sending to engine");
		break;

	case OSYNC_MESSAGE_FINALIZE:
		if (pp->is_initialized)
			osync_member_finalize(pp->member);

		reply = osync_message_new_reply(message, NULL);
		if (!reply) {
			fprintf(stderr, "Unable to make new reply\n");
			osync_trace(TRACE_EXIT_ERROR, "%s: Unable to make new reply", __func__);
			exit(1);
		}

		if (!osync_queue_send_message(pp->outgoing, NULL, reply, NULL)) {
			fprintf(stderr, "Unable to send reply\n");
			osync_trace(TRACE_EXIT_ERROR, "%s: Unable to send reply", __func__);
			exit(1);
		}

		/*FIXME: how to wait for a message to be sent?
		 * We need to wait for the reply to be sent before exiting
		 */

		osync_trace(TRACE_EXIT, "%s", __func__);
		exit(0);
		break;

	case OSYNC_MESSAGE_CONNECT:
		osync_member_read_sink_info_full(member, message);

		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);

		/* connect() needs to tell the engine if it must perform a
		 * slow-sync, use add_reply_data() method for this
		 */
		ctx->add_reply_data = add_connect_reply_data;
		
		osync_member_connect(member, (OSyncEngCallback)message_callback, ctx);
		break;

	case OSYNC_MESSAGE_GET_CHANGES:
		osync_member_read_sink_info_full(member, message);

		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);
  		osync_member_get_changeinfo(member, (OSyncEngCallback)message_callback, ctx);
		break;

	case OSYNC_MESSAGE_COMMIT_CHANGE:
		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);
		OSyncChange *change;
  		osync_demarshal_change(message, member->group->conv_env, &change);
		osync_change_set_member(change, member);

		/* commit_change() needs to return some data back to the engine,
		 * use the add_reply_data() method for this
		 */
		ctx->change = change;
		ctx->add_reply_data = add_commit_change_reply_data;

	  	osync_member_commit_change(member, change, (OSyncEngCallback)message_callback, ctx);
		break;

	case OSYNC_MESSAGE_SYNC_DONE:
		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);
  		osync_member_sync_done(member, (OSyncEngCallback)message_callback, ctx);
		break;

	case OSYNC_MESSAGE_DISCONNECT:
		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);
  		osync_member_disconnect(member, (OSyncEngCallback)message_callback, ctx);
		break;

	case OSYNC_MESSAGE_REPLY:
		break;

  	case OSYNC_MESSAGE_ERRORREPLY:
		break;

	case OSYNC_MESSAGE_GET_CHANGEDATA:
		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);

  		osync_demarshal_change(message, member->group->conv_env, &change);
		osync_change_set_member(change, member);

		/* get_changedata needs to return the data from the change object back */
		ctx->change = change;
		ctx->add_reply_data = add_get_changedata_reply_data;

		osync_member_get_change_data(member, change, (OSyncEngCallback)message_callback, ctx);
		osync_trace(TRACE_EXIT, "message_handler");
		break;

  	case OSYNC_MESSAGE_COMMITTED_ALL:
		ctx = g_malloc0(sizeof(context));
		ctx->pp = pp;
		ctx->message = message;
		osync_message_ref(message);
  		osync_member_committed_all(member, (OSyncEngCallback)message_callback, ctx);
		break;

	/*case OSYNC_MESSAGE_READ_CHANGE:
		osync_demarshal_change( queue, &change, &error );
		osync_member_read_change(client->member, change, (OSyncEngCallback)message_callback, message);
		osync_trace(TRACE_EXIT, "message_handler");
		break;
	*/

	case OSYNC_MESSAGE_CALL_PLUGIN:
		/*
		char *function = itm_message_get_data(message, "function");
		void *data = itm_message_get_data(message, "data");
		OSyncError *error = NULL;
		void *replydata = osync_member_call_plugin(client->member, function, data, &error);

		if (itm_message_get_data(message, "want_reply")) {
			ITMessage *reply = NULL;
			if (!osync_error_is_set(&error)) {
				reply = itm_message_new_methodreply(client, message);
				itm_message_set_data(message, "reply", replydata);
			} else {
				reply = itm_message_new_errorreply(client, message);
				itm_message_set_error(reply, error);
			}

			itm_message_send_reply(reply);
		}
		*/
		break;
	case OSYNC_MESSAGE_QUEUE_HUP:
		osync_trace(TRACE_INTERNAL, "%s: ERROR: Queue hangup", __func__);
		fprintf(stderr, "Pipe closed! Exiting.\n");
		osync_trace(TRACE_EXIT, "%s: Exiting application. Goodbye.", __func__);
		exit(1);
		break;
	default:
		osync_trace(TRACE_INTERNAL, "%s: ERROR: Unknown message", __func__);
		g_assert_not_reached();
		break;
	}

	if (reply)
		osync_message_unref(reply);

	osync_trace(TRACE_EXIT, "%s", __func__);
	return;

error:;

	OSyncMessage *errorreply = osync_message_new_errorreply(message, NULL);
	if (!errorreply) {
		fprintf(stderr, "Unable to make new reply\n");
		osync_trace(TRACE_EXIT_ERROR, "%s", __func__);
		exit(1);
	}

	osync_marshal_error(errorreply, error);

	if (!osync_queue_send_message(pp->outgoing, NULL, errorreply, NULL)) {
		fprintf(stderr, "Unable to send error\n");
		osync_trace(TRACE_EXIT_ERROR, "%s", __func__);
		exit(1);
	}

	osync_message_unref(errorreply);

	osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error));
	osync_error_free(&error);
}

/** add get_changedat-specific data to the get_changedata reply */
static osync_bool add_get_changedata_reply_data(OSyncMessage *reply, context *ctx, OSyncError **error)
{
	OSyncChange *change = ctx->change;

	assert(change);

	osync_marshal_changedata(reply, change);

	return TRUE;
}

/** Add commit_change-specific data to the commit_change reply */
static osync_bool add_commit_change_reply_data(OSyncMessage *reply, context *ctx, OSyncError **error)
{
	OSyncChange *change = ctx->change;

	assert(change);

	osync_message_write_string(reply, osync_change_get_uid(change));

	return TRUE;
}

/** Add connect-specific data to the connect reply */
static osync_bool add_connect_reply_data(OSyncMessage *reply, context *ctx, OSyncError **error)
{
	OSyncMember *member = ctx->pp->member;

	assert(member);

	osync_member_write_sink_info(member, reply);
	
	return TRUE;
}

void message_callback(OSyncMember *member, context *ctx, OSyncError **error)
{
	/*FIXME: handle errors in this function */

	OSyncError *myerror = NULL;
	osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, member, ctx, error);

	OSyncMessage *message = ctx->message;
	PluginProcess *pp = ctx->pp;

	OSyncMessage *reply = NULL;

	if (osync_message_is_answered(message) == TRUE) {
		osync_message_unref(message);
		osync_trace(TRACE_EXIT, "%s", __func__);
		return;
	}

	if (!osync_error_is_set(error)) {
		reply = osync_message_new_reply(message, error);
		osync_debug("CLI", 4, "Member is replying with message %p to message %p:\"%lli-%i\" with no error", reply, message, message->id1, message->id2);
		/* Set method-specific data, if needed */
		if (ctx->add_reply_data)
			ctx->add_reply_data(reply, ctx, error);
	} else {
		reply = osync_message_new_errorreply(message, &myerror);
		osync_marshal_error(reply, *error);
		osync_debug("CLI", 1, "Member is replying with message %p to message %p:\"%lli-%i\" with error %i: %s", reply, message, message->id1, message->id2, osync_error_get_type(error), osync_error_print(error));
	}

	g_free(ctx);

	osync_queue_send_message(pp->outgoing, NULL, reply, NULL);
	osync_message_set_answered(message);

	osync_message_unref(message);
	osync_message_unref(reply);

	osync_trace(TRACE_EXIT, "%s", __func__);
}

void *osync_client_message_sink(OSyncMember *member, const char *name, void *data, osync_bool synchronous)
{
	/*TODO: Implement support for PLUGIN_MESSAGE */
/*
	OSyncClient *client = osync_member_get_data(member);
	OSyncEngine *engine = client->engine;
	if (!synchronous) {

		ITMessage *message = itm_message_new_signal(client, "PLUGIN_MESSAGE");
		osync_debug("CLI", 3, "Sending message %p PLUGIN_MESSAGE for message %s", message, name);
		itm_message_set_data(message, "data", data);
		itm_message_set_data(message, "name", g_strdup(name));
		itm_queue_send(engine->incoming, message);

		return NULL;
	} else {
		return engine->plgmsg_callback(engine, client, name, data, engine->plgmsg_userdata);
	}
*/
  return NULL;
}