updated licence header
[profile/ivi/message-port.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  * This file is part of message-port.
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include <message-port.h>
27 #include <bundle.h>
28 #include <string>
29 #include <iostream>
30
31 using namespace std;
32
33 class LocalPort {
34 protected:
35     static void OnMessage (int id, const char *r_app, const char *r_port, bool r_is_trusted, bundle *data)
36     {
37         cout << "Message received" << endl;
38     }
39
40 public:
41     LocalPort (const std::string &name, bool is_trusted)
42         : m_name (name), m_trusted(is_trusted) { }
43
44     bool Register () {
45         int res ;
46         res = m_trusted ? messageport_register_trusted_local_port (m_name.c_str(), OnMessage)
47                         : messageport_register_local_port (m_name.c_str(), OnMessage);
48
49         if (res < 0) {
50             cerr << "Failed to register port '"<< m_name << "'";
51             return false;
52         }
53         
54         return true;
55     }
56
57     bool SendMessage (const std::string &app_id, const std::string &port_name, bool is_trusted, bundle *data)
58     {
59         messageport_error_e res ;
60         
61         res = is_trusted ? messageport_send_bidirectional_trusted_message (m_port_id, app_id.c_str(), port_name.c_str(), data)
62                          : messageport_send_bidirectional_message (m_port_id, app_id.c_str(), port_name.c_str(), data);
63         if (res < 0) {
64             cerr << "Fail to send bidirectional message to '" << app_id << "/" << port_name << ":" << res ;
65             return false;
66         }
67
68         return true;
69     }
70
71     const std::string& name () {
72         return m_name;
73     }
74
75     bool isTrusted () {
76         return m_trusted;
77     }
78
79     int id () {
80         return m_port_id;
81     }
82
83 private:
84     std::string m_name;
85     bool        m_trusted;
86     int         m_port_id;
87 };
88
89 int main (int argc, const char *argv[])
90 {
91     GMainLoop *m_loop = g_main_loop_new (NULL, FALSE);
92
93     LocalPort port1 ("test_port1", false);
94
95     if (port1.Register () != true) {
96         cerr << "Failed to register local message port";
97     }
98     else cout << "Registered local message port : " << port1.name() << ", Id: "<< port1.id();
99
100     g_main_loop_run (m_loop);
101
102     g_main_loop_unref (m_loop);
103
104     return 0;
105 }
106