Get appid from pid
[platform/core/appfw/message-port.git] / src / 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        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 _IPC_CLIENT_H_
27 #define _IPC_CLIENT_H_
28
29 #include <string.h>
30 #include <vector>
31 #include <stdio.h>
32 #include <pthread.h>
33 #include <glib.h>
34
35 #include <ipc/ipc_message_macros.h>
36 #include <ipc/ipc_message_utils.h>
37
38
39 class IIpcClientEventListener;
40
41 /**
42  * @class IpcClient
43  * @brief This class provides methods for sending a message to an IPC server.
44  * @since 2.1
45  *
46  */
47 class IpcClient
48 {
49 public:
50         /**
51          * This is the default constructor for this class.
52          */
53         IpcClient(void);
54
55         /**
56          * This is the destructor for this class.
57          */
58         virtual ~IpcClient(void);
59
60         /**
61          * Constructs the instance of this class.
62          *
63          * @return An error code
64          * @param[in] serverName        The name of the server
65          * @param[in] pListener Set if the client want to handle a message from the IPC server.
66          *                                 @c NULL, otherwise.
67          * @exception E_SUCCESS         The method was successful.
68          * @exception E_OBJ_NOT_FOUND   The IPC server was not found.
69          * @exception E_OUT_OF_MEMORY   Insufficient memory.
70          * @exception E_SYSTEM          A system error occurred.
71          */
72         int Construct(const std::string& serverName, const IIpcClientEventListener* pListener = NULL);
73
74         /**
75          * Returns the name of the IPC server.
76          *
77          * @return The name of the IPC server.
78          */
79         std::string GetName(void) const;
80
81         /**
82          * Sends a request message to an IPC server.
83          *
84          * @code
85          *
86          *
87          * int
88          * CalculatorProxy::Add(int a , int b)
89          * {
90          *    int c = 0;
91          *
92          *    My_sum mySum(a, b, &c);
93          *
94          *    __pIpcClient->SendRequest(mySum);
95          *
96          *    return c;
97          * }
98          *
99          * @endcode
100          * @return An error code
101          * @param[in] message   The message to send
102          * @exception E_SUCCESS         The method was successful.
103          * @exception E_INVALID_STATE   The instance is in an invalid state.
104          * @exception E_OUT_OF_MEMORY   Insufficient memory.
105          * @exception E_SYSTEM          A system error occurred.
106          *
107          */
108         int SendRequest(const IPC::Message& message);
109
110         int SendRequest(IPC::Message* pMessage);
111
112 private:
113         IpcClient(const IpcClient& value);
114
115         IpcClient& operator =(const IpcClient& value);
116
117         int Send(IPC::Message* pMessage);
118
119         int SendAsync(IPC::Message* pMessage);
120
121         int SendSync(IPC::Message* pMessage);
122
123         int MakeConnection(bool forReverse = false);
124
125         int AcquireFd(void);
126
127         void ReleaseFd(int fd);
128
129         static gboolean OnReadMessage(GIOChannel* source, GIOCondition condition, gpointer data);
130
131         gboolean HandleReceivedMessage(GIOChannel* source, GIOCondition condition);
132
133 private:
134         GSource* __pReverseSource;
135         pthread_mutex_t* __pMutex;
136
137         std::vector <int> __fds;
138         //Tizen::Base::Runtime::Mutex* __pFdLock;
139         std::string __name;
140         std::string __appId;
141         IIpcClientEventListener* __pListener;
142
143         static const int __MAX_MESSAGE_BUFFER_SIZE = 1024;
144         char __messageBuffer[__MAX_MESSAGE_BUFFER_SIZE];
145         std::string __pending;
146 };
147
148 #endif // _IPC_CLIENT_H_