lltrustnet.cpp

Go to the documentation of this file.
00001 
00029 #include "llviewerprecompiledheaders.h"
00030 #include "lltrustnet.h"
00031 #include "llcachename.h"
00032 
00033 #include <iostream>
00034 
00035 LLTrustNet *gTrustNet = NULL;
00036 
00037 const LLString DELIMITER = "|";
00038 
00039 
00040 LLString LLTrustNetScoreRequest::asString()
00041 {
00042         llinfos << "Requesting id " << mID << "; name " << mName << "; type " << mType << llendl;
00043         return "GetScore|" + mName + "|" +  mID.asString() + "|" + mType;
00044 }
00045 
00046 LLString LLTrustNetRateRequest::asString()
00047 {
00048         std::ostringstream o; 
00049         o << mScore;
00050 
00051         llinfos << "Sending score for id " << mID << "; name " << mName << "; type " << mType << llendl;
00052         
00053         return "Rate|" + mName + "|" +  mID.asString() + "|" + mType + "|" + o.str() + "|" + mComment;
00054 }
00055 
00056 LLTrustNetScore::LLTrustNetScore(F32 score, LLString comments)
00057 {
00058         mOperation = TN_GET_SCORE;
00059         mScore = score;
00060 
00061         set(score,comments);
00062 
00063         if ( mScore != 0.0f )
00064         {
00065                 setState(TNS_RETRIEVED);
00066         }
00067         else
00068         {
00069                 setState(TNS_UNKNOWN);
00070         }
00071 }
00072 
00073 void LLTrustNetDatum::addCallback(trustnet_callback_t callback, void *user_data)
00074 {
00075         LLTrustNetCallbackEntry ent(callback, user_data);
00076         mCallbacks.push( ent );
00077 }
00078 
00079 void LLTrustNetDatum::notify(ETrustNetResult result)
00080 {
00081         LLUUID id = LLUUID::null;
00082 
00083         // If there's a parent we get the ID from it. There may not be one, such
00084         // as for data not associated with any particular avatar, such as log
00085         // entries and statistics.
00086         if ( mParent )
00087         {
00088                 id = mParent->getAvatar();
00089         }
00090 
00091         while( ! mCallbacks.empty() )
00092         {
00093                 LLTrustNetCallbackEntry *ent = &mCallbacks.front();
00094                 ent->callback(mOperation, result, id, ent->user_data);
00095 
00096                 mCallbacks.pop();
00097         }
00098 }
00099 
00100 static std::vector<LLString> tokenize(LLString text, U32 max_tokens = 0)
00101 {
00102         U32 count = 0;
00103 
00104         std::vector<LLString> tokens;
00105 
00106         LLString::size_type last_pos = text.find_first_not_of(DELIMITER, 0);
00107         LLString::size_type pos      = text.find_first_of(DELIMITER, last_pos);
00108 
00109         while(LLString::npos != pos && LLString::npos != last_pos && !(max_tokens > 0 && count >= max_tokens))
00110         {
00111                 llinfos << "token: " << text.substr(last_pos, pos - last_pos) << llendl;
00112                 tokens.push_back(text.substr(last_pos, pos - last_pos));
00113 
00114                 last_pos = text.find_first_not_of(DELIMITER, pos);
00115                 pos      = text.find_first_of(DELIMITER, last_pos);
00116 
00117                 count++;
00118         }
00119 
00120         if (LLString::npos != last_pos)
00121         {
00122                 // We stopped due to max token count, but there's still more data
00123                 // add it all as the last token
00124                 llinfos << "extra token: " << text.substr(last_pos) << llendl;
00125                 tokens.push_back(text.substr(last_pos));
00126         }
00127         lldebugs << "Got " << count << " tokens" << llendl;
00128 
00129         return tokens;
00130 }
00131 
00132 //static 
00133 void LLTrustNet::viewerCommCallback(LLString& data, LLViewerCircuit& circuit, void* userdata)
00134 {
00135         llinfos << "I got: " << data << llendl;
00136         std::vector<LLString> tokens = tokenize(data);
00137 
00138         if (tokens.size() < 1) return;
00139 
00140         gTrustNet->mCircuit = &circuit;
00141 
00142         if ( tokens[0] == "Init" )
00143         {
00144                 gTrustNet->mConnectionState = TRUSTNET_NO_CONNECTION;
00145         }
00146         else if ( tokens[0] == "LoginInProgress" )
00147         {
00148                 gTrustNet->mConnectionState = TRUSTNET_LOGGING_IN;
00149         }
00150         else if ( tokens[0] == "AdapterRunning" )
00151         {
00152                 gTrustNet->mConnectionState = TRUSTNET_CONNECTED;       
00153         }
00154         else if ( tokens[0] == "Score" && tokens.size() >= 4 )
00155         {
00156                 LLUUID avatar(tokens[1]);
00157                 LLString type = tokens[2];
00158                 F32 score = 0.0;
00159 
00160                 std::istringstream i(tokens[3]);
00161                 i >> score;
00162 
00163                 gTrustNet->setScore(avatar, type, score);
00164         }
00165         else
00166         {
00167                 llwarns << "Unknown command " << tokens[0] << llendl;
00168         }
00169 }
00170 
00171 LLTrustNet::LLTrustNet()
00172 {
00173         mCircuit = NULL;
00174         mConnectionState = TRUSTNET_NO_ADAPTER;
00175 
00176         // Register callback
00177         gViewerCommunication->registerExtension(
00178                 LLViewerExtension("DaleGlass.TrustNet", 1, viewerCommCallback, NULL,
00179                 "Dale Glass", "TrustNet Reputation System") );
00180 }
00181 
00182 LLTrustNetScore LLTrustNet::getScoreData(const LLUUID& avatar, const LLString& type)
00183 {
00184 
00185         // First check whether we already have it. If these map elements
00186         // don't exist, they'll be automatically created. The constructor
00187         // ensures they'll be created in an empty state that we use as
00188         // a placeholder for when it arrives.
00189         mCache[avatar].setAvatar(avatar);
00190         LLTrustNetScore score = mCache[avatar].mScores[type];
00191         mCache[avatar].mScores[type].setParent(&mCache[avatar]);
00192 
00193 
00194 
00195         ETrustNetDatumState st = score.getState();
00196 
00197         if ( st != TNS_RETRIEVED && st != TNS_FETCHING )
00198         {
00199                 LLString name;
00200                 char first[DB_FIRST_NAME_BUF_SIZE];
00201                 char last[DB_LAST_NAME_BUF_SIZE];
00202         
00203                 if ( gCacheName->getName(avatar, first, last) )
00204                 {
00205                         name = first;
00206                         name += " ";
00207                         name += last;
00208 
00209                         //score.mState = SCORE_FETCHING;
00210                         mCache[avatar].mScores[type].setState(TNS_FETCHING);
00211 
00212                         LLTrustNetScoreRequest *req = new LLTrustNetScoreRequest(avatar, name, type);
00213                         mQueue.push_back(req);
00214 
00215                         //avdata.mScores[type] = score;
00216                 }
00217 
00218         }
00219 
00220         return score;
00221 }
00222 
00223 F32 LLTrustNet::getScore(const LLUUID& avatar, const LLString& type)
00224 {
00225         if ( NULL == mCircuit )
00226         {
00227                 llwarns << "Circuit not yet established" << llendl;
00228                 return 0.0f;
00229         }
00230 
00231         LLTrustNetScore score = getScoreData(avatar, type);
00232         return score.mScore;
00233 }
00234 
00235 ETrustNetDatumState LLTrustNet::getScoreState(const LLUUID& avatar, const LLString& type)
00236 {
00237         if ( NULL == mCircuit )
00238         {
00239                 llwarns << "Circuit not yet established" << llendl;
00240                 return TNS_UNKNOWN;
00241         }
00242 
00243         LLTrustNetScore score = getScoreData(avatar, type);
00244         return score.getState();
00245 }
00246 
00247 
00248 void LLTrustNet::setScore(const LLUUID& avatar, const LLString& type, F32 newscore)
00249 {
00250 
00251         mCache[avatar].mScores[type].set(newscore,"");
00252         //mCache[avatar].mScores[type].mState = SCORE_RETRIEVED;
00253 
00254 }
00255 
00256 LLColor4 LLTrustNet::getScoreColor(const LLUUID& avatar,const LLString& type)
00257 {
00258         F32 score = getScore(avatar, type);
00259         LLColor4 color;
00260         
00261         if ( score == 0.0f )
00262         {
00263                 // Neutral
00264                 color = LLColor4::grey;
00265         }
00266         else if ( score > 0.0f )
00267         {
00268                 if ( score > 10.0f )
00269                 {
00270                         // Self
00271                         color = LLColor4(0.0f, 1.0f, 1.0f);
00272                 }
00273                 else
00274                 {
00275                         // Friend
00276                         color = LLColor4(0.0f, score / 10, 0.0f);
00277                 }
00278         }
00279         else
00280         {
00281                 // Enemy
00282                 color = LLColor4(score / 10, 0.0f, 0.0f);
00283         }
00284 
00285         return color;
00286 }
00287 
00288 LLString LLTrustNet::getConnectionStateString() const
00289 {
00290         switch( mConnectionState )
00291         {
00292                 case TRUSTNET_NO_ADAPTER: return "Adapter not being worn";
00293                 case TRUSTNET_NO_CONNECTION: return "No connection";
00294                 case TRUSTNET_CONNECTING: return "Connecting...";
00295                 case TRUSTNET_LOGGING_IN: return "Logging in...";
00296                 case TRUSTNET_LOGIN_ERROR: return "Login error";
00297                 case TRUSTNET_CONNECTED: return "Connected";
00298         }
00299 
00300         return "<BUG>";
00301 }
00302 
00303 void LLTrustNet::processRequests()
00304 {
00305         //if ( mQueue.empty() ) return;
00306 
00307         while(!mQueue.empty())
00308         {
00309                 LLTrustNetRequest *req = mQueue.front();
00310                 LLString reqtext = req->asString();
00311                 if ( reqtext.empty() )
00312                 {
00313                         llwarns << "BUG!" << llendl;
00314                 }
00315                 else
00316                 {
00317                         llinfos << "Requesting: " << reqtext << llendl;
00318                         mCircuit->sendReply( reqtext );
00319                 }
00320                 mQueue.pop_front();
00321                 delete req;
00322         }
00323 }

Generated on Thu Jul 1 06:09:23 2010 for Second Life Viewer by  doxygen 1.4.7