Get appid from pid in ipc
[platform/framework/native/appfw.git] / src / server / inc / FIo_IpcServer.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FIo_IpcServer.h
20  * @brief       This is the header file for the _IpcServer class.
21  *
22  * This file contains the declarations of _IpcServer.
23  */
24
25 #ifndef _FIO_INTERNAL_IPC_SERVER_H_
26 #define _FIO_INTERNAL_IPC_SERVER_H_
27
28 #include <string>
29 #include <pthread.h>
30 #include <map>
31 #include <glib.h>
32
33 #include <ipc/ipc_message_macros.h>
34 #include <ipc/ipc_message_utils.h>
35
36 #include <FBaseResult.h>
37 #include <FBaseObject.h>
38 #include <FBaseString.h>
39 #include <FAppTypes.h>
40
41 namespace Tizen { namespace Base { namespace Runtime
42 {
43 class _EventDispatcher;
44 }}}
45
46 namespace Tizen { namespace Io
47 {
48
49 class _IIpcServerEventListener;
50
51 /**
52  * @class _IpcServer
53  * @brief This class provides methods to handle IPC request messages.
54  *
55  */
56 class _OSP_EXPORT_ _IpcServer
57         : public Tizen::Base::Object
58 {
59 public:
60         _IpcServer(void);
61
62         virtual ~_IpcServer(void);
63
64         /**
65          * Constructs the instance of this class and starts the IPC server.
66          *
67          * @return An error code
68          * @param[in] name                      The name of IPC server
69          * @param[in] listener          The listener for IPC server
70          * @param[in] runOnCallerThread Set to @c true, if the server runs on the caller thread
71          *                                      @c false, if the server runs on its own thread.
72          * @exception E_SUCCESS         The method was successful.
73          * @exception E_OUT_OF_MEMORY   Insufficient memory.
74          * @exception E_SYSTEM          Occurs when runOnCallerThread is set to true where the caller thread is worker thread.
75          */
76         result Construct(const Tizen::Base::String& name, const _IIpcServerEventListener& listener, bool runOnCallerThread = true);
77
78         /**
79          * Returns the name of the IPC server.
80          *
81          * @return The name of the IPC server.
82          */
83         Tizen::Base::String GetName(void) const;
84
85         /**
86          * Returns the id the of the client which sent a request message.
87          *
88          * @return The id of the IPC client.
89          * @remark This can be called only in a message handler.
90          */
91         int GetClientId(void) const;
92
93         int GetClientProcessId(void) const;
94
95         Tizen::Base::String GetClientAppId(void) const;
96
97         Tizen::Base::String GetClientAppExecutableName(void) const;
98
99         /**
100          * Returns the package id of the client which sent a request message.
101          *
102          * @return The package id of the IPC client.
103          * @remark This can be called only in a message handler.
104          */
105         Tizen::App::PackageId GetClientPackageId(void) const;
106
107         /**
108          * Returns the application id of the client which sent a request message.
109          *
110          * @return The application id of the IPC client.
111          * @remark This can be called only in a message handler.
112          */
113         Tizen::App::AppId GetClientApplicationId(void) const;
114
115         /**
116          * Stops the IPC server.
117          *
118          * @return An error code
119          * @exception E_SUCCESS         The method was successful.
120          * @exception E_INVALID_STATE   The IPC server has not been started.
121          */
122         result Stop(void);
123
124         /**
125          * Sends a message to an IPC client.
126          *
127          * @return An error code
128          * @param[in] clientId      The id of the IPC client
129          * @param[in] message   The message to send
130          * @exception E_SUCCESS The method was successful.
131          * @exception E_INVALID_ARG             The message is synchronous.
132          * @exception E_INVALID_OPERATION       The client didn't set a listener.
133          * @exception E_OUT_OF_MEMORY   Insufficient memory.
134          * @exception E_SYSTEM          A system error occurred.
135          *
136          * @remark Only an asychronous message can be sent to an IPC client.
137          */
138         result SendResponse(int clientId, const IPC::Message& message);
139
140         result Start(void);
141
142         result SendResponse(int clientId, IPC::Message* pMessage);
143
144         bool Send(IPC::Message* msg);
145
146 private:
147         _IpcServer(const _IpcServer& value);
148
149         _IpcServer& operator =(const _IpcServer& value);
150
151         static void* ThreadProc(void* pParam);
152
153         void Run(void* pParam);
154
155         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
156
157         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
158
159         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
160
161         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
162
163         struct  _ClientInfo;
164
165         /**
166          *      @struct __ChannelInfo
167          *      @brief  This struct represent a channel.
168          */
169         struct  _ChannelInfo
170         {
171                 _ChannelInfo(void);
172                 ~_ChannelInfo(void);
173
174                 struct _ClientInfo* pClientInfo;
175                 GIOChannel* pGIOChannel;
176                 GSource* pGSource;
177                 bool destroySource;
178         };
179
180         /**
181          *      @struct __ClientInfo
182          *      @brief  This struct represent a client connected to this server.
183          */
184         struct  _ClientInfo
185         {
186                 _ClientInfo(void);
187                 ~_ClientInfo(void);
188
189                 int clientId;                              /**< the client id */
190                 _IpcServer* pIpcServer;                    /**< the pointer to an _ IpcServer */
191                 GIOChannel* pReverseChannel;               /**< the channel for sending reverse message */
192                 std::vector <struct _ChannelInfo*> channels;   /**< the set of channels associated with a client */
193                 Tizen::Base::String appId;
194         };
195
196         Tizen::Base::String __name;
197         bool __runOnCallerThread;
198         Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
199         _IIpcServerEventListener* __pListener;
200
201         pthread_t __handlerThread;
202         GMainContext* __pHandlerGMainContext;
203         GMainLoop* __pHandlerGMainLoop;
204
205         // handling connection
206         GSource* __pConnectGSource;
207
208         // handling received message
209         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
210         std::string __pending;
211
212         // current message handling context
213         GIOChannel* __pCurrentChannel;
214         _ClientInfo* __pCurrentClientInfo;
215
216         std::map <int, _ClientInfo*> __clients;   // pid of client is used for key
217 }; // _IpcServer
218
219 } } // Tizen::Io
220
221 #endif // _FIO_INTERNAL_IPC_SERVER_H_