summaryrefslogtreecommitdiff
path: root/indra/llcommon/lleventdispatcher.h
AgeCommit message (Collapse)Author
2023-07-13DRTVWR-558: LLEventDispatcher uses LL::apply(), not boost::fusion.Nat Goodspeed
While calling a C++ function with arguments taken from a runtime-variable data structure necessarily involves a bit of hocus-pocus, the best you can say for the boost::fusion based implementation is that it worked. Sadly, template recursion limited its applicability to a handful of function arguments. Now that we have LL::apply(), use that instead. This implementation is much more straightforward. In particular, the LLSDArgsSource class, whose job was to dole out elements of an LLSD array one at a time for the template recursion, goes away entirely. Make virtual LLEventDispatcher::DispatchEntry::call() return LLSD instead of void. All LLEventDispatcher target functions so far have been void; any function that wants to respond to its invoker must do so explicitly by calling sendReply() or constructing an LLEventAPI::Response instance. Supporting non- void functions permits LLEventDispatcher to respond implicitly with the returned value. Of course this requires a wrapper for void target functions that returns LLSD::isUndefined(). Break out LLEventDispatcher::reply() from callFail(), so we can reply with success as well as failure. Make LLEventDispatcher::try_call_log() prepend the actual leaf class name and description to any error returned by three-arg try_call(). That try_call() overload reported "LLEventDispatcher(desc): " for a couple specific errors, but no others. Hoist to try_call_log() to apply uniformly. Introduce new try_call_one() method to diagnose name-not-found errors and catch internal DispatchError and LL::apply_error exceptions. try_call_one() returns a std::pair, containing either an error message or an LLSD value. Make try_call_log() and three-arg try_call() accept LLSD 'name' instead of plain std::string, allowing for the possibility of an array or map. That lets us extend three-arg try_call() to break out new cases for the function selector LLSD: isUndefined(), isArray(), isMap() and (current case) scalar String. If try_call_one() reports an error, log it and try to send reply, as now. If it returns LLSD::isUndefined(), e.g. from a void target function wrapper, do nothing. But if it returns an LLSD map, try to send that back to the invoker. And if it returns an LLSD scalar or array, wrap it in a map with key "data" to respond to the invoker. Allowing a target function to return its result rather than explicitly sending it opens the possibility of batched requests (aggregate 'name') returning batched responses. Almost every place that constructs LLEventDispatcher's internal DispatchError exception called stringize() to format the what() string. Simplify calls by making DispatchError accept variadic arguments and forward to stringize(). Add LL::invoke() to apply.h. Like LL::apply(), this is a (limited) C++14 foreshadowing of std::invoke(), with preprocessor conditionals to switch to std::invoke() when that's available. Introduce LL::invoke() to handle a callable that's actually a pointer to method. Now our C++14 apply() implementation can accept pointer to method, using invoke() to generalize the actual function call. Also anticipate std::bind_front() with LL::bind_front(). For apply(func, std::array) and our extensions apply(func, std::vector) and apply(func, LLSD), we can't pass a pointer to method as the func unless the second argument happens to be an array or vector of pointers (or references) to instances of exactly the right class -- and of course LLSD can't store such at all. It's tempting to pass std::bind(std::mem_fn(ptr_to_method), instance), but that won't work: std::bind() requires a value or placeholder for each argument to pass to the bound function. The bind() expression above would only work for a nullary method. std::bind_front() would work, but that doesn't arrive until C++20. Again, once we get there we'll defer to the std:: implementation. Instead of the generic __cplusplus, check the appropriate feature-test macro for availability of each of std::invoke(), std::apply() and std::bind_front(). Change apply() error handling from assert() to new LL::apply_error exception. LLEventDispatcher must be able to intercept apply() errors. Move validation and synthesis of the relevant error message to new apply.cpp source file. Add to llptrto.h new LL::get_ref() and LL::get_ptr() template functions to unify the cases of a calling template accepting either a pointer or a reference. Wrapping the parameter in either get_ref() or get_ptr() allows dereferencing the parameter as desired. Move LL::apply(function, LLSD) argument validation/manipulation to a non- template function in llsdutil.cpp: no need to replicate that logic in the template for every CALLABLE specialization. The trouble with passing bind_front(std::mem_fn(ptr_to_method), instance) to apply() is that since bind_front() accepts and forwards variadic additional arguments, apply() can't infer the arity of the bound ptr_to_method. Address that by introducing apply_n<arity>(function, LLSD), permitting a caller to infer the arity of ptr_to_method and explicitly pass it to apply_n(). Polish up lleventdispatcher_test.cpp accordingly. Wrong LLSD type and wrong number of arguments now produce different (somewhat more informative) error messages. Moreover, passing too many entries in an LLSD array used to work: the extra arguments used to be ignored. Now we require that the size of the array match the arity of the target function. Change the too-many-arguments tests from success testing to error testing. Replace 'foreach' aka BOOST_FOREACH macro invocations with range 'for'. Replace STRINGIZE(item0 << item1 << ...) with stringize(item0, item1, ...). (cherry picked from commit 9c049563b5480bb7e8ed87d9313822595b479c3b)
2023-07-13DRTVWR-558: Pull in LLEventDispatcher / LLDispatchListener fixes.Nat Goodspeed
For LLEventDispatcher::add(), use simpler std::enable_if construct that avoids the need to restate the whole conditional. Derive LLDispatchListener from LLEventStream, instead of containing an instance. This sets up for LazyEventAPI. Don't allow tweaking an LLDispatchListener (or subclass LLEventAPI) name. (cherry-picked from af4fbc1f8a9 on the lazy-eventpump branch) (cherry picked from commit 419e7a4230ae662b035ae771af8e7d8bceb2c8b1)
2023-07-13DRTVWR-558: Generalize LLEventDispatcher::add() constraints.Nat Goodspeed
Instead of checking whether an add() parameter is exactly LLSD or LLSDMap, check whether it's convertible to LLSD -- which handles those cases and more. (cherry picked from commit fa168c11f64771dadc5df86d14ca2f07eba3b8ba) (cherry picked from commit 6b5bfc1cf674fc568d86d7ed623fd7bb3ee2f646)
2023-07-13DRTVWR-558: LLEventAPI allows all LLEventDispatcher add() overloads.Nat Goodspeed
Previously, LLEventAPI intentionally hid all but one of the many add() overloads supported by its LLEventDispatcher base class. The reason was that certain of the add() methods take an optional fourth parameter that's an LLSD::Map describing the expected parameter structure, while others take a fourth templated parameter that's an instance getter callable. This led to ambiguity, especially when passed an LLSDMap instance that's convertible to LLSD but isn't literally LLSD. At the time, it was simpler to constrain the add() methods inherited from LLEventDispatcher. But by adding new std::enable_if constraints to certain LLEventDispatcher add() methods, we've resolved the ambiguities, so LLEventAPI subclasses can now use any add() overload (as claimed on the relevant Confluence page). LLEventDispatcher comments have always loftily claimed that an instance getter callable may return either a pointer or a reference, doesn't matter. But it does when trying to pass the getter's result to boost::fusion::push_back(): a reference must be wrapped with std::ref() while a pointer cannot be. std::ref(pointer) produces errors. Introduce LLEventDispatcher::invoker:: bindable() overloads to Do The Right Thing whether passed a pointer or a reference. (cherry picked from commit 743f487c2e123171c9fc6d5b84d768f1d856d569) (cherry picked from commit 8618e41b3489e321ecd70eb65ec4d9ca7e2f75c6)
2023-07-13DRTVWR-558: Change LLEventDispatcher error action (also LLEventAPI).Nat Goodspeed
Originally the LLEventAPI mechanism was primarily used for VITA testing. In that case it was okay for the viewer to crash with LL_ERRS if the test script passed a bad request. With puppetry, hopefully new LEAP scripts will be written to engage LLEventAPIs in all sorts of interesting ways. Change error handling from LL_ERRS to LL_WARNS. Furthermore, if the incoming request contains a "reply" key, send back an error response to the requester. Update lleventdispatcher_test.cpp accordingly. (cherry picked from commit de0539fcbe815ceec2041ecc9981e3adf59f2806) (cherry picked from commit 4b60941952e97691f11806062f4bc66dd5ac8dae)
2023-07-13DRTVWR-558: Tweak LLEventDispatcher.Nat Goodspeed
Instead of std::map<std::string, boost::shared_ptr>, use std::unique_ptr as the mapped_type, using emplace() to store new entries. This more correctly captures the desired semantics: we have no intention of passing around the pointers in the map, we just want the map to delete them on destruction. Use std::function instead of boost::function. (cherry picked from commit 7ba53ef82db5683756e296225f0c8b838420a26e)
2022-06-21DRTVWR-564: Per NickyD, need not test static_cast result for nullptr.Nat Goodspeed
2022-06-21DRTVWR-564: Fix LLEventDispatcher::addMethod() for LazyEventAPI.Nat Goodspeed
A classic LLEventAPI subclass calls LLEventDispatcher::add() methods in its own constructor. At that point, addMethod() can reliably dynamic_cast its 'this' pointer to the new subclass. But because of the way LazyEventAPI queues up add() calls, they're invoked in the (new) LLEventAPI constructor itself. The subclass constructor body hasn't even started running, and LLEventDispatcher::addMethod()'s dynamic_cast to the LLEventAPI subclass returns nullptr. addMethod() claims the new subclass isn't derived from LLEventDispatcher, which is confusing since it is. It works to change addMethod()'s dynamic_cast to static_cast. Flesh out lazyeventapi_test.cpp. post() maps with "op" keys to actually try to engage the registered operation. Give the operation an observable side effect; use ensure_mumble() to verify. Also verify that LazyEventAPI has captured the subject LLEventAPI's metadata in a way we can retrieve.
2022-06-18DRTVWR-564: WIP: Add LazyEventAPI and tests. Tests don't yet pass.Nat Goodspeed
LazyEventAPI is a registrar that implicitly instantiates some particular LLEventAPI subclass on demand: that is, when LLEventPumps::obtain() tries to find an LLEventPump by the registered name. This leverages the new LLEventPumps::registerPumpFactory() machinery. Fix registerPumpFactory() to adapt the passed PumpFactory to accept TypeFactory parameters (two of which it ignores). Supplement it with unregisterPumpFactory() to support LazyEventAPI instances with lifespans shorter than the process -- which may be mostly test programs, but still a hole worth closing. Similarly, add unregisterTypeFactory(). A LazyEventAPI subclass takes over responsibility for specifying the LLEventAPI's name, desc, field, plus whatever add() calls will be needed to register the LLEventAPI's operations. This is so we can (later) enhance LLLeapListener to consult LazyEventAPI instances for not-yet-instantiated LLEventAPI metadata, as well as enumerating existing LLEventAPI instances. The trickiest part of this is capturing calls to the various LLEventDispatcher::add() overloads in such a way that, when the LLEventAPI subclass is eventually instantiated, we can replay them in the new instance. LLEventAPI acquires a new protected constructor specifically for use by a subclass registered by a companion LazyEventAPI. It accepts a const reference to LazyEventAPIParams, intended to be opaque to the LLEventAPI subclass; the subclass must declare a constructor that accepts and forwards the parameter block to the new LLEventAPI constructor. The implementation delegates to the existing LLEventAPI constructor, plus it runs deferred add() calls. LLDispatchListener now derives from LLEventStream instead of containing it as a data member. The reason is that if LLEventPumps::obtain() implicitly instantiates it, LLEventPumps's destructor will try to destroy it by deleting the LLEventPump*. If the LLEventPump returned by the factory function is a data member of an outer class, that won't work so well. But if LLDispatchListener (and by implication, LLEventAPI and any subclass) is derived from LLEventPump, then the virtual destructor will Do The Right Thing. Change LLDispatchListener to *not* allow tweaking the LLEventPump name. Since the overwhelming use case for LLDispatchListener is LLEventAPI, accepting but silently renaming an LLEventAPI subclass would ensure nobody could reach it. Change LLEventDispatcher's use of std::enable_if to control the set of add() overloads available for the intended use cases. Apparently this formulation is just as functional at the method declaration point, while avoiding the need to restate the whole enable_if expression at the method definition point. Add lazyeventapi_test.cpp to exercise.
2022-06-15DRTVWR-558: Generalize LLEventDispatcher::add() constraints.Nat Goodspeed
Instead of checking whether an add() parameter is exactly LLSD or LLSDMap, check whether it's convertible to LLSD -- which handles those cases and more. (cherry picked from commit fa168c11f64771dadc5df86d14ca2f07eba3b8ba)
2022-06-15DRTVWR-558: LLEventAPI allows all LLEventDispatcher add() overloads.Nat Goodspeed
Previously, LLEventAPI intentionally hid all but one of the many add() overloads supported by its LLEventDispatcher base class. The reason was that certain of the add() methods take an optional fourth parameter that's an LLSD::Map describing the expected parameter structure, while others take a fourth templated parameter that's an instance getter callable. This led to ambiguity, especially when passed an LLSDMap instance that's convertible to LLSD but isn't literally LLSD. At the time, it was simpler to constrain the add() methods inherited from LLEventDispatcher. But by adding new std::enable_if constraints to certain LLEventDispatcher add() methods, we've resolved the ambiguities, so LLEventAPI subclasses can now use any add() overload (as claimed on the relevant Confluence page). LLEventDispatcher comments have always loftily claimed that an instance getter callable may return either a pointer or a reference, doesn't matter. But it does when trying to pass the getter's result to boost::fusion::push_back(): a reference must be wrapped with std::ref() while a pointer cannot be. std::ref(pointer) produces errors. Introduce LLEventDispatcher::invoker:: bindable() overloads to Do The Right Thing whether passed a pointer or a reference. (cherry picked from commit 743f487c2e123171c9fc6d5b84d768f1d856d569)
2022-06-15DRTVWR-558: Change LLEventDispatcher error action (also LLEventAPI).Nat Goodspeed
Originally the LLEventAPI mechanism was primarily used for VITA testing. In that case it was okay for the viewer to crash with LL_ERRS if the test script passed a bad request. With puppetry, hopefully new LEAP scripts will be written to engage LLEventAPIs in all sorts of interesting ways. Change error handling from LL_ERRS to LL_WARNS. Furthermore, if the incoming request contains a "reply" key, send back an error response to the requester. Update lleventdispatcher_test.cpp accordingly. (cherry picked from commit de0539fcbe815ceec2041ecc9981e3adf59f2806)
2017-02-23DRTVWR-418: Fix a round of compile errors surfaced by -std=c++11.Nat Goodspeed
These are mostly things that were in fact erroneous, but accepted by older compilers. This changeset has not yet been built with Visual Studio 2013 or Linux gcc, even with -std=c++11. This changeset has not been built *without* -std=c++11. It should be used in conjunction with a corresponding change to LL_BUILD_DARWIN_BASE_SWITCHES in viewer-build-variables/variables. This is a work in progress. We do not assert that this changeset completes the work needed to turn on -std=c++11, even on the Mac.
2015-11-10remove execute permission from many files that should not have itOz Linden
2013-03-29Update Mac and Windows breakpad builds to latestGraham Madarasz
2011-02-01Replace boost::ptr_map<name, etc> with std::map<name, shared_ptr>.Nat Goodspeed
On Windows, unlike on Mac or Linux, boost::ptr_map<> started insisting on this concept of clonability. In other words, it wants to own a unique instance of the pointee; if you copy a value_type -- even to dereference an iterator! -- it wants to construct a whole new instance of the mapped_type. That's nuts. A std::map<..., boost::shared_ptr<>> has the property I want (the mapped_type goes away when the entry is erased), plus it's willing to pass around the shared_ptr to the same instance of the mapped_type. This change also permits simplifying a couple awkward kludges I'd already had to make to accommodate ptr_map's idiosyncracies.
2011-01-31Resolve LLEventDispatcher::add(function(const LLSD&)) ambiguity.Nat Goodspeed
A free function or static method accepting(const LLSD&) was being intercepted by the free-function-with-arbitrary-parameters overload instead of the original Callable overload. Added an overload that specifically redirects that case. Documented limit of ~6 arbitrary parameters for directly-called functions (5 for methods). Beyond that many, you have to write a Callable wrapper function and unpack the parameters from the LLSD by hand.
2011-01-28Extend LLEventAPI to directly call other functions & methods.Nat Goodspeed
Until now, LLEventAPI has only been able to register functions specifically accepting(const LLSD&). Typically you add a wrapper method to your LLEventAPI subclass, register that, have it extract desired params from the incoming LLSD and then call the actual function of interest. With help from Alain, added new LLEventAPI::add() methods capable of registering functions/methods with arbitrary parameter signatures. The code uses boost::fusion magic to implicitly match incoming LLSD arguments to the function's formal parameter list, bypassing the need for an explicit helper method. New add() methods caused an ambiguity with a previous convenience overload. Removed that overload and fixed the one existing usage. Replaced LLEventDispatcher::get() with try_call() -- it's no longer easy to return a Callable for caller to call directly. But the one known use of that feature simply used it to avoid fatal LL_ERRS on unknown function-name string, hence the try_call() approach actually addresses that case more directly. Added indra/common/lleventdispatcher_test.cpp to exercise new functionality.
2010-08-13Change license from GPL to LGPL (version 2.1)Oz Linden
2009-12-22Add lleventhost queries to list LLEventAPIs or describe one.Nat Goodspeed
Add LLEventDispatcher::getDispatchKey() to retrieve a previously- inaccessible value.
2009-11-12Introduce LLEventDispatcher::begin()/end() to iterate over (name, desc) pairsNat Goodspeed
for all registered operations. (untested) Introduce LLEventDispatcher::getMetadata(name) query so you can discover, for a given named operation, its query string and required parameters. (untested) Introduce LLEventDispatcher::add() convenience methods allowing you to omit description strings. Fix LLLoginInstance (which uses a non-LLEventAPI LLEventDispatcher) back to description-less add() calls. However, filter LLEventDispatcher::add() methods inherited by LLEventAPI so that an LLEventAPI subclass *must* provide a description string.
2009-11-11Add LLEventAPI class, formalizing the mechanism by which we wrap a C++ APINat Goodspeed
with an event API. In addition to the LLEventPump name on which to listen, LLEventAPI accepts a documentation string for event API introspection. Give every LLEventDispatcher::add() overload a new documentation string parameter for event API introspection. Convert every existing event API to new conventions, introducing suitable documentation strings for the API and each of its operations.
2009-11-03mergeLoren Shih
--HG-- branch : avatar-pipeline
2009-10-09DEV-40930: Added ["change"] key to login-module status events. ChangedNat Goodspeed
existing event calls to use state as "offline" or "online", with "change" indicating the reason for this status event. Changed disconnect() to send state "offline", change "disconnect" -- instead of replaying last auth failure. Changed unit tests accordingly. Changed LLLoginInstance::handleLoginEvent() to use LLEventDispatcher to route calls to handleLoginFailure() et al. Added LLEventDispatcher::get() to allow retrieving Callable by name and testing for empty().
2009-07-01Merged in windows LL_COMMON_API dll linkage fixes.brad kittenbrink
2009-07-01Fixups for windows llcommon dll linkage errors that got dropped in the merge ↵brad kittenbrink
up to viewer-2.0.0-3
2009-07-01DEV-31980: provide a way to retrieve LLDispatchListener's tweaked ↵Nat Goodspeed
LLEventPump name
2009-06-24DEV-31980: remove cruft from lleventdispatcher.hNat Goodspeed
2009-06-24DEV-31980: Extend LLEventDispatcher to handle const as well as non-constNat Goodspeed
methods. Introduce LLAppViewerListener based on LLDispatchListener, instantiate a static one in llappviewer.cpp. Initial implementation only supports ["op"] == "requestQuit".
2009-06-19DEV-31980: extract dispatch-by-string-name logic from LLAresListener to newNat Goodspeed
LLEventDispatcher and LLDispatchListener classes. See LLAresListener for example usage.