#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;
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)
{
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]);
}
}
}
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);
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;
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);
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);
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
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;
}
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);
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) {
return -1;
}
- g_atomic_int_set(&(_handle->post_thread_run), __POST_THREAD_RUN);
-
*handle = _handle;
return 0;