File: dbus-rebound.c

package info (click to toggle)
dbusada 0.6.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 748 kB
  • sloc: ada: 5,892; ansic: 98; makefile: 81; sh: 47
file content (148 lines) | stat: -rw-r--r-- 3,330 bytes parent folder | download
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
/*
 * D_Bus/Ada - An Ada binding to D-Bus
 *
 * Copyright (C) 2011  Reto Buerki <reet@codelabs.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <glib.h>
#include <glib/gstdio.h>

#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>

#define NAME "dbus.ada.server"
#define ADDR "./dbusada.addr"

static DBusServer *server = NULL;
static GMainLoop *loop = NULL;

static void assert_no_error (const DBusError *e)
{
	if (G_UNLIKELY (dbus_error_is_set (e)))
	{
		g_error ("Got error: %s: %s", e->name, e->message);
	}
}

static DBusHandlerResult message_handler (DBusConnection *conn,
		DBusMessage *msg, void *data)
{
	DBusMessage* reply;
	dbus_uint32_t serial, r_serial = 0;

	const dbus_uint32_t s_serial = dbus_message_get_serial (msg);
	const char *dest = dbus_message_get_sender (msg);

	reply = dbus_message_copy (msg);

	if (!dbus_message_set_reply_serial (reply, s_serial))
	{
		g_error ("Could not set reply serial");
	}

	if (!dbus_message_set_sender (reply, NAME))
	{
		g_error ("Could not set sender name");
	}

	if (!dbus_message_set_destination (reply, dest))
	{
		g_error ("Could not set destination");
	}

	/* send the reply && flush the connection */
	if (!dbus_connection_send (conn, reply, &serial))
	{
		g_error ("Out of memory");
	}
	dbus_connection_flush (conn);

	dbus_message_unref (reply);

	return DBUS_HANDLER_RESULT_HANDLED;
}

static void handle_connection (DBusServer *server,
		DBusConnection *conn, void *data)
{
	DBusObjectPathVTable vtable = {
		NULL, &message_handler, NULL, NULL, NULL, NULL
	};

	if (!dbus_connection_register_object_path (conn, "/", &vtable, NULL))
	{
		g_error ("Can't register local test object");
		return;
	}

	dbus_connection_ref(conn);
	dbus_connection_setup_with_g_main(conn, NULL);
}

static void setup ()
{
	char *address;
	DBusError err;
	int fd, len;

	dbus_error_init (&err);

	server = dbus_server_listen ("unix:tmpdir=/tmp", &err);
	assert_no_error (&err);
	g_assert (server != NULL);

	address = dbus_server_get_address (server);

	if (!g_file_set_contents (ADDR, address, -1, NULL))
	{
		g_error ("Can't create server address file");
	}
	dbus_free (address);

	dbus_server_setup_with_g_main (server, NULL);
	dbus_server_set_new_connection_function (server,
			handle_connection, NULL, NULL);
}

static void teardown ()
{
	if (server != NULL)
	{
		dbus_server_disconnect (server);
		dbus_server_unref (server);
		server = NULL;
	}

	g_unlink (ADDR);
}

void sigterm (int signum)
{
	g_main_loop_quit (loop);
}

int main (int argc, char **argv)
{
	signal(SIGTERM, sigterm);

	loop = g_main_loop_new (NULL, FALSE);
	setup ();
	g_main_loop_run (loop);

	teardown();
}