/** * @file lltrustnet.cpp * @brief LLTrustNet 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_LLTRUSTNET_H #define LL_LLTRUSTNET_H #include #include "llviewercommunication.h" #include "llviewerprecompiledheaders.h" #include "lluuid.h" #include "lltimer.h" /** * @brief TrustNet operations */ typedef enum e_trustnet_operation { TN_UNKNOWN, TN_GET_SCORE, TN_EXPLAIN, TN_RATE } ETrustNetOperation; /** * @brief Operation result */ typedef enum e_trustnet_result { TN_OK, TN_TIMEOUT, TN_INTERNAL_ERROR, TN_ERROR } ETrustNetResult; /** * @brief Callback for a TrustNet request * Checking the operation, result and avatar arguments is only needed if you use the same callback function * for multiple requests. The callback will only be called for the request it was specified for. The callback will * only be called once for every time it's set. * @param operation Operation that was completed * @param result Result of the operation * @param avatar Avatar the request was for, if applies. LLUUID::null otherwise * @param user_data Pointer to whatever data was passed when the callback was registered */ typedef void(*trustnet_callback_t)(ETrustNetOperation operation, ETrustNetResult result, const LLUUID &avatar, void *user_data); class LLTrustNet; class LLTrustNetAvatarData; /** * @brief Callback entry */ class LLTrustNetCallbackEntry { public: LLTrustNetCallbackEntry(trustnet_callback_t cb = NULL, void *ud = NULL): callback(cb), user_data(ud) {} trustnet_callback_t callback; void *user_data; }; /** * @brief State of the connection to the TrustNet server */ typedef enum e_trustnet_state { TRUSTNET_NO_ADAPTER, TRUSTNET_NO_CONNECTION, TRUSTNET_CONNECTING, TRUSTNET_LOGGING_IN, TRUSTNET_LOGIN_ERROR, TRUSTNET_CONNECTED } ETrustNetState; /** * @brief State of a TrustNet score */ typedef enum e_trustnet_datum_state { TNS_UNKNOWN, TNS_FETCHING, TNS_RETRIEVED } ETrustNetDatumState; /** * @brief Represents a request to the TrustNet server * Requests need to be queued up and slowed down if needed to avoid overwhelming * the adapter script. Longer term this will be replaced with a direct connection * to the TrustNet server, but the queueing system will remain. */ class LLTrustNetRequest { public: LLTrustNetRequest() {} /** * @brief Returns a string that contains the raw data to send to the adapter object */ virtual LLString asString() { return ""; } virtual ~LLTrustNetRequest() {} }; /** * @brief TrustNet score request */ class LLTrustNetScoreRequest : public LLTrustNetRequest { public: LLTrustNetScoreRequest(const LLUUID &id, const LLString &name, const LLString &type = "behavior") : mID(id), mName(name), mType(type) {} virtual LLString asString(); private: LLUUID mID; LLString mName; LLString mType; }; /** * @brief TrustNet rating submission */ class LLTrustNetRateRequest : public LLTrustNetRequest { public: LLTrustNetRateRequest(const LLUUID &id, const LLString &name, const LLString &type = "behavior", U32 score = 0, const LLString &comment = "") : mID(id), mName(name), mType(type), mScore(score), mComment(comment) {} virtual LLString asString(); private: LLUUID mID; LLString mName; LLString mType; U32 mScore; LLString mComment; }; /** * @brief TrustNet explain request * This request is sent to ask the server to explain why somebody has the rating they do */ class LLTrustNetExplainRequest : public LLTrustNetRequest { public: LLTrustNetExplainRequest(const LLUUID &id, const LLString &name) : mID(id), mName(name) {} virtual LLString asString() { return "Explain|" + mName + "|" + mID.asString(); } private: LLUUID mID; LLString mName; }; /** * @brief Represents a piece of data retrieved from the TrustNet server */ class LLTrustNetDatum { public: LLTrustNetDatum() : mOperation(TN_UNKNOWN), mState(TNS_UNKNOWN), mParent(NULL) {} /** * @brief Adds a callback to call when the object changes */ void addCallback(trustnet_callback_t callback, void *user_data = NULL); /** * @brief Returns the datum's age. * Used for caching */ F32 getAge() const { return mTimer.getElapsedTimeF32(); } ETrustNetDatumState getState() const { return mState; } protected: friend class LLTrustNet; void setState(ETrustNetDatumState s) { mState = s; } /** * @brief Initializes internal data. called by derived objects * May be set to NULL if there's no parent * @param p Pointer to the parent LLTrustNetAvatarData object */ void setParent(const LLTrustNetAvatarData *p) { mParent = p; } protected: /** * @brief Calls all the registered callbacks * @param result Result of the operation */ void notify(ETrustNetResult result = TN_OK); /** * @brief Called by derived objects when the data changes */ void updated(ETrustNetResult state = TN_OK) { mTimer.start(); setState(TNS_RETRIEVED); notify(state); } ETrustNetOperation mOperation; private: std::queue mCallbacks; LLTimer mTimer; ETrustNetDatumState mState; /** * @brief Pointer to the parent LLTrustNetAvatarData object * Used to retrieve data about the avatar the information is associated to */ const LLTrustNetAvatarData *mParent; }; /** * @brief TrustNet score */ class LLTrustNetScore : public LLTrustNetDatum { public: LLTrustNetScore(const F32 score = 0.0f, const LLString comment = ""); /** * @brief Sets the score to a new value * @param score Score * @param comment Comment associated to it */ void set(F32 score, const LLString &comment = "") { mScore = score; mComments = comment; updated(); } F32 mScore; LLString mComments; private: }; /** * @brief Cached data about an avatar */ class LLTrustNetAvatarData { public: std::map mScores; void setAvatar(const LLUUID& av) { mAvatar = av;} LLUUID getAvatar() const { return mAvatar; } protected: LLUUID mAvatar; }; /** * @brief Interface to the TrustNet database */ class LLTrustNet { public: LLTrustNet(); /** * @brief Returns the score for the avatar * @param avatar Avatar * @param type Score type * @returns Score, 0.0 if unknown or doesn't have any */ F32 getScore(const LLUUID& avatar, const LLString& type); /** * @brief State of a score retrieval * @param avatar Avatar * @param type Score type * @returns State */ ETrustNetDatumState getScoreState(const LLUUID& avatar, const LLString& type); /** * @brief Returns the color corresponding to the avatar's score * This function calculates the color used to represent the avatar's score * @param avatar Avatar * @param type Score type * @returns Color */ LLColor4 getScoreColor(const LLUUID& avatar, const LLString& type); /** * @brief Processes queued requests */ void processRequests(); /** * @brief State of the TrustNet connection */ ETrustNetState getConnectionState() const { return mConnectionState; } /** * @brief State of the TrustNet connection as a string */ LLString getConnectionStateString() const; static void viewerCommCallback(LLString& data, LLViewerCircuit& circuit, void* userdata); protected: void setScore(const LLUUID &avatar, const LLString& type, F32 score); LLTrustNetScore getScoreData(const LLUUID& avatar, const LLString& type); LLViewerCircuit *mCircuit; std::map mCache; std::deque mQueue; ETrustNetState mConnectionState; }; extern LLTrustNet* gTrustNet; #endif