Browse Source

[1956] Replaced clock_gettime with posix_time.

Marcin Siodelski 13 years ago
parent
commit
bff3d4786c

+ 1 - 5
src/lib/dhcp/pkt4.cc

@@ -53,7 +53,6 @@ Pkt4::Pkt4(uint8_t msg_type, uint32_t transid)
     memset(chaddr_, 0, MAX_CHADDR_LEN);
     memset(sname_, 0, MAX_SNAME_LEN);
     memset(file_, 0, MAX_FILE_LEN);
-    memset(&timestamp_, 0, sizeof(timestamp_));
 }
 
 Pkt4::Pkt4(const uint8_t* data, size_t len)
@@ -82,7 +81,6 @@ Pkt4::Pkt4(const uint8_t* data, size_t len)
 
     data_.resize(len);
     memcpy(&data_[0], data, len);
-    memset(&timestamp_, 0, sizeof(timestamp_));
 }
 
 size_t
@@ -309,9 +307,7 @@ Pkt4::getOption(uint8_t type) {
 
 void
 Pkt4::updateTimestamp() {
-    if (clock_gettime(CLOCK_REALTIME, &timestamp_) < 0) {
-        isc_throw(isc::Unexpected, "Failed to get timestamp for packet");
-    }
+    timestamp_ = boost::posix_time::microsec_clock::universal_time();
 }
 
 } // end of namespace isc::dhcp

+ 3 - 2
src/lib/dhcp/pkt4.h

@@ -19,6 +19,7 @@
 #include <time.h>
 #include <vector>
 #include <boost/shared_ptr.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 #include "asiolink/io_address.h"
 #include "util/buffer.h"
 #include "dhcp/option.h"
@@ -328,7 +329,7 @@ public:
     /// packet is received or send.
     ///
     /// @return packet timestamp.
-    timespec getTimestamp() const { return timestamp_; }
+    const boost::posix_time::ptime& getTimestamp() const { return timestamp_; }
 
     /// @brief Sets interface name.
     ///
@@ -504,7 +505,7 @@ protected:
     isc::dhcp::Option::OptionCollection options_;
 
     /// packet timestamp
-    timespec timestamp_;
+    boost::posix_time::ptime timestamp_;
 }; // Pkt4 class
 
 typedef boost::shared_ptr<Pkt4> Pkt4Ptr;

+ 1 - 5
src/lib/dhcp/pkt6.cc

@@ -38,7 +38,6 @@ Pkt6::Pkt6(const uint8_t* buf, uint32_t buf_len, DHCPv6Proto proto /* = UDP */)
     bufferOut_(0) {
     data_.resize(buf_len);
     memcpy(&data_[0], buf, buf_len);
-    memset(&timestamp_, 0, sizeof(timestamp_));
 }
 
 Pkt6::Pkt6(uint8_t msg_type, uint32_t transid, DHCPv6Proto proto /*= UDP*/) :
