face: avoid multiple onFail events

refs #1497

Change-Id: I8fda3fea5cd8a314b30eef45da104109e9748afc
diff --git a/daemon/face/datagram-face.hpp b/daemon/face/datagram-face.hpp
index e6a6929..2dbe8fa 100644
--- a/daemon/face/datagram-face.hpp
+++ b/daemon/face/datagram-face.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -155,7 +156,7 @@
 
     if (!m_socket->is_open())
     {
-      onFail("Tunnel closed");
+      fail("Tunnel closed");
       return;
     }
 
@@ -168,11 +169,11 @@
 
     if (error == boost::asio::error::eof)
     {
-      onFail("Tunnel closed");
+      fail("Tunnel closed");
     }
     else
     {
-      onFail("Send operation failed, closing socket: " +
+      fail("Send operation failed, closing socket: " +
              error.category().message(error.value()));
     }
     return;
@@ -196,7 +197,7 @@
                << "] Close tunnel");
 
   closeSocket();
-  onFail("Close tunnel");
+  fail("Close tunnel");
 }
 
 template <class T>
@@ -224,7 +225,7 @@
     // this should be unnecessary, but just in case
     if (!m_socket->is_open())
     {
-      onFail("Tunnel closed");
+      fail("Tunnel closed");
       return;
     }
 
@@ -237,11 +238,11 @@
 
     if (error == boost::asio::error::eof)
     {
-      onFail("Tunnel closed");
+      fail("Tunnel closed");
     }
     else
     {
-      onFail("Receive operation failed, closing socket: " +
+      fail("Receive operation failed, closing socket: " +
              error.category().message(error.value()));
     }
     return;
diff --git a/daemon/face/ethernet-face.cpp b/daemon/face/ethernet-face.cpp
index 27d4968..27f34ee 100644
--- a/daemon/face/ethernet-face.cpp
+++ b/daemon/face/ethernet-face.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -110,7 +111,7 @@
       pcap_close(m_pcap);
       m_pcap = 0;
 
-      onFail("Face closed");
+      fail("Face closed");
     }
 }
 
@@ -166,7 +167,7 @@
     {
       NFD_LOG_WARN("[id:" << getId() << ",endpoint:" << m_interfaceName
                    << "] Trying to send on closed face");
-      onFail("Face closed");
+      fail("Face closed");
       return;
     }
 
@@ -278,7 +279,7 @@
 
   if (!m_pcap)
     {
-      onFail("Face closed");
+      fail("Face closed");
       return;
     }
 
@@ -295,7 +296,7 @@
     }
 
   close();
-  onFail(msg);
+  fail(msg);
 }
 
 size_t
diff --git a/daemon/face/face.cpp b/daemon/face/face.cpp
index 75600b4..9d2368b 100644
--- a/daemon/face/face.cpp
+++ b/daemon/face/face.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -39,6 +40,7 @@
   , m_remoteUri(remoteUri)
   , m_localUri(localUri)
   , m_isOnDemand(false)
+  , m_isFailed(false)
 {
   onReceiveInterest += bind(&increaseCounter, ref(m_counters.getNInInterests()));
   onReceiveData     += bind(&increaseCounter, ref(m_counters.getNInDatas()));
@@ -115,4 +117,15 @@
   }
 }
 
+void
+Face::fail(const std::string& reason)
+{
+  if (m_isFailed) {
+    return;
+  }
+
+  m_isFailed = true;
+  this->onFail(reason);
+}
+
 } //namespace nfd
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index c5e6ee8..987853e 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -159,6 +160,11 @@
   void
   setOnDemand(bool isOnDemand);
 
+  /** \brief fail the face and raise onFail event if it's UP; otherwise do nothing
+   */
+  void
+  fail(const std::string& reason);
+
 private:
   void
   setId(FaceId faceId);
@@ -171,6 +177,7 @@
   FaceUri m_remoteUri;
   FaceUri m_localUri;
   bool m_isOnDemand;
+  bool m_isFailed;
 
   // allow setting FaceId
   friend class FaceTable;
