Change from INET tcp to Unix domain socket
[framework/multimedia/media-server.git] / src / server / media-server-db.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 /**
23  * This file defines api utilities of media db write.
24  *
25  * @file                media-server-db.c
26  * @author      Haejeong Kim(backto.kim@samsung.com)
27  * @version     1.0
28  * @brief
29  */
30
31 #include <unistd.h>
32
33 #include "media-util.h"
34 #include "media-server-ipc.h"
35 #include "media-common-types.h"
36
37 #include "media-server-dbg.h"
38 #include "media-server-socket.h"
39 #include "media-server-db.h"
40
41 static GMainLoop *g_db_mainloop = NULL;
42 static bool db_thread_ready = FALSE;
43
44 GMainLoop *ms_db_get_mainloop(void)
45 {
46         return g_db_mainloop;
47 }
48 gboolean ms_db_get_thread_status(void)
49 {
50         return db_thread_ready;
51 }
52
53 gboolean ms_db_thread(void *data)
54 {
55         int sockfd = -1;
56         int tcp_sockfd = -1;
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;
64
65         /* Connect Media DB*/
66         if(media_db_connect(&db_handle) != MS_MEDIA_ERR_NONE) {
67                 MS_DBG_ERR("Failed to connect DB\n");
68                 return FALSE;
69         }
70
71         /* Create Socket*/
72         ret = ms_ipc_create_server_socket(MS_PROTOCOL_UDP, MS_DB_UPDATE_PORT, &sockfd);
73         if(ret != MS_MEDIA_ERR_NONE) {
74                 /* Disconnect DB*/
75                 media_db_disconnect(db_handle);
76
77                 MS_DBG_ERR("Failed to create socket\n");
78                 return FALSE;
79         }
80
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);
84 #else
85         ret = ms_ipc_create_server_socket(MS_PROTOCOL_TCP, MS_DB_BATCH_UPDATE_PORT, &tcp_sockfd);
86 #endif
87         if(ret != MS_MEDIA_ERR_NONE) {
88                 /* Disconnect DB*/
89                 media_db_disconnect(db_handle);
90                 close(sockfd);
91                 MS_DBG_ERR("Failed to create socket\n");
92                 return FALSE;
93         }
94
95         context = g_main_context_new();
96         /*Init main loop*/
97         g_db_mainloop = g_main_loop_new(context, FALSE);
98         //context = g_main_loop_get_context(g_db_mainloop);
99
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);
103
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);
107
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);
111
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);
115
116         g_main_context_push_thread_default(context);
117
118         MS_DBG("*******************************************");
119         MS_DBG("*** Media Server DB thread is running ***");
120         MS_DBG("*******************************************");
121
122         db_thread_ready = TRUE;
123
124         g_main_loop_run(g_db_mainloop);
125
126         MS_DBG("*** Media Server DB thread will be closed ***");
127
128         db_thread_ready = FALSE;
129
130         g_io_channel_shutdown(channel,  FALSE, NULL);
131         g_io_channel_unref(channel);
132
133         /* Disconnect DB*/
134         media_db_disconnect(db_handle);
135
136         /*close socket*/
137         close(sockfd);
138         close(tcp_sockfd);
139
140         g_main_loop_unref(g_db_mainloop);
141
142         return FALSE;
143 }