4503a47ced36f1a9223ad280c22589af657ebd7e
[platform/framework/native/appfw.git] / src / io / inc / FIo_IpcClient.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_IpcClient.h
20  * @brief       This is the header file for the _IpcClient class.
21  *
22  * This file contains the declarations of _IpcClient.
23  */
24
25
26 #ifndef _FIO_INTERNAL_IPC_CLIENT_H_
27 #define _FIO_INTERNAL_IPC_CLIENT_H_
28
29 #include <string>
30 #include <pthread.h>
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 <FOspConfig.h>
40
41
42 namespace Tizen { namespace Base { namespace Runtime
43 {
44 class Mutex;
45 }}}
46 namespace Tizen { namespace Io
47 {
48
49 class _IIpcClientEventListener;
50
51 /**
52  * @class _IpcClient
53  * @brief This class provides methods for sending a message to an IPC server.
54  * @since 2.1
55  *
56  */
57 class _OSP_EXPORT_ _IpcClient
58         : Tizen::Base::Object
59 {
60 public:
61         /**
62          * This is the default constructor for this class.
63          * @since 2.1
64          */
65         _IpcClient(void);
66
67         /**
68          * This is the destructor for this class.
69          * @since 2.1
70          */
71         virtual ~_IpcClient(void);
72
73         /**
74          * Constructs the instance of this class.
75          *
76          * @since 2.1
77          *
78          * @return An error code
79          * @param[in] serverName        The name of the server
80          * @param[in] pListener Set if the client want to handle a message from the IPC server.
81          *                                 @c null, otherwise.
82          * @exception E_SUCCESS         The method was successful.
83          * @exception E_OBJ_NOT_FOUND   The IPC server was not found.
84          * @exception E_OUT_OF_MEMORY   Insufficient memory.
85          * @exception E_SYSTEM          A system error occurred.
86          */
87         result Construct(const Tizen::Base::String& serverName, const _IIpcClientEventListener* pListener = null);
88
89         /**
90          * Sends a request message to an IPC server.
91          *
92          * @since 2.1
93          * @code
94          *
95          *
96          * int
97          * CalculatorProxy::Add(int a , int b)
98          * {
99          *    int c = 0;
100          *
101          *    My_sum mySum(a, b, &c);
102          *
103          *    __pIpcClient->SendRequest(mySum);
104          *
105          *    return c;
106          * }
107          *
108          * @endcode
109          * @return An error code
110          * @param[in] message   The message to send
111          * @exception E_SUCCESS         The method was successful.
112          * @exception E_INVALID_STATE   The instance is in an invalid state.
113          * @exception E_OUT_OF_MEMORY   Insufficient memory.
114          * @exception E_SYSTEM          A system error occurred.
115          *
116          */
117         result SendRequest(const IPC::Message& message);
118
119         result SendRequest(IPC::Message* pMessage);
120
121 private:
122         _IpcClient(const _IpcClient& value);
123
124         _IpcClient& operator =(const _IpcClient& value);
125
126         result Send(IPC::Message* pMessage);
127
128         result SendAsync(IPC::Message* pMessage);
129
130         result SendSync(IPC::Message* pMessage);
131
132         result MakeConnection(bool forReverse = false);
133
134         int AcquireFd(void);
135
136         void ReleaseFd(int fd);
137
138         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
139
140         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition);
141
142 private:
143         GSource* __pReverseSource;
144
145         std::vector <int> __fds;
146         int __fdCount;
147         Tizen::Base::Runtime::Mutex* __pFdLock;
148         Tizen::Base::String __name;
149         _IIpcClientEventListener* __pListener;
150
151         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
152         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
153         std::string __pending;
154 };
155
156 } } // Tizen::Io
157
158 #endif // _FIO_INTERNAL_IPC_CLIENT_H_