Implement service communication
[platform/core/security/key-manager.git] / src / manager / client-async / connection-thread.h
1 /*
2  *  Copyright (c) 2000 - 2014 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  * @file       connection-thread.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <thread>
25 #include <mutex>
26 #include <string>
27 #include <dpl/exception.h>
28 #include <noncopyable.h>
29 #include <client-common.h>
30 #include <async-request.h>
31 #include <descriptor-set.h>
32 #include <service.h>
33
34 namespace CKM {
35
36 class ConnectionThread
37 {
38 public:
39     DECLARE_EXCEPTION_TYPE(CKM::Exception, PipeError)
40
41     ConnectionThread();
42     virtual ~ConnectionThread();
43
44     NONCOPYABLE(ConnectionThread);
45
46     void run();
47
48     void sendMessage(AsyncRequest&& request);
49
50     bool finished() const { return m_finished; }
51
52 private:
53     void threadLoop();
54
55     void newRequest(int pipe, short revents);
56
57     // reads notification pipe
58     void readPipe(int pipe, short revents);
59
60     Service& getService(const std::string& interface);
61
62     // Helper class that creates a pipe before thread is started
63     class Pipe {
64     public:
65         Pipe();
66         ~Pipe();
67
68         NONCOPYABLE(Pipe);
69
70         void notify();
71         int output() const { return m_pipe[0]; }
72
73     private:
74         int m_pipe[2];
75     };
76     // shared vars
77     Pipe m_pipe;
78     AsyncRequest::Queue m_waitingReqs;
79     std::mutex m_mutex;
80     bool m_join;
81     bool m_finished;
82
83     // parent thread vars
84     std::thread m_thread;
85
86     // child thread vars
87     std::map<std::string, Service> m_services;
88     DescriptorSet m_descriptors;
89 };
90
91 } /* namespace CKM */