diff --git a/daemon/face/stream-face.hpp b/daemon/face/stream-face.hpp
index daf8ddf..12cc2bc 100644
--- a/daemon/face/stream-face.hpp
+++ b/daemon/face/stream-face.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -211,7 +212,7 @@
                << "] Close connection");
 
   closeSocket();
-  this->onFail("Close connection");
+  this->fail("Close connection");
 }
 
 template<class T, class U>
@@ -223,7 +224,7 @@
 
   if (!m_socket->is_open())
     {
-      this->onFail("Connection closed");
+      this->fail("Connection closed");
       return;
     }
 
@@ -245,11 +246,11 @@
 
   if (error == boost::asio::error::eof)
     {
-      this->onFail("Connection closed");
+      this->fail("Connection closed");
     }
   else
     {
-      this->onFail("Send or receive operation failed, closing socket: " +
+      this->fail("Send or receive operation failed, closing socket: " +
                    error.category().message(error.value()));
     }
 }
@@ -327,7 +328,7 @@
                    << "closing down the face");
 
       closeSocket();
-      this->onFail("Failed to parse incoming packet or it is too large to process, "
+      this->fail("Failed to parse incoming packet or it is too large to process, "
                    "closing down the face");
       return;
     }
diff --git a/daemon/face/websocket-face.cpp b/daemon/face/websocket-face.cpp
index 5dfe516..9e56bb5 100644
--- a/daemon/face/websocket-face.cpp
+++ b/daemon/face/websocket-face.cpp
@@ -1,12 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology,
- *                     The University of Memphis
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -65,7 +65,7 @@
       websocketpp::lib::error_code ecode;
       m_server.close(m_handle, websocketpp::close::status::normal, "closed by nfd", ecode);
 
-      onFail("Face closed");
+      fail("Face closed");
     }
 }
 
diff --git a/tests/daemon/face/dummy-face.hpp b/tests/daemon/face/dummy-face.hpp
index 7039286..ca45722 100644
--- a/tests/daemon/face/dummy-face.hpp
+++ b/tests/daemon/face/dummy-face.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,10 +21,10 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
-#ifndef NFD_TESTS_NFD_FACE_DUMMY_FACE_HPP
-#define NFD_TESTS_NFD_FACE_DUMMY_FACE_HPP
+#ifndef NFD_TESTS_DAEMON_FACE_DUMMY_FACE_HPP
+#define NFD_TESTS_DAEMON_FACE_DUMMY_FACE_HPP
 
 #include "face/face.hpp"
 #include "face/local-face.hpp"
@@ -90,4 +91,4 @@
 } // namespace tests
 } // namespace nfd
 
-#endif // NFD_TESTS_NFD_FACE_DUMMY_FACE_HPP
+#endif // NFD_TESTS_DAEMON_FACE_DUMMY_FACE_HPP
diff --git a/tests/daemon/face/face.cpp b/tests/daemon/face/face.cpp
index 46c0345..b82e0fa 100644
--- a/tests/daemon/face/face.cpp
+++ b/tests/daemon/face/face.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -68,6 +69,43 @@
   BOOST_CHECK_EQUAL(counters.getNOutDatas()    , 0);
 }
 
+class FaceFailTestFace : public DummyFace
+{
+public:
+  FaceFailTestFace()
+    : failCount(0)
+  {
+    this->onFail += bind(&FaceFailTestFace::failHandler, this, _1);
+  }
+
+  void
+  failOnce()
+  {
+    this->fail("reason");
+  }
+
+private:
+  void
+  failHandler(const std::string& reason)
+  {
+    BOOST_CHECK_EQUAL(reason, "reason");
+    ++this->failCount;
+  }
+
+public:
+  int failCount;
+};
+
+BOOST_AUTO_TEST_CASE(FailTwice)
+{
+  FaceFailTestFace face;
+  BOOST_CHECK_EQUAL(face.failCount, 0);
+  face.failOnce();
+  BOOST_CHECK_EQUAL(face.failCount, 1);
+  face.failOnce();
+  BOOST_CHECK_EQUAL(face.failCount, 1);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests