tizen beta release
[framework/web/wrt-commons.git] / modules / rpc / include / dpl / rpc / rpc_function.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        rpc_function.h
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the header file for RPC function
21  */
22 #ifndef DPL_RPC_FUNCTION_H
23 #define DPL_RPC_FUNCTION_H
24
25 #include <dpl/exception.h>
26 #include <dpl/binary_queue.h>
27 #include <dpl/scoped_array.h>
28 #include <dpl/string.h>
29 #include <dpl/serialization.h>
30 #include <string>
31
32 namespace DPL
33 {
34 namespace RPC
35 {
36
37 class RPCFunction : public IStream
38 {
39 protected:
40     BinaryQueue m_buffer; ///< Serialized RPC function call as a binary queue
41
42 public:
43     class Exception
44     {
45     public:
46         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
47         DECLARE_EXCEPTION_TYPE(Base, ParseFailed)
48     };
49
50     /**
51      * Constructor
52      */
53     RPCFunction()
54     {
55     }
56
57     /**
58      * Constructor
59      *
60      * @param buffer Binary queue to copy initialization data from
61      */
62     RPCFunction(const BinaryQueue &buffer)
63     {
64         m_buffer.AppendCopyFrom(buffer);
65     }
66
67     /**
68      * Destructor
69      */
70     virtual ~RPCFunction()
71     {
72     }
73
74     /**
75      * Append argument to call
76      *
77      * @param[in] arg Template based argument to append
78      * @return none
79      * @warning Carefully add any pointers to buffer because of template nature of this method
80      */
81     template<typename Type>
82     void AppendArg(const Type &arg)
83     {
84         size_t argSize = sizeof(arg);
85         m_buffer.AppendCopy(&argSize, sizeof(argSize));
86         m_buffer.AppendCopy(&arg, sizeof(arg));
87     }
88
89     /**
90      * Append @a std::string argument to call
91      *
92      * @param[in] arg String to append to function call
93      * @return none
94      */
95     void AppendArg(const std::string &arg)
96     {
97         size_t argSize = arg.size();
98         m_buffer.AppendCopy(&argSize, sizeof(argSize));
99         m_buffer.AppendCopy(arg.c_str(), argSize);
100     }
101
102     /**
103      * Append @a DPL::String argument to call
104      *
105      * @param[in] arg String to append to function call
106      * @return none
107      */
108     void AppendArg(const String &arg)
109     {
110         std::string localStdString = ToUTF8String(arg);
111         AppendArg(localStdString);
112     }
113
114     /**
115      * Consume argument from call. Arguments are retrieved in non-reversed order
116      * (same as they were pushed onto RPC function argument stack)
117      *
118      * @param[out] arg Reference to output template based argument
119      * @warning Carefully add any pointers to buffer because of template nature of this method
120      * @return none
121      */
122     template<typename Type>
123     void ConsumeArg(Type &arg)
124     {
125         Try
126         {
127             size_t argSize = sizeof(arg);
128             m_buffer.FlattenConsume(&argSize, sizeof(argSize));
129
130             if (argSize != sizeof(arg))
131                 ThrowMsg(Exception::ParseFailed, "Stream parse CRC failed");
132
133             m_buffer.FlattenConsume(&arg, sizeof(arg));
134         }
135         Catch (BinaryQueue::Exception::OutOfData)
136         {
137             ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
138         }
139     }
140
141     /**
142      * Consume @a std::string argument from call. Arguments are retrieved in non-reversed order
143      * (same as they were pushed onto RPC function argument stack)
144      *
145      * @param[out] arg Reference to output @a std::string argument
146      * @return none
147      */
148     void ConsumeArg(std::string &arg)
149     {
150         Try
151         {
152             std::string::size_type size;
153             m_buffer.FlattenConsume(&size, sizeof(size));
154             ScopedArray<char> str(new char[size]);
155             m_buffer.FlattenConsume(str.Get(), size);
156             arg = std::string(str.Get(), str.Get() + size);
157         }
158         Catch (BinaryQueue::Exception::OutOfData)
159         {
160             ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
161         }
162     }
163
164     /**
165      * Consume @a DPL::String argument from call. Arguments are converted to UTF-8 string
166      *
167      * @param[out] arg Reference to output @a DPL::String argument
168      * @return none
169      */
170     void ConsumeArg(String &arg)
171     {
172         std::string consumedStdString;
173         ConsumeArg(consumedStdString);
174         arg = FromUTF8String(consumedStdString);
175     }
176
177     /**
178      * Serialize all function parameters into single binary queue
179      *
180      * @return Serialized binary queue representation of RPC function
181      */
182     BinaryQueue Serialize() const
183     {
184         return m_buffer;
185     }
186
187     /**
188      * Reads binary data from serialized stream
189      *
190      * @param num number of bytes to read
191      * @param bytes buffer for read data
192      */
193     virtual void Read(size_t num, void * bytes)
194     {
195         m_buffer.FlattenConsume(bytes, num);
196     }
197
198     /**
199      * Writes binary data to serialized stream
200      *
201      * @param num number of bytes to write to serialization buffer
202      * @param bytes buffer for data to write
203      */
204     virtual void Write(size_t num, const void * bytes)
205     {
206         m_buffer.AppendCopy(bytes, num);
207     }
208 };
209
210 }
211 } // namespace DPL
212
213 #endif // DPL_RPC_FUNCTION_H