summaryrefslogtreecommitdiff
path: root/daemon
diff options
context:
space:
mode:
authorJeonghoon Park <jh1979.park@samsung.com>2018-06-20 15:21:17 +0900
committerJeonghoon Park <jh1979.park@samsung.com>2018-06-21 15:50:59 +0900
commite6cb3990b32d81cd1a2b8b5c4d7257a34ab7c657 (patch)
treeec596528f3cef5c990a54ef88b6c5550b602610b /daemon
parent5131db3c2a88babb0b572fa107a1e150af9778dc (diff)
downloadtizen-things-daemon-e6cb3990b32d81cd1a2b8b5c4d7257a34ab7c657.tar.gz
tizen-things-daemon-e6cb3990b32d81cd1a2b8b5c4d7257a34ab7c657.tar.bz2
tizen-things-daemon-e6cb3990b32d81cd1a2b8b5c4d7257a34ab7c657.zip
handling project name on both of daemon and things lib
Change-Id: I3e42272df71e735896d2ec42b684b10172443688
Diffstat (limited to 'daemon')
-rw-r--r--daemon/include/ttd-app-data.h27
-rw-r--r--daemon/src/ttd-app-data.c62
-rw-r--r--daemon/src/ttd-app-interface.c170
3 files changed, 207 insertions, 52 deletions
diff --git a/daemon/include/ttd-app-data.h b/daemon/include/ttd-app-data.h
new file mode 100644
index 0000000..9b9218b
--- /dev/null
+++ b/daemon/include/ttd-app-data.h
@@ -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
index 0000000..b92ead8
--- /dev/null
+++ b/daemon/src/ttd-app-data.c
@@ -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;
+}
diff --git a/daemon/src/ttd-app-interface.c b/daemon/src/ttd-app-interface.c
index 9ff1401..39a4a97 100644
--- a/daemon/src/ttd-app-interface.c
+++ b/daemon/src/ttd-app-interface.c
@@ -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
@@ -31,22 +32,27 @@
"<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;