certifiacte validation code added
[profile/ivi/message-port.git] / examples / test-app.c
1 #include <glib.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <message-port.h>
5 #include <bundle.h>
6 #include "common/log.h"
7
8 GMainLoop *__loop = NULL;
9
10 static void _dump_data (const char *key, const int type, const bundle_keyval_t *kv, void *user_data)
11 {
12     gchar *val = NULL;
13     size_t size;
14     bundle_keyval_get_basic_val ((bundle_keyval_t*)kv, (void**)&val, &size);
15     DBG ("       %s - %s", key, val);
16 }
17
18 void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
19 {
20     gchar *name = NULL;
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");
24     g_free (name);
25     g_assert (data);
26
27     bundle_foreach (data, _dump_data, NULL);
28     g_main_loop_quit (__loop);
29 }
30
31 void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data)
32 {
33     gchar *name = NULL;
34     gboolean found = FALSE;
35     bundle *b = NULL;
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");
39     g_free (name);
40
41     g_assert (data);
42
43     bundle_foreach (data, _dump_data, NULL);
44
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);
47     if (!found) {
48         DBG ("PARENT: Could not found remote port (%d)", res);
49         exit (-1);
50     }
51
52     DBG ("PARENT: Found remote prot");
53
54     bundle *reply = bundle_create ();
55
56     bundle_add (reply, "Results", "GOT_IT");
57
58     DBG ("PARENT: Sending reply ....");
59     res = messageport_send_message (remote_app_id, remote_port, reply);
60     bundle_free (reply);
61     if (res != MESSAGEPORT_ERROR_NONE)
62     {
63         DBG ("PARENT: Faile to send message to server : %d", res);
64     }
65     else DBG ("PARENT: Data sent successfully");
66
67 }
68
69 int _register_test_port (const gchar *port_name, messageport_message_cb cb)
70 {
71     int port_id = messageport_register_trusted_local_port (port_name, cb);
72
73     if (port_id > MESSAGEPORT_ERROR_NONE) {
74         gchar *name = NULL;
75         messageport_get_local_port_name (port_id, &name); 
76         g_free (name);
77     }
78     else {
79         DBG ("Failed to register port : %d", port_id);
80     }
81     return port_id;
82 }
83
84 int main (int argc, char *argv[])
85 {
86     pid_t child_pid;
87
88     __loop = g_main_loop_new (NULL, FALSE);
89     child_pid = fork ();
90     
91     if (child_pid < 0) {
92         g_error ("Failed to fork ");
93     }
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);
98        
99     }
100     else {
101         /* child process */
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 */
105         sleep (5);
106         gchar *parent_app_id = g_strdup_printf ("%d", getppid());
107         gboolean found;
108         messageport_error_e res = messageport_check_trusted_remote_port (parent_app_id, "test_parent_port", &found);
109
110         if (!found) {
111             DBG ("CHILD : Could not found remote port (%d)", res);
112             return -1;
113         }
114
115         DBG ("CHILD : Found remote prot..., sending data to remote port (%s:%s)", parent_app_id, "test_parent_port");
116
117         bundle *b = bundle_create ();
118         bundle_add (b, "Name", "Amarnath");
119         bundle_add (b, "Email", "amarnath.valluri@intel.com");
120
121         res = messageport_send_bidirectional_trusted_message(port_id, parent_app_id, "test_parent_port", b);
122         bundle_free (b);
123         if (res != MESSAGEPORT_ERROR_NONE)
124         {
125             DBG ("CHILD: Fail to send message to server : %d", res);
126         }
127         else DBG ("CHILD : Data sent successfully");
128     }
129
130     g_main_loop_run (__loop);
131
132     g_main_loop_unref (__loop);
133
134     return 0;
135 }
136