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_OUT_OF_MEMORY Insufficient memory.
127 * @exception E_SYSTEM A system error occurred.
129 * @remark Only an asychronous message can be sent to an IPC client.
131 result SendResponse(int clientId, const IPC::Message& message);
135 result SendResponse(int clientId, IPC::Message* pMessage);
137 bool Send(IPC::Message* msg);
140 _IpcServer(const _IpcServer& value);
142 _IpcServer& operator =(const _IpcServer& value);
144 static void* ThreadProc(void* pParam);
146 void Run(void* pParam);
148 static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
150 static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
152 gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
154 static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
159 * @struct __ChannelInfo
160 * @brief This struct represent a channel.
167 struct _ClientInfo* pClientInfo;
168 GIOChannel* pGIOChannel;
174 * @struct __ClientInfo
175 * @brief This struct represent a client connected to this server.
182 int clientId; /**< the client id */
183 _IpcServer* pIpcServer; /**< the pointer to an _ IpcServer */
184 GIOChannel* pReverseChannel; /**< the channel for sending reverse message */
185 std::vector <struct _ChannelInfo*> channels; /**< the set of channels associated with a client */
186 Tizen::Base::String appId;
189 Tizen::Base::String __name;
190 bool __runOnCallerThread;
191 Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
192 _IIpcServerEventListener* __pListener;
194 pthread_t __handlerThread;
195 GMainContext* __pHandlerGMainContext;
196 GMainLoop* __pHandlerGMainLoop;
198 // handling connection
199 GSource* __pConnectGSource;
201 // handling received message
202 char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
203 std::string __pending;
205 // current message handling context
206 GIOChannel* __pCurrentChannel;
207 _ClientInfo* __pCurrentClientInfo;
209 std::map <int, _ClientInfo*> __clients; // pid of client is used for key
214 #endif // _FIO_INTERNAL_IPC_SERVER_H_