session.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef ISC_SESSION_H
  15. #define ISC_SESSION_H 1
  16. #include <cc/data.h>
  17. #include <cc/session_config.h>
  18. #include <cc/proto_defs.h>
  19. #include <exceptions/exceptions.h>
  20. #include <string>
  21. #include <boost/function.hpp>
  22. namespace asio {
  23. class io_service;
  24. }
  25. namespace isc {
  26. namespace cc {
  27. class SessionImpl;
  28. class SessionError : public isc::Exception {
  29. public:
  30. SessionError(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) {}
  32. };
  33. /// \brief A standard Exception class that is thrown when a
  34. /// blocking readData call does not read the given number of
  35. /// bytes before the timeout expires
  36. class SessionTimeout : public isc::Exception {
  37. public:
  38. SessionTimeout(const char* file, size_t line, const char* what) :
  39. isc::Exception(file, line, what) {}
  40. };
  41. /// \brief The AbstractSession class is an abstract base class that
  42. /// defines the interfaces of Session.
  43. /// The intended primary usage of abstraction is to allow tests for the
  44. /// user class of Session without requiring actual communication
  45. /// channels.
  46. /// For simplicity we only define the methods that are necessary for
  47. /// existing test cases that use this base class. Eventually we'll
  48. /// probably have to extend them.
  49. class AbstractSession {
  50. ///
  51. /// \name Constructors, Assignment Operator and Destructor.
  52. ///
  53. /// Note: The copy constructor and the assignment operator are
  54. /// intentionally defined as private to make it explicit that
  55. /// this is a pure base class.
  56. //@{
  57. private:
  58. AbstractSession(const AbstractSession& source);
  59. AbstractSession& operator=(const AbstractSession& source);
  60. protected:
  61. /// \brief The default constructor.
  62. ///
  63. /// This is intentionally defined as \c protected as this base
  64. /// class should never be instantiated (except as part of a
  65. /// derived class).
  66. AbstractSession() {}
  67. public:
  68. /// \brief The destructor.
  69. virtual ~AbstractSession() {}
  70. //@}
  71. virtual void establish(const char* socket_file) = 0;
  72. virtual void disconnect() = 0;
  73. virtual int group_sendmsg(isc::data::ConstElementPtr msg,
  74. std::string group,
  75. std::string instance =
  76. CC_INSTANCE_WILDCARD,
  77. std::string to = CC_TO_WILDCARD,
  78. bool want_answer = false) = 0;
  79. virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
  80. isc::data::ConstElementPtr& msg,
  81. bool nonblock = true,
  82. int seq = -1) = 0;
  83. virtual void subscribe(std::string group,
  84. std::string instance = "*") = 0;
  85. virtual void unsubscribe(std::string group,
  86. std::string instance = "*") = 0;
  87. virtual void startRead(boost::function<void()> read_callback) = 0;
  88. virtual int reply(isc::data::ConstElementPtr envelope,
  89. isc::data::ConstElementPtr newmsg) = 0;
  90. virtual bool hasQueuedMsgs() const = 0;
  91. /// \brief Sets the default timeout for blocking reads
  92. /// in this session to the given number of milliseconds
  93. /// \param milliseconds the timeout for blocking reads in
  94. /// milliseconds; if this is set to 0, reads will block
  95. /// forever.
  96. virtual void setTimeout(size_t milliseconds) = 0;
  97. /// \brief Returns the current timeout for blocking reads
  98. /// \return The timeout (in milliseconds)
  99. virtual size_t getTimeout() const = 0;
  100. };
  101. class Session : public AbstractSession {
  102. private:
  103. SessionImpl* impl_;
  104. private:
  105. Session(const Session& source);
  106. Session& operator=(const Session& source);
  107. public:
  108. Session(asio::io_service& ioservice);
  109. virtual ~Session();
  110. virtual void startRead(boost::function<void()> read_callback);
  111. virtual void establish(const char* socket_file = NULL);
  112. virtual void disconnect();
  113. virtual void subscribe(std::string group,
  114. std::string instance = "*");
  115. virtual void unsubscribe(std::string group,
  116. std::string instance = "*");
  117. /// \brief Send a message to a group.
  118. ///
  119. /// \todo Can someone explain how the group, instance and to work?
  120. /// What is the desired semantics here?
  121. /// \param msg The message to send.
  122. /// \param group Part of addressing.
  123. /// \param instance Part of addressing.
  124. /// \param to Part of addressing.
  125. /// \param want_answer Require an answer? If it is true and there's
  126. /// no recipient, the message queue answers by an error
  127. /// instead.
  128. /// \return The squence number of the message sent. It can be used
  129. /// to wait for an answer by group_recvmsg.
  130. virtual int group_sendmsg(isc::data::ConstElementPtr msg,
  131. std::string group,
  132. std::string instance = "*",
  133. std::string to = "*",
  134. bool want_answer = false);
  135. virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
  136. isc::data::ConstElementPtr& msg,
  137. bool nonblock = true,
  138. int seq = -1);
  139. virtual int reply(isc::data::ConstElementPtr envelope,
  140. isc::data::ConstElementPtr newmsg);
  141. virtual bool hasQueuedMsgs() const;
  142. virtual void setTimeout(size_t milliseconds);
  143. virtual size_t getTimeout() const;
  144. /// @brief returns socket descriptor from underlying socket connection
  145. ///
  146. /// @param returns socket descriptor used for session connection
  147. virtual int getSocketDesc() const;
  148. private:
  149. // The following two methods are virtual to allow tests steal and
  150. // replace them. It is not expected to be specialized by a derived
  151. // class. Actually, it is not expected to inherit from this class
  152. // to begin with.
  153. virtual void sendmsg(isc::data::ConstElementPtr header);
  154. virtual void sendmsg(isc::data::ConstElementPtr header,
  155. isc::data::ConstElementPtr payload);
  156. bool recvmsg(isc::data::ConstElementPtr& msg,
  157. bool nonblock = true,
  158. int seq = -1);
  159. bool recvmsg(isc::data::ConstElementPtr& env,
  160. isc::data::ConstElementPtr& msg,
  161. bool nonblock = true,
  162. int seq = -1);
  163. };
  164. } // namespace cc
  165. } // namespace isc
  166. #endif // ISC_SESSION_H
  167. // Local Variables:
  168. // mode: c++
  169. // End: