예제 #1
0
int
main (int    argc,
      char **argv)
{
  DBusConnection *connection;
  DBusError error;
  EchoData echo_data;
  int result;
  
  echo_data.argc = argc;
  echo_data.argv = argv;
  
  dbus_error_init (&error);
  connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
  if (connection == NULL)
    {
      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  loop = _dbus_loop_new ();
  if (loop == NULL)
    die ("No memory\n");
  
  if (!test_connection_setup (loop, connection))
    die ("No memory\n");

  if (!dbus_connection_add_filter (connection,
                                   filter_func, NULL, NULL))
    die ("No memory");

  if (!dbus_connection_register_object_path (connection,
                                             echo_path,
                                             &echo_vtable,
                                             (void*) &echo_data))
    die ("No memory");

  {
    void *d;
    if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
      die ("No memory");
    if (d != (void*) &echo_data)
      die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
  }
  
  result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
                                  0, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "Error %s\n", error.message);
      _dbus_verbose ("*** Failed to acquire service: %s\n",
                     error.message);
      dbus_error_free (&error);
      exit (1);
    }

  _dbus_verbose ("*** Test service entering main loop\n");
  _dbus_loop_run (loop);

  test_connection_shutdown (loop, connection);

  dbus_connection_remove_filter (connection, filter_func, NULL);
  
  dbus_connection_unref (connection);

  _dbus_loop_unref (loop);
  loop = NULL;
  
  dbus_shutdown ();

  _dbus_verbose ("*** Test service exiting\n");
  
  return 0;
}
예제 #2
0
int main() {
	DBusGProxy *proxy;
	DBusGConnection *sigcon;
	GError *error;
	DBusError *dbuserror;
	int retval;

	g_type_init();
	error = NULL;

	/* Obtain a connection to the Session Bus */
	sigcon = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
	if(!sigcon) {
		g_printerr("Failed to connect to Session bus: %s\n", error -> message);
		g_error_free(error);
		exit(1);
	}

	con = dbus_g_connection_get_connection(sigcon);

	dbuserror = NULL;

	/* Request the DBus daemon for the name org.DBusTest.SignalTest */
	/* WARNING: Note that the function does not do an exit(1) even
	 * when it doesn't get the name. This may lead to the program
	 * not terminating, because the releaseName method will fail
	 * subsequently and onNameLost will not be called. Thus the program
	 * will not exit the main loop
	 */

	retval = dbus_bus_request_name(con, "org.DBusTest.SignalTest", DBUS_NAME_FLAG_ALLOW_REPLACEMENT, dbuserror);
	switch(retval) {
		case -1: {
			g_printerr("Couldn't acquire name org.DBusTest.SignalTest for our connection: %s\n", dbuserror -> message);
			g_printerr("This program may not terminate as a result of this error!\n");
			dbus_error_free(dbuserror);
			break;
		}
		case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
			g_printerr("dbus_bus_request_name(): We now own the name org.DBusTest.SignalTest!\n");
			break;
		case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
			g_printerr("dbus_bus_request_name(): We are standing in queue for our name!\n");
			break;
		case DBUS_REQUEST_NAME_REPLY_EXISTS:
			g_printerr("dbus_bus_request_name(): :-( The name we asked for already exists!\n");
			break;
		case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
			g_printerr("dbus_bus_request_name(): Eh? We already own this name!\n");
			break;
		default:
			g_printerr("dbus_bus_request_name(): Unknown result = %d\n", retval);
	}

	/* Prepare a main loop */
	mainloop = g_main_loop_new(NULL, false);

	/* Create a proxy object for the DBus daemon and register the signal handler
	 * onNameLost() with the signal 'NameLost' from the DBus daemon */

	proxy = dbus_g_proxy_new_for_name(sigcon, "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus");

	dbus_g_proxy_add_signal(proxy, "NameLost", G_TYPE_STRING, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal(proxy, "NameLost", onNameLost, NULL, NULL);

	/* Set a timeout of 1s to call releaseName() */
	g_timeout_add(1000, releaseName, (gpointer) sigcon);

	/* Enter the Main Loop */
	g_main_loop_run(mainloop);

	/* Cleanup code */
	g_object_unref(proxy);

	return 0;
}