Change to use handle instead of context parameter
[platform/core/connectivity/smartcard-service.git] / server / ClientInstance.cpp
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 /* standard library header */
18 #include <pthread.h>
19
20 /* SLP library header */
21
22 /* local header */
23 #include "Debug.h"
24 #include "ClientInstance.h"
25 #include "ServerResource.h"
26 #include "SignatureHelper.h"
27
28 namespace smartcard_service_api
29 {
30         void ClientInstance::setPID(int pid)
31         {
32                 this->pid = pid;
33         }
34
35         ServiceInstance *ClientInstance::createService()
36         {
37                 ServiceInstance *result = NULL;
38
39                 result = new ServiceInstance(this);
40                 if (result != NULL)
41                 {
42                         mapServices.insert(make_pair(result->getHandle(), result));
43                 }
44                 else
45                 {
46                         _ERR("alloc failed");
47                 }
48
49                 return result;
50         }
51
52         ServiceInstance *ClientInstance::getService(unsigned int handle)
53         {
54                 ServiceInstance *result = NULL;
55                 map<unsigned int, ServiceInstance *>::iterator item;
56
57                 if ((item = mapServices.find(handle)) != mapServices.end())
58                 {
59                         result = item->second;
60                 }
61
62                 return result;
63         }
64
65         void ClientInstance::removeService(unsigned int handle)
66         {
67                 map<unsigned int, ServiceInstance *>::iterator item;
68
69                 if ((item = mapServices.find(handle)) != mapServices.end())
70                 {
71                         delete item->second;
72                         mapServices.erase(item);
73                 }
74         }
75
76         void ClientInstance::removeServices()
77         {
78                 map<unsigned int, ServiceInstance *>::iterator item;
79
80                 for (item = mapServices.begin(); item != mapServices.end(); item++)
81                 {
82                         delete item->second;
83                 }
84
85                 mapServices.clear();
86         }
87
88         bool ClientInstance::sendMessageToAllServices(int socket, Message &msg)
89         {
90                 bool result = true;
91                 map<unsigned int, ServiceInstance *>::iterator item;
92
93                 for (item = mapServices.begin(); item != mapServices.end(); item++)
94                 {
95                         if (ServerIPC::getInstance()->sendMessage(socket, &msg) == false)
96                                 result = false;
97                 }
98
99                 return result;
100         }
101
102         void ClientInstance::generateCertificationHashes()
103         {
104                 SignatureHelper::getCertificationHashes(getPID(), certHashes);
105         }
106 } /* namespace smartcard_service_api */