Files
SingularityViewer/indra/llmessage/llsdmessage.h
Aleric Inglewood 2a88f7d7c4 ResponderAdapter stuff
Renamed AICurlInterface::Responder to AICurlInterface::ResponderBase,
but without the virtual 'event' methods.
Derived from that: Responder and ReponderWithCompleted, where the
first defines result = 0, ErrorWithContent and error, and the latter
completedRaw and completed.
Added HttpClient::IgnoreBody, derived from Responder and implementing
'result' doing nothing; HttpClient::Ignore is now derived from
IgnoreBody and defines the still pure virtual getHTTPTimeoutPolicy.

Added ResponderBase::decode_body, which is now the sole place
where the code makes the decision wether some response data might be
LLSD or not based on the http status result. Before it just tried
to decode everything as LLSD, which seems a bit nonsense.

ResponderWithCompleted::completed no longer does anything, since
classes derived from ResponderWithCompleted are expected to override it,
or never call it by overriding completedRaw.

Entry point is now ResponderBase::finished = 0, instead of
completedRaw, where ResponderWithCompleted implements finished by
called completedRaw, but Responder doesn't: that directly calls
result/errorWithContent/error. Or, for the hack ResponderAdapter,
the entry points are pubResult/pubErrorWithContent.
Those are now the ONLY public methods, so more confusion.
mFinished is now set in all cases.

As a result of all that, it is no longer possible to accidently
pass a responder to ResponderAdapter that would break because it
expects completed() and completedRaw() to be called.

Added LLBufferArray::writeChannelTo.

Fixed bug for BlockingResponder::body (returned reference to temporary).

LLSDMessage::ResponderAdapter now allows a "timeoutpolicy" name
to be passed (not doing so results in the default timings), so
that the timeout policy of the used responder is retained.

Fixed llfasttimerview.cpp to test LLSDSerialize::fromXML() to return
a positive value instead of non-zero, because it may return -1 when the
parsing fails (three places).

Removed LLHTTPClient::Responder as base class from
LLFloaterRegionDebugConsole completely: it isn't a responder!

Several other responder classes were simplified a bit in order to
compile again with the above changes.
2012-10-26 04:13:29 +02:00

169 lines
6.7 KiB
C++

/**
* @file llsdmessage.h
* @author Nat Goodspeed
* @date 2008-10-30
* @brief API intended to unify sending capability, UDP and TCP messages:
* https://wiki.lindenlab.com/wiki/Viewer:Messaging/Messaging_Notes
*
* $LicenseInfo:firstyear=2008&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, 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$
*/
#if ! defined(LL_LLSDMESSAGE_H)
#define LL_LLSDMESSAGE_H
#include "llerror.h" // LOG_CLASS()
#include "llevents.h" // LLEventPumps
#include "llhttpclient.h"
#include <string>
#include <stdexcept>
class LLSD;
class AIHTTPTimeoutPolicy;
extern AIHTTPTimeoutPolicy eventResponder_timeout;
/**
* Class managing the messaging API described in
* https://wiki.lindenlab.com/wiki/Viewer:Messaging/Messaging_Notes
*/
class LLSDMessage
{
LOG_CLASS(LLSDMessage);
public:
LLSDMessage();
/// Exception if you specify arguments badly
struct ArgError: public std::runtime_error
{
ArgError(const std::string& what):
std::runtime_error(std::string("ArgError: ") + what) {}
};
/**
* The response idiom used by LLSDMessage -- LLEventPump names on which to
* post reply or error -- is designed for the case in which your
* reply/error handlers are methods on the same class as the method
* sending the message. Any state available to the sending method that
* must be visible to the reply/error methods can conveniently be stored
* on that class itself, if it's not already.
*
* The LLHTTPClient::Responder idiom requires a separate instance of a
* separate class so that it can dispatch to the code of interest by
* calling canonical virtual methods. Interesting state must be copied
* into that new object.
*
* With some trepidation, because existing response code is packaged in
* LLHTTPClient::Responder subclasses, we provide this adapter class
* <i>for transitional purposes only.</i> Instantiate a new heap
* ResponderAdapter with your new LLHTTPClient::ResponderPtr. Pass
* ResponderAdapter::getReplyName() and/or getErrorName() in your
* LLSDMessage (or LLViewerRegion::getCapAPI()) request event. The
* ResponderAdapter will call the appropriate Responder method, then
* @c delete itself.
*/
class ResponderAdapter
{
public:
/**
* Bind the new LLHTTPClient::Responder subclass instance.
*
* Passing the constructor a name other than the default is only
* interesting if you suspect some usage will lead to an exception or
* log message.
*/
ResponderAdapter(LLHTTPClient::Responder* responder,
const std::string& name="ResponderAdapter");
/// EventPump name on which LLSDMessage should post reply event
std::string getReplyName() const { return mReplyPump.getName(); }
/// EventPump name on which LLSDMessage should post error event
std::string getErrorName() const { return mErrorPump.getName(); }
/// Name of timeout policy to use.
std::string getTimeoutPolicyName() const;
private:
// We have two different LLEventStreams, though we route them both to
// the same listener, so that we can bind an extra flag identifying
// which case (reply or error) reached that listener.
bool listener(const LLSD&, bool success);
LLHTTPClient::ResponderPtr mResponder;
LLEventStream mReplyPump, mErrorPump;
};
/**
* Force our implementation file to be linked with caller. The .cpp file
* contains a static instance of this class, which must be linked into the
* executable to support the canonical listener. But since the primary
* interface to that static instance is via a named LLEventPump rather
* than by direct reference, the linker doesn't necessarily perceive the
* necessity to bring in the translation unit. Referencing this dummy
* method forces the issue.
*/
static void link();
private:
friend class LLCapabilityListener;
/// Responder used for internal purposes by LLSDMessage and
/// LLCapabilityListener. Others should use higher-level APIs.
class EventResponder: public LLHTTPClient::Responder
{
public:
virtual AIHTTPTimeoutPolicy const& getHTTPTimeoutPolicy(void) const { return *mHTTPTimeoutPolicy; }
/**
* LLHTTPClient::Responder that dispatches via named LLEventPump instances.
* We bind LLEventPumps, even though it's an LLSingleton, for testability.
* We bind the string names of the desired LLEventPump instances rather
* than actually obtain()ing them so we only obtain() the one we're going
* to use. If the caller doesn't bother to listen() on it, the other pump
* may never materialize at all.
* @a target and @a message are only to clarify error processing.
* For a capability message, @a target should be the region description,
* @a message should be the capability name.
* For a service with a visible URL, pass the URL as @a target and the HTTP verb
* (e.g. "POST") as @a message.
*/
EventResponder(LLEventPumps& pumps,
const LLSD& request,
const std::string& target, const std::string& message,
const std::string& replyPump, const std::string& errorPump);
void setTimeoutPolicy(std::string const& name);
virtual void result(const LLSD& data);
virtual void errorWithContent(U32 status, const std::string& reason, const LLSD& content);
private:
LLEventPumps& mPumps;
LLReqID mReqID;
const std::string mTarget, mMessage, mReplyPump, mErrorPump;
AIHTTPTimeoutPolicy const* mHTTPTimeoutPolicy;
};
private:
bool httpListener(const LLSD&);
LLEventStream mEventPump;
};
#endif /* ! defined(LL_LLSDMESSAGE_H) */