From: Amarnath Valluri Date: Fri, 18 Oct 2013 11:07:29 +0000 (+0300) Subject: examples: add example to demonstrate API usage in C++ X-Git-Tag: accepted/tizen/20131112.202916~23 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b867ae5a4930dcd8e24c6244b314123b3b3033aa;p=profile%2Fivi%2Fmessage-port.git examples: add example to demonstrate API usage in C++ lib: fixed gboolean -> bool issue --- diff --git a/configure.ac b/configure.ac index a2c7b8f..c293083 100644 --- a/configure.ac +++ b/configure.ac @@ -56,6 +56,9 @@ AC_ARG_ENABLE(examples, [--enable-examples example applications], [enable_examples=$enable_examples], [enable_examples=no]) AM_CONDITIONAL(BUILD_EXAMPLES, [test "x$enable_examples" = "xyes"]) +if test "x$enable_examples" = "xyes"; then + AC_PROG_CXX +fi # Checks for header files. AC_CHECK_HEADERS([string.h]) diff --git a/examples/Makefile.am b/examples/Makefile.am index 4db2803..2a4cb5d 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,8 +1,10 @@ -bin_PROGRAMS = msgport-example-app +bin_PROGRAMS = msgport-example-app msgport-example-app-cpp msgport_example_app_SOURCES = test-app.c - msgport_example_app_LDADD = ../lib/libmessage-port.la $(GLIB_LIBS) $(BUNDLE_LIBS) - msgport_example_app_CPPFLAGS = -I../lib/ -I ../ $(GLIB_CFLAGS) $(BUNDLE_CFLAGS) +msgport_example_app_cpp_SOURCES = test-app.cpp +msgport_example_app_cpp_LDADD = ../lib/libmessage-port.la $(GLIB_LIBS) $(BUNDLE_LIBS) +msgport_example_app_cpp_CXXFLAGS = -I../lib/ -I ../ $(GLIB_CFLAGS) $(BUNDLE_CFLAGS) + diff --git a/examples/test-app.c b/examples/test-app.c index 08a8b92..1cea322 100644 --- a/examples/test-app.c +++ b/examples/test-app.c @@ -19,7 +19,7 @@ void (_on_child_got_message)(int port_id, const char* remote_app_id, const char* { gchar *name = NULL; messageport_get_local_port_name (port_id, &name), - DBG ("CHILD: GOT MESSAGE at prot %s FROM :'%s' - '%s", name, + DBG ("CHILD: GOT MESSAGE at prot '%s' FROM :'%s' - '%s", name, remote_app_id ? remote_app_id : "unknwon app", remote_port ? remote_port : "unknwon"); g_free (name); g_assert (data); @@ -45,7 +45,7 @@ void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char messageport_error_e res = trusted_message ? messageport_check_trusted_remote_port (remote_app_id, remote_port, &found) : messageport_check_remote_port (remote_app_id, remote_port, &found); if (!found) { - DBG ("PARENT: Could not found remote port (%d)", res); + WARN ("PARENT: Could not found remote port (%d)", res); exit (-1); } @@ -56,13 +56,15 @@ void (_on_parent_got_message)(int port_id, const char* remote_app_id, const char bundle_add (reply, "Results", "GOT_IT"); DBG ("PARENT: Sending reply ...."); - res = messageport_send_message (remote_app_id, remote_port, reply); + res = trusted_message ? messageport_send_trusted_message (remote_app_id, remote_port, reply) + : messageport_send_message (remote_app_id, remote_port, reply); bundle_free (reply); if (res != MESSAGEPORT_ERROR_NONE) { - DBG ("PARENT: Faile to send message to server : %d", res); + WARN ("PARENT: Faile to send message to server : %d", res); } else DBG ("PARENT: Data sent successfully"); + g_main_loop_quit (__loop); } @@ -76,7 +78,7 @@ int _register_test_port (const gchar *port_name, messageport_message_cb cb) g_free (name); } else { - DBG ("Failed to register port : %d", port_id); + WARN ("Failed to register port : %d", port_id); } return port_id; } @@ -89,27 +91,39 @@ int main (int argc, char *argv[]) child_pid = fork (); if (child_pid < 0) { - g_error ("Failed to fork "); + ERR ("Failed to fork "); } else if (child_pid > 0) { /* prent process : server port */ int port_id = _register_test_port ("test_parent_port", _on_parent_got_message); - DBG ("PARENT ; registered port %d", port_id); - + if (port_id < 0) { + WARN ("PARENT: Exiting..."); + exit (-1); + } + else { + DBG ("PARENT ; registered port %d", port_id); + } } else { /* child process */ int port_id = _register_test_port ("test_child_port", _on_child_got_message); - DBG ("CHILD ; registered port %d", port_id); + if (port_id < 0) { + WARN ("CHILD: Exiting..."); + exit (-1); + } + else DBG ("CHILD ; registered port %d", port_id); + + DBG("CHILD: Waiting for sometime to get server port ready...."); /* sleep sometime till server port is ready */ - sleep (5); + sleep (3); + gchar *parent_app_id = g_strdup_printf ("%d", getppid()); gboolean found; messageport_error_e res = messageport_check_trusted_remote_port (parent_app_id, "test_parent_port", &found); if (!found) { - DBG ("CHILD : Could not found remote port (%d)", res); - return -1; + WARN ("CHILD : Could not found remote port (%d)", res); + exit(-1); } DBG ("CHILD : Found remote prot..., sending data to remote port (%s:%s)", parent_app_id, "test_parent_port"); @@ -122,7 +136,8 @@ int main (int argc, char *argv[]) bundle_free (b); if (res != MESSAGEPORT_ERROR_NONE) { - DBG ("CHILD: Fail to send message to server : %d", res); + WARN ("CHILD: Fail to send message to server : %d", res); + exit (-1); } else DBG ("CHILD : Data sent successfully"); } @@ -131,6 +146,8 @@ int main (int argc, char *argv[]) g_main_loop_unref (__loop); + DBG ("TEST RSULT : SUCCESS"); + return 0; } diff --git a/examples/test-app.cpp b/examples/test-app.cpp new file mode 100644 index 0000000..390b595 --- /dev/null +++ b/examples/test-app.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +using namespace std; + +class LocalPort { +protected: + static void OnMessage (int id, const char *r_app, const char *r_port, bool r_is_trusted, bundle *data) + { + cout << "Message received" << endl; + } + +public: + LocalPort (const std::string &name, bool is_trusted) + : m_name (name), m_trusted(is_trusted) { } + + bool Register () { + int res ; + res = m_trusted ? messageport_register_trusted_local_port (m_name.c_str(), OnMessage) + : messageport_register_local_port (m_name.c_str(), OnMessage); + + if (res < 0) { + cerr << "Failed to register port '"<< m_name << "'"; + return false; + } + + return true; + } + + bool SendMessage (const std::string &app_id, const std::string &port_name, bool is_trusted, bundle *data) + { + messageport_error_e res ; + + res = is_trusted ? messageport_send_bidirectional_trusted_message (m_port_id, app_id.c_str(), port_name.c_str(), data) + : messageport_send_bidirectional_message (m_port_id, app_id.c_str(), port_name.c_str(), data); + if (res < 0) { + cerr << "Fail to send bidirectional message to '" << app_id << "/" << port_name << ":" << res ; + return false; + } + + return true; + } + +private: + std::string m_name; + bool m_trusted; + int m_port_id; +}; + +int main (int argc, const char *argv[]) +{ + GMainLoop *m_loop = g_main_loop_new (NULL, FALSE); + + LocalPort port1 ("test_port1", false); + + port1.Register (); + + g_main_loop_run (m_loop); + + g_main_loop_unref (m_loop); + + return 0; +} + diff --git a/lib/message-port.h b/lib/message-port.h index ed823f8..30f4109 100644 --- a/lib/message-port.h +++ b/lib/message-port.h @@ -12,6 +12,10 @@ #include #include +#ifndef __cplusplus +typedef gboolean bool; +#endif + G_BEGIN_DECLS /** @@ -39,7 +43,7 @@ typedef enum _messageport_error_e * @remarks @a data must be released with bundle_free() by you * @remark @a remote_app_id and @a remote_port will be set if the remote application sends a bidirectional message, otherwise they are NULL. */ -typedef void (*messageport_message_cb)(int id, const char* remote_app_id, const char* remote_port, gboolean trusted_message, bundle* data); +typedef void (*messageport_message_cb)(int id, const char* remote_app_id, const char* remote_port, bool trusted_message, bundle* data); /** * @brief Registers the local message port. @n @@ -85,7 +89,7 @@ messageport_register_trusted_local_port(const char* local_port, messageport_mess * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ EXPORT_API messageport_error_e -messageport_check_remote_port(const char* remote_app_id, const char *remote_port, gboolean *exist); +messageport_check_remote_port(const char* remote_app_id, const char *remote_port, bool *exist); /** * @brief Checks if the trusted message port of a remote application is registered. @@ -101,7 +105,7 @@ messageport_check_remote_port(const char* remote_app_id, const char *remote_port * @retval #MESSAGEPORT_ERROR_IO_ERROR Internal I/O error */ EXPORT_API messageport_error_e -messageport_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, gboolean *exist); +messageport_check_trusted_remote_port(const char* remote_app_id, const char *remote_port, bool *exist); /** * @brief Sends a message to the message port of a remote application. @@ -235,7 +239,7 @@ messageport_get_local_port_name(int id, char **name); * @retval #MESSAGEPORT_ERROR_OUT_OF_MEMORY Out of memory */ EXPORT_API messageport_error_e -messageport_check_trusted_local_port(int id, gboolean *is_trusted); +messageport_check_trusted_local_port(int id, bool *is_trusted); G_END_DECLS diff --git a/packaging/message-port.spec b/packaging/message-port.spec index 857a1bd..f5160bb 100644 --- a/packaging/message-port.spec +++ b/packaging/message-port.spec @@ -86,6 +86,7 @@ make %{?_smp_mflags} %defattr(-,root,root,-) %if %{build_examples} == 1 %{_bindir}/msgport-example-app +%{_bindir}/msgport-example-app-cpp %endif %{_libdir}/pkgconfig/%{name}.pc %{_includedir}/*.h