From 3afb63a650b8b925ce1ba722dd42b7418f623713 Mon Sep 17 00:00:00 2001
From: Florent Le Coz <louiz@louiz.org>
Date: Sat, 21 Dec 2013 21:04:41 +0100
Subject: Shutdown cleanly on SIGINT

---
 src/xmpp/xmpp_component.cpp | 18 +++++++++++++++++-
 src/xmpp/xmpp_component.hpp | 13 ++++++++++++-
 2 files changed, 29 insertions(+), 2 deletions(-)

(limited to 'src/xmpp')

diff --git a/src/xmpp/xmpp_component.cpp b/src/xmpp/xmpp_component.cpp
index 433f87a..dc77934 100644
--- a/src/xmpp/xmpp_component.cpp
+++ b/src/xmpp/xmpp_component.cpp
@@ -24,7 +24,8 @@
 XmppComponent::XmppComponent(const std::string& hostname, const std::string& secret):
   served_hostname(hostname),
   secret(secret),
-  authenticated(false)
+  authenticated(false),
+  doc_open(false)
 {
   this->parser.add_stream_open_callback(std::bind(&XmppComponent::on_remote_stream_open, this,
                                                   std::placeholders::_1));
@@ -51,6 +52,11 @@ bool XmppComponent::start()
   return this->connect("127.0.0.1", "5347");
 }
 
+bool XmppComponent::is_document_open() const
+{
+  return this->doc_open;
+}
+
 void XmppComponent::send_stanza(const Stanza& stanza)
 {
   std::string str = stanza.to_string();
@@ -66,6 +72,7 @@ void XmppComponent::on_connected()
   node["xmlns:stream"] = STREAM_NS;
   node["to"] = this->served_hostname;
   this->send_stanza(node);
+  this->doc_open = true;
 }
 
 void XmppComponent::on_connection_close()
@@ -79,6 +86,14 @@ void XmppComponent::parse_in_buffer()
   this->in_buf.clear();
 }
 
+void XmppComponent::shutdown()
+{
+  for (auto it = this->bridges.begin(); it != this->bridges.end(); ++it)
+  {
+    it->second->shutdown();
+  }
+}
+
 void XmppComponent::on_remote_stream_open(const XmlNode& node)
 {
   log_debug("XMPP DOCUMENT OPEN: " << node.to_string());
@@ -145,6 +160,7 @@ void XmppComponent::close_document()
 {
   log_debug("XMPP SENDING: </stream:stream>");
   this->send_data("</stream:stream>");
+  this->doc_open = false;
 }
 
 void XmppComponent::handle_handshake(const Stanza& stanza)
diff --git a/src/xmpp/xmpp_component.hpp b/src/xmpp/xmpp_component.hpp
index 1a7fc6b..1952e19 100644
--- a/src/xmpp/xmpp_component.hpp
+++ b/src/xmpp/xmpp_component.hpp
@@ -23,7 +23,14 @@ public:
   void on_connected() override final;
   void on_connection_close() override final;
   void parse_in_buffer() override final;
-
+  /**
+   * Send a "close" message to all our connected peers.  That message
+   * depends on the protocol used (this may be a QUIT irc message, or a
+   * <stream/>, etc).  We may also directly close the connection, or we may
+   * wait for the remote peer to acknowledge it before closing.
+   */
+  void shutdown();
+  bool is_document_open() const;
   /**
    * Connect to the XMPP server.
    * Returns false if we failed to connect
@@ -115,6 +122,10 @@ private:
   std::string served_hostname;
   std::string secret;
   bool authenticated;
+  /**
+   * Whether or not OUR XMPP document is open
+   */
+  bool doc_open;
 
   std::unordered_map<std::string, std::function<void(const Stanza&)>> stanza_handlers;
 
-- 
cgit v1.2.3