2003-01-05 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 "connection.h"
25 #include <dbus/dbus-list.h>
26
27 static void
28 server_watch_callback (DBusWatch     *watch,
29                        unsigned int   condition,
30                        void          *data)
31 {
32   DBusServer *server = data;
33
34   dbus_server_handle_watch (server, watch, condition);
35 }
36
37 static void
38 add_server_watch (DBusWatch      *watch,
39                   DBusServer     *server)
40 {
41   bus_loop_add_watch (watch, server_watch_callback, server,
42                       NULL);
43 }
44
45 static void
46 remove_server_watch (DBusWatch      *watch,
47                      DBusServer     *server)
48 {
49   bus_loop_remove_watch (watch, server_watch_callback, server);
50 }
51
52 static void
53 setup_server (DBusServer *server)
54 {
55   dbus_server_set_watch_functions (server,
56                                    (DBusAddWatchFunction) add_server_watch,
57                                    (DBusRemoveWatchFunction) remove_server_watch,
58                                    server,
59                                    NULL);
60 }
61
62 static void
63 new_connection_callback (DBusServer     *server,
64                          DBusConnection *new_connection,
65                          void           *data)
66 {
67   if (!bus_connection_setup (new_connection))
68     ; /* we won't have ref'd the connection so it will die */
69 }
70
71 int
72 main (int argc, char **argv)
73 {
74   DBusServer *server;
75   DBusResultCode result;
76
77   if (argc < 2)
78     {
79       _dbus_warn ("Give the server address as an argument\n");
80       return 1;
81     }
82
83   server = dbus_server_listen (argv[1], &result);
84   if (server == NULL)
85     {
86       _dbus_warn ("Failed to start server on %s: %s\n",
87                   argv[1], dbus_result_to_string (result));
88       return 1;
89     }
90
91   setup_server (server);
92
93   bus_connection_init ();
94   
95   dbus_server_set_new_connection_function (server,
96                                            new_connection_callback,
97                                            NULL, NULL);
98   
99   bus_loop_run ();
100   
101   dbus_server_disconnect (server);
102   dbus_server_unref (server);
103
104   return 0;
105 }