llleap.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file llleap.h
  3. * @brief Declaration of the class implementing "LLSD Event API Plugin"
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLLEAP_H
  33. #define LL_LLLEAP_H
  34. #include <stdexcept>
  35. #include "llinstancetracker.h"
  36. // LLSD Event API Plugin class. Because it is managed by LLInstanceTracker, you
  37. // can instantiate LLLeap and forget the instance unless you need it later.
  38. // Each instance manages an LLProcess; when the child process terminates,
  39. // LLLeap deletes itself. We do not require an unique LLInstanceTracker key.
  40. //
  41. // The fact that a given LLLeap instance vanishes when its child process
  42. // terminates makes it problematic to store an LLLeap* anywhere. Any stored
  43. // LLLeap* pointer should be validated before use by LLLeap::getNamedInstance()
  44. // (see LLInstanceTracker).
  45. class LLLeap : public LLInstanceTracker<LLLeap>
  46. {
  47. public:
  48. // Pass a brief string description, mostly for logging purposes. The desc
  49. // need not be unique, but obviously the clearer we can make it, the
  50. // easier these things will be to debug. The strings are the command line
  51. // used to launch the desired plugin process.
  52. //
  53. // Pass exc = false to suppress LLLeap::Error exception. Obviously in that
  54. // case the caller cannot discover the nature of the error, merely that an
  55. // error of some kind occurred (because create() returned NULL). Either
  56. // way, the error is logged.
  57. static LLLeap* create(const std::string& desc,
  58. const std::vector<std::string>& plugin,
  59. bool exc = true);
  60. // Pass a brief string description, mostly for logging purposes. The desc
  61. // need not be unique, but obviously the clearer we can make it, the
  62. // easier these things will be to debug. Pass a command-line string
  63. // to launch the desired plugin process.
  64. //
  65. // Pass exc = false to suppress LLLeap::Error exception. Obviously in that
  66. // case the caller cannot discover the nature of the error, merely that an
  67. // error of some kind occurred (because create() returned NULL). Either
  68. // way, the error is logged.
  69. static LLLeap* create(const std::string& desc, const std::string& plugin,
  70. bool exc = true);
  71. // Pass an LLSD map to specify desc, executable, args et al.
  72. //
  73. // Pass exc = false to suppress LLLeap::Error exception. Obviously in that
  74. // case the caller cannot discover the nature of the error, merely that an
  75. // error of some kind occurred (because create() returned NULL). Either
  76. // way, the error is logged.
  77. //
  78. // Note (HB): to avoid implementing the LLInitParam class templates (which
  79. // we do not use at all in the Cool VL Viewer, since they were originally
  80. // only used by the v2 UI of LL's viewer), I re-implemented this method to
  81. // accept only a LLSD::Map containing the following fields:
  82. // - params["desc"] : optional LLSD::String. The description of this LEAP.
  83. // - params["executable"] : mandatory LLSD::String. The file name for the
  84. // program or script to run.
  85. // - params["args"] : optional LLSD::Array. The command line options and
  86. // arguments to pass to the "executable" process.
  87. // - params["cwd"] : optional LLSD::String. The working directory to set
  88. // for the "executable" process.
  89. // - params["attached"] : optional LLSD::Boolean. Set to force-kill the
  90. // process on this LLeap's instance destruction.
  91. // WARNING: depending on the OS, this could kill
  92. // the viewer as well !
  93. static LLLeap* create(const LLSD& params, bool exc = true);
  94. // Exception thrown for invalid create() arguments, e.g. no plugin program.
  95. // This way, the caller can catch LLLeap::Error and try to recover.
  96. // Note: I also made it so that LLProcess (which we only use with LLLeap
  97. // in the Cool VL Viewer) uses LLLeap::Error to throw, instead of an
  98. // internal LLProcessError like in LL's viewer: this allows to catch any
  99. // process error as well when launching a new LLLeap. HB
  100. struct Error : public std::runtime_error
  101. {
  102. LL_INLINE Error(const std::string& what)
  103. : std::runtime_error(what)
  104. {
  105. }
  106. LL_INLINE Error(const char* what)
  107. : std::runtime_error(what)
  108. {
  109. }
  110. };
  111. ~LLLeap() override = default;
  112. // To toggle binary LLSD stream from the viewer to the LEAP plugin
  113. virtual void enableBinaryOutput(bool enable) = 0;
  114. // To toggle binary LLSD stream from the LEAP plugin to the viewer (broken)
  115. virtual void enableBinaryInput(bool enable) = 0;
  116. // Let's offer some introspection methods for LLLeap and its associated
  117. // LLProcess. HB
  118. virtual bool binaryOutputEnabled() const = 0;
  119. virtual bool binaryInputEnabled() const = 0;
  120. virtual const std::string& getDesc() const = 0;
  121. virtual const std::string& getProcDesc() const = 0;
  122. virtual const std::string& getExecutable() const = 0;
  123. virtual const std::string& getInterpreter() const = 0;
  124. virtual const std::string& getCwd() const = 0;
  125. virtual const std::vector<std::string>& getArgs() const = 0;
  126. protected:
  127. // Use the static create() methods to instantiate a new LLLeap.
  128. LLLeap() = default;
  129. };
  130. #endif // LL_LLLEAP_H