Tizen 2.1 base
[platform/core/connectivity/smartcard-service.git] / common / include / IPCHelper.h
1 /*
2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 #ifndef IPCHELPER_H_
19 #define IPCHELPER_H_
20
21 /* standard library header */
22 #include <glib.h>
23 #include <pthread.h>
24 #ifdef USE_IPC_EPOLL
25 #include <sys/epoll.h>
26 #endif
27
28 /* SLP library header */
29
30 /* local header */
31 #include "Message.h"
32 #include "DispatcherHelper.h"
33
34 namespace smartcard_service_api
35 {
36         class IPCHelper
37         {
38         protected:
39                 static const int IPC_SERVER_PORT = 8989;
40                 static const int IPC_SERVER_MAX_CLIENT = 10;
41
42                 int ipcSocket;
43                 unsigned int watchId;
44                 GIOChannel *ioChannel;
45                 pthread_mutex_t ipcLock;
46                 DispatcherHelper *dispatcher;
47 #ifdef CLIENT_IPC_THREAD
48 #ifdef USE_IPC_EPOLL
49                 static const int EPOLL_SIZE = 5;
50                 int fdPoll;
51                 struct epoll_event *pollEvents;
52 #else
53                 fd_set fdSetRead;
54 #endif
55                 pthread_t readThread;
56
57                 static void *threadRead(void *data);
58                 int eventPoll();
59 #endif
60
61                 static gboolean channelCallbackFunc(GIOChannel* channel, GIOCondition condition, gpointer data);
62
63                 virtual int handleIOErrorCondition(void *channel, GIOCondition condition) = 0;
64                 virtual int handleInvalidSocketCondition(void *channel, GIOCondition condition) = 0;
65                 virtual int handleIncomingCondition(void *channel, GIOCondition condition) = 0;
66
67         public:
68                 IPCHelper();
69                 virtual ~IPCHelper();
70
71                 bool createListenSocket();
72                 bool createConnectSocket();
73                 void destroyListenSocket();
74                 void destroyConnectSocket();
75
76                 bool sendMessage(int socket, Message *msg);
77                 bool sendMessage(int socket, ByteArray &buffer);
78                 Message *retrieveMessage();
79                 ByteArray retrieveBuffer(int socket);
80                 Message *retrieveMessage(int socket);
81
82                 void setDispatcher(DispatcherHelper *dispatcher);
83
84 #ifdef CLIENT_IPC_THREAD
85                 friend void *threadRead(void *data);
86 #endif
87                 friend gboolean channelCallbackFunc(GIOChannel* channel, GIOCondition condition, gpointer data);
88         };
89
90 } /* namespace smartcard_service_api */
91 #endif /* IPCHELPER_H_ */