session.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <string>
  17. #include <boost/function.hpp>
  18. #include <exceptions/exceptions.h>
  19. #include <cc/data.h>
  20. #include <cc/session_config.h>
  21. namespace asio {
  22. class io_service;
  23. }
  24. namespace isc {
  25. namespace cc {
  26. class SessionImpl;
  27. class SessionError : public isc::Exception {
  28. public:
  29. SessionError(const char* file, size_t line, const char* what) :
  30. isc::Exception(file, line, what) {}
  31. };
  32. /// \brief A standard Exception class that is thrown when a
  33. /// blocking readData call does not read the given number of
  34. /// bytes before the timeout expires
  35. class SessionTimeout : public isc::Exception {
  36. public:
  37. SessionTimeout(const char* file, size_t line, const char* what) :
  38. isc::Exception(file, line, what) {}
  39. };
  40. /// \brief The AbstractSession class is an abstract base class that
  41. /// defines the interfaces of Session.
  42. /// The intended primary usage of abstraction is to allow tests for the
  43. /// user class of Session without requiring actual communication
  44. /// channels.
  45. /// For simplicity we only define the methods that are necessary for
  46. /// existing test cases that use this base class. Eventually we'll
  47. /// probably have to extend them.
  48. class AbstractSession {
  49. ///
  50. /// \name Constructors, Assignment Operator and Destructor.
  51. ///
  52. /// Note: The copy constructor and the assignment operator are
  53. /// intentionally defined as private to make it explicit that
  54. /// this is a pure base class.
  55. //@{
  56. private:
  57. AbstractSession(const AbstractSession& source);
  58. AbstractSession& operator=(const AbstractSession& source);
  59. protected:
  60. /// \brief The default constructor.
  61. ///
  62. /// This is intentionally defined as \c protected as this base
  63. /// class should never be instantiated (except as part of a
  64. /// derived class).
  65. AbstractSession() {}
  66. public:
  67. /// \brief The destructor.
  68. virtual ~AbstractSession() {}
  69. //@}
  70. virtual void establish(const char* socket_file) = 0;
  71. virtual void disconnect() = 0;
  72. virtual int group_sendmsg(isc::data::ConstElementPtr msg,
  73. std::string group,
  74. std::string instance = "*",
  75. std::string to = "*") = 0;
  76. virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
  77. isc::data::ConstElementPtr& msg,
  78. bool nonblock = true,
  79. int seq = -1) = 0;
  80. virtual void subscribe(std::string group,
  81. std::string instance = "*") = 0;
  82. virtual void unsubscribe(std::string group,
  83. std::string instance = "*") = 0;
  84. virtual void startRead(boost::function<void()> read_callback) = 0;
  85. virtual int reply(isc::data::ConstElementPtr envelope,
  86. isc::data::ConstElementPtr newmsg) = 0;
  87. virtual bool hasQueuedMsgs() const = 0;
  88. /// \brief Sets the default timeout for blocking reads
  89. /// in this session to the given number of milliseconds
  90. /// \param milliseconds the timeout for blocking reads in
  91. /// milliseconds; if this is set to 0, reads will block
  92. /// forever.
  93. virtual void setTimeout(size_t milliseconds) = 0;
  94. /// \brief Returns the current timeout for blocking reads
  95. /// \return The timeout (in milliseconds)
  96. virtual size_t getTimeout() const = 0;
  97. };
  98. class Session : public AbstractSession {
  99. private:
  100. SessionImpl* impl_;
  101. private:
  102. Session(const Session& source);
  103. Session& operator=(const Session& source);
  104. public:
  105. Session(asio::io_service& ioservice);
  106. virtual ~Session();
  107. virtual void startRead(boost::function<void()> read_callback);
  108. virtual void establish(const char* socket_file = NULL);
  109. virtual void disconnect();
  110. virtual void subscribe(std::string group,
  111. std::string instance = "*");
  112. virtual void unsubscribe(std::string group,
  113. std::string instance = "*");
  114. virtual int group_sendmsg(isc::data::ConstElementPtr msg,
  115. std::string group,
  116. std::string instance = "*",
  117. std::string to = "*");
  118. virtual bool group_recvmsg(isc::data::ConstElementPtr& envelope,
  119. isc::data::ConstElementPtr& msg,
  120. bool nonblock = true,
  121. int seq = -1);
  122. virtual int reply(isc::data::ConstElementPtr envelope,
  123. isc::data::ConstElementPtr newmsg);
  124. virtual bool hasQueuedMsgs() const;
  125. virtual void setTimeout(size_t milliseconds);
  126. virtual size_t getTimeout() const;
  127. private:
  128. void sendmsg(isc::data::ConstElementPtr msg);
  129. void sendmsg(isc::data::ConstElementPtr env,
  130. isc::data::ConstElementPtr msg);
  131. bool recvmsg(isc::data::ConstElementPtr& msg,
  132. bool nonblock = true,
  133. int seq = -1);
  134. bool recvmsg(isc::data::ConstElementPtr& env,
  135. isc::data::ConstElementPtr& msg,
  136. bool nonblock = true,
  137. int seq = -1);
  138. };
  139. } // namespace cc
  140. } // namespace isc
  141. #endif // _ISC_SESSION_H
  142. // Local Variables:
  143. // mode: c++
  144. // End: