PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llplugin/llpluginmessage.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 438 lines | 181 code | 59 blank | 198 comment | 10 complexity | 9a6639ec8495221b3154b087d1ad3f9e MD5 | raw file
  1/** 
  2 * @file llpluginmessage.cpp
  3 * @brief LLPluginMessage encapsulates the serialization/deserialization of messages passed to and from plugins.
  4 *
  5 * @cond
  6 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 * @endcond
 27 */
 28
 29#include "linden_common.h"
 30
 31#include "llpluginmessage.h"
 32#include "llsdserialize.h"
 33#include "u64.h"
 34
 35/**
 36 * Constructor.
 37 */
 38LLPluginMessage::LLPluginMessage()
 39{
 40}
 41
 42/**
 43 * Constructor.
 44 *
 45 * @param[in] p Existing message
 46 */
 47LLPluginMessage::LLPluginMessage(const LLPluginMessage &p)
 48{
 49	mMessage = p.mMessage;
 50}
 51
 52/**
 53 * Constructor.
 54 *
 55 * @param[in] message_class Message class
 56 * @param[in] message_name Message name
 57 */
 58LLPluginMessage::LLPluginMessage(const std::string &message_class, const std::string &message_name)
 59{
 60	setMessage(message_class, message_name);
 61}
 62
 63
 64/**
 65 * Destructor.
 66 */
 67LLPluginMessage::~LLPluginMessage()
 68{
 69}
 70
 71/**
 72 * Reset all internal state.
 73 */
 74void LLPluginMessage::clear()
 75{
 76	mMessage = LLSD::emptyMap();
 77	mMessage["params"] = LLSD::emptyMap();
 78}
 79
 80/**
 81 * Sets the message class and name. Also has the side-effect of clearing any key-value pairs in the message.
 82 *
 83 * @param[in] message_class Message class
 84 * @param[in] message_name Message name
 85 */
 86void LLPluginMessage::setMessage(const std::string &message_class, const std::string &message_name)
 87{
 88	clear();
 89	mMessage["class"] = message_class;
 90	mMessage["name"] = message_name;
 91}
 92
 93/**
 94 * Sets a key/value pair in the message, where the value is a string.
 95 *
 96 * @param[in] key Key
 97 * @param[in] value String value
 98 */
 99void LLPluginMessage::setValue(const std::string &key, const std::string &value)
