From 134db946b35fd0c8f7a7abbbe36824bbbca14ea1 Mon Sep 17 00:00:00 2001 From: Amarnath Valluri Date: Sat, 12 Oct 2013 15:57:19 +0300 Subject: [PATCH] bidirectional messages implemented, updated the example code --- daemon/dbus-service.c | 4 ++- examples/Makefile.am | 2 +- examples/test-app.c | 97 ++++++++++++++++++++++++++++++++++++--------------- lib/message-port.c | 62 ++++++++++++++++++++------------ lib/message-port.h | 36 +++++++++++-------- lib/msgport-manager.c | 26 ++++++++++++++ lib/msgport-service.c | 23 +++++++++++- lib/msgport-service.h | 3 ++ 8 files changed, 185 insertions(+), 68 deletions(-) diff --git a/daemon/dbus-service.c b/daemon/dbus-service.c index 106d751..4a5876d 100644 --- a/daemon/dbus-service.c +++ b/daemon/dbus-service.c @@ -86,6 +86,7 @@ _dbus_service_handle_send_message ( GError *error; g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), FALSE); + DBG ("Send Message rquest on service %p to remote service id : %d", dbus_service, remote_service_id); manager = msgport_dbus_manager_get_manager (dbus_service->priv->owner); peer_dbus_service = msgport_manager_get_service_by_id (manager, remote_service_id); if (!peer_dbus_service) { @@ -166,10 +167,10 @@ MsgPortDbusService * msgport_dbus_service_new (MsgPortDbusManager *owner, const gchar *name, gboolean is_trusted) { static guint object_conter = 0; + MsgPortDbusService *dbus_service = NULL; GDBusConnection *connection = NULL; gchar *object_path = 0; - GError *error = NULL; connection = msgport_dbus_manager_get_connection (owner), @@ -271,6 +272,7 @@ msgport_dbus_service_send_message (MsgPortDbusService *dbus_service, GVariant *d { g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), FALSE); + DBG ("Sending message to %p from '%s:%s'", dbus_service, r_app_id, r_port); msgport_dbus_glue_service_emit_on_message (dbus_service->priv->dbus_skeleton, data, r_app_id, r_port, r_is_trusted); return TRUE; diff --git a/examples/Makefile.am b/examples/Makefile.am index 4ccd858..4db2803 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -4,5 +4,5 @@ msgport_example_app_SOURCES = test-app.c msgport_example_app_LDADD = ../lib/libmessage-port.la $(GLIB_LIBS) $(BUNDLE_LIBS) -msgport_example_app_CPPFLAGS = -I../lib/ $(GLIB_CFLAGS) $(BUNDLE_CFLAGS) +msgport_example_app_CPPFLAGS = -I../lib/ -I ../ $(GLIB_CFLAGS) $(BUNDLE_CFLAGS) diff --git a/examples/test-app.c b/examples/test-app.c index 4abf38b..44d9498 100644 --- a/examples/test-app.c +++ b/examples/test-app.c @@ -1,7 +1,9 @@ #include #include +#include #include #include +#include "common/log.h" GMainLoop *__loop = NULL; @@ -10,14 +12,29 @@ static void _dump_data (const char *key, const int type, const bundle_keyval_t * gchar *val = NULL; size_t size; bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size); - g_print (" %s - %s\n", key, val); + DBG (" %s - %s", key, val); } -void (_on_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data) +void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data) { gchar *name = NULL; messageport_get_local_port_name (port_id, &name), - g_print ("SERVER: GOT MESSAGE at prot %s FROM :'%s' - '%s\n", name, + DBG ("CHILD: GOT MESSAGE at prot %s FROM :'%s' - '%s", name, + remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon"); + g_free (name); + g_assert (data); + + bundle_foreach (data, _dump_data, NULL); + g_main_loop_quit (__loop); +} + +void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data) +{ + gchar *name = NULL; + gboolean found = FALSE; + bundle *b = NULL; + messageport_get_local_port_name (port_id, &name), + DBG ("PARENT: GOT MESSAGE at prot %s FROM :'%s' - '%s", name, remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon"); g_free (name); @@ -25,12 +42,47 @@ void (_on_got_message)(int port_id, const char* remote_app_id, const char* remot bundle_foreach (data, _dump_data, NULL); - g_main_loop_quit (__loop); + messageport_error_e res = trusted_message ? messageport_check_trusted_remote_port (remote_app_id, remote_port, &found) + : messageport_check_remote_port (remote_app_id, remote_port, &found); + if (!found) { + DBG ("PARENT: Could not found remote port (%d)", res); + exit (-1); + } + + DBG ("PARENT: Found remote prot"); + + bundle *reply = bundle_create (); + + bundle_add (reply, "Results", "GOT_IT"); + + DBG ("PARENT: Sending reply ...."); + res = messageport_send_message (remote_app_id, remote_port, reply); + bundle_free (reply); + if (res != MESSAGEPORT_ERROR_NONE) + { + DBG ("PARENT: Faile to send message to server : %d", res); + } + else DBG ("PARENT: Data sent successfully"); + +} + +int _register_test_port (const gchar *port_name, messageport_message_cb cb) +{ + int port_id = messageport_register_local_port (port_name, cb); + + if (port_id > MESSAGEPORT_ERROR_NONE) { + gchar *name = NULL; + messageport_get_local_port_name (port_id, &name); + g_free (name); + } + else { + DBG ("Failed to register port : %d", port_id); + } + return port_id; } int main (int argc, char *argv[]) { - const gchar *port_name = "test_port"; pid_t child_pid; __loop = g_main_loop_new (NULL, FALSE); @@ -41,49 +93,38 @@ int main (int argc, char *argv[]) } else if (child_pid > 0) { /* prent process : server port */ - int port_id = messageport_register_local_port (port_name, _on_got_message); - - if (port_id > MESSAGEPORT_ERROR_NONE) { - gchar *name = NULL; - messageport_get_local_port_name (port_id, &name); - g_print ("Registerd Port : %s(Id: %d)\n", name, port_id); - g_free (name); - } - else { - g_print ("Failed to register port : %d \n", port_id); - return -1; - } + int port_id = _register_test_port ("test_parent_port", _on_parent_got_message); + DBG ("PARENT ; registered port %d", port_id); + } else { /* child process */ + int port_id = _register_test_port ("test_child_port", _on_child_got_message); + DBG ("CHILD ; registered port %d", port_id); /* sleep sometime till server port is ready */ sleep (5); - gchar *app_id = g_strdup_printf ("%d", getppid()); + gchar *parent_app_id = g_strdup_printf ("%d", getppid()); gboolean found; - messageport_error_e res = messageport_check_remote_port (app_id, port_name, &found); + messageport_error_e res = messageport_check_remote_port (parent_app_id, "test_parent_port", &found); if (!found) { - g_print ("CHILD : Could not found remote port (%d)", res); + DBG ("CHILD : Could not found remote port (%d)", res); return -1; } - g_print ("CHILD : Found remote prot\n"); + DBG ("CHILD : Found remote prot..., sending data to remote port (%s:%s)", parent_app_id, "test_parent_port"); bundle *b = bundle_create (); - bundle_add (b, "Name", "Amarnath"); bundle_add (b, "Email", "amarnath.valluri@intel.com"); - g_print ("CHILD : Sending data ....\n"); - res = messageport_send_message (app_id, port_name, b); + res = messageport_send_bidirectional_message(port_id, parent_app_id, "test_parent_port", b); bundle_free (b); if (res != MESSAGEPORT_ERROR_NONE) { - g_print ("CHILD: Faile to send message to server : %d", res); + DBG ("CHILD: Fail to send message to server : %d", res); } - else g_print ("CHILD : Data sent successfully"); - - exit (0); + else DBG ("CHILD : Data sent successfully"); } g_main_loop_run (__loop); diff --git a/lib/message-port.c b/lib/message-port.c index 249dc11..0dac6ad 100644 --- a/lib/message-port.c +++ b/lib/message-port.c @@ -15,89 +15,105 @@ _messageport_register_port (const char *name, gboolean is_trusted, messageport_m return port_id > 0 ? port_id : (int)res; } -static int +static messageport_error_e _messageport_check_remote_port (const char *app_id, const char *port, gboolean is_trusted, gboolean *exists) { - guint service_id; messageport_error_e res; MsgPortManager *manager = msgport_get_manager (); - res = msgport_manager_check_remote_service (manager, app_id, port, FALSE, &service_id); + res = msgport_manager_check_remote_service (manager, app_id, port, FALSE, NULL); if (exists) *exists = (res == MESSAGEPORT_ERROR_NONE); return (int) res; } -static int +static messageport_error_e _messageport_send_message (const char *app_id, const char *port, gboolean is_trusted, bundle *message) { -DBG("{"); MsgPortManager *manager = msgport_get_manager (); + GVariant *v_data = bundle_to_variant_map (message); - messageport_error_e res; - res = msgport_manager_send_message (manager, app_id, port, is_trusted, v_data); -DBG("}"); - return (int) res; + return msgport_manager_send_message (manager, app_id, port, is_trusted, v_data); +} + +messageport_error_e +_messageport_send_bidirectional_message (int id, const gchar *remote_app_id, const gchar *remote_port, gboolean is_trusted, bundle *message) +{ + MsgPortManager *manager = msgport_get_manager (); + + GVariant *v_data = bundle_to_variant_map (message); + + return msgport_manager_send_bidirectional_message (manager, id, remote_app_id, remote_port, is_trusted, v_data); } /* * API */ -int messageport_register_local_port(const char* local_port, messageport_message_cb callback) +int +messageport_register_local_port(const char* local_port, messageport_message_cb callback) { return _messageport_register_port (local_port, FALSE, callback); } -int messageport_register_trusted_local_port (const char *local_port, messageport_message_cb callback) +messageport_error_e +messageport_register_trusted_local_port (const char *local_port, messageport_message_cb callback) { return _messageport_register_port (local_port, TRUE, callback); } -int messageport_check_remote_port (const char *remote_app_id, const char *port_name, gboolean *exists) +messageport_error_e +messageport_check_remote_port (const char *remote_app_id, const char *port_name, gboolean *exists) { return _messageport_check_remote_port (remote_app_id, port_name, FALSE, exists); } -int messageport_check_trusted_remote_port (const char *remote_app_id, const char *port_name, gboolean *exists) +messageport_error_e +messageport_check_trusted_remote_port (const char *remote_app_id, const char *port_name, gboolean *exists) { return _messageport_check_remote_port (remote_app_id, port_name, TRUE, exists); } -int messageport_send_message (const char* remote_app_id, const char* remote_port, bundle* message) +messageport_error_e +messageport_send_message (const char* remote_app_id, const char* remote_port, bundle* message) { return _messageport_send_message (remote_app_id, remote_port, FALSE, message); } -int messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message) +messageport_error_e +messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message) { return _messageport_send_message (remote_app_id, remote_port, TRUE, message); } -int messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data) +messageport_error_e +messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data) { - return _messageport_send_message (remote_app_id, remote_port, FALSE, data); + return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, FALSE, data); } -int messageport_send_bidirectional_trusted_message (int id, const char *remote_app_id, const char *remote_port, bundle *data) +messageport_error_e +messageport_send_bidirectional_trusted_message (int id, const char *remote_app_id, const char *remote_port, bundle *data) { - return _messageport_send_message (remote_app_id, remote_port, TRUE, data); + return _messageport_send_bidirectional_message (id, remote_app_id, remote_port, TRUE, data); } -int messageport_get_local_port_name(int port_id, char **name_out) +messageport_error_e +messageport_get_local_port_name(int port_id, char **name_out) { MsgPortManager *manager = msgport_get_manager (); - return (int)msgport_manager_get_service_name (manager, port_id, name_out); + return msgport_manager_get_service_name (manager, port_id, name_out); } -int messageport_check_trusted_local_port (int id, bool *exists) +messageport_error_e +messageport_check_trusted_local_port (int id, gboolean *trusted) { (void) id; - if (exists) *exists = FALSE; + if (trusted) *trusted = FALSE; return MESSAGEPORT_ERROR_NONE; } diff --git a/lib/message-port.h b/lib/message-port.h index 31a94b8..ed823f8 100644 --- a/lib/message-port.h +++ b/lib/message-port.h @@ -12,8 +12,6 @@ #include #include -typedef gboolean bool; - G_BEGIN_DECLS /** @@ -41,7 +39,7 @@ typedef enum _messageport_error_e * @remarks @a data must be released with bundle_free() by you * @remark @a remote_app_id and @a remote_port will be set if the remote application sends a bidirectional message, otherwise they are NULL. */ -typedef void (*messageport_message_cb)(int id, const char* remote_app_id, const char* remote_port, bool trusted_message, bundle* data); +typedef void (*messageport_message_cb)(int id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data); /** * @brief Registers the local message port. @n @@ -55,7 +53,8 @@ typedef void (*messageport_message_cb)(int id, const char* remote_app_id, const * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ -EXPORT_API int messageport_register_local_port(const char* local_port, messageport_message_cb callback); +EXPORT_API int +messageport_register_local_port(const char* local_port, messageport_message_cb callback); /** * @brief Registers the trusted local message port. @n @@ -70,7 +69,8 @@ EXPORT_API int messageport_register_local_port(const char* local_port, messagepo * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ -EXPORT_API int messageport_register_trusted_local_port(const char* local_port, messageport_message_cb callback); +EXPORT_API int +messageport_register_trusted_local_port(const char* local_port, messageport_message_cb callback); /** * @brief Checks if the message port of a remote application is registered. @@ -84,7 +84,8 @@ EXPORT_API int messageport_register_trusted_local_port(const char* local_port, m * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ -EXPORT_API int messageport_check_remote_port(const char* remote_app_id, const char *remote_port, bool* exist); +EXPORT_API messageport_error_e +messageport_check_remote_port(const char* remote_app_id, const char *remote_port, gboolean *exist); /** * @brief Checks if the trusted message port of a remote application is registered. @@ -99,7 +100,8 @@ EXPORT_API int messageport_check_remote_port(const char* remote_app_id, const ch * @retval #MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH The remote application is not signed with the same certificate * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ -EXPORT_API int messageport_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool* exist); +EXPORT_API messageport_error_e +messageport_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, gboolean *exist); /** * @brief Sends a message to the message port of a remote application. @@ -127,7 +129,8 @@ EXPORT_API int messageport_check_trusted_remote_port(const char* remote_app_id, * bundle_free(b); * @endcode */ -EXPORT_API int messageport_send_message(const char* remote_app_id, const char* remote_port, bundle* message); +EXPORT_API messageport_error_e +messageport_send_message(const char* remote_app_id, const char* remote_port, bundle* message); /** * @brief Sends a trusted message to the message port of a remote application. @n @@ -145,7 +148,8 @@ EXPORT_API int messageport_send_message(const char* remote_app_id, const char* r * @retval #MESSAGEPORT_ERROR_MAX_EXCEEDED The size of message has exceeded the maximum limit * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ -EXPORT_API int messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message); +EXPORT_API messageport_error_e +messageport_send_trusted_message(const char* remote_app_id, const char* remote_port, bundle* message); /** * @brief Sends a message to the message port of a remote application. This method is used for the bidirectional communication. @@ -183,7 +187,8 @@ EXPORT_API int messageport_send_trusted_message(const char* remote_app_id, const * bundle_free(b); * } */ -EXPORT_API int messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data); +EXPORT_API messageport_error_e +messageport_send_bidirectional_message(int id, const char* remote_app_id, const char* remote_port, bundle* data); /** * @brief Sends a trusted message to the message port of a remote application. This method is used for the bidirectional communication. @@ -202,7 +207,8 @@ EXPORT_API int messageport_send_bidirectional_message(int id, const char* remote * @retval #MESSAGEPORT_ERROR_MAX_EXCEEDED The size of message has exceeded the maximum limit * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ -EXPORT_API int messageport_send_bidirectional_trusted_message(int id, const char* remote_app_id, const char* remote_port, bundle* data); +EXPORT_API messageport_error_e +messageport_send_bidirectional_trusted_message(int id, const char* remote_app_id, const char* remote_port, bundle* data); /** * @brief Gets the name of the local message port. @@ -215,19 +221,21 @@ EXPORT_API int messageport_send_bidirectional_trusted_message(int id, const char * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory * @remarks @a name must be released with free() by you */ -EXPORT_API int messageport_get_local_port_name(int id, char **name); +EXPORT_API messageport_error_e +messageport_get_local_port_name(int id, char **name); /** * @brief Checks if the local message port is trusted. * * @param [in] id The message port id returned by messageport_register_local_port() or messageport_register_trusted_local_port() - * @param [out] @c true if the local message port is trusted + * @param [out] is_trusted true if the local message port is trusted * @return 0 on success, otherwise a negative error value. * @retval #MESSAGEPORT_ERROR_NONE Successful * @retval #MESSAGEPORT_ERROR_INVALID_PARAMETER Invalid parameter * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory */ -EXPORT_API int messageport_check_trusted_local_port(int id, bool *trusted); +EXPORT_API messageport_error_e +messageport_check_trusted_local_port(int id, gboolean *is_trusted); G_END_DECLS diff --git a/lib/msgport-manager.c b/lib/msgport-manager.c index ca762c9..e073c2d 100644 --- a/lib/msgport-manager.c +++ b/lib/msgport-manager.c @@ -184,6 +184,7 @@ msgport_manager_unregister_servcie (MsgPortManager *manager, int service_id) service = _get_local_port (manager, service_id); if (!service) { + WARN ("No local service found for service id '%d'", service_id); return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND; } @@ -271,3 +272,28 @@ msgport_manager_send_message (MsgPortManager *manager, const gchar *remote_app_i return res; } +messageport_error_e +msgport_manager_send_bidirectional_message (MsgPortManager *manager, int local_port_id, const gchar *remote_app_id, const gchar *remote_port, gboolean is_trusted, GVariant *data) +{ + MsgPortService *service = NULL; + guint remote_service_id = 0; + messageport_error_e res = 0; + + g_return_val_if_fail (manager && MSGPORT_IS_MANAGER (manager), MESSAGEPORT_ERROR_IO_ERROR); + g_return_val_if_fail (manager->proxy, MESSAGEPORT_ERROR_IO_ERROR); + g_return_val_if_fail (local_port_id > 0 && remote_app_id && remote_port, MESSAGEPORT_ERROR_INVALID_PARAMETER); + + service = _get_local_port (manager, local_port_id); + if (!service) { + WARN ("No local service found for service id '%d'", local_port_id); + return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND; + } + if ( (res = msgport_manager_check_remote_service (manager, remote_app_id, remote_port, is_trusted, &remote_service_id) != MESSAGEPORT_ERROR_NONE)) { + WARN ("No remote port informatuon for %s:%s, error : %d", remote_app_id, remote_port, res); + return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND; + } + + DBG ("Sending message from local service '%p' to remote sercie id '%d'", service, remote_service_id); + return msgport_service_send_message (service, remote_service_id, data); +} + diff --git a/lib/msgport-service.c b/lib/msgport-service.c index 8ccb448..0f370c7 100644 --- a/lib/msgport-service.c +++ b/lib/msgport-service.c @@ -159,7 +159,28 @@ gboolean msgport_service_unregister (MsgPortService *service) { g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), FALSE); + g_return_val_if_fail (service->proxy, FALSE); - return TRUE; + /* fire and forget */ + return msgport_dbus_glue_service_call_unregister_sync (service->proxy, NULL, NULL); +} + +messageport_error_e +msgport_service_send_message (MsgPortService *service, guint remote_service_id, GVariant *message) +{ + GError *error = NULL; + g_return_val_if_fail (service && MSGPORT_IS_SERVICE (service), MESSAGEPORT_ERROR_IO_ERROR); + g_return_val_if_fail (service->proxy, MESSAGEPORT_ERROR_IO_ERROR); + g_return_val_if_fail (message, MESSAGEPORT_ERROR_INVALID_PARAMETER); + + msgport_dbus_glue_service_call_send_message_sync (service->proxy, remote_service_id, message, NULL, &error); + + if (error) { + WARN ("Fail to send message on service %p to %d : %s", service, remote_service_id, error->message); + g_error_free (error); + return MESSAGEPORT_ERROR_IO_ERROR; + } + + return MESSAGEPORT_ERROR_NONE; } diff --git a/lib/msgport-service.h b/lib/msgport-service.h index 9fb5d74..aac8266 100644 --- a/lib/msgport-service.h +++ b/lib/msgport-service.h @@ -39,6 +39,9 @@ msgport_service_id (MsgPortService *service); gboolean msgport_service_unregister (MsgPortService *service); +messageport_error_e +msgport_service_send_message (MsgPortService *service, guint remote_service_id, GVariant *message); + G_END_DECLS #endif /* __MSGPORT_SERVICE_H */ -- 2.7.4