4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 * This file defines api utilities of media db write.
25 * @file media-server-db.c
26 * @author Haejeong Kim(backto.kim@samsung.com)
33 #include "media-util.h"
34 #include "media-server-ipc.h"
35 #include "media-common-types.h"
37 #include "media-server-dbg.h"
38 #include "media-server-socket.h"
39 #include "media-server-db.h"
41 static GMainLoop *g_db_mainloop = NULL;
42 static bool db_thread_ready = FALSE;
44 GMainLoop *ms_db_get_mainloop(void)
48 gboolean ms_db_get_thread_status(void)
50 return db_thread_ready;
53 gboolean ms_db_thread(void *data)
57 int ret = MS_MEDIA_ERR_NONE;
58 GSource *source = NULL;
59 GIOChannel *channel = NULL;
60 GSource *tcp_source = NULL;
61 GIOChannel *tcp_channel = NULL;
62 GMainContext *context = NULL;
63 MediaDBHandle *db_handle = NULL;
66 if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) {
67 MS_DBG_ERR("Failed to connect DB\n");
72 ret = ms_ipc_create_server_socket(MS_PROTOCOL_UDP, MS_DB_UPDATE_PORT, &sockfd);
73 if(ret != MS_MEDIA_ERR_NONE) {
75 media_db_disconnect(db_handle);
77 MS_DBG_ERR("Failed to create socket\n");
81 /* Create TCP Socket for batch query*/
82 #ifdef _USE_UDS_SOCKET_TCP_
83 ret = ms_ipc_create_server_tcp_socket(MS_PROTOCOL_TCP, MS_DB_BATCH_UPDATE_TCP_PORT, &tcp_sockfd);
85 ret = ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_DB_BATCH_UPDATE_PORT, &tcp_sockfd);
87 if(ret != MS_MEDIA_ERR_NONE) {
89 media_db_disconnect(db_handle);
91 MS_DBG_ERR("Failed to create socket\n");
95 context = g_main_context_new();
97 g_db_mainloop = g_main_loop_new(context, FALSE);
98 //context = g_main_loop_get_context(g_db_mainloop);
100 /* Create new channel to watch udp socket */
101 channel = g_io_channel_unix_new(sockfd);
102 source = g_io_create_watch(channel, G_IO_IN);
104 /* Set callback to be called when socket is readable */
105 g_source_set_callback(source, (GSourceFunc)ms_read_db_socket, db_handle, NULL);
106 g_source_attach(source, context);
108 /* Create new channel to watch TCP socket */
109 tcp_channel = g_io_channel_unix_new(tcp_sockfd);
110 tcp_source = g_io_create_watch(tcp_channel, G_IO_IN);
112 /* Set callback to be called when socket is readable */
113 g_source_set_callback(tcp_source, (GSourceFunc)ms_read_db_tcp_socket, db_handle, NULL);
114 g_source_attach(tcp_source, context);
116 g_main_context_push_thread_default(context);
118 MS_DBG("*******************************************");
119 MS_DBG("*** Media Server DB thread is running ***");
120 MS_DBG("*******************************************");
122 db_thread_ready = TRUE;
124 g_main_loop_run(g_db_mainloop);
126 MS_DBG("*** Media Server DB thread will be closed ***");
128 db_thread_ready = FALSE;
130 g_io_channel_shutdown(channel, FALSE, NULL);
131 g_io_channel_unref(channel);
134 media_db_disconnect(db_handle);
140 g_main_loop_unref(g_db_mainloop);