示例#1
0
文件: server.c 项目: herzi/p2p-dbus
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);
}
static void
init_network_manager (GUPnPNetworkManager *manager)
{
        GUPnPNetworkManagerPrivate *priv;
        DBusError derror;
        GMainContext *main_context;

        priv = manager->priv;

        g_object_get (manager, "main-context", &main_context, NULL);

        /* Do fake open to initialize types */
        dbus_g_connection_open ("", NULL);
        dbus_error_init (&derror);
        priv->dbus_connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &derror);
        if (priv->dbus_connection == NULL) {
                g_message ("Failed to connect to System Bus: %s",
                           derror.message);
                return;
        }

        dbus_connection_setup_with_g_main (priv->dbus_connection, main_context);

        priv->connection =
            dbus_connection_get_g_connection (priv->dbus_connection);

        priv->manager_proxy = dbus_g_proxy_new_for_name (priv->connection,
                                                         DBUS_SERVICE_NM,
                                                         MANAGER_PATH,
                                                         MANAGER_INTERFACE);

        dbus_g_proxy_add_signal (priv->manager_proxy,
                                 "DeviceAdded",
                                 DBUS_TYPE_G_OBJECT_PATH,
                                 G_TYPE_INVALID);
        dbus_g_proxy_connect_signal (priv->manager_proxy,
                                     "DeviceAdded",
                                     G_CALLBACK (on_device_added),
                                     manager,
                                     NULL);

        dbus_g_proxy_add_signal (priv->manager_proxy,
                                 "DeviceRemoved",
                                 DBUS_TYPE_G_OBJECT_PATH,
                                 G_TYPE_INVALID);
        dbus_g_proxy_connect_signal (priv->manager_proxy,
                                     "DeviceRemoved",
                                     G_CALLBACK (on_device_removed),
                                     manager,
                                     NULL);

        dbus_g_proxy_begin_call (priv->manager_proxy,
                                 "GetDevices",
                                 get_devices_cb,
                                 manager,
                                 NULL,
                                 G_TYPE_INVALID);
}
static void
setup (Test *test,
    gconstpointer data)
{
  GError *error = NULL;
  GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };

  tp_debug_set_flags ("all");
  test->dbus = tp_tests_dbus_daemon_dup_or_die ();

  test->mainloop = g_main_loop_new (NULL, FALSE);
  test->error = NULL;

  test->client_libdbus = dbus_bus_get_private (DBUS_BUS_STARTER, NULL);
  g_assert (test->client_libdbus != NULL);
  dbus_connection_setup_with_g_main (test->client_libdbus, NULL);
  dbus_connection_set_exit_on_disconnect (test->client_libdbus, FALSE);
  test->client_dbusglib = dbus_connection_get_g_connection (
      test->client_libdbus);
  dbus_g_connection_ref (test->client_dbusglib);
  test->client_bus = tp_dbus_daemon_new (test->client_dbusglib);
  g_assert (test->client_bus != NULL);

  test->service_conn = tp_tests_object_new_static_class (
        EXAMPLE_TYPE_CONTACT_LIST_CONNECTION,
        "account", "*****@*****.**",
        "protocol", "simple-protocol",
        NULL);
  test->service_conn_as_base = TP_BASE_CONNECTION (test->service_conn);
  g_assert (test->service_conn != NULL);
  g_assert (test->service_conn_as_base != NULL);

  g_assert (tp_base_connection_register (test->service_conn_as_base, "simple",
        &test->conn_name, &test->conn_path, &error));
  g_assert_no_error (error);

  test->cwr_ready = FALSE;
  test->cwr_error = NULL;

  test->conn = tp_connection_new (test->client_bus, test->conn_name,
      test->conn_path, &error);
  g_assert (test->conn != NULL);
  g_assert_no_error (error);

  tp_cli_connection_call_connect (test->conn, -1, NULL, NULL, NULL, NULL);

  g_assert (!tp_proxy_is_prepared (test->conn, TP_CONNECTION_FEATURE_CORE));
  g_assert (!tp_proxy_is_prepared (test->conn,
        TP_CONNECTION_FEATURE_CONNECTED));
  g_assert (!tp_proxy_is_prepared (test->conn, TP_CONNECTION_FEATURE_BALANCE));

  tp_tests_proxy_run_until_prepared (test->conn, features);
}
gboolean
gupnp_network_manager_is_available (GMainContext *main_context)
{
        DBusConnection *connection;
        DBusError derror;
        DBusGConnection *gconnection;
        DBusGProxy *dbus_proxy;
        GError *error = NULL;
        gboolean ret = FALSE;

        /* Do fake open to initialize types */
        dbus_g_connection_open ("", NULL);
        dbus_error_init (&derror);
        connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &derror);
        if (connection == NULL) {
                g_message ("Failed to connect to System Bus: %s",
                           derror.message);
                return FALSE;
        }

        dbus_connection_setup_with_g_main (connection, main_context);
        gconnection = dbus_connection_get_g_connection (connection);

        dbus_proxy = dbus_g_proxy_new_for_name (gconnection,
                                                DBUS_SERVICE_DBUS,
                                                DBUS_PATH_DBUS,
                                                DBUS_INTERFACE_DBUS);

        if (!dbus_g_proxy_call (dbus_proxy,
                                "NameHasOwner",
                                &error,
                                G_TYPE_STRING, DBUS_SERVICE_NM,
                                G_TYPE_INVALID,
                                G_TYPE_BOOLEAN, &ret,
                                G_TYPE_INVALID)) {
                g_warning ("%s.NameHasOwner() failed: %s",
                           DBUS_INTERFACE_DBUS,
                           error->message);
                g_error_free (error);
        }

        g_object_unref (dbus_proxy);
        dbus_connection_close (connection);
        dbus_g_connection_unref (gconnection);

        return ret;
}