diff options
Diffstat (limited to 'linden/indra/llmessage/llsdrpcclient.cpp')
-rw-r--r-- | linden/indra/llmessage/llsdrpcclient.cpp | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/linden/indra/llmessage/llsdrpcclient.cpp b/linden/indra/llmessage/llsdrpcclient.cpp new file mode 100644 index 0000000..a9ff722 --- /dev/null +++ b/linden/indra/llmessage/llsdrpcclient.cpp | |||
@@ -0,0 +1,249 @@ | |||
1 | /** | ||
2 | * @file llsdrpcclient.cpp | ||
3 | * @author Phoenix | ||
4 | * @date 2005-11-05 | ||
5 | * @brief Implementation of the llsd client classes. | ||
6 | * | ||
7 | * Copyright (c) 2005-2007, Linden Research, Inc. | ||
8 | * | ||
9 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
10 | * to you under the terms of the GNU General Public License, version 2.0 | ||
11 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
12 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
13 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
14 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
15 | * | ||
16 | * There are special exceptions to the terms and conditions of the GPL as | ||
17 | * it is applied to this Source Code. View the full text of the exception | ||
18 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
19 | * online at http://secondlife.com/developers/opensource/flossexception | ||
20 | * | ||
21 | * By copying, modifying or distributing this software, you acknowledge | ||
22 | * that you have read and understood your obligations described above, | ||
23 | * and agree to abide by those obligations. | ||
24 | * | ||
25 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
26 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
27 | * COMPLETENESS OR PERFORMANCE. | ||
28 | */ | ||
29 | |||
30 | #include "linden_common.h" | ||
31 | #include "llsdrpcclient.h" | ||
32 | |||
33 | #include "llbufferstream.h" | ||
34 | #include "llfiltersd2xmlrpc.h" | ||
35 | #include "llmemtype.h" | ||
36 | #include "llpumpio.h" | ||
37 | #include "llsd.h" | ||
38 | #include "llsdserialize.h" | ||
39 | #include "llurlrequest.h" | ||
40 | |||
41 | /** | ||
42 | * String constants | ||
43 | */ | ||
44 | static std::string LLSDRPC_RESPONSE_NAME("response"); | ||
45 | static std::string LLSDRPC_FAULT_NAME("fault"); | ||
46 | |||
47 | /** | ||
48 | * LLSDRPCResponse | ||
49 | */ | ||
50 | LLSDRPCResponse::LLSDRPCResponse() : | ||
51 | mIsError(false), | ||
52 | mIsFault(false) | ||
53 | { | ||
54 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
55 | } | ||
56 | |||
57 | // virtual | ||
58 | LLSDRPCResponse::~LLSDRPCResponse() | ||
59 | { | ||
60 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
61 | } | ||
62 | |||
63 | bool LLSDRPCResponse::extractResponse(const LLSD& sd) | ||
64 | { | ||
65 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
66 | bool rv = true; | ||
67 | if(sd.has(LLSDRPC_RESPONSE_NAME)) | ||
68 | { | ||
69 | mReturnValue = sd[LLSDRPC_RESPONSE_NAME]; | ||
70 | mIsFault = false; | ||
71 | } | ||
72 | else if(sd.has(LLSDRPC_FAULT_NAME)) | ||
73 | { | ||
74 | mReturnValue = sd[LLSDRPC_FAULT_NAME]; | ||
75 | mIsFault = true; | ||
76 | } | ||
77 | else | ||
78 | { | ||
79 | mReturnValue.clear(); | ||
80 | mIsError = true; | ||
81 | rv = false; | ||
82 | } | ||
83 | return rv; | ||
84 | } | ||
85 | |||
86 | // virtual | ||
87 | LLIOPipe::EStatus LLSDRPCResponse::process_impl( | ||
88 | const LLChannelDescriptors& channels, | ||
89 | buffer_ptr_t& buffer, | ||
90 | bool& eos, | ||
91 | LLSD& context, | ||
92 | LLPumpIO* pump) | ||
93 | { | ||
94 | PUMP_DEBUG; | ||
95 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
96 | if(mIsError) | ||
97 | { | ||
98 | error(pump); | ||
99 | } | ||
100 | else if(mIsFault) | ||
101 | { | ||
102 | fault(pump); | ||
103 | } | ||
104 | else | ||
105 | { | ||
106 | response(pump); | ||
107 | } | ||
108 | PUMP_DEBUG; | ||
109 | return STATUS_DONE; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * LLSDRPCClient | ||
114 | */ | ||
115 | |||
116 | LLSDRPCClient::LLSDRPCClient() : | ||
117 | mState(STATE_NONE), | ||
118 | mQueue(EPBQ_PROCESS) | ||
119 | { | ||
120 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
121 | } | ||
122 | |||
123 | // virtual | ||
124 | LLSDRPCClient::~LLSDRPCClient() | ||
125 | { | ||
126 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
127 | } | ||
128 | |||
129 | bool LLSDRPCClient::call( | ||
130 | const std::string& uri, | ||
131 | const std::string& method, | ||
132 | const LLSD& parameter, | ||
133 | LLSDRPCResponse* response, | ||
134 | EPassBackQueue queue) | ||
135 | { | ||
136 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
137 | //llinfos << "RPC: " << uri << "." << method << "(" << *parameter << ")" | ||
138 | // << llendl; | ||
139 | if(method.empty() || !response) | ||
140 | { | ||
141 | return false; | ||
142 | } | ||
143 | mState = STATE_READY; | ||
144 | mURI.assign(uri); | ||
145 | std::stringstream req; | ||
146 | req << LLSDRPC_REQUEST_HEADER_1 << method | ||
147 | << LLSDRPC_REQUEST_HEADER_2; | ||
148 | LLSDSerialize::toNotation(parameter, req); | ||
149 | req << LLSDRPC_REQUEST_FOOTER; | ||
150 | mRequest = req.str(); | ||
151 | mQueue = queue; | ||
152 | mResponse = response; | ||
153 | return true; | ||
154 | } | ||
155 | |||
156 | bool LLSDRPCClient::call( | ||
157 | const std::string& uri, | ||
158 | const std::string& method, | ||
159 | const std::string& parameter, | ||
160 | LLSDRPCResponse* response, | ||
161 | EPassBackQueue queue) | ||
162 | { | ||
163 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
164 | //llinfos << "RPC: " << uri << "." << method << "(" << parameter << ")" | ||
165 | // << llendl; | ||
166 | if(method.empty() || parameter.empty() || !response) | ||
167 | { | ||
168 | return false; | ||
169 | } | ||
170 | mState = STATE_READY; | ||
171 | mURI.assign(uri); | ||
172 | std::stringstream req; | ||
173 | req << LLSDRPC_REQUEST_HEADER_1 << method | ||
174 | << LLSDRPC_REQUEST_HEADER_2 << parameter | ||
175 | << LLSDRPC_REQUEST_FOOTER; | ||
176 | mRequest = req.str(); | ||
177 | mQueue = queue; | ||
178 | mResponse = response; | ||
179 | return true; | ||
180 | } | ||
181 | |||
182 | // virtual | ||
183 | LLIOPipe::EStatus LLSDRPCClient::process_impl( | ||
184 | const LLChannelDescriptors& channels, | ||
185 | buffer_ptr_t& buffer, | ||
186 | bool& eos, | ||
187 | LLSD& context, | ||
188 | LLPumpIO* pump) | ||
189 | { | ||
190 | PUMP_DEBUG; | ||
191 | LLMemType m1(LLMemType::MTYPE_IO_SD_CLIENT); | ||
192 | if((STATE_NONE == mState) || (!pump)) | ||
193 | { | ||
194 | // You should have called the call() method already. | ||
195 | return STATUS_PRECONDITION_NOT_MET; | ||
196 | } | ||
197 | EStatus rv = STATUS_DONE; | ||
198 | switch(mState) | ||
199 | { | ||
200 | case STATE_READY: | ||
201 | { | ||
202 | PUMP_DEBUG; | ||
203 | // lldebugs << "LLSDRPCClient::process_impl STATE_READY" << llendl; | ||
204 | buffer->append( | ||
205 | channels.out(), | ||
206 | (U8*)mRequest.c_str(), | ||
207 | mRequest.length()); | ||
208 | context[CONTEXT_DEST_URI_SD_LABEL] = mURI; | ||
209 | mState = STATE_WAITING_FOR_RESPONSE; | ||
210 | break; | ||
211 | } | ||
212 | case STATE_WAITING_FOR_RESPONSE: | ||
213 | { | ||
214 | PUMP_DEBUG; | ||
215 | // The input channel has the sd response in it. | ||
216 | //lldebugs << "LLSDRPCClient::process_impl STATE_WAITING_FOR_RESPONSE" | ||
217 | // << llendl; | ||
218 | LLBufferStream resp(channels, buffer.get()); | ||
219 | LLSD sd; | ||
220 | LLSDSerialize::fromNotation(sd, resp); | ||
221 | LLSDRPCResponse* response = (LLSDRPCResponse*)mResponse.get(); | ||
222 | if (!response) | ||
223 | { | ||
224 | mState = STATE_DONE; | ||
225 | break; | ||
226 | } | ||
227 | response->extractResponse(sd); | ||
228 | if(EPBQ_PROCESS == mQueue) | ||
229 | { | ||
230 | LLPumpIO::chain_t chain; | ||
231 | chain.push_back(mResponse); | ||
232 | pump->addChain(chain, DEFAULT_CHAIN_EXPIRY_SECS); | ||
233 | } | ||
234 | else | ||
235 | { | ||
236 | pump->respond(mResponse.get()); | ||
237 | } | ||
238 | mState = STATE_DONE; | ||
239 | break; | ||
240 | } | ||
241 | case STATE_DONE: | ||
242 | default: | ||
243 | PUMP_DEBUG; | ||
244 | llinfos << "invalid state to process" << llendl; | ||
245 | rv = STATUS_ERROR; | ||
246 | break; | ||
247 | } | ||
248 | return rv; | ||
249 | } | ||