handling project name on both of daemon and things lib 57/182057/2
authorJeonghoon Park <jh1979.park@samsung.com>
Wed, 20 Jun 2018 06:21:17 +0000 (15:21 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Thu, 21 Jun 2018 06:50:59 +0000 (15:50 +0900)
Change-Id: I3e42272df71e735896d2ec42b684b10172443688

daemon/include/ttd-app-data.h [new file with mode: 0644]
daemon/src/ttd-app-data.c [new file with mode: 0644]
daemon/src/ttd-app-interface.c
lib/things-service/include/things-service.h
lib/things-service/src/things-service.c

diff --git a/daemon/include/ttd-app-data.h b/daemon/include/ttd-app-data.h
new file mode 100644 (file)
index 0000000..9b9218b
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TT_DAEMON_APP_DATA_H__
+#define __TT_DAEMON_APP_DATA_H__
+
+typedef struct __ttd_app_data ttd_app_data;
+
+ttd_app_data *ttd_app_data_new(const char *project, const char *data);
+void ttd_app_data_free(ttd_app_data *app_data);
+const char *ttd_app_data_get_project_name(ttd_app_data *app_data);
+const char *ttd_app_data_get_data(ttd_app_data *app_data);
+
+#endif /* __TT_DAEMON_APP_DATA_H__ */
diff --git a/daemon/src/ttd-app-data.c b/daemon/src/ttd-app-data.c
new file mode 100644 (file)
index 0000000..b92ead8
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <glib.h>
+#include "ttd-log.h"
+#include "ttd-app-data.h"
+
+struct __ttd_app_data {
+       char *p_name;
+       char *data;
+};
+
+ttd_app_data *ttd_app_data_new(const char *project, const char *data)
+{
+       ttd_app_data *app_data = NULL;
+
+       retv_if(!project, NULL);
+       retv_if(!data, NULL);
+
+       app_data = g_try_malloc0(sizeof(ttd_app_data));
+       retv_if(!app_data, NULL);
+
+       app_data->p_name = g_strdup(project);
+       app_data->data = g_strdup(data);
+
+       return app_data;
+}
+
+void ttd_app_data_free(ttd_app_data *app_data)
+{
+       if (!app_data)
+               return;
+
+       g_free(app_data->p_name);
+       g_free(app_data->data);
+       g_free(app_data);
+}
+
+const char *ttd_app_data_get_project_name(ttd_app_data *app_data)
+{
+       retv_if(!app_data, NULL);
+       return app_data->p_name;
+}
+
+const char *ttd_app_data_get_data(ttd_app_data *app_data)
+{
+       retv_if(!app_data, NULL);
+       return app_data->data;
+}
index 9ff1401..39a4a97 100644 (file)
@@ -21,6 +21,7 @@
 #include "ttd-app-interface.h"
 #include "ttd-http.h"
 #include "common-app-inf.h"
+#include "ttd-app-data.h"
 
 #define POST_DATA_URL "http://apitest.showiot.xyz/api/data"
 #define __POST_THREAD_RUN 1
 "<node>" \
 "  <interface name='"TTD_APP_INF_BUS_INF"'>" \
 "    <method name='"TTD_APP_INF_METHOD_REG"'>" \
+"      <arg type='s' name='project' direction='in'/>" \
 "      <arg type='s' name='token' direction='in'/>" \
 "      <arg type='s' name='appID' direction='in'/>" \
 "      <arg type='u' name='appPID' direction='in'/>" \
 "      <arg type='i' name='response' direction='out'/>" \
+"      <arg type='s' name='response_message' direction='out'/>" \
 "    </method>" \
 "    <method name='"TTD_APP_INF_METHOD_UNREG"'>" \
+"      <arg type='s' name='project' direction='in'/>" \
 "      <arg type='s' name='token' direction='in'/>" \
 "      <arg type='s' name='appID' direction='in'/>" \
 "      <arg type='u' name='appPID' direction='in'/>" \
 "      <arg type='i' name='response' direction='out'/>" \
+"      <arg type='s' name='response_message' direction='out'/>" \
 "    </method>" \
 "  </interface>" \
 "</node>"
 
 typedef struct __thread_data_s {
        char *name;
+       char *project;
        int disconnected;
        GMainContext *context;
        GMainLoop *loop;
@@ -63,9 +69,10 @@ struct _ttd_app_inf_h {
        GHashTable *app_sock_hash;
        GThread *post_thread;
        GAsyncQueue *data_queue;
-       int post_thread_run;
 };
 
+static const gpointer queue_quit_marker = (gpointer) &ttd_app_interface_init;
+
 static char *
 __get_addr_name(const gchar *token, const gchar *appID, guint appPID)
 {
@@ -124,8 +131,14 @@ __receive_msg(GIOChannel *ch, GIOCondition cond, gpointer user_data)
                                g_free(strv[i]);
                                continue;
                        } else {
+                               ttd_app_data *app_data = NULL;
                                _D("strv[%d] : %s", i, strv[i]);
-                               g_async_queue_push(data->data_queue, strv[i]);
+                               app_data = ttd_app_data_new(data->project, strv[i]);
+                               if (app_data)
+                                       g_async_queue_push(data->data_queue, app_data);
+                               else
+                                       _E("failed to creat app_data for %s", strv[i]);
+                               g_free(strv[i]);
                        }
                }
        }
