2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_IpcServer.h
20 * @brief This is the header file for the _IpcServer class.
22 * This file contains the declarations of _IpcServer.
25 #ifndef _FIO_INTERNAL_IPC_SERVER_H_
26 #define _FIO_INTERNAL_IPC_SERVER_H_
33 #include <ipc/ipc_message_macros.h>
34 #include <ipc/ipc_message_utils.h>
36 #include <FBaseResult.h>
37 #include <FBaseObject.h>
38 #include <FBaseString.h>
39 #include <FAppTypes.h>
41 namespace Tizen { namespace Base { namespace Runtime
43 class _EventDispatcher;
46 namespace Tizen { namespace Io
49 class _IIpcServerEventListener;
53 * @brief This class provides methods to handle IPC request messages.
56 class _OSP_EXPORT_ _IpcServer
57 : public Tizen::Base::Object
62 virtual ~_IpcServer(void);
65 * Constructs the instance of this class and starts the IPC server.
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.
76 result Construct(const Tizen::Base::String& name, const _IIpcServerEventListener& listener, bool runOnCallerThread = true);
79 * Returns the name of the IPC server.
81 * @return The name of the IPC server.
83 Tizen::Base::String GetName(void) const;
86 * Returns the id the of the client which sent a request message.
88 * @return The id of the IPC client.
89 * @remark This can be called only in a message handler.
91 int GetClientId(void) const;
94 * Returns the process id of the client which sent a request message.
96 * @return The process id of the IPC client.
97 * @remark This can be called only in a message handler.
99 int GetClientProcessId(void) const;
102 * Returns the package id of the client which sent a request message.
104 * @return The package id of the IPC client.
105 * @remark This can be called only in a message handler.
107 Tizen::Base::String GetClientAppId(void) const;
110 * Returns the executable name of the client which sent a request message.
112 * @return The executable name of the IPC client.
113 * @remark This can be called only in a message handler.
115 Tizen::Base::String GetClientAppExecutableName(void) const;
118 * Returns the application id of the client which sent a request message.
120 * @return The application id of the IPC client.
121 * @remark This can be called only in a message handler.
123 Tizen::App::AppId GetClientApplicationId(void) const;
126 * Stops the IPC server.
128 * @return An error code
129 * @exception E_SUCCESS The method was successful.
130 * @exception E_INVALID_STATE The IPC server has not been started.
135 * Sends a message to an IPC client.
137 * @return An error code
138 * @param[in] clientId The id of the IPC client
139 * @param[in] message The message to send
140 * @exception E_SUCCESS The method was successful.
141 * @exception E_INVALID_ARG The message is synchronous.
142 * @exception E_INVALID_OPERATION The client didn't set a listener.
143 * @exception E_OUT_OF_MEMORY Insufficient memory.
144 * @exception E_SYSTEM A system error occurred.
146 * @remark Only an asychronous message can be sent to an IPC client.
148 result SendResponse(int clientId, const IPC::Message& message);
152 result SendResponse(int clientId, IPC::Message* pMessage);
154 bool Send(IPC::Message* msg);
157 _IpcServer(const _IpcServer& value);
159 _IpcServer& operator =(const _IpcServer& value);
161 static void* ThreadProc(void* pParam);
163 void Run(void* pParam);
165 static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
167 static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
169 gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
171 static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
176 * @struct __ChannelInfo
177 * @brief This struct represent a channel.
184 struct _ClientInfo* pClientInfo;
185 GIOChannel* pGIOChannel;
191 * @struct __ClientInfo
192 * @brief This struct represent a client connected to this server.
199 int clientId; /**< the client id */
200 _IpcServer* pIpcServer; /**< the pointer to an _ IpcServer */
201 GIOChannel* pReverseChannel; /**< the channel for sending reverse message */
202 std::vector <struct _ChannelInfo*> channels; /**< the set of channels associated with a client */
203 Tizen::Base::String pkgId;
204 Tizen::Base::String appExecutableName;
207 Tizen::Base::String __name;
208 bool __runOnCallerThread;
209 Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
210 _IIpcServerEventListener* __pListener;
212 pthread_t __handlerThread;
213 GMainContext* __pHandlerGMainContext;
214 GMainLoop* __pHandlerGMainLoop;
216 // handling connection
217 GSource* __pConnectGSource;
219 // handling received message
220 char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
221 std::string __pending;
223 // current message handling context
224 GIOChannel* __pCurrentChannel;
225 _ClientInfo* __pCurrentClientInfo;
227 std::map <int, _ClientInfo*> __clients; // pid of client is used for key
232 #endif // _FIO_INTERNAL_IPC_SERVER_H_