Add a retry logic in IPC
[platform/framework/native/appfw.git] / src / server / inc / FIo_IpcServer.h
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FIo_IpcServer.h
19  * @brief       This is the header file for the _IpcServer class.
20  *
21  * This file contains the declarations of _IpcServer.
22  */
23
24 #ifndef _FIO_INTERNAL_IPC_SERVER_H_
25 #define _FIO_INTERNAL_IPC_SERVER_H_
26
27 #include <string>
28 #include <pthread.h>
29 #include <map>
30 #include <glib.h>
31
32 #include <ipc/ipc_message_macros.h>
33 #include <ipc/ipc_message_utils.h>
34
35 #include <FBaseResult.h>
36 #include <FBaseObject.h>
37 #include <FBaseString.h>
38 #include <FAppTypes.h>
39
40 namespace Tizen { namespace Base { namespace Runtime
41 {
42 class _EventDispatcher;
43 }}}
44
45 namespace Tizen { namespace Io
46 {
47
48 class _IIpcServerEventListener;
49
50 /**
51  * @class _IpcServer
52  * @brief This class provides methods to handle IPC request messages.
53  *
54  */
55 class _OSP_EXPORT_ _IpcServer
56         : public Tizen::Base::Object
57 {
58 public:
59         _IpcServer(void);
60
61         virtual ~_IpcServer(void);
62
63         /**
64          * Constructs the instance of this class and starts the IPC server.
65          *
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.
74          */
75         result Construct(const Tizen::Base::String& name, const _IIpcServerEventListener& listener, bool runOnCallerThread = true);
76
77         /**
78          * Returns the name of the IPC server.
79          *
80          * @return The name of the IPC server.
81          */
82         Tizen::Base::String GetName(void) const;
83
84         /**
85          * Returns the id the of the client which sent a request message.
86          *
87          * @return The id of the IPC client.
88          * @remark This can be called only in a message handler.
89          */
90         int GetClientId(void) const;
91
92         /**
93          * Returns the package id of the client which sent a request message.
94          *
95          * @return The package id of the IPC client.
96          * @remark This can be called only in a message handler.
97          */
98         Tizen::App::PackageId GetClientPackageId(void) const;
99
100         /**
101          * Returns the application id of the client which sent a request message.
102          *
103          * @return The application id of the IPC client.
104          * @remark This can be called only in a message handler.
105          */
106         Tizen::App::AppId GetClientApplicationId(void) const;
107
108         /**
109          * Stops the IPC server.
110          *
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.
114          */
115         result Stop(void);
116
117         /**
118          * Sends a message to an IPC client.
119          *
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.
128          *
129          * @remark Only an asychronous message can be sent to an IPC client.
130          */
131         result SendResponse(int clientId, const IPC::Message& message);
132
133         result Start(void);
134
135         result SendResponse(int clientId, IPC::Message* pMessage);
136
137         bool Send(IPC::Message* msg);
138
139 private:
140         _IpcServer(const _IpcServer& value);
141
142         _IpcServer& operator =(const _IpcServer& value);
143
144         static void* ThreadProc(void* pParam);
145
146         void Run(void* pParam);
147
148         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
149
150         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
151
152         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
153
154         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
155
156         struct  _ClientInfo;
157
158         /**
159          *      @struct __ChannelInfo
160          *      @brief  This struct represent a channel.
161          */
162         struct  _ChannelInfo
163         {
164                 _ChannelInfo(void);
165                 ~_ChannelInfo(void);
166
167                 struct _ClientInfo* pClientInfo;
168                 GIOChannel* pGIOChannel;
169                 GSource* pGSource;
170                 bool destroySource;
171         };
172
173         /**
174          *      @struct __ClientInfo
175          *      @brief  This struct represent a client connected to this server.
176          */
177         struct  _ClientInfo
178         {
179                 _ClientInfo(void);
180                 ~_ClientInfo(void);
181
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;
187         };
188
189         Tizen::Base::String __name;
190         bool __runOnCallerThread;
191         Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
192         _IIpcServerEventListener* __pListener;
193
194         pthread_t __handlerThread;
195         GMainContext* __pHandlerGMainContext;
196         GMainLoop* __pHandlerGMainLoop;
197
198         // handling connection
199         GSource* __pConnectGSource;
200
201         // handling received message
202         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
203         std::string __pending;
204
205         // current message handling context
206         GIOChannel* __pCurrentChannel;
207         _ClientInfo* __pCurrentClientInfo;
208
209         std::map <int, _ClientInfo*> __clients;   // pid of client is used for key
210 }; // _IpcServer
211
212 } } // Tizen::Io
213
214 #endif // _FIO_INTERNAL_IPC_SERVER_H_