@@ -210,6 +223,14 @@ THREAD_EXIT:
        return NULL;
 }
 
+static void thread_data_set_project(thread_data *data, const char *project)
+{
+       ret_if(!data);
+       ret_if(!project);
+
+       data->project = g_strdup(project);
+}
+
 static void thread_data_set_name(thread_data *data, const char *name)
 {
        ret_if(!data);
@@ -297,7 +318,7 @@ create_thread_for_socket(GSocket *sock, GSocketAddress *addr)
 
 static int
 __get_new_connection_thread(ttd_app_inf_h handle,
-       const gchar *token, const gchar *appID, guint appPID)
+       const gchar *token, const gchar *appID, guint appPID, const char *project)
 {
        char *addr_name = NULL;
        GSocket *socket = NULL;
@@ -309,6 +330,7 @@ __get_new_connection_thread(ttd_app_inf_h handle,
        retv_if(!token, -1);
        retv_if(!appID, -1);
        retv_if(!appPID, -1);
+       retv_if(!project, -1);
 
        addr_name = __get_addr_name(token, appID, appPID);
        retv_if(!addr_name, -1);
@@ -339,6 +361,7 @@ __get_new_connection_thread(ttd_app_inf_h handle,
        thread_data_set_queue(thread_d, handle->data_queue);
        thread_data_set_main_data(thread_d, handle);
        thread_data_set_name(thread_d, addr_name);
+       thread_data_set_project(thread_d, project);
        g_hash_table_insert(handle->app_sock_hash, addr_name, thread_d);
 
        g_object_unref(socket);
@@ -361,81 +384,116 @@ FREE_N_RETURN_ERROR:
        return -1;
 }
 
+static void __data_queue_item_free(gpointer data)
+{
+       if (!data)
+               return;
+
+       if (data == queue_quit_marker)
+               return;
+
+       ttd_app_data_free(data);
+}
+
 static void __handle_method_register(GVariant *parameters,
        GDBusMethodInvocation *invocation, ttd_app_inf_h handle)
 {
+       const gchar *project = NULL;
        const gchar *token = NULL;
        const gchar *appID = NULL;
        guint appPID = 0;
-       gint response = NULL;
+       gint response = 0;
+       const gchar *response_msg = NULL;
 
-       g_variant_get(parameters, "(&s&su)", &token, &appID, &appPID);
-       _D("received register request from [%s-%u] - token[%s]",
-               appID, appPID, token);
+       g_variant_get(parameters, "(&s&s&su)", &project, &token, &appID, &appPID);
+       _D("received register request from [%s-%u] - project[%s], token[%s]",
+               appID, appPID, project, token);
 
        if (!appID) {
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to get appID");
-               return;
+               response = -1;
+               response_msg = "failed to get appID";
+               goto METHOD_RETURN;
        }
 
        if (!appPID) {
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to get appPID");
-               return;
+               response = -1;
+               response_msg = "failed to get appPID";
+               goto METHOD_RETURN;
        }
 
        if (!token) {
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to get token");
-               return;
+               response = -1;
+               response_msg = "failed to get token";
+               goto METHOD_RETURN;
+       }
+
+       if (!project) {
+               response = -1;
+               response_msg = "failed to get project";
+               goto METHOD_RETURN;
        }
 
-       response = __get_new_connection_thread(handle, token, appID, appPID);
+       response =
+               __get_new_connection_thread(handle, token, appID, appPID, project);
        if (!response)
-               g_dbus_method_invocation_return_value(invocation,
-                       g_variant_new("(i)", response));
+               response_msg = "OK";
        else
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to connect");
+               response_msg = "failed to connect";
+
+METHOD_RETURN:
+       g_dbus_method_invocation_return_value(invocation,
+               g_variant_new("(is)", response, response_msg));
+
 }
 
 static void __handle_method_unregister(GVariant *parameters,
        GDBusMethodInvocation *invocation, ttd_app_inf_h handle)
 {
+       const gchar *project = NULL;
        const gchar *token = NULL;
        const gchar *appID = NULL;
        guint appPID = 0;
+       gint response = 0;
+       const gchar *response_msg = NULL;
        char *key = NULL;
 
-       g_variant_get(parameters, "(&s&su)", &token, &appID, &appPID);
-       _D("received unregister request from [%s-%u] - token[%s]",
-               appID, appPID, token);
+       g_variant_get(parameters, "(&s&s&su)", &project, &token, &appID, &appPID);
+       _D("received unregister request from [%s-%u] - project[%s] - token[%s]",
+               appID, appPID, project, token);
 
        if (!appID) {
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to get appID");
-               return;
+               response = -1;
+               response_msg = "failed to get appID";
+               goto METHOD_RETURN;
        }
 
        if (!appPID) {
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to get appPID");
-               return;
+               response = -1;
+               response_msg = "failed to get appPID";
+               goto METHOD_RETURN;
        }
 
        if (!token) {
-               g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
-                       G_DBUS_ERROR_FAILED, "failed to get token");
-               return;
+               response = -1;
+               response_msg = "failed to get token";
+               goto METHOD_RETURN;
+       }
+
+       if (!project) {
+               response = -1;
+               response_msg = "failed to get project";
+               goto METHOD_RETURN;
        }
 
        key = __get_addr_name(token, appID, appPID);
        g_hash_table_remove(handle->app_sock_hash, key);
        g_free(key);
 
+       response_msg = "OK";
+
+METHOD_RETURN:
        g_dbus_method_invocation_return_value(invocation,
-               g_variant_new("(i)", 0));
+               g_variant_new("(is)", response, response_msg));
 }
 
 static void
