/* IN_args contains that path to the GPS device we wish to open */ static void gypsy_server_create (GypsyServer *gps, const char *IN_device_path, DBusGMethodInvocation *context) { GypsyServerPrivate *priv; GypsyClient *client; char *path, *device_name, *sender; GList *list; priv = GET_PRIVATE (gps); /* We might be in the termination timeout when we receive a new create request, so cancel that timeout */ if (priv->terminate_id > 0) { g_source_remove (priv->terminate_id); priv->terminate_id = 0; } g_debug ("Creating client for %s", IN_device_path); device_name = g_path_get_basename (IN_device_path); g_debug ("Device name: %s", device_name); path = g_strdup_printf ("%s%s", GYPSY_GPS_PATH, g_strdelimit (device_name, ":", '_')); g_free (device_name); client = (GypsyClient *) dbus_g_connection_lookup_g_object (priv->connection, path); if (client == NULL) { /* If there isn't already an object registered on that path create and register it */ client = g_object_new (GYPSY_TYPE_CLIENT, "device_path", IN_device_path, NULL); dbus_g_connection_register_g_object (priv->connection, path, G_OBJECT (client)); } else { /* Ref the client so that when one client calls shutdown we won't destroy another clients object */ g_object_ref (client); } g_debug ("Registered client on %s", path); /* Update the hash of open connnctions */ sender = dbus_g_method_get_sender (context); list = g_hash_table_lookup (priv->connections, sender); list = g_list_prepend (list, client); g_hash_table_insert (priv->connections, sender, list); priv->client_count++; dbus_g_method_return (context, path); g_free (path); }
int main() { DBusGlibObject1 *so = NULL; DBusGlibObject1 *look_so = NULL; DBusGConnection *bus; GMainLoop *mainLoop = NULL; DBusGProxy* proxy; unsigned int request_ret; GError *error = NULL; g_type_init(); so = (DBusGlibObject1 *)g_object_new(DBUS_GLIB_OBJECT1_TYPE,NULL); bus = dbus_g_bus_get(DBUS_BUS_SESSION,NULL); proxy = dbus_g_proxy_new_for_name(bus,DBUS_SERVICE_DBUS,DBUS_PATH_DBUS,DBUS_INTERFACE_DBUS); dbus_g_connection_register_g_object(bus,"/com/example/DBusGlibObject1",G_OBJECT(so)); look_so = (DBusGlibObject1 *)dbus_g_connection_lookup_g_object(bus, "/com/example/DBusGlibObject1"); if(so != look_so) { g_print("dbus_g_connection_lookup_g_object() API Fail to return registered object.\n"); return 1; } if(!org_freedesktop_DBus_request_name(proxy,"com.example.DBusGlibObject1",0,&request_ret,&error)) { g_print("Unable to register service\n"); return 1; } /* Tell DBus what the type signature of the signal callback is; this * allows us to sanity-check incoming messages before invoking the * callback. You need to do this once for each proxy you create, * not every time you want to connect to the signal. */ dbus_g_proxy_add_signal (proxy, "hello_signal", G_TYPE_STRING, G_TYPE_INVALID); /* Actually connect to the signal. Note you can call * dbus_g_proxy_connect_signal multiple times for one invocation of * dbus_g_proxy_add_signal. */ dbus_g_proxy_connect_signal (proxy, "hello_signal", G_CALLBACK (hello_signal_handler), NULL, NULL); mainLoop = g_main_loop_new(NULL,FALSE); g_main_loop_run(mainLoop); return 0; }
static void gypsy_server_shutdown (GypsyServer *gps, const char *IN_device_path, DBusGMethodInvocation *context) { GypsyServerPrivate *priv; GypsyClient *client; GList *list, *owner; char *path, *device_name, *sender; priv = GET_PRIVATE (gps); g_debug ("Finding client for %s", IN_device_path); device_name = g_path_get_basename (IN_device_path); g_debug ("Device name: %s", device_name); path = g_strdup_printf ("%s%s", GYPSY_GPS_PATH, device_name); client = (GypsyClient *) dbus_g_connection_lookup_g_object (priv->connection, path); g_free (path); if (client == NULL) { dbus_g_method_return_error (context, g_error_new (GYPSY_SERVER_ERROR, GYPSY_SERVER_ERROR_NO_CLIENT, "No such client: %s", device_name)); } else { if (--priv->client_count == 0) { if (priv->terminate_id == 0) { priv->terminate_id = g_timeout_add (TERMINATE_TIMEOUT, gypsy_terminate, gps); } } /* Update the hash of open connnctions */ sender = dbus_g_method_get_sender (context); list = g_hash_table_lookup (priv->connections, sender); owner = g_list_find (list, client); if (owner) { g_object_unref (owner->data); } list = g_list_remove (list, client); g_hash_table_insert (priv->connections, sender, list); dbus_g_method_return (context); } g_free (device_name); }