QPID-6392: handle detach event

git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1684680 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/qpid/cpp/src/qpid/broker/amqp/Connection.cpp b/qpid/cpp/src/qpid/broker/amqp/Connection.cpp
index 8e2bc86..c1c0989 100644
--- a/qpid/cpp/src/qpid/broker/amqp/Connection.cpp
+++ b/qpid/cpp/src/qpid/broker/amqp/Connection.cpp
@@ -411,6 +411,9 @@
         case PN_LINK_REMOTE_OPEN:
             doLinkRemoteOpen(pn_event_link(event));
             break;
+        case PN_LINK_REMOTE_DETACH:
+             doLinkRemoteDetach(pn_event_link(event), false);
+             break;
         case PN_LINK_REMOTE_CLOSE:
             doLinkRemoteClose(pn_event_link(event));
             break;
@@ -579,16 +582,22 @@
     }
 }
 
-// the peer has issued a Detach performative
+// the peer has issued a Detach performative with closed=true
 void Connection::doLinkRemoteClose(pn_link_t *link)
 {
+    doLinkRemoteDetach(link, true);
+}
+// the peer has issued a Detach performative
+void Connection::doLinkRemoteDetach(pn_link_t *link, bool closed)
+{
     if ((pn_link_state(link) & PN_LOCAL_CLOSED) == 0) {
-        pn_link_close(link);
+        if (closed) pn_link_close(link);
+        else pn_link_detach(link);
         Sessions::iterator session = sessions.find(pn_link_session(link));
         if (session == sessions.end()) {
             QPID_LOG(error, id << " peer attempted to detach link on unknown session!");
         } else {
-            session->second->detach(link);
+            session->second->detach(link, closed);
             QPID_LOG_CAT(debug, model, id << " link detached");
         }
     }
diff --git a/qpid/cpp/src/qpid/broker/amqp/Connection.h b/qpid/cpp/src/qpid/broker/amqp/Connection.h
index e97d041..0d06f18 100644
--- a/qpid/cpp/src/qpid/broker/amqp/Connection.h
+++ b/qpid/cpp/src/qpid/broker/amqp/Connection.h
@@ -103,6 +103,7 @@
     void doSessionRemoteClose(pn_session_t *session);
     void doLinkRemoteOpen(pn_link_t *link);
     void doLinkRemoteClose(pn_link_t *link);
+    void doLinkRemoteDetach(pn_link_t *link, bool closed);
     void doDeliveryUpdated(pn_delivery_t *delivery);
 };
 }}} // namespace qpid::broker::amqp
diff --git a/qpid/cpp/src/qpid/broker/amqp/Session.cpp b/qpid/cpp/src/qpid/broker/amqp/Session.cpp
index 24da9e0..aa4ba03 100644
--- a/qpid/cpp/src/qpid/broker/amqp/Session.cpp
+++ b/qpid/cpp/src/qpid/broker/amqp/Session.cpp
@@ -591,12 +591,12 @@
     }
 }
 
-void Session::detach(pn_link_t* link)
+void Session::detach(pn_link_t* link, bool closed)
 {
     if (pn_link_is_sender(link)) {
         OutgoingLinks::iterator i = outgoing.find(link);
         if (i != outgoing.end()) {
-            i->second->detached(true/*TODO: checked whether actually closed; see PROTON-773*/);
+            i->second->detached(closed);
             boost::shared_ptr<Queue> q = OutgoingFromQueue::getExclusiveSubscriptionQueue(i->second.get());
             if (q && !q->isAutoDelete() && !q->isDeleted()) {
                 connection.getBroker().deleteQueue(q->getName(), connection.getUserId(), connection.getMgmtId());
@@ -607,7 +607,7 @@
     } else {
         IncomingLinks::iterator i = incoming.find(link);
         if (i != incoming.end()) {
-            i->second->detached(true/*TODO: checked whether actually closed; see PROTON-773*/);
+            i->second->detached(closed);
             incoming.erase(i);
             QPID_LOG(debug, "Incoming link detached");
         }
diff --git a/qpid/cpp/src/qpid/broker/amqp/Session.h b/qpid/cpp/src/qpid/broker/amqp/Session.h
index ea3fb82..2537d6c 100644
--- a/qpid/cpp/src/qpid/broker/amqp/Session.h
+++ b/qpid/cpp/src/qpid/broker/amqp/Session.h
@@ -65,7 +65,7 @@
      * called for links initiated by the peer
      */
     void attach(pn_link_t*);
-    void detach(pn_link_t*);
+    void detach(pn_link_t*, bool closed);
     void readable(pn_link_t*, pn_delivery_t*);
     void writable(pn_link_t*, pn_delivery_t*);
     bool dispatch();