/indra/llplugin/llpluginmessage.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 137 lines · 53 code · 28 blank · 56 comment · 0 complexity · 8b9d0a7d8054ec4f8eaa555a37381b42 MD5 · raw file

  1. /**
  2. * @file llpluginmessage.h
  3. *
  4. * @cond
  5. * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. * @endcond
  26. */
  27. #ifndef LL_LLPLUGINMESSAGE_H
  28. #define LL_LLPLUGINMESSAGE_H
  29. #include "llsd.h"
  30. /**
  31. * @brief LLPluginMessage encapsulates the serialization/deserialization of messages passed to and from plugins.
  32. */
  33. class LLPluginMessage
  34. {
  35. LOG_CLASS(LLPluginMessage);
  36. public:
  37. LLPluginMessage();
  38. LLPluginMessage(const LLPluginMessage &p);
  39. LLPluginMessage(const std::string &message_class, const std::string &message_name);
  40. ~LLPluginMessage();
  41. // reset all internal state
  42. void clear(void);
  43. // Sets the message class and name
  44. // Also has the side-effect of clearing any key/value pairs in the message.
  45. void setMessage(const std::string &message_class, const std::string &message_name);
  46. // Sets a key/value pair in the message
  47. void setValue(const std::string &key, const std::string &value);
  48. void setValueLLSD(const std::string &key, const LLSD &value);
  49. void setValueS32(const std::string &key, S32 value);
  50. void setValueU32(const std::string &key, U32 value);
  51. void setValueBoolean(const std::string &key, bool value);
  52. void setValueReal(const std::string &key, F64 value);
  53. void setValuePointer(const std::string &key, void *value);
  54. std::string getClass(void) const;
  55. std::string getName(void) const;
  56. // Returns true if the specified key exists in this message (useful for optional parameters)
  57. bool hasValue(const std::string &key) const;
  58. // get the value of a particular key as a string. If the key doesn't exist in the message, an empty string will be returned.
  59. std::string getValue(const std::string &key) const;
  60. // get the value of a particular key as LLSD. If the key doesn't exist in the message, a null LLSD will be returned.
  61. LLSD getValueLLSD(const std::string &key) const;
  62. // get the value of a key as a S32. If the value wasn't set as a S32, behavior is undefined.
  63. S32 getValueS32(const std::string &key) const;
  64. // get the value of a key as a U32. Since there isn't an LLSD type for this, we use a hexadecimal string instead.
  65. U32 getValueU32(const std::string &key) const;
  66. // get the value of a key as a Boolean.
  67. bool getValueBoolean(const std::string &key) const;
  68. // get the value of a key as a float.
  69. F64 getValueReal(const std::string &key) const;
  70. // get the value of a key as a pointer.
  71. void* getValuePointer(const std::string &key) const;
  72. // Flatten the message into a string
  73. std::string generate(void) const;
  74. // Parse an incoming message into component parts
  75. // (this clears out all existing state before starting the parse)
  76. // Returns -1 on failure, otherwise returns the number of key/value pairs in the message.
  77. int parse(const std::string &message);
  78. private:
  79. LLSD mMessage;
  80. };
  81. /**
  82. * @brief Listener for plugin messages.
  83. */
  84. class LLPluginMessageListener
  85. {
  86. public:
  87. virtual ~LLPluginMessageListener();
  88. /** Plugin receives message from plugin loader shell. */
  89. virtual void receivePluginMessage(const LLPluginMessage &message) = 0;
  90. };
  91. /**
  92. * @brief Dispatcher for plugin messages.
  93. *
  94. * Manages the set of plugin message listeners and distributes messages to plugin message listeners.
  95. */
  96. class LLPluginMessageDispatcher
  97. {
  98. public:
  99. virtual ~LLPluginMessageDispatcher();
  100. void addPluginMessageListener(LLPluginMessageListener *);
  101. void removePluginMessageListener(LLPluginMessageListener *);
  102. protected:
  103. void dispatchPluginMessage(const LLPluginMessage &message);
  104. /** A set of message listeners. */
  105. typedef std::set<LLPluginMessageListener*> listener_set_t;
  106. /** The set of message listeners. */
  107. listener_set_t mListeners;
  108. };
  109. #endif // LL_LLPLUGINMESSAGE_H