Change to use handle instead of context parameter
authorWonkyu Kwon <wonkyu.kwon@samsung.com>
Mon, 17 Jun 2013 05:58:41 +0000 (14:58 +0900)
committerWonkyu Kwon <wonkyu.kwon@samsung.com>
Wed, 24 Jul 2013 08:56:37 +0000 (17:56 +0900)
Change-Id: I529959a2365f494aa72dd9a910956c4ba4a3645b

12 files changed:
client/ClientDispatcher.cpp
client/SEService.cpp
client/include/ClientDispatcher.h
client/include/SEService.h
server/ClientInstance.cpp
server/ServerDispatcher.cpp
server/ServerResource.cpp
server/ServerSEService.cpp
server/ServiceInstance.cpp
server/include/ClientInstance.h
server/include/ServerResource.h
server/include/ServiceInstance.h

index c2267c6..3bfe9a3 100644 (file)
@@ -45,20 +45,20 @@ namespace smartcard_service_api
                return clientDispatcher;
        }
 
-       bool ClientDispatcher::addSEService(void *context, SEService *service)
+       bool ClientDispatcher::addSEService(unsigned int handle, SEService *service)
        {
                bool result = true;
-               map<void *, SEService *>::iterator item;
+               map<unsigned int, SEService *>::iterator item;
 
                _BEGIN();
 
-               if ((item = mapSESerivces.find(context)) == mapSESerivces.end())
+               if ((item = mapSESerivces.find(handle)) == mapSESerivces.end())
                {
-                       mapSESerivces.insert(make_pair(context, service));
+                       mapSESerivces.insert(make_pair(handle, service));
                }
                else
                {
-                       _DBG("SEService [%p] exists", context);
+                       _DBG("SEService [%p] exists", handle);
                }
 
                _END();
@@ -66,13 +66,13 @@ namespace smartcard_service_api
                return result;
        }
 
