sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / 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  * @since 2.1
55  *
56  */
57 class _OSP_EXPORT_ _IpcServer
58         : public Tizen::Base::Object
59 {
60 public:
61         /**
62          * This is the default constructor for this class.
63          * @since 2.1
64          */
65         _IpcServer(void);
66
67         /**
68          * This is the destructor for this class.
69          * @since 2.1
70          */
71         virtual ~_IpcServer(void);
72
73         /**
74          * Constructs the instance of this class and starts the IPC server.
75          *
76          * @since 2.1
77          * @return An error code
78          * @param[in] name                      The name of IPC server
79          * @param[in] listener          The listener for IPC server
80          * @param[in] runOnCallerThread Set to @c true, if the server runs on the caller thread
81          *                                      @c false, if the server runs on its own thread.
82          * @exception E_SUCCESS         The method was successful.
83          * @exception E_OUT_OF_MEMORY   Insufficient memory.
84          * @exception E_SYSTEM          Occurs when runOnCallerThread is set to true where the caller thread is worker thread.
85          */
86         result Construct(const Tizen::Base::String& name, const _IIpcServerEventListener& listener, bool runOnCallerThread = true);
87
88         /**
89          * Returns the name of the IPC server.
90          *
91          * @since 2.1
92          * @return The name of the IPC server.
93          */
94         Tizen::Base::String GetName(void) const;
95
96         /**
97          * Returns the id the of the client which sent a request message.
98          *
99          * @since 2.1
100          * @return The id of the IPC client.
101          * @remark This can be called only in a message handler.
102          */
103         int GetClientId(void) const;
104
105         /**
106          * Returns the process id of the client which sent a request message.
107          *
108          * @since 2.1
109          * @return The process id of the IPC client.
110          * @remark This can be called only in a message handler.
111          */
112         int GetClientProcessId(void) const;
113
114         /**
115          * Returns the app id of the client which sent a request message.
116          *
117          * @since 2.1
118          * @return The app id of the IPC client.
119          * @remark This can be called only in a message handler.
120          */
121         Tizen::App::AppId GetClientAppId(void) const;
122
123         /**
124          * Returns the executable name of the client which sent a request message.
125          *
126          * @since 2.1
127          * @return The executable name of the IPC client.
128          * @remark This can be called only in a message handler.
129          */
130         Tizen::Base::String GetClientAppExecutableName(void) const;
131
132         /**
133          * Stops the IPC server.
134          *
135          * @since 2.1
136          * @return An error code
137          * @exception E_SUCCESS         The method was successful.
138          * @exception E_INVALID_STATE   The IPC server has not been started.
139          */
140         result Stop(void);
141
142         /**
143          * Sends a message to an IPC client.
144          *
145          * @since 2.1
146          * @return An error code
147          * @param[in] clientId      The id of the IPC client
148          * @param[in] message   The message to send
149          * @exception E_SUCCESS The method was successful.
150          * @exception E_INVALID_ARG             The message is synchronous.
151          * @exception E_INVALID_OPERATION       The client didn't set a listener.
152          * @exception E_OUT_OF_MEMORY   Insufficient memory.
153          * @exception E_SYSTEM          A system error occurred.
154          *
155          * @remark Only an asychronous message can be sent to an IPC client.
156          */
157         result SendResponse(int clientId, const IPC::Message& message);
158
159         result Start(void);
160
161         result SendResponse(int clientId, IPC::Message* pMessage);
162
163         bool Send(IPC::Message* msg);
164
165 private:
166         _IpcServer(const _IpcServer& value);
167
168         _IpcServer& operator =(const _IpcServer& value);
169
170         static void* ThreadProc(void* pParam);
171
172         void Run(void* pParam);
173
174         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
175
176         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
177
178         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
179
180         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
181
182         struct  _ClientInfo;
183
184         /**
185          *      @struct __ChannelInfo
186          *      @brief  This struct represent a channel.
187          *      @since 2.1
188          */
189         struct  _ChannelInfo
190         {
191                 _ChannelInfo(void);
192                 ~_ChannelInfo(void);
193
194                 struct _ClientInfo* pClientInfo;
195                 GIOChannel* pGIOChannel;
196                 GSource* pGSource;
197                 bool destroySource;
198         };
199
200         /**
201          *      @struct __ClientInfo
202          *      @brief  This struct represent a client connected to this server.
203          *      @since 2.1
204          */
205         struct  _ClientInfo
206         {
207                 _ClientInfo(void);
208                 ~_ClientInfo(void);
209
210                 int clientId;                              /**< the client id */
211                 _IpcServer* pIpcServer;                    /**< the pointer to an _ IpcServer */
212                 GIOChannel* pReverseChannel;               /**< the channel for sending reverse message */
213                 std::vector <struct _ChannelInfo*> channels;   /**< the set of channels associated with a client */
214                 Tizen::Base::String appId;
215                 Tizen::Base::String appExecutableName;
216         };
217
218         Tizen::Base::String __name;
219         bool __runOnCallerThread;
220         Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
221         _IIpcServerEventListener* __pListener;
222
223         pthread_t __handlerThread;
224         GMainContext* __pHandlerGMainContext;
225         GMainLoop* __pHandlerGMainLoop;
226
227         // handling connection
228         GSource* __pConnectGSource;
229
230         // handling received message
231         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
232         std::string __pending;
233
234         // current message handling context
235         GIOChannel* __pCurrentChannel;
236         _ClientInfo* __pCurrentClientInfo;
237
238         std::map <int, _ClientInfo*> __clients;   // pid of client is used for key
239 }; // _IpcServer
240
241 } } // Tizen::Io
242
243 #endif // _FIO_INTERNAL_IPC_SERVER_H_