1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * Copyright (C) 2013 Intel Corporation.
6 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 #include <message-port.h>
30 GMainLoop *__loop = NULL;
32 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
36 bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
37 g_message (" %s - %s", key, val);
40 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
43 messageport_get_local_port_name (port_id, &name),
44 g_message ("CHILD: GOT MESSAGE at prot '%s' FROM :'%s' - '%s", name,
45 remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon");
49 bundle_foreach (data, _dump_data, NULL);
50 g_main_loop_quit (__loop);
53 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
56 gboolean found = FALSE;
58 messageport_get_local_port_name (port_id, &name),
59 g_message ("PARENT: GOT MESSAGE at prot %s FROM :'%s' - '%s", name,
60 remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon");
65 bundle_foreach (data, _dump_data, NULL);
67 messageport_error_e res = trusted_message ? messageport_check_trusted_remote_port (remote_app_id, remote_port, &found)
68 : messageport_check_remote_port (remote_app_id, remote_port, &found);
70 g_warning ("PARENT: Could not found remote port (%d)", res);
74 g_message ("PARENT: Found remote prot");
76 bundle *reply = bundle_create ();
78 bundle_add (reply, "Results", "GOT_IT");
80 g_message ("PARENT: Sending reply ....");
81 res = trusted_message ? messageport_send_trusted_message (remote_app_id, remote_port, reply)
82 : messageport_send_message (remote_app_id, remote_port, reply);
84 if (res != MESSAGEPORT_ERROR_NONE)
86 g_warning ("PARENT: Faile to send message to server : %d", res);
88 else g_message ("PARENT: Data sent successfully");
89 g_main_loop_quit (__loop);
93 int _register_test_port (const gchar *port_name, messageport_message_cb cb)
95 int port_id = messageport_register_trusted_local_port (port_name, cb);
97 if (port_id > MESSAGEPORT_ERROR_NONE) {
99 messageport_get_local_port_name (port_id, &name);
103 g_warning ("Failed to register port : %d", port_id);
108 int main (int argc, char *argv[])
112 __loop = g_main_loop_new (NULL, FALSE);
116 g_error ("Failed to fork ");
118 else if (child_pid > 0) {
119 /* prent process : server port */
120 int port_id = _register_test_port ("test_parent_port", _on_parent_got_message);
122 g_warning ("PARENT: Exiting...");
126 g_message ("PARENT ; registered port %d", port_id);
131 int port_id = _register_test_port ("test_child_port", _on_child_got_message);
133 g_warning ("CHILD: Exiting...");
136 else g_message ("CHILD ; registered port %d", port_id);
138 g_message("CHILD: Waiting for sometime to get server port ready....");
139 /* sleep sometime till server port is ready */
142 gchar *parent_app_id = g_strdup_printf ("%d", getppid());
144 messageport_error_e res = messageport_check_trusted_remote_port (parent_app_id, "test_parent_port", &found);
147 g_warning ("CHILD : Could not found remote port (%d)", res);
151 g_message ("CHILD : Found remote prot..., sending data to remote port (%s:%s)", parent_app_id, "test_parent_port");
153 bundle *b = bundle_create ();
154 bundle_add (b, "Name", "Amarnath");
155 bundle_add (b, "Email", "amarnath.valluri@intel.com");
157 res = messageport_send_bidirectional_trusted_message(port_id, parent_app_id, "test_parent_port", b);
159 if (res != MESSAGEPORT_ERROR_NONE)
161 g_warning ("CHILD: Fail to send message to server : %d", res);
164 else g_message ("CHILD : Data sent successfully");
167 g_main_loop_run (__loop);
169 g_main_loop_unref (__loop);
171 g_message ("TEST RSULT : SUCCESS");