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) {
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),
{
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;
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)
#include <glib.h>
#include <unistd.h>
+#include <stdlib.h>
#include <message-port.h>
#include <bundle.h>
+#include "common/log.h"
GMainLoop *__loop = NULL;
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);
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);
}
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);
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;
}
#include <bundle.h>
#include <glib.h>
-typedef gboolean bool;
-
G_BEGIN_DECLS
/**
* @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
* @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
* @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.
* @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.
* @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.
* 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
* @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.
* 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.
* @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.
* @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
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;
}
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);
+}
+
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;
}
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 */