Browse Source

added setTimeout(size_t milliseconds) and getTimeout() functions to set the timeout used for blocking reads on sessions

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac296@2648 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 14 years ago
parent
commit
97cd04f9c0
3 changed files with 35 additions and 8 deletions
  1. 20 7
      src/lib/cc/session.cc
  2. 11 0
      src/lib/cc/session.h
  3. 4 1
      src/lib/cc/session_unittests.cc

+ 20 - 7
src/lib/cc/session.cc

@@ -61,7 +61,8 @@ class SessionImpl {
 public:
     SessionImpl(io_service& io_service) :
         sequence_(-1), queue_(Element::createList()),
-        io_service_(io_service), socket_(io_service_), data_length_(0)
+        io_service_(io_service), socket_(io_service_), data_length_(0),
+        timeout_(4000)
     {}
     void establish(const char& socket_file);
     void disconnect();
@@ -69,8 +70,10 @@ public:
     size_t readDataLength();
     // Blocking read. Will throw a SessionTimeout if the timeout value
     // (in seconds) is thrown. If timeout is 0 it will block forever
-    void readData(void* data, size_t datalen, size_t timeout = 5);
+    void readData(void* data, size_t datalen);
     void startRead(boost::function<void()> user_handler);
+    virtual void setTimeout(size_t seconds) { timeout_ = seconds; };
+    virtual size_t getTimeout() { return timeout_; };
 
     long int sequence_; // the next sequence number to use
     std::string lname_;
@@ -90,6 +93,8 @@ private:
     uint32_t data_length_;
     boost::function<void()> user_handler_;
     asio::error_code error_;
+    // timeout for blocking reads (in seconds, defaults to 4)
+    size_t timeout_;
 };
 
 void
@@ -145,7 +150,7 @@ SessionImpl::setResult(bool* result, const asio::error_code b) {
 }
 
 void
-SessionImpl::readData(void* data, size_t datalen, size_t timeout) {
+SessionImpl::readData(void* data, size_t datalen) {
     bool timer_result = false;
     bool read_result = false;
     try {
@@ -154,8 +159,8 @@ SessionImpl::readData(void* data, size_t datalen, size_t timeout) {
                                      &read_result, _1));
         asio::deadline_timer timer(socket_.io_service());
     
-        if (timeout != 0) {
-            timer.expires_from_now(boost::posix_time::seconds(timeout));
+        if (getTimeout() != 0) {
+            timer.expires_from_now(boost::posix_time::milliseconds(getTimeout()));
             timer.async_wait(boost::bind(&SessionImpl::setResult,
                                          this, &timer_result, _1));
         }
@@ -445,10 +450,18 @@ Session::reply(ElementPtr& envelope, ElementPtr& newmsg) {
 }
 
 bool
-Session::hasQueuedMsgs()
-{
+Session::hasQueuedMsgs() {
     return (impl_->queue_->size() > 0);
 }
 
+void
+Session::setTimeout(size_t milliseconds) {
+    impl_->setTimeout(milliseconds);
+}
+
+size_t
+Session::getTimeout() {
+    return impl_->getTimeout();
+}
 }
 }

+ 11 - 0
src/lib/cc/session.h

@@ -97,6 +97,8 @@ namespace isc {
             virtual int reply(isc::data::ElementPtr& envelope,
                                isc::data::ElementPtr& newmsg) = 0;
             virtual bool hasQueuedMsgs() = 0;
+            virtual void setTimeout(size_t milliseconds) = 0;
+            virtual size_t getTimeout() = 0;
         };
 
     class Session : public AbstractSession {
@@ -130,6 +132,15 @@ namespace isc {
             virtual int reply(isc::data::ElementPtr& envelope,
                               isc::data::ElementPtr& newmsg);
             virtual bool hasQueuedMsgs();
+            /// \brief Set the default timeout for blocking reads
+            ///        in this session to the given number of milliseconds
+            /// \param milliseconds the timeout for blocking reads in
+            ///        milliseconds, if this is set to 0, reads will block
+            ///        forever. Defaults to 4000.
+            virtual void setTimeout(size_t milliseconds);
+            /// \brief Returns the current timeout for blocking reads
+            /// \return The timeout (in milliseconds)
+            virtual size_t getTimeout();
     private:
             void sendmsg(isc::data::ElementPtr& msg);
             void sendmsg(isc::data::ElementPtr& env,

+ 4 - 1
src/lib/cc/session_unittests.cc

@@ -172,7 +172,10 @@ TEST(Session, timeout_on_connect) {
     ::unlink("/tmp/mysock.sock");
     TestDomainSocket tds(my_io_service, "/tmp/mysock.sock");
     Session sess(my_io_service);
-
+    // set to a short timeout so the test doesn't take too long
+    EXPECT_EQ(4000, sess.getTimeout());
+    sess.setTimeout(100);
+    EXPECT_EQ(100, sess.getTimeout());
     // no answer, should timeout
     EXPECT_THROW(sess.establish("/tmp/mysock.sock"), isc::cc::SessionTimeout);
 }