summaryrefslogtreecommitdiff
path: root/indra/llmessage/llexperiencecache.cpp
blob: 0d8f76c7e287afc9afefa5f8157dc66739529c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/** 
 * @file llexperiencecache.cpp
 * @brief llexperiencecache and related class definitions
 *
 * $LicenseInfo:firstyear=2012&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "llavatarname.h"
#include "llframetimer.h"
#include "llhttpclient.h"
#include "llsdserialize.h"
#include <set>
#include <map>

#include "llexperiencecache.h"



namespace LLExperienceCache
{
	bool sRunning = false;
	std::string sLookupURL;

	typedef std::set<LLUUID> ask_queue_t;
	ask_queue_t sAskQueue;

	typedef std::map<LLUUID, F64> pending_queue_t;
	pending_queue_t sPendingQueue;


	cache_t sCache;

	LLFrameTimer sRequestTimer;



	void processExperience( const LLUUID& agent_id, const LLExperienceData& experience, bool add_to_cache ) 
	{
		if(add_to_cache)
		{
			sCache[agent_id]=experience;

			sPendingQueue.erase(agent_id);


			//signal
		}
	}

	void initClass( bool running )
	{
		sRunning = false;
	}

	const cache_t& getCached()
	{
		return sCache;
	}



	void importFile(std::istream& istr)
	{
		LLSD data;
		S32 parse_count = LLSDSerialize::fromXMLDocument(data, istr);
		if(parse_count < 1) return;

		LLSD agents = data["agents"];

		LLUUID agent_id;
		LLExperienceData experience;
		LLSD::map_const_iterator it = agents.beginMap();
		for(; it != agents.endMap() ; ++it)
		{
			agent_id.set(it->first);
			experience.fromLLSD( it->second);
			sCache[agent_id]=experience;
		}

		LL_INFOS("ExperienceCache") << "loaded " << sCache.size() << LL_ENDL;
	}

	void exportFile(std::ostream& ostr)
	{
		LLSD agents;

		cache_t::const_iterator it =sCache.begin();
		for( ; it != sCache.end() ; ++it)
		{
			agents[it->first.asString()] = it->second.asLLSD();
		}

		LLSD data;
		data["agents"] = agents;

		LLSDSerialize::toPrettyXML(data, ostr);
	}

	class LLExperienceResponder : public LLHTTPClient::Responder
	{
	public:
		LLExperienceResponder(const std::vector<LLUUID>& agent_ids)
			:mAgentIds(agent_ids)
		{

		}

		virtual void completedHeader(U32 status, const std::string& reason, const LLSD& content)
		{
			mHeaders = content;
		}

		virtual void result(const LLSD& content)
		{
			LLSD agents = content["agents"];
			LLSD::array_const_iterator it = agents.beginArray();
			for( /**/ ; it != agents.endArray(); ++it)
			{
				const LLSD& row = *it;
				LLUUID agent_id = row["id"].asUUID();

				LLExperienceData experience;

				if(experience.fromLLSD(row))
				{
					LL_DEBUGS("ExperienceCache") << __FUNCTION__ << "Received result for " << agent_id 
						<< "display '" << experience.mDisplayName << "'" << LL_ENDL ;

					processExperience(agent_id, experience, true);
				}
			}

			LLSD unresolved_agents = content["bad_ids"];
			S32 num_unresolved = unresolved_agents.size();
			if(num_unresolved > 0)
			{
				LL_DEBUGS("ExperienceCache") << __FUNCTION__ << "Ignoreing " << num_unresolved 
					<< " bad ids" << LL_ENDL ;
			}

			LL_DEBUGS("ExperienceCache") << __FUNCTION__ << sCache.size() << " cached experiences" << LL_ENDL;
		}

	private:
		std::vector<LLUUID> mAgentIds;
		LLSD mHeaders;
	};

	void requestExperiences() 
	{
		if(sAskQueue.empty())
			return;

		F64 now = LLFrameTimer::getTotalSeconds();

		const U32 NAME_URL_MAX = 4096;
		const U32 NAME_URL_SEND_THRESHOLD = 3000;

		std::string url;
		url.reserve(NAME_URL_MAX);

		std::vector<LLUUID> agent_ids;
		agent_ids.reserve(128);

		url += sLookupURL;

		std::string arg="?ids=";

		for(ask_queue_t::const_iterator it = sAskQueue.begin(); it != sAskQueue.end() ; ++it)
		{
			const LLUUID& agent_id = *it;
		
			url += arg;
			url += agent_id.asString();
			agent_ids.push_back(agent_id);

			sPendingQueue[agent_id] = now;

			arg[0]='&';

			if(url.size() > NAME_URL_SEND_THRESHOLD)
			{
				 LLHTTPClient::get(url, new LLExperienceResponder(agent_ids));
				 url = sLookupURL;
				 arg[0]='?';
				 agent_ids.clear();
			}
		}

		if(url.size() > sLookupURL.size())
		{
			LLHTTPClient::get(url, new LLExperienceResponder(agent_ids));
		}

		sAskQueue.clear();
	}

	bool isRequestPending(const LLUUID& agent_id)
	{
		bool isPending = false;
		const F64 PENDING_TIMEOUT_SECS = 5.0 * 60.0;

		pending_queue_t::const_iterator it = sPendingQueue.find(agent_id);

		if(it != sPendingQueue.end())
		{
			F64 expire_time = LLFrameTimer::getTotalSeconds() - PENDING_TIMEOUT_SECS;
			isPending = (it->second > expire_time);
		}

		return isPending;
	}


	void setLookupURL( const std::string& lookup_url )
	{
		sLookupURL = lookup_url;
	}

	bool hasLookupURL()
	{
		return !sLookupURL.empty();
	}

	void idle()
	{
		sRunning = true;

		if(!sAskQueue.empty())
		{
			requestExperiences();
		}
	}

	void erase( const LLUUID& agent_id )
	{
		sCache.erase(agent_id);
	}

	void fetch( const LLUUID& agent_id ) 
	{
		LL_DEBUGS("ExperienceCache") << __FUNCTION__ << "queue request for agent" << agent_id << LL_ENDL ;
		sAskQueue.insert(agent_id);
	}

	void insert( const LLUUID& agent_id, const LLExperienceData& experience_data )
	{
		sCache[agent_id]=experience_data;
	}

	bool get( const LLUUID& agent_id, LLExperienceData* experience_data )
	{
		if(sRunning) 
		{

			cache_t::const_iterator it = sCache.find(agent_id);
			if (it != sCache.end())
			{
				llassert(experience_data);
				*experience_data = it->second;
				return true;
			}
		}
		
		if(!isRequestPending(agent_id))
		{
			fetch(agent_id);
		}

		return false;
	}


}

bool LLExperienceData::fromLLSD( const LLSD& sd )
{
	mDisplayName = sd["display_name"].asString();
	mDescription = sd["username"].asString();

	if(mDisplayName.empty() || mDescription.empty()) return false;

	mDescription += " % Hey, this is a description!";
	return true;
}

LLSD LLExperienceData::asLLSD() const
{
	LLSD sd;
	sd["display_name"] = mDisplayName;
	sd["username"] = mDescription.substr(0, llmin(mDescription.size(),mDescription.find(" %")));
	return sd;
}