2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIo_IpcServer.h
19 * @brief This is the header file for the _IpcServer class.
21 * This file contains the declarations of _IpcServer.
24 #ifndef _FIO_INTERNAL_IPC_SERVER_H_
25 #define _FIO_INTERNAL_IPC_SERVER_H_
32 #include <ipc/ipc_message_macros.h>
33 #include <ipc/ipc_message_utils.h>
35 #include <FBaseResult.h>
36 #include <FBaseObject.h>
37 #include <FBaseString.h>
38 #include <FAppTypes.h>
40 namespace Tizen { namespace Base { namespace Runtime
42 class _EventDispatcher;
45 namespace Tizen { namespace Io
48 class _IIpcServerEventListener;
52 * @brief This class provides methods to handle IPC request messages.
55 class _OSP_EXPORT_ _IpcServer
56 : public Tizen::Base::Object
61 virtual ~_IpcServer(void);
64 * Constructs the instance of this class and starts the IPC server.
66 * @return An error code
67 * @param[in] name The name of IPC server
68 * @param[in] listener The listener for IPC server
69 * @param[in] runOnCallerThread Set to @c true, if the server runs on the caller thread
70 * @c false, if the server runs on its own thread.
71 * @exception E_SUCCESS The method was successful.
72 * @exception E_OUT_OF_MEMORY Insufficient memory.
73 * @exception E_SYSTEM Occurs when runOnCallerThread is set to true where the caller thread is worker thread.
75 result Construct(const Tizen::Base::String& name, const _IIpcServerEventListener& listener, bool runOnCallerThread = true);
78 * Returns the name of the IPC server.
80 * @return The name of the IPC server.
82 Tizen::Base::String GetName(void) const;
85 * Returns the id the of the client which sent a request message.
87 * @return The id of the IPC client.
88 * @remark This can be called only in a message handler.
90 int GetClientId(void) const;
93 * Returns the package id of the client which sent a request message.
95 * @return The package id of the IPC client.
96 * @remark This can be called only in a message handler.
98 Tizen::App::PackageId GetClientPackageId(void) const;
101 * Returns the application id of the client which sent a request message.
103 * @return The application id of the IPC client.
104 * @remark This can be called only in a message handler.
106 Tizen::App::AppId GetClientApplicationId(void) const;
109 * Stops the IPC server.
111 * @return An error code
112 * @exception E_SUCCESS The method was successful.
113 * @exception E_INVALID_STATE The IPC server has not been started.
118 * Sends a message to an IPC client.
120 * @return An error code
121 * @param[in] clientId The id of the IPC client
122 * @param[in] message The message to send
123 * @exception E_SUCCESS The method was successful.
124 * @exception E_INVALID_ARG The message is synchronous.
125 * @exception E_INVALID_OPERATION The client didn't set a listener.
126 * @exception E_RESOURCE_UNAVAILABLE The socket buffer is full.
127 * @exception E_OUT_OF_MEMORY Insufficient memory.
128 * @exception E_SYSTEM A system error occurred.
130 * @remark Only an asychronous message can be sent to an IPC client.
132 result SendResponse(int clientId, const IPC::Message& message);
134 result SendResponse(int clientId, IPC::Message* pMessage);
138 bool Send(IPC::Message* msg);
141 _IpcServer(const _IpcServer& value);
143 _IpcServer& operator =(const _IpcServer& value);
145 static void* ThreadProc(void* pParam);
147 void Run(void* pParam);
149 static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
151 static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
153 gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
155 static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
160 * @struct __ChannelInfo
161 * @brief This struct represent a channel.
168 struct _ClientInfo* pClientInfo;
169 GIOChannel* pGIOChannel;
175 * @struct __ClientInfo
176 * @brief This struct represent a client connected to this server.
183 int clientId; /**< the client id */
184 _IpcServer* pIpcServer; /**< the pointer to an _ IpcServer */
185 GIOChannel* pReverseChannel; /**< the channel for sending reverse message */
186 std::vector <struct _ChannelInfo*> channels; /**< the set of channels associated with a client */
187 Tizen::Base::String appId;
190 Tizen::Base::String __name;
191 bool __runOnCallerThread;
192 Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
193 _IIpcServerEventListener* __pListener;
195 pthread_t __handlerThread;
196 GMainContext* __pHandlerGMainContext;
197 GMainLoop* __pHandlerGMainLoop;
199 // handling connection
200 GSource* __pConnectGSource;
202 // handling received message
203 char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
204 std::string __pending;
206 // current message handling context
207 GIOChannel* __pCurrentChannel;
208 _ClientInfo* __pCurrentClientInfo;
210 std::map <int, _ClientInfo*> __clients; // pid of client is used for key
215 #endif // _FIO_INTERNAL_IPC_SERVER_H_