update the latest source
[platform/core/connectivity/smartcard-service.git] / client / ClientIPC.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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 /* standard library header */
19 #include <sys/socket.h>
20 #include <unistd.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "ClientIPC.h"
27 #include "DispatcherMsg.h"
28
29 namespace smartcard_service_api
30 {
31         ClientIPC::ClientIPC():IPCHelper()
32         {
33         }
34
35         ClientIPC::~ClientIPC()
36         {
37         }
38
39         ClientIPC &ClientIPC::getInstance()
40         {
41                 static ClientIPC clientIPC;
42
43                 return clientIPC;
44         }
45
46         int ClientIPC::handleIOErrorCondition(void *channel, GIOCondition condition)
47         {
48                 SCARD_BEGIN();
49
50                 /* finalize context */
51                 if (watchId != 0)
52                 {
53                         g_source_remove(watchId);
54                         watchId = 0;
55                 }
56
57                 if (ioChannel != NULL)
58                 {
59                         g_io_channel_unref(ioChannel);
60                         ioChannel = NULL;
61                 }
62
63                 if (ipcSocket != -1)
64                 {
65                         shutdown(ipcSocket, SHUT_RDWR);
66                         close(ipcSocket);
67
68                         ipcSocket = -1;
69                 }
70
71                 /* push disconnect message */
72                 DispatcherMsg *dispMsg = new DispatcherMsg();
73
74                 dispMsg->message = Message::MSG_OPERATION_RELEASE_CLIENT;
75                 dispMsg->error = -1;
76
77                 if (dispatcher != NULL)
78                         dispatcher->pushMessage(dispMsg);
79
80                 SCARD_END();
81
82                 return FALSE;
83         }
84
85         int ClientIPC::handleInvalidSocketCondition(void *channel, GIOCondition condition)
86         {
87                 SCARD_BEGIN();
88
89                 /* finalize context */
90
91                 SCARD_END();
92
93                 return FALSE;
94         }
95
96         int ClientIPC::handleIncomingCondition(void *channel, GIOCondition condition)
97         {
98                 int result = FALSE;
99
100                 SCARD_BEGIN();
101
102                 if (channel == ioChannel)
103                 {
104                         Message *msg = NULL;
105
106                         SCARD_DEBUG("message from server to client socket");
107
108                         /* read message */
109                         msg = retrieveMessage();
110                         if (msg != NULL)
111                         {
112                                 DispatcherMsg *dispMsg = new DispatcherMsg(msg);
113
114                                 /* set peer socket */
115                                 dispMsg->setPeerSocket(ipcSocket);
116
117                                 /* push to dispatcher */
118                                 if (dispatcher != NULL)
119                                         dispatcher->pushMessage(dispMsg);
120
121                                 result = TRUE;
122                         }
123                         else
124                         {
125                                 /* clear client connection */
126                         }
127
128                         delete msg;
129                 }
130                 else
131                 {
132                         SCARD_DEBUG_ERR("Unknown channel event [%p]", channel);
133                 }
134
135                 SCARD_END();
136
137                 return result;
138         }
139
140 } /* namespace open_mobile_api */