update the latest source
[platform/core/connectivity/smartcard-service.git] / server / ServiceInstance.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
20 /* SLP library header */
21
22 /* local header */
23 #include "Debug.h"
24 #include "ServiceInstance.h"
25 #include "ClientInstance.h"
26 #include "ServerResource.h"
27
28 namespace smartcard_service_api
29 {
30         unsigned int ServiceInstance::openSession(Terminal *terminal, ByteArray packageCert, void *caller)
31         {
32                 unsigned int handle = IntegerHandle::assignHandle();
33
34                 ServerSession *session = new ServerSession((ServerReader *)0, packageCert, caller, terminal);
35
36                 mapSessions.insert(make_pair(handle, make_pair(session, terminal)));
37
38                 return handle;
39         }
40
41         ServerSession *ServiceInstance::getSession(unsigned int session)
42         {
43                 ServerSession *result = NULL;
44                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
45
46                 if ((item = mapSessions.find(session)) != mapSessions.end())
47                 {
48                         /*if (item->second.first == session)*/
49                                 result = item->second.first;
50                 }
51
52                 return result;
53         }
54
55         Terminal *ServiceInstance::getTerminal(unsigned int session)
56         {
57                 Terminal *result = NULL;
58                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
59
60                 if ((item = mapSessions.find(session)) != mapSessions.end())
61                 {
62                         result = item->second.second;
63                 }
64
65                 return result;
66         }
67
68         void ServiceInstance::closeSession(unsigned int session)
69         {
70                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
71
72                 if ((item = mapSessions.find(session)) != mapSessions.end())
73                 {
74                         closeChannelsBySession(session);
75
76                         item->second.first->closeSync();
77
78                         mapSessions.erase(item);
79
80                         IntegerHandle::releaseHandle(session);
81                 }
82         }
83
84         void ServiceInstance::closeSessions()
85         {
86                 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
87
88                 closeChannels();
89
90                 for (item = mapSessions.begin(); item != mapSessions.end(); item++)
91                 {
92                         item->second.first->closeSync();
93
94                         IntegerHandle::releaseHandle(item->first);
95                 }
96
97                 mapSessions.clear();
98         }
99
100         unsigned int ServiceInstance::openChannel(unsigned int session, int channelNum)
101         {
102                 Terminal *terminal = getTerminal(session);
103                 ServerChannel *channel = NULL;
104                 unsigned int handle = -1;
105
106                 /* create ServerChannel */
107                 channel = new ServerChannel((ServerSession *)session, (void *)parent->getPID(), channelNum, terminal);
108                 if (channel != NULL)
109                 {
110                         handle = IntegerHandle::assignHandle();
111                         mapChannels.insert(make_pair(handle, make_pair(session, channel)));
112                 }
113                 else
114                 {
115                         SCARD_DEBUG_ERR("alloc failed");
116                 }
117
118                 return handle;
119         }
120
121         ServerChannel *ServiceInstance::getChannel(/*unsigned int session, */unsigned int channel)
122         {
123                 ServerChannel *result = NULL;
124                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
125
126                 if ((item = mapChannels.find(channel)) != mapChannels.end())
127                 {
128                         /*if (item->second.first == session)*/
129                                 result = item->second.second;
130                 }
131
132                 return result;
133         }
134
135         unsigned int ServiceInstance::getChannelCountBySession(unsigned int session)
136         {
137                 unsigned int channelCount = 0;
138                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
139
140                 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
141                 {
142                         if (item->second.first == session)
143                                 channelCount++;
144                 }
145
146                 return channelCount;
147         }
148
149         void ServiceInstance::closeChannel(unsigned int channel)
150         {
151                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
152
153                 if ((item = mapChannels.find(channel)) != mapChannels.end())
154                 {
155                         /* destroy ServerChannel */
156                         delete item->second.second;
157
158                         mapChannels.erase(item);
159
160                         IntegerHandle::releaseHandle(channel);
161                 }
162         }
163
164         void ServiceInstance::closeChannelsBySession(unsigned int session)
165         {
166                 size_t i;
167                 vector<unsigned int> list;
168                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
169
170                 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
171                 {
172                         if (item->second.first == session)
173                                 list.push_back(item->second.first);
174                 }
175
176                 for (i = 0; i < list.size(); i++)
177                 {
178                         closeChannel(list[i]);
179                 }
180         }
181
182         void ServiceInstance::closeChannels()
183         {
184                 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
185
186                 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
187                 {
188                         /* destroy ServerChannel */
189                         delete item->second.second;
190
191                         IntegerHandle::releaseHandle(item->first);
192                 }
193
194                 mapChannels.clear();
195         }
196
197 } /* namespace smartcard_service_api */