PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmessage/llareslistener.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 97 lines | 44 code | 6 blank | 47 comment | 2 complexity | 5479e5b3753947e31451b55383e32af2 MD5 | raw file
 1/**
 2 * @file   llareslistener.cpp
 3 * @author Nat Goodspeed
 4 * @date   2009-03-18
 5 * @brief  Implementation for llareslistener.
 6 * 
 7 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 8 * Second Life Viewer Source Code
 9 * Copyright (C) 2010, Linden Research, Inc.
10 * 
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation;
14 * version 2.1 of the License only.
15 * 
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * Lesser General Public License for more details.
20 * 
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24 * 
25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
26 * $/LicenseInfo$
27 */
28
29// Precompiled header
30#include "linden_common.h"
31// associated header
32#include "llareslistener.h"
33// STL headers
34// std headers
35// external library headers
36// other Linden headers
37#include "llares.h"
38#include "llerror.h"
39#include "llevents.h"
40#include "llsdutil.h"
41
42LLAresListener::LLAresListener(LLAres* llares):
43    LLEventAPI("LLAres",
44               "LLAres listener to request DNS operations"),
45    mAres(llares)
46{
47    // add() every method we want to be able to invoke via this event API.
48    // Optional last parameter validates expected LLSD request structure.
49    add("rewriteURI",
50        "Given [\"uri\"], return on [\"reply\"] an array of alternative URIs.\n"
51        "On failure, returns an array containing only the original URI, so\n"
52        "failure case can be processed like success case.",
53        &LLAresListener::rewriteURI,
54        LLSD().with("uri", LLSD()).with("reply", LLSD()));
55}
56
57/// This UriRewriteResponder subclass packages returned URIs as an LLSD
58/// array to send back to the requester.
59class UriRewriteResponder: public LLAres::UriRewriteResponder
60{
61public:
62    /**
63     * Specify the request, containing the event pump name on which to send
64     * the reply.
65     */
66    UriRewriteResponder(const LLSD& request):
67        mReqID(request),
68        mPumpName(request["reply"])
69    {}
70
71    /// Called by base class with results. This is called in both the
72    /// success and error cases. On error, the calling logic passes the
73    /// original URI.
74    virtual void rewriteResult(const std::vector<std::string>& uris)
75    {
76        LLSD result;
77        for (std::vector<std::string>::const_iterator ui(uris.begin()), uend(uris.end());
78             ui != uend; ++ui)
79        {
80            result.append(*ui);
81        }
82        // This call knows enough to avoid trying to insert a map key into an
83        // LLSD array. It's there so that if, for any reason, we ever decide
84        // to change the response from array to map, it will Just Start Working.
85        mReqID.stamp(result);
86        LLEventPumps::instance().obtain(mPumpName).post(result);
87    }
88
89private:
90    LLReqID mReqID;
91    const std::string mPumpName;
92};
93
94void LLAresListener::rewriteURI(const LLSD& data)
95{
96    mAres->rewriteURI(data["uri"], new UriRewriteResponder(data));
97}