/
server.c
195 lines (168 loc) · 5.49 KB
/
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
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
/* This file is part of herzi's playground
*
* Copyright (C) 2010 Sven Herzberg
*
* This work is provided "as is"; redistribution and modification
* in whole or in part, in any medium, physical or electronic is
* permitted without restriction.
*
* This work 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.
*
* In no event shall the authors or contributors be liable for any
* direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute
* goods or services; loss of use, data, or profits; or business
* interruption) however caused and on any theory of liability, whether
* in contract, strict liability, or tort (including negligence or
* otherwise) arising in any way out of the use of this software, even
* if advised of the possibility of such damage.
*/
#include <stdlib.h>
#include <dbus/dbus-glib-lowlevel.h>
typedef GObject P2PObject;
typedef GObjectClass P2PObjectClass;
static GType p2p_object_get_type (void); /* exported type */
static gboolean p2p_object_echo (P2PObject* self,
gchar * text_in,
gchar **text_out,
GError **error);
#include "glue.h"
static void
new_connection_cb (DBusServer * server,
DBusConnection* connection,
gpointer user_data)
{
dbus_int32_t slot = -1;
GObject * object;
if (!dbus_connection_allocate_data_slot (&slot))
{
g_warning ("error allocating data slot for DBusConnection");
dbus_connection_close (connection);
return;
}
dbus_connection_ref (connection);
dbus_connection_set_allow_anonymous (connection, TRUE);
dbus_connection_setup_with_g_main (connection, NULL);
object = g_object_new (p2p_object_get_type (), NULL);
dbus_g_connection_register_g_object (dbus_connection_get_g_connection (connection),
"/", object);
dbus_connection_set_data (connection, slot,
object, g_object_unref);
}
guint16
my_dbus_server_get_port (DBusServer* server)
{
DBusAddressEntry**entries;
DBusError error;
guint16 port = 0;
char * address = dbus_server_get_address (server);
int n_entries;
int i;
dbus_error_init (&error);
if (!dbus_parse_address (address, &entries, &n_entries, &error))
{
g_warning ("error parsing server address: %s",
error.message);
dbus_error_free (&error);
free (address);
exit (1);
return 0;
}
for (i = 0; i < n_entries; i++)
{
if (!strcmp ("tcp", dbus_address_entry_get_method (entries[i])))
{
port = atoi (dbus_address_entry_get_value (entries[i], "port"));
break;
}
}
dbus_address_entries_free (entries);
free (address);
return port;
}
int
main (int argc,
char**argv)
{
DBusServer* server;
GMainLoop * loop;
DBusError derror;
guint16 port;
GError * error = NULL;
gchar * command;
g_type_init ();
dbus_error_init (&derror);
server = dbus_server_listen("tcp:bind=*", &derror);
if (dbus_error_is_set (&derror))
{
g_warning ("error setting up peer-to-peer server: %s",
derror.message);
dbus_error_free (&derror);
return 1;
}
dbus_server_set_new_connection_function(server, &new_connection_cb,
NULL, NULL);
port = my_dbus_server_get_port (server);
command = g_strdup_printf ("./client %s %d",
"localhost",
port);
if (argc > 1)
{
g_print ("starting \"%s\" twice:\n", command);
if (!g_spawn_command_line_async (command, &error))
{
g_warning ("error spawning child: %s",
error->message);
g_clear_error (&error);
return 1;
}
if (!g_spawn_command_line_async (command, &error))
{
g_warning ("error spawning child: %s",
error->message);
g_clear_error (&error);
return 1;
}
}
else
{
g_print ("start the clients like this: \"%s\" (replace \"localhost\" with the IP address)\n",
command);
}
g_free (command);
dbus_server_setup_with_g_main (server, NULL);
loop = g_main_loop_new (NULL, FALSE);
g_timeout_add (argc > 1 ? 1000 : 60 * 1000, (GSourceFunc)g_main_loop_quit, loop);
g_main_loop_run (loop);
g_main_loop_unref (loop);
dbus_server_unref (server);
return 0;
}
/* implementation of the exported type */
G_DEFINE_TYPE (P2PObject, p2p_object, G_TYPE_OBJECT);
static void
p2p_object_init (P2PObject* self)
{}
static void
p2p_object_class_init (P2PObjectClass* self_class)
{
dbus_g_object_type_install_info (G_TYPE_FROM_CLASS (self_class),
&dbus_glib_p2p_object_object_info);
}
static gboolean
p2p_object_echo (P2PObject* self,
gchar * text_in,
gchar **text_out,
GError **error)
{
if (!G_TYPE_CHECK_INSTANCE_TYPE (self, p2p_object_get_type ()))
{
g_set_error (error, 0, 0, "invalid object type");
return FALSE;
}
*text_out = g_strdup_printf ("%1$s %1$s", text_in);
return TRUE;
}
/* vim:set et sw=2 cino=t0,f0,(0,{s,>2s,n-1s,^-1s,e2s: */