2003-01-04 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / bus / main.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* main.c  main() for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "loop.h"
24 #include <dbus/dbus-list.h>
25 #include <dbus/dbus.h>
26
27 static DBusList *connections = NULL;
28
29 static void
30 connection_error_handler (DBusConnection *connection,
31                           DBusResultCode  error_code,
32                           void           *data)
33 {
34   _dbus_warn ("Error on connection: %s\n",
35               dbus_result_to_string (error_code));
36
37   /* we don't want to be called again since we're dropping the connection */
38   dbus_connection_set_error_function (connection, NULL, NULL, NULL);
39
40   _dbus_list_remove (&connections, connection);    
41   dbus_connection_unref (connection);
42 }
43
44 static void
45 connection_watch_callback (DBusWatch     *watch,
46                            unsigned int   condition,
47                            void          *data)
48 {
49   DBusConnection *connection = data;
50
51   dbus_connection_handle_watch (connection, watch, condition);
52 }
53
54 static void
55 server_watch_callback (DBusWatch     *watch,
56                        unsigned int   condition,
57                        void          *data)
58 {
59   DBusServer *server = data;
60
61   dbus_server_handle_watch (server, watch, condition);
62 }
63
64 static void
65 add_connection_watch (DBusWatch      *watch,
66                       DBusConnection *connection)
67 {
68   bus_loop_add_watch (watch, connection_watch_callback, connection,
69                       NULL);
70 }
71
72 static void
73 remove_connection_watch (DBusWatch      *watch,
74                          DBusConnection *connection)
75 {
76   bus_loop_remove_watch (watch, connection_watch_callback, connection);
77 }
78
79 static void
80 add_server_watch (DBusWatch      *watch,
81                   DBusServer     *server)
82 {
83   bus_loop_add_watch (watch, server_watch_callback, server,
84                       NULL);
85 }
86
87 static void
88 remove_server_watch (DBusWatch      *watch,
89                      DBusServer     *server)
90 {
91   bus_loop_remove_watch (watch, server_watch_callback, server);
92 }
93
94 static dbus_bool_t
95 setup_connection (DBusConnection *connection)
96 {
97   if (!_dbus_list_append (&connections, connection))
98     {
99       dbus_connection_disconnect (connection);
100       return FALSE;
101     }
102
103   dbus_connection_ref (connection);
104   
105   dbus_connection_set_watch_functions (connection,
106                                        (DBusAddWatchFunction) add_connection_watch,
107                                        (DBusRemoveWatchFunction) remove_connection_watch,
108                                        connection,
109                                        NULL);
110
111   dbus_connection_set_error_function (connection,
112                                       connection_error_handler,
113                                       NULL, NULL);
114
115   return TRUE;
116 }
117
118 static void
119 setup_server (DBusServer *server)
120 {
121   dbus_server_set_watch_functions (server,
122                                    (DBusAddWatchFunction) add_server_watch,
123                                    (DBusRemoveWatchFunction) remove_server_watch,
124                                    server,
125                                    NULL);
126 }
127
128 static void
129 new_connection_callback (DBusServer     *server,
130                          DBusConnection *new_connection,
131                          void           *data)
132 {
133   if (!setup_connection (new_connection))
134     ; /* we won't have ref'd the connection so it will die */
135 }
136
137 int
138 main (int argc, char **argv)
139 {
140   DBusServer *server;
141   DBusResultCode result;
142
143   if (argc < 2)
144     {
145       _dbus_warn ("Give the server address as an argument\n");
146       return 1;
147     }
148
149   server = dbus_server_listen (argv[1], &result);
150   if (server == NULL)
151     {
152       _dbus_warn ("Failed to start server on %s: %s\n",
153                   argv[1], dbus_result_to_string (result));
154       return 1;
155     }
156
157   setup_server (server);
158
159   dbus_server_set_new_connection_function (server,
160                                            new_connection_callback,
161                                            NULL, NULL);
162   
163   bus_loop_run ();
164   
165   dbus_server_disconnect (server);
166   dbus_server_unref (server);
167
168   return 0;
169 }