VOM: Fix connection state
[vpp.git] / src / vpp-api / vom / hw.cpp
index fee0e86..0952b60 100644 (file)
@@ -27,11 +27,6 @@ HW::cmd_q::cmd_q()
 
 HW::cmd_q::~cmd_q()
 {
-  m_connected = false;
-
-  if (m_rx_thread && m_rx_thread->joinable()) {
-    m_rx_thread->join();
-  }
 }
 
 HW::cmd_q&
@@ -76,26 +71,25 @@ HW::cmd_q::enqueue(std::queue<cmd*>& cmds)
   }
 }
 
-void
-HW::cmd_q::dequeue(cmd* c)
+bool
+HW::cmd_q::connect()
 {
-  c->retire(m_conn);
-  m_pending.erase(c);
-}
+  if (m_connected)
+    return m_connected;
 
-void
-HW::cmd_q::dequeue(std::shared_ptr<cmd> c)
-{
-  c->retire(m_conn);
-  m_pending.erase(c.get());
+  if (0 == m_conn.connect()) {
+    m_connected = true;
+    m_rx_thread.reset(new std::thread(&HW::cmd_q::rx_run, this));
+  }
+  return (m_connected);
 }
 
 void
-HW::cmd_q::connect()
+HW::cmd_q::disconnect()
 {
-  if (m_connected) {
-    m_conn.disconnect();
-  }
+
+  if (!m_connected)
+    return;
 
   m_connected = false;
 
@@ -103,10 +97,7 @@ HW::cmd_q::connect()
     m_rx_thread->join();
   }
 
-  m_conn.connect();
-
-  m_connected = true;
-  m_rx_thread.reset(new std::thread(&HW::cmd_q::rx_run, this));
+  m_conn.disconnect();
 }
 
 void
@@ -127,9 +118,9 @@ HW::cmd_q::write()
   rc_t rc = rc_t::OK;
 
   /*
- * The queue is enabled, Execute each command in the queue.
- * If one execution fails, abort the rest
- */
  * The queue is enabled, Execute each command in the queue.
  * If one execution fails, abort the rest
  */
   auto it = m_queue.begin();
 
   while (it != m_queue.end()) {
@@ -139,41 +130,28 @@ HW::cmd_q::write()
 
     if (m_enabled) {
       /*
- * before we issue the command we must move it to the pending
- * store
- * ince a async event can be recieved before the command
- * completes
- */
-      m_pending[c.get()] = c;
-
+       * before we issue the command we must move it to the pending
+       * store
+       * ince a async event can be recieved before the command
+       * completes
+       */
       rc = c->issue(m_conn);
 
-      if (rc_t::INPROGRESS == rc) {
+      if (rc_t::OK == rc) {
         /*
- * this command completes asynchronously
- * leave the command in the pending store
- */
+         * move to the next
+         */
       } else {
         /*
- * the command completed, remove from the pending store
- */
-        m_pending.erase(c.get());
-
-        if (rc_t::OK == rc) {
-          /*
- * move to the next
- */
-        } else {
-          /*
- * barf out without issuing the rest
- */
-          break;
-        }
+         * barf out without issuing the rest
+         */
+        VOM_LOG(log_level_t::ERROR) << "Failed to execute: " << c->to_string();
+        break;
       }
     } else {
       /*
- * The HW is disabled, so set each command as succeeded
- */
      * The HW is disabled, so set each command as succeeded
      */
       c->succeeded();
     }
 
@@ -181,8 +159,8 @@ HW::cmd_q::write()
   }
 
   /*
- * erase all objects in the queue
- */
  * erase all objects in the queue
  */
   m_queue.erase(m_queue.begin(), m_queue.end());
 
   return (rc);
@@ -230,22 +208,16 @@ HW::enqueue(std::queue<cmd*>& cmds)
   m_cmdQ->enqueue(cmds);
 }
 
-void
-HW::dequeue(cmd* cmd)
-{
-  m_cmdQ->dequeue(cmd);
-}
-
-void
-HW::dequeue(std::shared_ptr<cmd> cmd)
+bool
+HW::connect()
 {
-  m_cmdQ->dequeue(cmd);
+  return m_cmdQ->connect();
 }
 
 void
-HW::connect()
+HW::disconnect()
 {
-  m_cmdQ->connect();
+  m_cmdQ->disconnect();
 }
 
 void
@@ -275,7 +247,6 @@ HW::poll()
   HW::write();
 
   return (m_poll_state);
-  return (true);
 }
 
 template <>