f8c945162a5e2af54ea5d54180177210d091f22e
[platform/core/appfw/message-port-dbus.git] / tests / test-app.cpp
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 <message-port.h>
25 #include <bundle.h>
26 #include <string>
27 #include <iostream>
28
29 using namespace std;
30
31 class LocalPort {
32 protected:
33     static void OnMessage (int id, const char *r_app, const char *r_port, bool r_is_trusted, bundle *data)
34     {
35         cout << "Message received" << endl;
36     }
37
38 public:
39     LocalPort (const std::string &name, bool is_trusted)
40         : m_name (name), m_trusted(is_trusted) { }
41
42     bool Register () {
43         int res ;
44         res = m_trusted ? messageport_register_trusted_local_port (m_name.c_str(), OnMessage)
45                         : messageport_register_local_port (m_name.c_str(), OnMessage);
46
47         if (res < 0) {
48             cerr << "Failed to register port '"<< m_name << "'";
49             return false;
50         }
51         
52         return true;
53     }
54
55     bool SendMessage (const std::string &app_id, const std::string &port_name, bool is_trusted, bundle *data)
56     {
57         messageport_error_e res ;
58         
59         res = is_trusted ? messageport_send_bidirectional_trusted_message (m_port_id, app_id.c_str(), port_name.c_str(), data)
60                          : messageport_send_bidirectional_message (m_port_id, app_id.c_str(), port_name.c_str(), data);
61         if (res < 0) {
62             cerr << "Fail to send bidirectional message to '" << app_id << "/" << port_name << ":" << res ;
63             return false;
64         }
65
66         return true;
67     }
68
69     const std::string& name () {
70         return m_name;
71     }
72
73     bool isTrusted () {
74         return m_trusted;
75     }
76
77     int id () {
78         return m_port_id;
79     }
80
81 private:
82     std::string m_name;
83     bool        m_trusted;
84     int         m_port_id;
85 };
86
87 int main (int argc, const char *argv[])
88 {
89     GMainLoop *m_loop = g_main_loop_new (NULL, FALSE);
90
91     LocalPort port1 ("test_port1", false);
92
93     if (port1.Register () != true) {
94         cerr << "Failed to register local message port";
95     }
96     else cout << "Registered local message port : " << port1.name() << ", Id: "<< port1.id();
97
98     g_main_loop_run (m_loop);
99
100     g_main_loop_unref (m_loop);
101
102     return 0;
103 }
104