@@ -52,7 +51,6 @@ Pkt6::Pkt6(uint8_t msg_type, uint32_t transid, DHCPv6Proto proto /*= UDP*/) :
     local_port_(0),
     remote_port_(0),
     bufferOut_(0) {
-    memset(&timestamp_, 0, sizeof(timestamp_));
 }
 
 uint16_t Pkt6::len() {
@@ -206,9 +204,7 @@ void Pkt6::repack() {
 
 void
 Pkt6::updateTimestamp() {
-    if (clock_gettime(CLOCK_REALTIME, &timestamp_) < 0) {
-        isc_throw(isc::Unexpected, "Failed to get timestamp for packet");
-    }
+    timestamp_ = boost::posix_time::microsec_clock::universal_time();
 }
 
 

+ 3 - 2
src/lib/dhcp/pkt6.h

@@ -19,6 +19,7 @@
 #include <time.h>
 #include <boost/shared_ptr.hpp>
 #include <boost/shared_array.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 #include "asiolink/io_address.h"
 #include "dhcp/option.h"
 
@@ -227,7 +228,7 @@ public:
     /// packet is received or send.
     ///
     /// @return packet timestamp.
-    timespec getTimestamp() const { return timestamp_; }
+    const boost::posix_time::ptime& getTimestamp() const { return timestamp_; }
 
     /// @brief Sets interface name.
     ///
@@ -324,7 +325,7 @@ protected:
     isc::util::OutputBuffer bufferOut_;
 
     /// packet timestamp
-    timespec timestamp_;
+    boost::posix_time::ptime timestamp_;
 }; // Pkt6 class
 
 typedef boost::shared_ptr<Pkt6> Pkt6Ptr;

+ 20 - 5
src/lib/dhcp/tests/pkt4_unittest.cc

@@ -600,14 +600,29 @@ TEST(Pkt4Test, metaFields) {
 
 TEST(Pkt4Test, Timestamp) {
     Pkt4* pkt = new Pkt4(DHCPOFFER, 1234);
-    ASSERT_NO_THROW(pkt->updateTimestamp());
-    timespec ts_packet = pkt->getTimestamp();
-    timespec ts_now;
-    ASSERT_FALSE(clock_gettime(CLOCK_REALTIME, &ts_now) < 0);
-    EXPECT_TRUE(ts_packet.tv_sec >= ts_now.tv_sec);
+
+    // Update packet time.
+    pkt->updateTimestamp();
+
+    // Get updated packet time.
+    boost::posix_time::ptime ts_packet = pkt->getTimestamp();
+
+    // After timestamp is updated it should be date-time.
+    ASSERT_FALSE(ts_packet.is_not_a_date_time());
+
+    // Check current time.
+    boost::posix_time::ptime ts_now =
+        boost::posix_time::microsec_clock::universal_time();
+
+    // Calculate period between packet time and now.
+    boost::posix_time::time_period ts_period(ts_packet, ts_now);
+
+    // Duration should be positive or zero.
+    EXPECT_TRUE(ts_period.length().total_microseconds() >= 0);
 
     delete pkt;
 }
 
 
+
 } // end of anonymous namespace

+ 21 - 5
src/lib/dhcp/tests/pkt6_unittest.cc

@@ -16,6 +16,7 @@
 #include <iostream>
 #include <sstream>
 #include <arpa/inet.h>
+#include <boost/date_time/posix_time/posix_time.hpp>
 #include <gtest/gtest.h>
 
 #include <asiolink/io_address.h>
@@ -206,11 +207,26 @@ TEST_F(Pkt6Test, addGetDelOptions) {
 
 TEST_F(Pkt6Test, Timestamp) {
     Pkt6* pkt = new Pkt6(DHCPV6_SOLICIT, 0x020304);
-    ASSERT_NO_THROW(pkt->updateTimestamp());
-    timespec ts_packet = pkt->getTimestamp();
-    timespec ts_now;
-    ASSERT_FALSE(clock_gettime(CLOCK_REALTIME, &ts_now) < 0);
-    EXPECT_TRUE(ts_packet.tv_sec >= ts_now.tv_sec);
+    // Update packet time.
+    pkt->updateTimestamp();
+
+    // Get updated packet time.
+    boost::posix_time::ptime ts_packet = pkt->getTimestamp();
+
+    // After timestamp is updated it should be date-time.
+    ASSERT_FALSE(ts_packet.is_not_a_date_time());
+
+    // Check current time.
+    boost::posix_time::ptime ts_now =
+        boost::posix_time::microsec_clock::universal_time();
+
+    // Calculate period between packet time and now.
+    boost::posix_time::time_period ts_period(ts_packet, ts_now);
+
+    // Duration should be positive or zero.
+    EXPECT_TRUE(ts_period.length().total_microseconds() >= 0);
+
+    delete pkt;
 }
 
 }