Browse Source

[2157] use boost::optional for getRequestOpcode()

Yoshitaka Aharen 12 years ago
parent
commit
9aaf0f9883

+ 38 - 44
src/bin/auth/statistics.cc.pre

@@ -24,6 +24,8 @@
 
 #include <statistics/counter.h>
 
+#include <boost/optional.hpp>
+
 using namespace isc::dns;
 using namespace isc::auth;
 using namespace isc::statistics;
@@ -151,14 +153,11 @@ Counters::incRequest(const MessageAttributes& msgattrs) {
     }
 
     // OPCODE
-    try {
-        server_msg_counter_.inc(
-            opcode_to_msgcounter[msgattrs.getRequestOpCode().getCode()]);
-    } catch (const isc::InvalidOperation& ex) {
-        // The exception will be thrown if OpCode is not set
-        //  (e.g. message is short).
-        // Increment MSG_OPCODE_OTHER.
-        server_msg_counter_.inc(MSG_OPCODE_OTHER);
+    const boost::optional<const isc::dns::Opcode&> opcode =
+        msgattrs.getRequestOpCode();
+    // Increment opcode counter only if the opcode exists.
+    if (opcode) {
+        server_msg_counter_.inc(opcode_to_msgcounter[opcode.get().getCode()]);
     }
 }
 
@@ -198,47 +197,42 @@ Counters::incResponse(const MessageAttributes& msgattrs,
         server_msg_counter_.inc(MSG_REQUEST_BADEDNSVER);
     }
 
-    try {
-        if (msgattrs.getRequestOpCode() == Opcode::QUERY()) {
-            // compound attributes
-            const unsigned int answer_rrs =
-                response.getRRCount(Message::SECTION_ANSWER);
-            const bool is_aa_set =
-                response.getHeaderFlag(Message::HEADERFLAG_AA);
-
-            if (is_aa_set) {
-                // QryAuthAns
-                server_msg_counter_.inc(MSG_QRYAUTHANS);
-            } else {
-                // QryNoAuthAns
-                server_msg_counter_.inc(MSG_QRYNOAUTHANS);
-            }
+    const boost::optional<const isc::dns::Opcode&> opcode =
+        msgattrs.getRequestOpCode();
+    if (opcode && opcode.get() == Opcode::QUERY()) {
+        // compound attributes
+        const unsigned int answer_rrs =
+            response.getRRCount(Message::SECTION_ANSWER);
+        const bool is_aa_set =
+            response.getHeaderFlag(Message::HEADERFLAG_AA);
+
+        if (is_aa_set) {
+            // QryAuthAns
+            server_msg_counter_.inc(MSG_QRYAUTHANS);
+        } else {
+            // QryNoAuthAns
+            server_msg_counter_.inc(MSG_QRYNOAUTHANS);
+        }
 
-            if (rcode == Rcode::NOERROR_CODE) {
-                if (answer_rrs > 0) {
-                    // QrySuccess
-                    server_msg_counter_.inc(MSG_QRYSUCCESS);
+        if (rcode == Rcode::NOERROR_CODE) {
+            if (answer_rrs > 0) {
+                // QrySuccess
+                server_msg_counter_.inc(MSG_QRYSUCCESS);
+            } else {
+                if (is_aa_set) {
+                    // QryNxrrset
+                    server_msg_counter_.inc(MSG_QRYNXRRSET);
                 } else {
-                    if (is_aa_set) {
-                        // QryNxrrset
-                        server_msg_counter_.inc(MSG_QRYNXRRSET);
-                    } else {
-                        // QryReferral
-                        server_msg_counter_.inc(MSG_QRYREFERRAL);
-                    }
-                }
-            } else if (rcode == Rcode::REFUSED_CODE) {
-                if (!response.getHeaderFlag(Message::HEADERFLAG_RD)) {
-                    // AuthRej
-                    server_msg_counter_.inc(MSG_QRYREJECT);
+                    // QryReferral
+                    server_msg_counter_.inc(MSG_QRYREFERRAL);
                 }
             }
+        } else if (rcode == Rcode::REFUSED_CODE) {
+            if (!response.getHeaderFlag(Message::HEADERFLAG_RD)) {
+                // AuthRej
+                server_msg_counter_.inc(MSG_QRYREJECT);
+            }
         }
-    } catch (const isc::InvalidOperation& ex) {
-        // The exception will be thrown if OpCode is not set
-        //  (e.g. message is short).
-        // Do not increment the counters above since they are incremented
-        // only if Opcode is Query.
     }
 }
 

+ 3 - 3
src/bin/auth/statistics.h

@@ -77,13 +77,13 @@ public:
     {}
 
     /// \brief Return opcode of the request.
-    /// \return opcode of the request
+    /// \return opcode of the request wrapped with boost::optional
     /// \throw isc::InvalidOperation Opcode is not set
-    const isc::dns::Opcode& getRequestOpCode() const {
+    const boost::optional<const isc::dns::Opcode&> getRequestOpCode() const {
         if (req_opcode_) {
             return (req_opcode_.get());
         } else {
-            isc_throw(isc::InvalidOperation, "Opcode is not set");
+            return boost::none;
         }
     }
 

+ 0 - 3
src/bin/auth/tests/auth_srv_unittest.cc

@@ -295,7 +295,6 @@ TEST_F(AuthSrvTest, shortMessage) {
     std::map<std::string, int> expect;
     expect["request.v4"] = 1;
     expect["request.udp"] = 1;
-    expect["opcode.other"] = 1;
     checkStatisticsCounters(stats_after, expect);
 }
 
@@ -309,7 +308,6 @@ TEST_F(AuthSrvTest, response) {
     std::map<std::string, int> expect;
     expect["request.v4"] = 3;
     expect["request.udp"] = 3;
-    expect["opcode.other"] = 3;
     checkStatisticsCounters(stats_after, expect);
 }
 
@@ -335,7 +333,6 @@ TEST_F(AuthSrvTest, ednsBadVers) {
     expect["request.v4"] = 1;
     expect["request.badednsver"] = 1;
     expect["request.udp"] = 1;
-    expect["opcode.other"] = 1;
     expect["responses"] = 1;
     expect["rcode.badvers"] = 1;
     checkStatisticsCounters(stats_after, expect);

+ 4 - 4
src/bin/auth/tests/statistics_unittest.cc.pre

@@ -80,13 +80,13 @@ TEST_F(CountersTest, invalidParameter) {
 TEST_F(CountersTest, invalidOperationForGetRequestOpCode) {
     MessageAttributes msgattrs;
 
-    // getRequestOpCode() will throw isc::InvalidOperation when called before
+    // getRequestOpCode() should return boost::none when called before
     // opcode is set with setRequestOpCode().
-    EXPECT_THROW(msgattrs.getRequestOpCode(), isc::InvalidOperation);
+    EXPECT_FALSE(msgattrs.getRequestOpCode());
 
     msgattrs.setRequestOpCode(Opcode::QUERY());
-    // getRequestOpCode() will not throw at this point.
-    EXPECT_NO_THROW(msgattrs.getRequestOpCode());
+    // getRequestOpCode() should be Opcode::QUERY.
+    EXPECT_EQ(Opcode::QUERY(), msgattrs.getRequestOpCode().get());
 }
 
 TEST_F(CountersTest, incrementResponse) {