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