-       void ClientDispatcher::removeSEService(void *context)
+       void ClientDispatcher::removeSEService(unsigned int handle)
        {
-               map<void *, SEService *>::iterator item;
+               map<unsigned int, SEService *>::iterator item;
 
                _BEGIN();
 
-               if ((item = mapSESerivces.find(context)) != mapSESerivces.end())
+               if ((item = mapSESerivces.find(handle)) != mapSESerivces.end())
                {
                        mapSESerivces.erase(item);
                }
@@ -174,7 +174,7 @@ namespace smartcard_service_api
                case Message::MSG_NOTIFY_SE_INSERTED :
                case Message::MSG_NOTIFY_SE_REMOVED :
                        {
-                               map<void *, SEService *>::iterator item;
+                               map<unsigned int, SEService *>::iterator item;
 
                                for (item = mapSESerivces.begin(); item != mapSESerivces.end(); item++)
                                {
@@ -190,7 +190,7 @@ namespace smartcard_service_api
 
                case Message::MSG_OPERATION_RELEASE_CLIENT :
                        {
-                               map<void *, SEService *>::iterator item;
+                               map<unsigned int, SEService *>::iterator item;
 
                                for (item = mapSESerivces.begin(); item != mapSESerivces.end(); item++)
                                {
index dc98793..8f6d0ac 100644 (file)
 
 namespace smartcard_service_api
 {
-       SEService::SEService() :
-               SEServiceHelper()
+       SEService::SEService() : SEServiceHelper(),
+               handle(-1), context(NULL),
+               handler(NULL), listener(NULL)
        {
-               this->context = NULL;
-               this->handler = NULL;
-               this->listener = NULL;
-               connected = false;
        }
 
        SEService::SEService(void *user_data, serviceConnected handler)
                throw(ErrorIO &, ErrorIllegalParameter &) :
-               SEServiceHelper()
+               SEServiceHelper(), handle(-1),
+               listener(NULL)
        {
-               this->listener = NULL;
-               connected = false;
-
                initialize(user_data, handler);
        }
 
        SEService::SEService(void *user_data, SEServiceListener *listener)
                throw(ErrorIO &, ErrorIllegalParameter &) :
-               SEServiceHelper()
+               SEServiceHelper(), handle(-1),
+               handler(NULL)
        {
-               this->handler = NULL;
-               connected = false;
-
                initialize(user_data, listener);
        }
 
@@ -114,6 +107,7 @@ namespace smartcard_service_api
                        Message msg;
 
                        msg.message = Message::MSG_REQUEST_SHUTDOWN;
+                       msg.param1 = (unsigned long)handle;
                        msg.error = (unsigned long)this; /* using error to context */
                        msg.caller = (void *)this;
                        msg.callback = (void *)NULL;
@@ -141,6 +135,7 @@ namespace smartcard_service_api
                        Message msg;
 
                        msg.message = Message::MSG_REQUEST_SHUTDOWN;
+                       msg.param1 = (unsigned long)handle;
                        msg.error = (unsigned long)this; /* using error to context */
                        msg.caller = (void *)this;
                        msg.callback = (void *)this; /* if callback is class instance, it means synchronized call */
@@ -153,7 +148,7 @@ namespace smartcard_service_api
                                rv = waitTimedCondition(0);
                                if (rv == 0)
                                {
-                                       ClientDispatcher::getInstance().removeSEService(context);
+                                       ClientDispatcher::getInstance().removeSEService(handle);
 
                                        connected = false;
                                }
@@ -206,8 +201,6 @@ namespace smartcard_service_api
                        return result;
                }
 
-               clientDispatcher->addSEService(context, this);
-
                {
                        /* send message to load se */
                        Message msg;
@@ -311,6 +304,9 @@ namespace smartcard_service_api
                        _INFO("[MSG_REQUEST_READERS]");
 
                        service->connected = true;
+                       service->handle = (unsigned int)msg->param2;
+
+                       ClientDispatcher::getInstance().addSEService(service->handle, service);
 
                        /* parse message data */
                        service->parseReaderInformation(msg->param1, msg->data);
@@ -409,7 +405,7 @@ namespace smartcard_service_api
                        {
                                service->listener->errorHandler(service, msg->error, service->context);
 
-                               ClientDispatcher::getInstance().removeSEService(service->context);
+                               ClientDispatcher::getInstance().removeSEService(service->handle);
                                service->connected = false;
                        }
                        else
index 3305b28..3d8fab5 100644 (file)
@@ -34,7 +34,7 @@ namespace smartcard_service_api
        class ClientDispatcher: public DispatcherHelper
        {
        private:
-               map<void *, SEService *> mapSESerivces;
+               map<unsigned int, SEService *> mapSESerivces;
 
                ClientDispatcher();
                ~ClientDispatcher();
@@ -44,8 +44,8 @@ namespace smartcard_service_api
        public:
                static ClientDispatcher &getInstance();
 
-               bool addSEService(void *context, SEService *service);
-               void removeSEService(void *context);
+               bool addSEService(unsigned int handle, SEService *service);
+               void removeSEService(unsigned int handle);
        };
 
 } /* namespace open_mobile_api */
index 0070fe3..e671953 100644 (file)
@@ -35,10 +35,10 @@ namespace smartcard_service_api
 {
        typedef void (*serviceConnected)(SEServiceHelper *service, void *context);
 
-       class SEService: public SEServiceHelper
+       class SEService : public SEServiceHelper
        {
        private:
-               int pid;
+               unsigned int handle;
                void *context;
                serviceConnected handler;
                SEServiceListener *listener;
index f33cbc7..9cd3373 100644 (file)
@@ -32,36 +32,29 @@ namespace smartcard_service_api
                this->pid = pid;
        }
 
-       ServiceInstance *ClientInstance::createService(unsigned int context)
+       ServiceInstance *ClientInstance::createService()
        {
                ServiceInstance *result = NULL;
 
-               if ((result = getService(context)) == NULL)
+               result = new ServiceInstance(this);
+               if (result != NULL)
                {
-                       result = new ServiceInstance(this, context);
-                       if (result != NULL)
-                       {
-                               mapServices.insert(make_pair(context, result));
-                       }
-                       else
-                       {
-                               _ERR("alloc failed");
-                       }
+                       mapServices.insert(make_pair(result->getHandle(), result));
                }
                else
                {
-                       _ERR("service already exist [%d]", context);
+                       _ERR("alloc failed");
                }
 
                return result;
        }
 
-       ServiceInstance *ClientInstance::getService(unsigned int context)
+       ServiceInstance *ClientInstance::getService(unsigned int handle)
        {
                ServiceInstance *result = NULL;
                map<unsigned int, ServiceInstance *>::iterator item;
 
-               if ((item = mapServices.find(context)) != mapServices.end())
+               if ((item = mapServices.find(handle)) != mapServices.end())
                {
                        result = item->second;
                }
@@ -69,11 +62,11 @@ namespace smartcard_service_api
                return result;
        }
 
-       void ClientInstance::removeService(unsigned int context)
+       void ClientInstance::removeService(unsigned int handle)
        {
                map<unsigned int, ServiceInstance *>::iterator item;
 
-               if ((item = mapServices.find(context)) != mapServices.end())
+               if ((item = mapServices.find(handle)) != mapServices.end())
                {
                        delete item->second;
                        mapServices.erase(item);
index 39192f1..54cf372 100644 (file)
@@ -83,6 +83,7 @@ namespace smartcard_service_api
                                Message response(*msg);
                                ByteArray info;
                                ClientInstance *instance = NULL;
+                               ServiceInstance *service = NULL;
 
                                response.param1 = 0;
                                response.param2 = 0;
@@ -103,9 +104,10 @@ namespace smartcard_service_api
                                        }
 
                                        /* create service */
-                                       if (resource->createService(socket, (unsigned int)msg->userParam) != NULL)
+                                       if ((service = resource->createService(socket)) != NULL)
                                        {
                                                response.error = SCARD_ERROR_OK;
+                                               response.param2 = service->getHandle();
 
                                                if ((count = resource->getReadersInformation(info)) > 0)
                                                {
@@ -144,7 +146,7 @@ namespace smartcard_service_api
 
                                response.error = SCARD_ERROR_OK;
 
-                               resource->removeService(socket, msg->error/* service context */);
+                               resource->removeService(socket, msg->param1);
 
                                /* response to client */
                                ServerIPC::getInstance()->sendMessage(socket, &response);
index 4f70519..287bf0d 100644 (file)
@@ -186,19 +186,16 @@ namespace smartcard_service_api
                mapClients.clear();
        }
 
-       ServiceInstance *ServerResource::createService(int socket, unsigned int context)
+       ServiceInstance *ServerResource::createService(int socket)
        {
                ServiceInstance *result = NULL;
                ClientInstance *instance = NULL;
 
                if ((instance = getClient(socket)) != NULL)
                {
-                       if ((result = instance->getService(context)) == NULL)
+                       if ((result = instance->createService()) == NULL)
                        {
-                               if ((result = instance->createService(context)) == NULL)
-                               {
-                                       _ERR("ClientInstance::createService failed [%d] [%d]", socket, context);
-                               }
+                               _ERR("ClientInstance::createService failed [%d]", socket);
                        }
                }
                else
@@ -209,14 +206,14 @@ namespace smartcard_service_api
                return result;
        }
 
-       ServiceInstance *ServerResource::getService(int socket, unsigned int context)
+       ServiceInstance *ServerResource::getService(int socket, unsigned int handle)
        {
                ServiceInstance *result = NULL;
                ClientInstance *instance = NULL;
 
                if ((instance = getClient(socket)) != NULL)
                {
-                       result = instance->getService(context);
+                       result = instance->getService(handle);
                }
                else
                {
@@ -226,13 +223,13 @@ namespace smartcard_service_api
                return result;
        }
 
-       void ServerResource::removeService(int socket, unsigned int context)
+       void ServerResource::removeService(int socket, unsigned int handle)
        {
                ClientInstance *instance = NULL;
 
                if ((instance = getClient(socket)) != NULL)
                {
-                       instance->removeService(context);
+                       instance->removeService(handle);
                }
                else
                {
@@ -322,13 +319,13 @@ namespace smartcard_service_api
                return result;
        }
 
-       unsigned int ServerResource::createSession(int socket, unsigned int context, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
+       unsigned int ServerResource::createSession(int socket, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
        {
                unsigned int result = -1;
                Terminal *temp = NULL;
                ServiceInstance *instance = NULL;
 
-               if ((instance = getService(socket, context)) != NULL)
+               if ((instance = getService(socket, handle)) != NULL)
                {
                        if ((temp = getTerminalByReaderID(readerID)) != NULL)
                        {
@@ -337,57 +334,57 @@ namespace smartcard_service_api
                }
                else
                {
-                       _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+                       _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
                }
 
                return result;
        }
 
-       ServerSession *ServerResource::getSession(int socket, unsigned int context, unsigned int sessionID)
+       ServerSession *ServerResource::getSession(int socket, unsigned int handle, unsigned int sessionID)
        {
                ServerSession *result = NULL;
                ServiceInstance *instance = NULL;
 
-               if ((instance = getService(socket, context)) != NULL)
+               if ((instance = getService(socket, handle)) != NULL)
                {
                        result = instance->getSession(sessionID);
                }
                else
                {
-                       _ERR("Session doesn't exist : socket [%d], context [%d], handle [%d]", socket, context, sessionID);
+                       _ERR("Session doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, sessionID);
                }
 
                return result;
        }
 
-       unsigned int ServerResource::getChannelCount(int socket, unsigned int context, unsigned int sessionID)
+       unsigned int ServerResource::getChannelCount(int socket, unsigned int handle, unsigned int sessionID)
        {
                unsigned int result = -1;
                ServiceInstance *instance = NULL;
 
-               if ((instance = getService(socket, context)) != NULL)
+               if ((instance = getService(socket, handle)) != NULL)
                {
                        result = instance->getChannelCountBySession(sessionID);
                }
                else
                {
-                       _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+                       _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
                }
 
                return result;
        }
 
-       void ServerResource::removeSession(int socket, unsigned int context, unsigned int sessionID)
+       void ServerResource::removeSession(int socket, unsigned int handle, unsigned int sessionID)
        {
                ServiceInstance *instance = NULL;
 
-               if ((instance = getService(socket, context)) != NULL)
+               if ((instance = getService(socket, handle)) != NULL)
                {
                        instance->closeSession(sessionID);
                }
                else
                {
-                       _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+                       _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
                }
        }
 
@@ -594,13 +591,13 @@ namespace smartcard_service_api
                return result;
        }
 
-       unsigned int ServerResource::createChannel(int socket, unsigned int context, unsigned int sessionID, int channelType, ByteArray aid)
+       unsigned int ServerResource::createChannel(int socket, unsigned int handle, unsigned int sessionID, int channelType, ByteArray aid)
                throw(ExceptionBase &)
        {
                unsigned int result = -1;
                ServiceInstance *service = NULL;
 
-               if ((service = getService(socket, context)) != NULL)
+               if ((service = getService(socket, handle)) != NULL)
                {
                        if (service->isVaildSessionHandle(sessionID) == true)
                        {
@@ -631,41 +628,41 @@ namespace smartcard_service_api
                }
                else
                {
-                       _ERR("getService is failed [%d] [%d]", socket, context);
+                       _ERR("getService is failed [%d] [%d]", socket, handle);
                        throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
                }
 
                return result;
        }
 
-       Channel *ServerResource::getChannel(int socket, unsigned int context, unsigned int channelID)
+       Channel *ServerResource::getChannel(int socket, unsigned int handle, unsigned int channelID)
        {
                Channel *result = NULL;
                ServiceInstance *instance = NULL;
 
-               if ((instance = getService(socket, context)) != NULL)
+               if ((instance = getService(socket, handle)) != NULL)
                {
                        result = instance->getChannel(channelID);
                }
                else
                {
-                       _ERR("Channel doesn't exist : socket [%d], context [%d], handle [%d]", socket, context, channelID);
+                       _ERR("Channel doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, channelID);
                }
 
                return result;
        }
 
-       void ServerResource::removeChannel(int socket, unsigned int context, unsigned int channelID)
+       void ServerResource::removeChannel(int socket, unsigned int handle, unsigned int channelID)
        {
                ServiceInstance *instance = NULL;
 
-               if ((instance = getService(socket, context)) != NULL)
+               if ((instance = getService(socket, handle)) != NULL)
                {
                        instance->closeChannel(channelID);
                }
                else
                {
-                       _ERR("getService doesn't exist : socket [%d], context [%d]", socket, context);
+                       _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
                }
        }
 
@@ -870,11 +867,11 @@ namespace smartcard_service_api
                return (getTerminalByReaderID(reader) != NULL);
        }
 
-       bool ServerResource::isValidSessionHandle(int socket, unsigned int context, unsigned int session)
+       bool ServerResource::isValidSessionHandle(int socket, unsigned int handle, unsigned int session)
        {
                ServiceInstance *instance = NULL;
 
-               return (((instance = getService(socket, context)) != NULL) && (instance->isVaildSessionHandle(session)));
+               return (((instance = getService(socket, handle)) != NULL) && (instance->isVaildSessionHandle(session)));
        }
 
        int ServerResource::getReadersInformation(ByteArray &info)
index cb2465a..789fa16 100644 (file)
@@ -194,12 +194,14 @@ namespace smartcard_service_api
                Message *msg = (Message *)message;
                Message response(*msg);
                ServerResource &resource = ServerResource::getInstance();
+               ServiceInstance *service;
 
-               if (resource.createService(socket, msg->error) != NULL)
+               if ((service = resource.createService(socket)) != NULL)
                {
                        _ERR("client added : pid [%d]", msg->error);
 
                        response.error = SCARD_ERROR_OK;
+                       response.param2 = service->getHandle();
 
                        if ((count = resource.getReadersInformation(info)) > 0)
                        {
index a324dd5..aaff33f 100644 (file)
 
 namespace smartcard_service_api
 {
+       ServiceInstance::ServiceInstance(ClientInstance *parent) :
+               parent(parent)
+       {
+               handle = IntegerHandle::assignHandle();
+       }
+
+       ServiceInstance::~ServiceInstance()
+       {
+               closeSessions();
+
+               if (handle != IntegerHandle::INVALID_HANDLE) {
+                       IntegerHandle::releaseHandle(handle);
+                       handle = IntegerHandle::INVALID_HANDLE;
+               }
+       }
+
        unsigned int ServiceInstance::openSession(Terminal *terminal, vector<ByteArray> &certHashes, void *caller)
        {
                unsigned int handle = IntegerHandle::assignHandle();
index e4c883d..67de51e 100644 (file)
@@ -64,7 +64,7 @@ namespace smartcard_service_api
                void setPID(int pid);
                inline int getPID() { return pid; }
 
-               ServiceInstance *createService(unsigned int context);
+               ServiceInstance *createService();
                ServiceInstance *getService(unsigned int context);
                void removeService(unsigned int context);
                void removeServices();
index 337a9f0..b04ae4c 100644 (file)
@@ -121,22 +121,22 @@ namespace smartcard_service_api
                void removeClient(int socket);
                void removeClients();
 
-               ServiceInstance *createService(int socket, unsigned int context);
-               ServiceInstance *getService(int socket, unsigned int context);
-               void removeService(int socket, unsigned int context);
+               ServiceInstance *createService(int socket);
+               ServiceInstance *getService(int socket, unsigned int handle);
+               void removeService(int socket, unsigned int handle);
                void removeServices(int socket);
 
-               unsigned int createSession(int socket, unsigned int context, unsigned int readerID, vector<ByteArray> &certHashes, void *caller);
-               ServerSession *getSession(int socket, unsigned int context, unsigned int sessionID);
-               unsigned int getChannelCount(int socket, unsigned int context, unsigned int sessionID);
-               void removeSession(int socket, unsigned int context, unsigned int session);
-               bool isValidSessionHandle(int socket, unsigned int context, unsigned int sessionID);
+               unsigned int createSession(int socket, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller);
+               ServerSession *getSession(int socket, unsigned int handle, unsigned int sessionID);
+               unsigned int getChannelCount(int socket, unsigned int handle, unsigned int sessionID);
+               void removeSession(int socket, unsigned int handle, unsigned int session);
+               bool isValidSessionHandle(int socket, unsigned int handle, unsigned int sessionID);
 
-               unsigned int createChannel(int socket, unsigned int context,
+               unsigned int createChannel(int socket, unsigned int handle,
                        unsigned int sessionID, int channelType, ByteArray aid)
                        throw(ExceptionBase &);
-               Channel *getChannel(int socket, unsigned int context, unsigned int channelID);
-               void removeChannel(int socket, unsigned int context, unsigned int channelID);
+               Channel *getChannel(int socket, unsigned int handle, unsigned int channelID);
+               void removeChannel(int socket, unsigned int handle, unsigned int channelID);
 
                void addAccessControlList(Terminal *terminal, AccessControlList *acl);
                void addAccessControlList(ServerChannel *channel, AccessControlList *acl);
index 51f7a77..f31ec4a 100644 (file)
@@ -34,20 +34,18 @@ namespace smartcard_service_api
        class ServiceInstance
        {
        private:
-               unsigned int context;
+               unsigned int handle;
                ClientInstance *parent;
                map<unsigned int, pair<ServerSession *, Terminal *> > mapSessions; /* session unique id <-> terminal instance map */
                map<unsigned int, pair<unsigned int, ServerChannel *> > mapChannels; /* channel unique id <-> (session unique id, channel instance) map */
 
        public:
-               ServiceInstance(ClientInstance *parent, unsigned int context)
-               {
-                       this->parent = parent;
-                       this->context = context;
-               }
-               ~ServiceInstance() { closeSessions(); };
+               ServiceInstance(ClientInstance *parent);
+               ~ServiceInstance();
 
-               inline bool operator ==(const unsigned int &context) const { return (this->context == context); }
+               inline unsigned int getHandle() { return handle; }
+
+               inline bool operator ==(const unsigned int &handle) const { return (this->handle == handle); }
                inline bool isVaildSessionHandle(unsigned int handle) { return (mapSessions.find(handle) != mapSessions.end()); }
                inline bool isVaildChannelHandle(unsigned int handle) { return (mapChannels.find(handle) != mapChannels.end()); }
                inline ClientInstance *getParent() { return parent; }
@@ -62,6 +60,7 @@ namespace smartcard_service_api
                unsigned int openChannel(unsigned int session, int channelNum, ByteArray response = ByteArray::EMPTY);
                ServerChannel *getChannel(/*unsigned int session, */unsigned int channel);
                unsigned int getChannelCountBySession(unsigned int session);
+
                void closeChannel(unsigned int channel);
                void closeChannelsBySession(unsigned int session);
                void closeChannels();