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