2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 /* standard library header */
20 /* SLP library header */
24 #include "ServiceInstance.h"
25 #include "ClientInstance.h"
26 #include "ServerResource.h"
28 namespace smartcard_service_api
30 unsigned int ServiceInstance::openSession(Terminal *terminal, ByteArray packageCert, void *caller)
32 unsigned int handle = IntegerHandle::assignHandle();
34 ServerSession *session = new ServerSession((ServerReader *)0, packageCert, caller, terminal);
36 mapSessions.insert(make_pair(handle, make_pair(session, terminal)));
41 ServerSession *ServiceInstance::getSession(unsigned int session)
43 ServerSession *result = NULL;
44 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
46 if ((item = mapSessions.find(session)) != mapSessions.end())
48 /*if (item->second.first == session)*/
49 result = item->second.first;
55 Terminal *ServiceInstance::getTerminal(unsigned int session)
57 Terminal *result = NULL;
58 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
60 if ((item = mapSessions.find(session)) != mapSessions.end())
62 result = item->second.second;
68 void ServiceInstance::closeSession(unsigned int session)
70 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
72 if ((item = mapSessions.find(session)) != mapSessions.end())
74 closeChannelsBySession(session);
76 item->second.first->closeSync();
78 mapSessions.erase(item);
80 IntegerHandle::releaseHandle(session);
84 void ServiceInstance::closeSessions()
86 map<unsigned int, pair<ServerSession *, Terminal *> >::iterator item;
90 for (item = mapSessions.begin(); item != mapSessions.end(); item++)
92 item->second.first->closeSync();
94 IntegerHandle::releaseHandle(item->first);
100 unsigned int ServiceInstance::openChannel(unsigned int session, int channelNum)
102 Terminal *terminal = getTerminal(session);
103 ServerChannel *channel = NULL;
104 unsigned int handle = -1;
106 /* create ServerChannel */
107 channel = new ServerChannel((ServerSession *)session, (void *)parent->getPID(), channelNum, terminal);
110 handle = IntegerHandle::assignHandle();
111 mapChannels.insert(make_pair(handle, make_pair(session, channel)));
115 SCARD_DEBUG_ERR("alloc failed");
121 ServerChannel *ServiceInstance::getChannel(/*unsigned int session, */unsigned int channel)
123 ServerChannel *result = NULL;
124 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
126 if ((item = mapChannels.find(channel)) != mapChannels.end())
128 /*if (item->second.first == session)*/
129 result = item->second.second;
135 unsigned int ServiceInstance::getChannelCountBySession(unsigned int session)
137 unsigned int channelCount = 0;
138 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
140 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
142 if (item->second.first == session)
149 void ServiceInstance::closeChannel(unsigned int channel)
151 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
153 if ((item = mapChannels.find(channel)) != mapChannels.end())
155 /* destroy ServerChannel */
156 delete item->second.second;
158 mapChannels.erase(item);
160 IntegerHandle::releaseHandle(channel);
164 void ServiceInstance::closeChannelsBySession(unsigned int session)
167 vector<unsigned int> list;
168 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
170 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
172 if (item->second.first == session)
173 list.push_back(item->second.first);
176 for (i = 0; i < list.size(); i++)
178 closeChannel(list[i]);
182 void ServiceInstance::closeChannels()
184 map<unsigned int, pair<unsigned int, ServerChannel *> >::iterator item;
186 for (item = mapChannels.begin(); item != mapChannels.end(); item++)
188 /* destroy ServerChannel */
189 delete item->second.second;
191 IntegerHandle::releaseHandle(item->first);
197 } /* namespace smartcard_service_api */