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