/** * @file llviewercommunication.cpp * @brief LLViewerCommunication definition * @author Dale Glass * * Copyright (c) 2005-2007, Linden Research, Inc. * * Second Life Viewer Source Code * The source code in this file ("Source Code") is provided by Linden Lab * to you under the terms of the GNU General Public License, version 2.0 * ("GPL"), unless you have obtained a separate licensing agreement * ("Other License"), formally executed by you and Linden Lab. Terms of * the GPL can be found in doc/GPL-license.txt in this distribution, or * online at http://secondlife.com/developers/opensource/gplv2 * * There are special exceptions to the terms and conditions of the GPL as * it is applied to this Source Code. View the full text of the exception * in the file doc/FLOSS-exception.txt in this software distribution, or * online at http://secondlife.com/developers/opensource/flossexception * * By copying, modifying or distributing this software, you acknowledge * that you have read and understood your obligations described above, * and agree to abide by those obligations. * * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, * COMPLETENESS OR PERFORMANCE. */ #ifndef LL_LLVIEWERCOMMUNICATION_H #define LL_LLVIEWERCOMMUNICATION_H #include /** * @brief Viewer/LSL script communication circuit * This object describes a connection between the viewer and an object. * * When an object sends a message to the viewer, the LLViewerCommunication class * calls the registered callback, and passes a reference to this object so that * it can reply to the request. */ class LLViewerCircuit { public: LLViewerCircuit(U32 channel) : mChannel(channel) {}; LLViewerCircuit() : mChannel(0) {} /** * @brief Returns the chat channel the object requested */ U32 getChannel() const { return mChannel; } /** * @brief Sends a reply to the object that made the request * @param data Text to send */ void sendReply(const LLString& data); protected: U32 mChannel; }; typedef void(*viewer_extension_callback_t)(LLString& data, LLViewerCircuit& circuit, void *user_data); /** * @brief Viewer extension class * Describes a Second Life viewer extension */ class LLViewerExtension { public: /** * @brief Creates a LLViewerExtension * @param name Extension name * @param version Extension's version * @param callback Callback to call when a script tries to communicate with this extension * @param user_data arbitrary data to pass to the callback * @param author Extension's author * @param description Extension's description */ LLViewerExtension(const LLString& name = "", const U32 version = 0, viewer_extension_callback_t callback = NULL, void *user_data = NULL, const LLString& author = "", const LLString& description = "") : mName(name), mAuthor(author), mDescription(description), mVersion(version), mCallback(callback), mUserData(user_data) {}; LLString asString() const { return mName; } LLString getName() const { return mName; } LLString getAuthor() const { return mAuthor; } LLString getDescription() const { return mDescription; } U32 getVersion() const { return mVersion; } /** * @brief Notifies the extension that a message has arrived * @param data String the object sent to the extension * @param circuit Circuit between viewer and object. Used to reply to the request. */ void notify(LLString& data, LLViewerCircuit& circuit) { mCallback(data, circuit, mUserData); } protected: LLString mName; LLString mAuthor; LLString mDescription; U32 mVersion; viewer_extension_callback_t mCallback; void *mUserData; }; /** * @brief Implements viewer/object communication * This object manages the process by registering extensions and handling * parsing of the protocol */ class LLViewerCommunication { public: LLViewerCommunication(); /** * @brief Parses a chat message and extracts protocol data from it if present * @param text Chat message * @param speaker Speaker's key * @returns TRUE if the message was a protocol message, FALSE otherwise. */ bool parse(const LLString &text, const LLUUID& speaker); bool parse(char *text, const LLUUID& speaker) { LLString tmp = text; return parse(tmp, speaker); } void registerExtension(LLViewerExtension ext) { mExtensions[ext.getName()] = ext; } protected: std::map mExtensions; std::map mCircuits; }; extern LLViewerCommunication *gViewerCommunication; #endif