-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics_server.c
136 lines (110 loc) · 3.73 KB
/
basics_server.c
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
#include <stdio.h>
#include <stdlib.h>
#include <gio/gio.h>
#include <assert.h>
#include "basics_gen.h"
#include "config.h"
struct TestingState {
GMainLoop *loop;
GDBusObjectManagerServer *object_manager;
Greetable *greetable_intf;
guint16 num_times_called;
unsigned int timer_count;
};
void terminate(gpointer user_data)
{
struct TestingState *ts = user_data;
puts("Terminating the main loop");
g_main_loop_quit(ts->loop);
}
gboolean timeout_handler(gpointer user_data)
{
struct TestingState *ts = user_data;
ts->timer_count--;
printf("timeout with remaining timeouts=%d\n", ts->timer_count);
greetable_emit_timer_expired(ts->greetable_intf, ts->timer_count);
return ts->timer_count != 0;
}
static gboolean on_handle_greet(
Greetable *interface,
GDBusMethodInvocation *invocation,
const gchar *recipient,
gpointer user_data)
{
struct TestingState *ts = user_data;
printf("%s %s\n", greetable_get_greeting(interface), recipient);
ts->num_times_called++;
greetable_complete_greet(interface, invocation, ts->num_times_called);
return TRUE;
}
static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
{
printf("Acquired bus \"%s\"\n", name);
struct TestingState *ts = user_data;
ts->object_manager = g_dbus_object_manager_server_new("/Testing");
ObjectSkeleton *object_skeleton;
Greetable *greetable;
object_skeleton = object_skeleton_new("/Testing/Greeter");
greetable = greetable_skeleton_new();
ts->greetable_intf = greetable;
object_skeleton_set_greetable(object_skeleton, greetable);
// Release the reference since it's owned by the object now
g_object_unref(greetable);
greetable_set_greeting(greetable, "Hello");
g_signal_connect(greetable, "handle-greet", G_CALLBACK(on_handle_greet), ts);
// Export the object. The object manager takes its own reference of the object, so we unref.
g_dbus_object_manager_server_export(ts->object_manager, G_DBUS_OBJECT_SKELETON(object_skeleton));
g_object_unref(object_skeleton);
// Export all objects
g_dbus_object_manager_server_set_connection(ts->object_manager, conn);
}
static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data)
{
printf("Acquired name \"%s\"\n", name);
}
static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data)
{
if (!conn) {
puts("Couldn't establish connection to bus");
} else {
printf("Lost name \"%s\"\n", name);
}
}
int main(int argc, char **argv)
{
printf(
"basics_server v%d.%d.%d\n",
Basics_VERSION_MAJOR,
Basics_VERSION_MINOR,
Basics_VERSION_SUB);
struct TestingState *ts = calloc(sizeof(*ts), 1);
assert(ts);
const guint name_ref = g_bus_own_name(
G_BUS_TYPE_SESSION,
"io.mangoh",
G_BUS_NAME_OWNER_FLAGS_NONE,
on_bus_acquired,
on_name_acquired,
on_name_lost,
ts,
NULL);
const guint interval_ms = 5 * 1000;
ts->timer_count = 3;
const guint timeout_event_id = g_timeout_add_full(
G_PRIORITY_DEFAULT, interval_ms, timeout_handler, ts, terminate);
printf(
"Registered timer with id=%d, timeout=%d, count=%d\n",
timeout_event_id,
interval_ms,
ts->timer_count);
// TODO: by passing NULL as the first parameter, we are setting the GMainContext* as the
// "default context". What is the purpose of this context parameter?
ts->loop = g_main_loop_new(NULL, FALSE);
puts("Running the main loop");
g_main_loop_run(ts->loop);
puts("The main loop has completed");
g_bus_unown_name(name_ref);
g_main_loop_unref(ts->loop);
free(ts);
return 0;
}