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