4 #include <message-port.h>
6 #include "common/log.h"
8 GMainLoop *__loop = NULL;
10 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
14 bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
15 DBG (" %s - %s", key, val);
18 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
21 messageport_get_local_port_name (port_id, &name),
22 DBG ("CHILD: GOT MESSAGE at prot %s FROM :'%s' - '%s", name,
23 remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon");
27 bundle_foreach (data, _dump_data, NULL);
28 g_main_loop_quit (__loop);
31 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
34 gboolean found = FALSE;
36 messageport_get_local_port_name (port_id, &name),
37 DBG ("PARENT: GOT MESSAGE at prot %s FROM :'%s' - '%s", name,
38 remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon");
43 bundle_foreach (data, _dump_data, NULL);
45 messageport_error_e res = trusted_message ? messageport_check_trusted_remote_port (remote_app_id, remote_port, &found)
46 : messageport_check_remote_port (remote_app_id, remote_port, &found);
48 DBG ("PARENT: Could not found remote port (%d)", res);
52 DBG ("PARENT: Found remote prot");
54 bundle *reply = bundle_create ();
56 bundle_add (reply, "Results", "GOT_IT");
58 DBG ("PARENT: Sending reply ....");
59 res = messageport_send_message (remote_app_id, remote_port, reply);
61 if (res != MESSAGEPORT_ERROR_NONE)
63 DBG ("PARENT: Faile to send message to server : %d", res);
65 else DBG ("PARENT: Data sent successfully");
69 int _register_test_port (const gchar *port_name, messageport_message_cb cb)
71 int port_id = messageport_register_trusted_local_port (port_name, cb);
73 if (port_id > MESSAGEPORT_ERROR_NONE) {
75 messageport_get_local_port_name (port_id, &name);
79 DBG ("Failed to register port : %d", port_id);
84 int main (int argc, char *argv[])
88 __loop = g_main_loop_new (NULL, FALSE);
92 g_error ("Failed to fork ");
94 else if (child_pid > 0) {
95 /* prent process : server port */
96 int port_id = _register_test_port ("test_parent_port", _on_parent_got_message);
97 DBG ("PARENT ; registered port %d", port_id);
102 int port_id = _register_test_port ("test_child_port", _on_child_got_message);
103 DBG ("CHILD ; registered port %d", port_id);
104 /* sleep sometime till server port is ready */
106 gchar *parent_app_id = g_strdup_printf ("%d", getppid());
108 messageport_error_e res = messageport_check_trusted_remote_port (parent_app_id, "test_parent_port", &found);
111 DBG ("CHILD : Could not found remote port (%d)", res);
115 DBG ("CHILD : Found remote prot..., sending data to remote port (%s:%s)", parent_app_id, "test_parent_port");
117 bundle *b = bundle_create ();
118 bundle_add (b, "Name", "Amarnath");
119 bundle_add (b, "Email", "amarnath.valluri@intel.com");
121 res = messageport_send_bidirectional_trusted_message(port_id, parent_app_id, "test_parent_port", b);
123 if (res != MESSAGEPORT_ERROR_NONE)
125 DBG ("CHILD: Fail to send message to server : %d", res);
127 else DBG ("CHILD : Data sent successfully");
130 g_main_loop_run (__loop);
132 g_main_loop_unref (__loop);