Change log macro
[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         gboolean ClientInstance::_getCertificationHashes(gpointer user_data)
31         {
32                 gboolean result = false;
33                 ClientInstance *instance = (ClientInstance *)user_data;
34
35                 SignatureHelper::getCertificationHashes(instance->getPID(), instance->certHashes);
36
37                 return result;
38         }
39
40         void ClientInstance::setPID(int pid)
41         {
42                 this->pid = pid;
43
44         }
45
46         ServiceInstance *ClientInstance::createService(unsigned int context)
47         {
48                 ServiceInstance *result = NULL;
49
50                 if ((result = getService(context)) == NULL)
51                 {
52                         result = new ServiceInstance(this, context);
53                         if (result != NULL)
54                         {
55                                 mapServices.insert(make_pair(context, result));
56                         }
57                         else
58                         {
59                                 _ERR("alloc failed");
60                         }
61                 }
62                 else
63                 {
64                         _ERR("service already exist [%d]", context);
65                 }
66
67                 return result;
68         }
69
70         ServiceInstance *ClientInstance::getService(unsigned int context)
71         {
72                 ServiceInstance *result = NULL;
73                 map<unsigned int, ServiceInstance *>::iterator item;
74
75                 if ((item = mapServices.find(context)) != mapServices.end())
76                 {
77                         result = item->second;
78                 }
79
80                 return result;
81         }
82
83         void ClientInstance::removeService(unsigned int context)
84         {
85                 map<unsigned int, ServiceInstance *>::iterator item;
86
87                 if ((item = mapServices.find(context)) != mapServices.end())
88                 {
89                         delete item->second;
90                         mapServices.erase(item);
91                 }
92         }
93
94         void ClientInstance::removeServices()
95         {
96                 map<unsigned int, ServiceInstance *>::iterator item;
97
98                 for (item = mapServices.begin(); item != mapServices.end(); item++)
99                 {
100                         delete item->second;
101                 }
102
103                 mapServices.clear();
104         }
105
106         bool ClientInstance::sendMessageToAllServices(int socket, Message &msg)
107         {
108                 bool result = true;
109                 map<unsigned int, ServiceInstance *>::iterator item;
110
111                 for (item = mapServices.begin(); item != mapServices.end(); item++)
112                 {
113                         if (ServerIPC::getInstance()->sendMessage(socket, &msg) == false)
114                                 result = false;
115                 }
116
117                 return result;
118         }
119
120         void ClientInstance::generateCertificationHashes()
121         {
122                 g_idle_add(_getCertificationHashes, (gpointer)this);
123         }
124 } /* namespace smartcard_service_api */