Update a service name
[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 application id of the client which sent a request message.
92          *
93          * @return The application id of the IPC client.
94          * @remark This can be called only in a message handler.
95          */
96         Tizen::App::AppId GetClientApplicationId(void) const;
97
98         /**
99          * Stops the IPC server.
100          *
101          * @return An error code
102          * @exception E_SUCCESS         The method was successful.
103          * @exception E_INVALID_STATE   The IPC server has not been started.
104          */
105         result Stop(void);
106
107         /**
108          * Sends a message to an IPC client.
109          *
110          * @return An error code
111          * @param[in] clientId      The id of the IPC client
112          * @param[in] message   The message to send
113          * @exception E_SUCCESS The method was successful.
114          * @exception E_INVALID_ARG             The message is synchronous.
115          * @exception E_INVALID_OPERATION       The client didn't set a listener.
116          * @exception E_OUT_OF_MEMORY   Insufficient memory.
117          * @exception E_SYSTEM          A system error occurred.
118          *
119          * @remark Only an asychronous message can be sent to an IPC client.
120          */
121         result SendResponse(int clientId, const IPC::Message& message);
122
123         result Start(void);
124
125         result SendResponse(int clientId, IPC::Message* pMessage);
126
127         bool Send(IPC::Message* msg);
128
129 private:
130         IpcServer(const IpcServer& value);
131
132         IpcServer& operator =(const IpcServer& value);
133
134         static void* ThreadProc(void* pParam);
135
136         void Run(void* pParam);
137
138         static gboolean OnConnectionRequest(GIOChannel* source, GIOCondition condition, gpointer data);
139
140         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
141
142         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition, gpointer data);
143
144         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
145
146         struct  _ClientInfo;
147
148         /**
149          *      @struct __ChannelInfo
150          *      @brief  This struct represent a channel.
151          */
152         struct  _ChannelInfo
153         {
154                 _ChannelInfo(void);
155                 ~_ChannelInfo(void);
156
157                 struct _ClientInfo* pClientInfo;
158                 GIOChannel* pGIOChannel;
159                 GSource* pGSource;
160                 bool destroySource;
161         };
162
163         /**
164          *      @struct __ClientInfo
165          *      @brief  This struct represent a client connected to this server.
166          */
167         struct  _ClientInfo
168         {
169                 _ClientInfo(void);
170                 ~_ClientInfo(void);
171
172                 int clientId;                              /**< the client id */
173                 IpcServer* pIpcServer;                    /**< the pointer to an _ IpcServer */
174                 GIOChannel* pReverseChannel;               /**< the channel for sending reverse message */
175                 std::vector <struct _ChannelInfo*> channels;   /**< the set of channels associated with a client */
176                 Tizen::Base::String appId;
177         };
178
179         Tizen::Base::String __name;
180         bool __runOnCallerThread;
181         Tizen::Base::Runtime::_EventDispatcher* __pEventDispatcher;
182         IIpcServerEventListener* __pListener;
183
184         pthread_t __handlerThread;
185         GMainContext* __pHandlerGMainContext;
186         GMainLoop* __pHandlerGMainLoop;
187
188         // handling connection
189         GSource* __pConnectGSource;
190
191         // handling received message
192         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
193         std::string __pending;
194
195         // current message handling context
196         GIOChannel* __pCurrentChannel;
197         _ClientInfo* __pCurrentClientInfo;
198
199         std::map <int, _ClientInfo*> __clients;   // pid of client is used for key
200 }; // IpcServer
201
202 #endif // _IPC_SERVER_H_