2dcef13c25415c13380d26c8939b3db76e7203fa
[profile/ivi/message-port.git] / examples / test-app.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * Copyright (C) 2013 Intel Corporation.
5  *
6  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  */
23
24 #include <glib.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <message-port.h>
28 #include <bundle.h>
29
30 GMainLoop *__loop = NULL;
31
32 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
33 {
34     gchar *val = NULL;
35     size_t size;
36     bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
37     g_message ("       %s - %s", key, val);
38 }
39
40 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
41 {
42     gchar *name = NULL;
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");
46     g_free (name);
47     g_assert (data);
48
49     bundle_foreach (data, _dump_data, NULL);
50     g_main_loop_quit (__loop);
51 }
52
53 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
54 {
55     gchar *name = NULL;
56     gboolean found = FALSE;
57     bundle *b = NULL;
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");
61     g_free (name);
62
63     g_assert (data);
64
65     bundle_foreach (data, _dump_data, NULL);
66
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);
69     if (!found) {
70         g_warning ("PARENT: Could not found remote port (%d)", res);
71         exit (-1);
72     }
73
74     g_message ("PARENT: Found remote prot");
75
76     bundle *reply = bundle_create ();
77
78     bundle_add (reply, "Results", "GOT_IT");
79
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);
83     bundle_free (reply);
84     if (res != MESSAGEPORT_ERROR_NONE)
85     {
86         g_warning ("PARENT: Faile to send message to server : %d", res);
87     }
88     else g_message ("PARENT: Data sent successfully");
89     g_main_loop_quit (__loop);
90
91 }
92
93 int _register_test_port (const gchar *port_name, messageport_message_cb cb)
94 {
95     int port_id = messageport_register_trusted_local_port (port_name, cb);
96
97     if (port_id > MESSAGEPORT_ERROR_NONE) {
98         gchar *name = NULL;
99         messageport_get_local_port_name (port_id, &name); 
100         g_free (name);
101     }
102     else {
103         g_warning ("Failed to register port : %d", port_id);
104     }
105     return port_id;
106 }
107
108 int main (int argc, char *argv[])
109 {
110     pid_t child_pid;
111
112     __loop = g_main_loop_new (NULL, FALSE);
113     child_pid = fork ();
114     
115     if (child_pid < 0) {
116         g_error ("Failed to fork ");
117     }
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);
121        if (port_id < 0) {
122            g_warning ("PARENT: Exiting...");
123            exit (-1);
124        }
125        else {
126            g_message ("PARENT ; registered port %d", port_id);
127        }
128     }
129     else {
130         /* child process */
131         int port_id = _register_test_port ("test_child_port", _on_child_got_message);
132         if (port_id < 0) {
133            g_warning ("CHILD: Exiting...");
134            exit (-1);
135         }
136         else g_message ("CHILD ; registered port %d", port_id);
137
138         g_message("CHILD: Waiting for sometime to get server port ready....");
139         /* sleep sometime till server port is ready */
140         sleep (3);
141
142         gchar *parent_app_id = g_strdup_printf ("%d", getppid());
143         gboolean found;
144         messageport_error_e res = messageport_check_trusted_remote_port (parent_app_id, "test_parent_port", &found);
145
146         if (!found) {
147             g_warning ("CHILD : Could not found remote port (%d)", res);
148             exit(-1);
149         }
150
151         g_message ("CHILD : Found remote prot..., sending data to remote port (%s:%s)", parent_app_id, "test_parent_port");
152
153         bundle *b = bundle_create ();
154         bundle_add (b, "Name", "Amarnath");
155         bundle_add (b, "Email", "amarnath.valluri@intel.com");
156
157         res = messageport_send_bidirectional_trusted_message(port_id, parent_app_id, "test_parent_port", b);
158         bundle_free (b);
159         if (res != MESSAGEPORT_ERROR_NONE)
160         {
161             g_warning ("CHILD: Fail to send message to server : %d", res);
162             exit (-1);
163         }
164         else g_message ("CHILD : Data sent successfully");
165     }
166
167     g_main_loop_run (__loop);
168
169     g_main_loop_unref (__loop);
170
171     g_message ("TEST RSULT : SUCCESS");
172
173     return 0;
174 }
175