100{
101	mMessage["params"][key] = value;
102}
103
104/**
105 * Sets a key/value pair in the message, where the value is LLSD.
106 *
107 * @param[in] key Key
108 * @param[in] value LLSD value
109 */
110void LLPluginMessage::setValueLLSD(const std::string &key, const LLSD &value)
111{
112	mMessage["params"][key] = value;
113}
114
115/**
116 * Sets a key/value pair in the message, where the value is signed 32-bit.
117 *
118 * @param[in] key Key
119 * @param[in] value 32-bit signed value
120 */
121void LLPluginMessage::setValueS32(const std::string &key, S32 value)
122{
123	mMessage["params"][key] = value;
124}
125
126/**
127 * Sets a key/value pair in the message, where the value is unsigned 32-bit. The value is stored as a string beginning with "0x".
128 *
129 * @param[in] key Key
130 * @param[in] value 32-bit unsigned value
131 */
132void LLPluginMessage::setValueU32(const std::string &key, U32 value)
133{
134	std::stringstream temp;
135	temp << "0x" << std::hex << value;
136	setValue(key, temp.str());
137}
138
139/**
140 * Sets a key/value pair in the message, where the value is a bool.
141 *
142 * @param[in] key Key
143 * @param[in] value Boolean value
144 */
145void LLPluginMessage::setValueBoolean(const std::string &key, bool value)
146{
147	mMessage["params"][key] = value;
148}
149
150/**
151 * Sets a key/value pair in the message, where the value is a double.
152 *
153 * @param[in] key Key
154 * @param[in] value Boolean value
155 */
156void LLPluginMessage::setValueReal(const std::string &key, F64 value)
157{
158	mMessage["params"][key] = value;
159}
160
161/**
162 * Sets a key/value pair in the message, where the value is a pointer. The pointer is stored as a string.
163 *
164 * @param[in] key Key
165 * @param[in] value Pointer value
166 */
167void LLPluginMessage::setValuePointer(const std::string &key, void* value)
168{
169	std::stringstream temp;
170	// iostreams should output pointer values in hex with an initial 0x by default.
171	temp << value;
172	setValue(key, temp.str());
173}
174
175/**
176 * Gets the message class.
177 *
178 * @return Message class
179 */
180std::string LLPluginMessage::getClass(void) const
181{
182	return mMessage["class"];
183}
184
185/**
186 * Gets the message name.
187 *
188 * @return Message name
189 */
190std::string LLPluginMessage::getName(void) const
191{
192	return mMessage["name"];
193}
194
195/**
196 *	Returns true if the specified key exists in this message (useful for optional parameters).
197 *
198 * @param[in] key Key
199 *
200 * @return True if key exists, false otherwise.
201 */
202bool LLPluginMessage::hasValue(const std::string &key) const
203{
204	bool result = false;
205	
206	if(mMessage["params"].has(key))
207	{
208		result = true;
209	}
210	
211	return result;
212}
213
214/**
215 *	Gets the value of a key as a string. If the key does not exist, an empty string will be returned.
216 *
217 * @param[in] key Key
218 *
219 * @return String value of key if key exists, empty string if key does not exist.
220 */
221std::string LLPluginMessage::getValue(const std::string &key) const
222{
223	std::string result;
224	
225	if(mMessage["params"].has(key))
226	{
227		result = mMessage["params"][key].asString();
228	}
229	
230	return result;
231}
232
233/**
234 *	Gets the value of a key as LLSD. If the key does not exist, a null LLSD will be returned.
235 *
236 * @param[in] key Key
237 *
238 * @return LLSD value of key if key exists, null LLSD if key does not exist.
239 */
240LLSD LLPluginMessage::getValueLLSD(const std::string &key) const
241{
242	LLSD result;
243
244	if(mMessage["params"].has(key))
245	{
246		result = mMessage["params"][key];
247	}
248	
249	return result;
250}
251
252/**
253 *	Gets the value of a key as signed 32-bit int. If the key does not exist, 0 will be returned.
254 *
255 * @param[in] key Key
256 *
257 * @return Signed 32-bit int value of key if key exists, 0 if key does not exist.
258 */
259S32 LLPluginMessage::getValueS32(const std::string &key) const
260{
261	S32 result = 0;
262
263	if(mMessage["params"].has(key))
264	{
265		result = mMessage["params"][key].asInteger();
266	}
267	
268	return result;
269}
270
271/**
272 *	Gets the value of a key as unsigned 32-bit int. If the key does not exist, 0 will be returned.
273 *
274 * @param[in] key Key
275 *
276 * @return Unsigned 32-bit int value of key if key exists, 0 if key does not exist.
277 */
278U32 LLPluginMessage::getValueU32(const std::string &key) const
279{
280	U32 result = 0;
281
282	if(mMessage["params"].has(key))
283	{
284		std::string value = mMessage["params"][key].asString();
285		
286		result = (U32)strtoul(value.c_str(), NULL, 16);
287	}
288	
289	return result;
290}
291
292/**
293 *	Gets the value of a key as a bool. If the key does not exist, false will be returned.
294 *
295 * @param[in] key Key
296 *
297 * @return Boolean value of key if it exists, false otherwise.
298 */
299bool LLPluginMessage::getValueBoolean(const std::string &key) const
300{
301	bool result = false;
302
303	if(mMessage["params"].has(key))
304	{
305		result = mMessage["params"][key].asBoolean();
306	}
307	
308	return result;
309}
310
311/**
312 *	Gets the value of a key as a double. If the key does not exist, 0 will be returned.
313 *
314 * @param[in] key Key
315 *
316 * @return Value as a double if key exists, 0 otherwise.
317 */
318F64 LLPluginMessage::getValueReal(const std::string &key) const
319{
320	F64 result = 0.0f;
321
322	if(mMessage["params"].has(key))
323	{
324		result = mMessage["params"][key].asReal();
325	}
326	
327	return result;
328}
329
330/**
331 *	Gets the value of a key as a pointer. If the key does not exist, NULL will be returned.
332 *
333 * @param[in] key Key
334 *
335 * @return Pointer value if key exists, NULL otherwise.
336 */
337void* LLPluginMessage::getValuePointer(const std::string &key) const
338{
339	void* result = NULL;
340
341	if(mMessage["params"].has(key))
342	{
343		std::string value = mMessage["params"][key].asString();
344		
345		result = (void*)llstrtou64(value.c_str(), NULL, 16);
346	}
347	
348	return result;
349}
350
351/**
352 *	Flatten the message into a string.
353 *
354 * @return Message as a string.
355 */
356std::string LLPluginMessage::generate(void) const
357{
358	std::ostringstream result;
359	
360	// Pretty XML may be slightly easier to deal with while debugging...
361//	LLSDSerialize::toXML(mMessage, result);
362	LLSDSerialize::toPrettyXML(mMessage, result);
363	
364	return result.str();
365}
366
367/**
368 *	Parse an incoming message into component parts. Clears all existing state before starting the parse.
369 *
370 * @return Returns -1 on failure, otherwise returns the number of key/value pairs in the incoming message.
371 */
372int LLPluginMessage::parse(const std::string &message)
373{
374	// clear any previous state
375	clear();
376
377	std::istringstream input(message);
378	
379	S32 parse_result = LLSDSerialize::fromXML(mMessage, input);
380	
381	return (int)parse_result;
382}
383
384
385/**
386 * Destructor
387 */
388LLPluginMessageListener::~LLPluginMessageListener()
389{
390	// TODO: should listeners have a way to ensure they're removed from dispatcher lists when deleted?
391}
392
393
394/**
395 * Destructor
396 */
397LLPluginMessageDispatcher::~LLPluginMessageDispatcher()
398{
399	
400}
401	
402/**
403 * Add a message listener. TODO:DOC need more info on what uses this. when are multiple listeners needed?
404 *
405 * @param[in] listener Message listener
406 */
407void LLPluginMessageDispatcher::addPluginMessageListener(LLPluginMessageListener *listener)
408{
409	mListeners.insert(listener);
410}
411
412/**
413 * Remove a message listener.
414 *
415 * @param[in] listener Message listener
416 */
417void LLPluginMessageDispatcher::removePluginMessageListener(LLPluginMessageListener *listener)
418{
419	mListeners.erase(listener);
420}
421
422/**
423 * Distribute a message to all message listeners.
424 *
425 * @param[in] message Message
426 */
427void LLPluginMessageDispatcher::dispatchPluginMessage(const LLPluginMessage &message)
428{
429	for (listener_set_t::iterator it = mListeners.begin();
430		it != mListeners.end();
431		)
432	{
433		LLPluginMessageListener* listener = *it;
434		listener->receivePluginMessage(message);
435		// In case something deleted an entry.
436		it = mListeners.upper_bound(listener);
437	}
438}