tizen 2.4 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/string.h>
28 #include <dpl/serialization.h>
29 #include <string>
30 #include <memory>
31
32 namespace DPL {
33 namespace RPC {
34 class RPCFunction : public IStream
35 {
36   protected:
37     BinaryQueue m_buffer; ///< Serialized RPC function call as a binary queue
38
39   public:
40     class Exception
41     {
42       public:
43         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
44         DECLARE_EXCEPTION_TYPE(Base, ParseFailed)
45     };
46
47     /**
48      * Constructor
49      */
50     RPCFunction()
51     {}
52
53     /**
54      * Constructor
55      *
56      * @param buffer Binary queue to copy initialization data from
57      */
58     RPCFunction(const BinaryQueue &buffer)
59     {
60         m_buffer.AppendCopyFrom(buffer);
61     }
62
63     /**
64      * Destructor
65      */
66     virtual ~RPCFunction()
67     {}
68
69     /**
70      * Append argument to call
71      *
72      * @param[in] arg Template based argument to append
73      * @return none
74      * @warning Carefully add any pointers to buffer because of template nature
75      * of this method
76      */
77     template<typename Type>
78     void AppendArg(const Type &arg)
79     {
80         size_t argSize = sizeof(arg);
81         m_buffer.AppendCopy(&argSize, sizeof(argSize));
82         m_buffer.AppendCopy(&arg, sizeof(arg));
83     }
84
85     /**
86      * Append @a std::string argument to call
87      *
88      * @param[in] arg String to append to function call
89      * @return none
90      */
91     void AppendArg(const std::string &arg)
92     {
93         size_t argSize = arg.size();
94         m_buffer.AppendCopy(&argSize, sizeof(argSize));
95         m_buffer.AppendCopy(arg.c_str(), argSize);
96     }
97
98     /**
99      * Append @a DPL::String argument to call
100      *
101      * @param[in] arg String to append to function call
102      * @return none
103      */
104     void AppendArg(const String &arg)
105     {
106         std::string localStdString = ToUTF8String(arg);
107         AppendArg(localStdString);
108     }
109
110     /**
111      * Consume argument from call. Arguments are retrieved in non-reversed order
112      * (same as they were pushed onto RPC function argument stack)
113      *
114      * @param[out] arg Reference to output template based argument
115      * @warning Carefully add any pointers to buffer because of template nature
116      * of this method
117      * @return none
118      */
119     template<typename Type>
120     void ConsumeArg(Type &arg)
121     {
122         Try
123         {
124             size_t argSize = sizeof(arg);
125             m_buffer.FlattenConsume(&argSize, sizeof(argSize));
126
127             if (argSize != sizeof(arg)) {
128                 ThrowMsg(Exception::ParseFailed, "Stream parse CRC failed");
129             }
130
131             m_buffer.FlattenConsume(&arg, sizeof(arg));
132         }
133         Catch(BinaryQueue::Exception::OutOfData)
134         {
135             ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
136         }
137     }
138
139     /**
140      * Consume @a std::string argument from call. Arguments are retrieved in
141      * non-reversed order
142      * (same as they were pushed onto RPC function argument stack)
143      *
144      * @param[out] arg Reference to output @a std::string argument
145      * @return none
146      */
147     void ConsumeArg(std::string &arg)
148     {
149         Try
150         {
151             std::string::size_type size;
152             m_buffer.FlattenConsume(&size, sizeof(size));
153             std::unique_ptr<char[]> str(new char[size]);
154             m_buffer.FlattenConsume(str.get(), size);
155             arg = std::string(str.get(), str.get() + size);
156         }
157         Catch(BinaryQueue::Exception::OutOfData)
158         {
159             ReThrowMsg(Exception::ParseFailed, "Unexpected end of stream");
160         }
161     }
162
163     /**
164      * Consume @a DPL::String argument from call. Arguments are converted to
165      * 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 } // namespace DPL
211
212 #endif // DPL_RPC_FUNCTION_H