@@ -517,27 +575,37 @@ static gpointer post_thread(gpointer user_data)
 
        retv_if(!handle, NULL);
 
-       while (g_atomic_int_get(&(handle->post_thread_run))) {
-               char *msg = NULL;
+       while (TRUE) {
+               ttd_app_data *app_data = NULL;
                int ret = 0;
 
-               msg = g_async_queue_timeout_pop(
-                               handle->data_queue, (guint64)__QUEUE_TIME_OUT*1000);
-               if (!msg)
+               app_data = g_async_queue_pop(handle->data_queue);
+               if (!app_data)
                        continue;
+
+               if (app_data == queue_quit_marker) {
+                       _D("receiving queue_quit_marker");
+                       break;
+               }
 #if 0  /* Enable it if API url for posting data is created */
                /* TODO : get API url for posting data to replace POST_DATA_URL */
-               ret = ttd_http_post_data(POST_DATA_URL, msg);
+               long res_code = 0;
+               char *url = NULL;
+
+               /* TODO : get url based on project name */
+               url = __get_projcet_url(ttd_app_data_get_project_name(app_data));
+               ret = ttd_http_appdata_post(url,
+                       ttd_app_data_get_data(app_data), &res_code);
                if (!ret) {
-                       _E("failed to post data, retry it");
-                       g_async_queue_push_front(handle->data_queue, msg);
+                       _E("failed to post data, retry it - code[%ld]", res_code);
+                       g_async_queue_push_front(handle->data_queue, app_data);
                        g_usleep((gulong)(G_USEC_PER_SEC/2));
                        /* 0.5 sec sleep before retry it */
                        continue;
                }
 #endif
-               _D("msg posted : %s", msg);
-               g_free(msg);
+               _D("msg posted : %s", ttd_app_data_get_data(app_data));
+               ttd_app_data_free(app_data);
        }
        return NULL;
 }
@@ -557,13 +625,13 @@ int ttd_app_interface_fini(ttd_app_inf_h handle)
                g_hash_table_unref(handle->app_sock_hash);
        }
 
