aboutsummaryrefslogtreecommitdiff
path: root/rodisc.c
blob: 068e9371b306111f1580156383fe60c0923bdfc1 (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
#include <stdlib.h>

#include "rodisc.h"

static GMainLoop *main_loop;

static GHashTable *discs;

static gchar **files = NULL;

static GOptionEntry entries[] =
{
    { "file", 'f', 0, G_OPTION_ARG_FILENAME_ARRAY, &files, "Image files to export as remote discs", "FILE"},
    { NULL }
};

RODisc *rodisc_new()
{
	return g_slice_new0(RODisc);
}

void rodisc_destroy(RODisc *disc)
{
	g_free(disc->id);
	g_free(disc->uri);
	g_object_unref(disc->file);
	g_free(disc->file_path);
	g_free(disc->label);
	g_slice_free(RODisc, disc);
}

RODisc *rodisc_lookup(const gchar *id)
{
	return g_hash_table_lookup(discs, id);
}

void rodisc_export(RODisc *disc)
{
	g_debug("Exporting %s to %s (volume '%s' type '%s' size %lu)",
	        disc->file_path, disc->uri, disc->label, disc->type, disc->size);
	g_hash_table_insert(discs, disc->id, disc);

	server_register(disc);

	mdns_publish(disc);
}

void rodisc_remove(RODisc *disc)
{
	g_debug("Unexporting %s", disc->uri);

	mdns_unpublish(disc);

	server_unregister(disc);

	g_hash_table_remove(discs, disc->id);
}

void rodisc_refresh(RODisc *disc)
{
	g_debug("Reexporting %s to %s (volume '%s' type '%s' size %lu)",
	        disc->file_path, disc->uri, disc->label, disc->type, disc->size);
	mdns_publish(disc);
}

static void refresh_all_discs_func(gpointer key, gpointer value, gpointer user_data)
{
	RODisc *disc = (RODisc*) value;
	rodisc_refresh(disc);
}

void rodisc_refresh_all()
{
	g_hash_table_foreach(discs, refresh_all_discs_func, NULL);
}

static void files_add()
{
	gchar **f;
	if (!files) return;
	for (f = files; *f; f++) {
		file_add_disc(*f);
	}
}

int main(int argc, char * argv[])
{
	GError *error = NULL;
	GOptionContext *context = g_option_context_new("- remote optical disc service");

	main_loop = g_main_loop_new(NULL, FALSE);

	discs = g_hash_table_new_full(g_str_hash, g_str_equal,
	                              NULL, (GDestroyNotify) rodisc_destroy);

	g_option_context_add_main_entries(context, entries, NULL);
	if (!g_option_context_parse(context, &argc, &argv, &error)) {
		g_printerr("Option parsing failed: %s\n", error->message);
		return EXIT_FAILURE;
	}

	if (!server_start()) {
		return EXIT_FAILURE;
	}

	if (!mdns_start()) {
		return EXIT_FAILURE;
	}

	if (!monitor_start()) {
		return EXIT_FAILURE;
	}

	files_add();

	g_main_loop_run(main_loop);

	monitor_stop();

	mdns_stop();

	server_stop();

	g_hash_table_destroy(discs);
	g_main_loop_unref(main_loop);
	g_option_context_free(context);

	return EXIT_SUCCESS;
}