Update the spec file
[platform/framework/native/channel-service.git] / inc / 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 _IPC_SERVER_H_
26 #define _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 #include "IIpcServerEventListener.h"
42
43 namespace Tizen { namespace Base { namespace Runtime
44 {
45 class _EventDispatcher;
46 }}}
47
48 /**
49  * @class IpcServer
50  * @brief This class provides methods to handle IPC request messages.
51  *
52  */
53 class _OSP_EXPORT_ IpcServer
54         : public Tizen::Base::Object
55 {
56 public:
57         IpcServer(void);
58
59         virtual ~IpcServer(void);
60
61         /**
62          * Constructs the instance of this class and starts the IPC server.
63          *
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.
72          */
73         result Construct(const Tizen::Base::String& name, const IIpcServerEventListener& listener, bool runOnCallerThread = true);
74
75         /**
76          * Returns the name of the IPC server.
77          *
78          * @return The name of the IPC server.
79          */
80         Tizen::Base::String GetName(void) const;
81
82         /**
83          * Returns the id the of the client which sent a request message.
84          *
85          * @return The id of the IPC client.
86          * @remark This can be called only in a message handler.
87          */
88         int GetClientId(void) const;
89
90         /**
91          * Returns the process id of the client which sent a request message.
92          *
93          * @return The process id of the IPC client.
94          * @remark This can be called only in a message handler.
95          */
96         int GetClientProcessId(void) const;
97
98         /**
99          * Returns the package id of the client which sent a request message.
100          *
101          * @return The package id of the IPC client.
102          * @remark This can be called only in a message handler.
103          */
104         Tizen::Base::String GetClientAppId(void) const;
105
106         /**
107          * Returns the executable name of the client which sent a request message.
108          *
109          * @return The executable name of the IPC client.
110          * @remark This can be called only in a message handler.
111          */
112         Tizen::Base::String GetClientAppExecutableName(void) const;
113
114         /**
115          * Returns the application id of the client which sent a request message.
116          *
117          * @return The application id of the IPC client.
118          * @remark This can be called only in a message handler.
119          */
120         Tizen::App::AppId GetClientApplicationId(void) const;
121
122         /**
123          * Stops the IPC server.
124          *
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.
128          */
129         result Stop(void);
130
131         /**
132          * Sends a message to an IPC client.
133          *
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.
142          *
143          * @remark Only an asychronous message can be sent to an IPC client.
144          */
145         result SendResponse(int clientId, const IPC::Message& message);
146
147         result Start(void);
148
149         result SendResponse(int clientId, IPC::Message* pMessage);
150
151         bool Send(IPC::Message* msg);
152
153 private:
154         IpcServer(const IpcServer& value);
155
156         IpcServer& operator =(const IpcServer& value);
157
158         static void* ThreadProc(void* pParam);
159
160         void Run(void* pParam);
161
162         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
163
164         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
165
166         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
167
168         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
169
170         struct  _ClientInfo;
171
172         /**
173          *      @struct __ChannelInfo
174          *      @brief  This struct represent a channel.
175          */
176         struct  _ChannelInfo
177         {
178                 _ChannelInfo(void);
179                 ~_ChannelInfo(void);
180
181                 struct _ClientInfo* pClientInfo;
182                 GIOChannel* pGIOChannel;
183                 GSource* pGSource;
184                 bool destroySource;
185         };
186
187         /**
188          *      @struct __ClientInfo
189          *      @brief  This struct represent a client connected to this server.
190          */
191         struct  _ClientInfo
192         {
193                 _ClientInfo(void);
194                 ~_ClientInfo(void);
195
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;
201         };
202
203         Tizen::Base::String __name;
204         bool __runOnCallerThread;
205         Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
206         IIpcServerEventListener* __pListener;
207
208         pthread_t __handlerThread;
209         GMainContext* __pHandlerGMainContext;
210         GMainLoop* __pHandlerGMainLoop;
211
212         // handling connection
213         GSource* __pConnectGSource;
214
215         // handling received message
216         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
217         std::string __pending;
218
219         // current message handling context
220         GIOChannel* __pCurrentChannel;
221         _ClientInfo* __pCurrentClientInfo;
222
223         std::map <int, _ClientInfo*> __clients;   // pid of client is used for key
224 }; // IpcServer
225
226 #endif // _IPC_SERVER_H_