-       if (handle->post_thread) {
-               g_atomic_int_set(&(handle->post_thread_run), __POST_THREAD_STOP);
-               g_thread_join(handle->post_thread);
+       if (handle->data_queue) {
+               g_async_queue_push_front(handle->data_queue, queue_quit_marker);
+               g_async_queue_ref(handle->data_queue);
        }
 
-       if (handle->data_queue)
-               g_async_queue_ref(handle->data_queue);
+       if (handle->post_thread)
+               g_thread_join(handle->post_thread);
 
        g_free(handle);
 
@@ -607,7 +675,7 @@ int ttd_app_interface_init(ttd_app_inf_h *handle)
                g_hash_table_new_full(g_str_hash, g_str_equal,
                        g_free, (GDestroyNotify)thread_quit_and_free);
 
-       _handle->data_queue = g_async_queue_new_full(g_free);
+       _handle->data_queue = g_async_queue_new_full(__data_queue_item_free);
        _handle->post_thread =
                g_thread_try_new(NULL, (GThreadFunc)post_thread, _handle, &error);
        if (!_handle->post_thread) {
@@ -617,8 +685,6 @@ int ttd_app_interface_init(ttd_app_inf_h *handle)
                return -1;
        }
 
-       g_atomic_int_set(&(_handle->post_thread_run), __POST_THREAD_RUN);
-
        *handle = _handle;
 
        return 0;
index 5710351..6b206c1 100644 (file)
@@ -19,7 +19,7 @@
 
 typedef struct _ts_data *ts_handle;
 
-int things_service_init(ts_handle *handle);
+int things_service_init(ts_handle *handle, const char *project);
 int things_service_fini(ts_handle handle);
 int things_service_send_data(ts_handle handle, const char *json_data);
 
index 270feb6..31f6e3a 100644 (file)
@@ -29,6 +29,7 @@
 struct _ts_data {
        char *app_id;
        char *token;
+       char *project;
        GSocket *socket;
        GSocketAddress *addr;
        GSocket *cl_socket;
@@ -103,6 +104,9 @@ static void _ts_handle_free(ts_handle handle)
        if (handle->app_id)
                g_free(handle->app_id);
 
+       if (handle->project)
+               g_free(handle->project);
+
        if (handle->token)
                g_free(handle->token);
 
@@ -195,12 +199,14 @@ static int __client_method_call(const char *method, ts_handle handle)
 {
        GVariant *response = NULL;
        int ret = 0;
+       const char *ret_msg = 0;
        GError *error = NULL;
 
        _D("call method [%s]", method);
        response = g_dbus_proxy_call_sync(handle->proxy,
                method,
-               g_variant_new("(ssu)", handle->token, handle->app_id, (guint)getpid()),
+               g_variant_new("(sssu)",
+                       handle->project, handle->token, handle->app_id, (guint)getpid()),
                G_DBUS_CALL_FLAGS_NONE,
                -1, /* The timeout in milliseconds or -1 to use the proxy default timeout. */
                NULL, /* cancellable */
@@ -210,11 +216,14 @@ static int __client_method_call(const char *method, ts_handle handle)
                g_error_free(error);
                return -1;
        }
-       g_variant_get(response, "(i)", ret);
-       _D("method[%s] get response ret[%d]", method, ret);
+       if (!response)
+               return -1;
+
+       g_variant_get(response, "(i&s)", ret, &ret_msg);
+       _D("method[%s] get response ret[%d] - %s", method, ret, ret_msg);
        g_variant_unref(response);
 
-       return 0;
+       return ret;
 }
 
 static int _client_call_register(ts_handle handle)
@@ -227,7 +236,7 @@ static int _client_call_unregister(ts_handle handle)
        return __client_method_call(TTD_APP_INF_METHOD_UNREG, handle);
 }
 
-int things_service_init(ts_handle *handle)
+int things_service_init(ts_handle *handle, const char *project)
 {
        ts_handle _handle = NULL;
        GError *error = NULL;
@@ -236,6 +245,7 @@ int things_service_init(ts_handle *handle)
        retv_if(!handle, -1);
 
        _handle = g_try_malloc0(sizeof(ts_handle));
+       _handle->project = g_strdup(project);
        _handle->app_id = _get_app_id();
        if (!_handle->app_id) {
                _E("failed to get app id");