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 _IPC_SERVER_H_
26 #define _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 #include "IIpcServerEventListener.h"
43 namespace Tizen { namespace Base { namespace Runtime
45 class _EventDispatcher;
50 * @brief This class provides methods to handle IPC request messages.
53 class _OSP_EXPORT_ IpcServer
54 : public Tizen::Base::Object
59 virtual ~IpcServer(void);
62 * Constructs the instance of this class and starts the IPC server.
64 * @return An error code
65 * @param[in] name The name of IPC server
66 * @param[in] listener The listener for IPC server
67 * @param[in] runOnCallerThread Set to @c true, if the server runs on the caller thread
68 * @c false, if the server runs on its own thread.
69 * @exception E_SUCCESS The method was successful.
70 * @exception E_OUT_OF_MEMORY Insufficient memory.
71 * @exception E_SYSTEM Occurs when runOnCallerThread is set to true where the caller thread is worker thread.
73 result Construct(const Tizen::Base::String& name, const IIpcServerEventListener& listener, bool runOnCallerThread = true);
76 * Returns the name of the IPC server.
78 * @return The name of the IPC server.
80 Tizen::Base::String GetName(void) const;
83 * Returns the id the of the client which sent a request message.
85 * @return The id of the IPC client.
86 * @remark This can be called only in a message handler.
88 int GetClientId(void) const;
91 * Returns the process id of the client which sent a request message.
93 * @return The process id of the IPC client.
94 * @remark This can be called only in a message handler.
96 int GetClientProcessId(void) const;
99 * Returns the package id of the client which sent a request message.
101 * @return The package id of the IPC client.
102 * @remark This can be called only in a message handler.
104 Tizen::Base::String GetClientAppId(void) const;
107 * Returns the executable name of the client which sent a request message.
109 * @return The executable name of the IPC client.
110 * @remark This can be called only in a message handler.
112 Tizen::Base::String GetClientAppExecutableName(void) const;
115 * Returns the application id of the client which sent a request message.
117 * @return The application id of the IPC client.
118 * @remark This can be called only in a message handler.
120 Tizen::App::AppId GetClientApplicationId(void) const;
123 * Stops the IPC server.
125 * @return An error code
126 * @exception E_SUCCESS The method was successful.
127 * @exception E_INVALID_STATE The IPC server has not been started.
132 * Sends a message to an IPC client.
134 * @return An error code
135 * @param[in] clientId The id of the IPC client
136 * @param[in] message The message to send
137 * @exception E_SUCCESS The method was successful.
138 * @exception E_INVALID_ARG The message is synchronous.
139 * @exception E_INVALID_OPERATION The client didn't set a listener.
140 * @exception E_OUT_OF_MEMORY Insufficient memory.
141 * @exception E_SYSTEM A system error occurred.
143 * @remark Only an asychronous message can be sent to an IPC client.
145 result SendResponse(int clientId, const IPC::Message& message);
149 result SendResponse(int clientId, IPC::Message* pMessage);
151 bool Send(IPC::Message* msg);
154 IpcServer(const IpcServer& value);
156 IpcServer& operator =(const IpcServer& value);
158 static void* ThreadProc(void* pParam);
160 void Run(void* pParam);
162 static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
164 static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
166 gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
168 static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
173 * @struct __ChannelInfo
174 * @brief This struct represent a channel.
181 struct _ClientInfo* pClientInfo;
182 GIOChannel* pGIOChannel;
188 * @struct __ClientInfo
189 * @brief This struct represent a client connected to this server.
196 int clientId; /**< the client id */
197 IpcServer* pIpcServer; /**< the pointer to an _ IpcServer */
198 GIOChannel* pReverseChannel; /**< the channel for sending reverse message */
199 std::vector <struct _ChannelInfo*> channels; /**< the set of channels associated with a client */
200 Tizen::Base::String appId;
203 Tizen::Base::String __name;
204 bool __runOnCallerThread;
205 Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
206 IIpcServerEventListener* __pListener;
208 pthread_t __handlerThread;
209 GMainContext* __pHandlerGMainContext;
210 GMainLoop* __pHandlerGMainLoop;
212 // handling connection
213 GSource* __pConnectGSource;
215 // handling received message
216 char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
217 std::string __pending;
219 // current message handling context
220 GIOChannel* __pCurrentChannel;
221 _ClientInfo* __pCurrentClientInfo;
223 std::map <int, _ClientInfo*> __clients; // pid of client is used for key
226 #endif // _IPC_SERVER_H_