summaryrefslogtreecommitdiff
path: root/indra/llmessage/llexperiencecache.h
blob: 937225a80af890e84b77dbea414be86f0dbc9b64 (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
/** 
 * @file llexperiencecache.h
 * @brief Caches information relating to experience keys
 *
 * $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$
 */



#ifndef LL_LLEXPERIENCECACHE_H
#define LL_LLEXPERIENCECACHE_H

#include "linden_common.h"
#include "llsingleton.h"
#include "llframetimer.h"
#include "llsd.h"
#include "llcorehttputil.h"
#include <boost/signals2.hpp>
#include <boost/function.hpp>

class LLSD;
class LLUUID;


class LLExperienceCache: public LLSingleton < LLExperienceCache >
{
    friend class LLSingleton < LLExperienceCache > ;

public:
    typedef boost::function<std::string(const std::string &)> CapabilityQuery_t;
    typedef boost::function<void(const LLSD &)> Callback_t;

    void cleanup();

    void erase(const LLUUID& key);
    bool fetch(const LLUUID& key, bool refresh = false);
    void insert(const LLSD& experience_data);
    const LLSD& get(const LLUUID& key);

    // If name information is in cache, callback will be called immediately.
    void get(const LLUUID& key, Callback_t slot);

    bool isRequestPending(const LLUUID& public_key);

    void setCapabilityQuery(CapabilityQuery_t queryfn);

    static const std::string NAME;			// "name"
    static const std::string EXPERIENCE_ID;	// "public_id"
    static const std::string AGENT_ID;      // "agent_id"
    static const std::string GROUP_ID;      // "group_id"
    static const std::string PROPERTIES;	// "properties"
    static const std::string EXPIRES;		// "expiration"  
    static const std::string DESCRIPTION;	// "description"
    static const std::string QUOTA;         // "quota"
    static const std::string MATURITY;      // "maturity"
    static const std::string METADATA;      // "extended_metadata"
    static const std::string SLURL;         // "slurl"

    static const std::string MISSING;       // "DoesNotExist"

    // should be in sync with experience-api/experiences/models.py
    static const int PROPERTY_INVALID;		// 1 << 0
    static const int PROPERTY_PRIVILEGED;	// 1 << 3
    static const int PROPERTY_GRID;			// 1 << 4
    static const int PROPERTY_PRIVATE;		// 1 << 5
    static const int PROPERTY_DISABLED;		// 1 << 6  
    static const int PROPERTY_SUSPENDED;	// 1 << 7

private:
    LLExperienceCache();
    virtual ~LLExperienceCache();

    virtual void initSingleton();


    // Callback types for get() 
    typedef boost::signals2::signal < void(const LLSD &) > callback_signal_t;
	typedef boost::shared_ptr<callback_signal_t> signal_ptr;
	// May have multiple callbacks for a single ID, which are
	// represented as multiple slots bound to the signal.
	// Avoid copying signals via pointers.
	typedef std::map<LLUUID, signal_ptr> signal_map_t;
	typedef std::map<LLUUID, LLSD> cache_t;
	
	typedef std::set<LLUUID> RequestQueue_t;
    typedef std::map<LLUUID, F64> PendingQueue_t;

	//--------------------------------------------
	static const std::string PRIVATE_KEY;	// "private_id"
	
	// default values
	static const F64 DEFAULT_EXPIRATION; 	// 600.0
	static const S32 DEFAULT_QUOTA; 		// 128 this is megabytes
	
//--------------------------------------------
    void processExperience(const LLUUID& public_key, const LLSD& experience);

//--------------------------------------------
	cache_t			mCache;
	signal_map_t	mSignalMap;	
	RequestQueue_t	mRequestQueue;
    PendingQueue_t  mPendingQueue;

    LLFrameTimer    mRequestTimer;
    LLFrameTimer    mEraseExpiredTimer;    // Periodically clean out expired entries from the cache
    CapabilityQuery_t mCapability;
    std::string     mCacheFileName;
    bool            mShutdown;

    void idleCoro();
	void eraseExpired();
    void requestExperiencesCoro(LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t &, std::string, RequestQueue_t);
    void requestExperiences();

	void setMaximumLookups(int maximumLookups);

    void bootstrap(const LLSD& legacyKeys, int initialExpiration);
    void exportFile(std::ostream& ostr) const;
    void importFile(std::istream& istr);

    // 
	const cache_t& getCached();

	// maps an experience private key to the experience id
	LLUUID getExperienceId(const LLUUID& private_key, bool null_if_not_found=false);

    //=====================================================================
    inline friend std::ostream &operator << (std::ostream &os, const LLExperienceCache &cache)
    {
        cache.exportFile(os);
        return os;
    }

    inline friend std::istream &operator >> (std::istream &is, LLExperienceCache &cache)
    {
        cache.importFile(is);
        return is;
    }
};

#endif // LL_LLEXPERIENCECACHE_H