]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/bcomm/dircomm.cpp
Fix bat bug that consumes connections + add braces on ifs + rename subroutines
[bacula/bacula] / bacula / src / qt-console / bcomm / dircomm.cpp
index 387794425339a859e5126f27d23ab1302c267348..2f49c0c5574d2fd8e75fd46f1fd52d7da007dff9 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
@@ -26,9 +26,7 @@
    Switzerland, email:ftf@fsfeurope.org.
 */
 /*
- *   Version $Id$
- *
- *  Console Class
+ *  DirComm, Director communications,class
  *
  *   Kern Sibbald, January MMVII
  *
@@ -38,6 +36,7 @@
 #include "console.h"
 #include "restore.h"
 #include "select.h"
+#include "textinput.h"
 #include "run/run.h"
 
 static int tls_pem_callback(char *buf, int size, const void *userdata);
@@ -50,6 +49,7 @@ DirComm::DirComm(Console *parent, int conn):  m_notifier(NULL),  m_api_set(false
    m_at_main_prompt = false;
    m_conn = conn;
    m_in_command = 0;
+   m_in_select = false;
 }
 
 DirComm::~DirComm()
@@ -88,15 +88,21 @@ bool DirComm::connect_dir()
       mainWin->set_status( tr("Already connected."));
       m_console->display_textf(_("Already connected\"%s\".\n"),
             m_console->m_dir->name());
-      if (mainWin->m_connDebug)
+      if (mainWin->m_connDebug) {
          Pmsg2(000, "DirComm %i BAILING already connected %s\n", m_conn, m_console->m_dir->name());
+      }
       goto bail_out;
    }
 
+   if (mainWin->m_connDebug) {
+      Pmsg2(000, "DirComm %i connecting %s\n", m_conn, m_console->m_dir->name());
+   }
    memset(jcr, 0, sizeof(JCR));
 
    mainWin->set_statusf(_("Connecting to Director %s:%d"), m_console->m_dir->address, m_console->m_dir->DIRport);
-   m_console->display_textf(_("Connecting to Director %s:%d\n\n"), m_console->m_dir->address, m_console->m_dir->DIRport);
+   if (m_conn == 0) {
+      m_console->display_textf(_("Connecting to Director %s:%d\n\n"), m_console->m_dir->address, m_console->m_dir->DIRport);
+   }
 
    /* Give GUI a chance */
    app->processEvents();
@@ -194,11 +200,12 @@ bool DirComm::connect_dir()
 
    mainWin->set_status(_("Initializing ..."));
 
-#ifndef HAVE_WIN32
-   /* Set up input notifier */
+   /* 
+    * Set up input notifier
+    */
    m_notifier = new QSocketNotifier(m_sock->m_fd, QSocketNotifier::Read, 0);
    QObject::connect(m_notifier, SIGNAL(activated(int)), this, SLOT(read_dir(int)));
-#endif
+   m_notifier->setEnabled(false);
 
    write(".api 1");
    m_api_set = true;
@@ -208,13 +215,15 @@ bool DirComm::connect_dir()
 
    mainWin->set_status(_("Connected"));
 
-   if (mainWin->m_connDebug)
+   if (mainWin->m_connDebug) {
       Pmsg2(000, "Returning TRUE from DirComm->connect_dir : %i %s\n", m_conn, m_console->m_dir->name());
+   }
    return true;
 
 bail_out:
-   if (mainWin->m_connDebug)
+   if (mainWin->m_connDebug) {
       Pmsg2(000, "Returning FALSE from DirComm->connect_dir : %i %s\n", m_conn, m_console->m_dir->name());
+   }
    delete jcr;
    return false;
 }
@@ -310,9 +319,7 @@ int DirComm::read()
          if (mainWin->m_commDebug) Pmsg1(000, "conn %i CMD OK\n", m_conn);
          m_at_prompt = false;
          m_at_main_prompt = false;
-//       Pmsg1(000, "before dec m_in_command=%d\n", m_in_command);
          if (--m_in_command < 0) {
-//          Pmsg0(000, "m_in_command < 0\n");
             m_in_command = 0;
          }
          mainWin->set_status(_("Command completed ..."));
@@ -322,7 +329,6 @@ int DirComm::read()
          m_at_prompt = false;
          m_at_main_prompt = false;
          m_in_command++;
-//       Pmsg1(000, "after inc m_in_command=%d\n", m_in_command);
          mainWin->set_status(_("Processing command ..."));
          continue;
       case BNET_MAIN_PROMPT:
@@ -332,10 +338,18 @@ int DirComm::read()
          mainWin->set_status(_("At main prompt waiting for input ..."));
          break;
       case BNET_PROMPT:
-         if (mainWin->m_commDebug) Pmsg1(000, "conn %i PROMPT\n", m_conn);
+         if (mainWin->m_commDebug) Pmsg2(000, "conn %i PROMPT m_in_select %i\n", m_conn, m_in_select);
          m_at_prompt = true;
          m_at_main_prompt = false;
          mainWin->set_status(_("At prompt waiting for input ..."));
+         /***** FIXME *****/
+         /* commented out until the prompt communication issue with the director is resolved 
+          * This is where I call a new text input dialog class to prevent the connection issues
+          * when a text input is requited.
+          *   if (!m_in_select) {
+          *      new textInputDialog(m_console, m_conn);
+          *   }
+          */
          break;
       case BNET_CMD_FAILED:
          if (mainWin->m_commDebug) Pmsg1(000, "CMD FAILED\n", m_conn);
@@ -353,9 +367,10 @@ int DirComm::read()
          }
          continue;
       case BNET_START_SELECT:
-         notify(false);
          if (mainWin->m_commDebug) Pmsg1(000, "conn %i START SELECT\n", m_conn);
+         m_in_select = true;
          new selectDialog(m_console, m_conn);
+         m_in_select = false;
          break;
       case BNET_YESNO:
          if (mainWin->m_commDebug) Pmsg1(000, "conn %i YESNO\n", m_conn);
@@ -381,8 +396,9 @@ int DirComm::read()
       case BNET_WARNING_MSG:
          if (mainWin->m_commDebug) Pmsg1(000, "conn %i WARNING MSG\n", m_conn);
          stat = sock_read();          /* get the message */
-         m_console->display_text(msg());
-         QMessageBox::critical(m_console, "Warning", msg(), QMessageBox::Ok);
+         if (!m_console->m_warningPrevent) {
+            QMessageBox::critical(m_console, "Warning", msg(), QMessageBox::Ok);
+         }
          break;
       case BNET_INFO_MSG:
          if (mainWin->m_commDebug) Pmsg1(000, "conn %i INFO MSG\n", m_conn);
@@ -406,7 +422,6 @@ int DirComm::read()
             m_notifier = NULL;
          }
          mainWin->set_status(_("Director disconnected."));
-//         QApplication::restoreOverrideCursor();
          stat = BNET_HARDEOF;
       }
       break;
@@ -417,10 +432,16 @@ int DirComm::read()
 /* Called by signal when the Director has output for us */
 void DirComm::read_dir(int /* fd */)
 {
-   if (mainWin->m_commDebug) Pmsg1(000, "conn %i read_dir\n", m_conn);
-   while (read() >= 0) {
-      m_console->display_text(msg());
+   int stat;
+   if (mainWin->m_commDebug) Pmsg1(000, "enter read_dir conn %i read_dir\n", m_conn);
+   stat = m_sock->wait_data(0, 5000);
+   if (stat > 0) {
+      if (mainWin->m_commDebug) Pmsg2(000, "read_dir conn %i stat=%d\n", m_conn, stat);
+      while (read() >= 0) {
+         m_console->display_text(msg());
+      }
    }
+   if (mainWin->m_commDebug) Pmsg2(000, "exit read_dir conn %i stat=%d\n", m_conn, stat);
 }
 
 /*
@@ -440,10 +461,7 @@ bool DirComm::notify(bool enable)
       prev_enabled = m_notifier->isEnabled();   
       m_notifier->setEnabled(enable);
       if (mainWin->m_connDebug) {
-         if (prev_enabled && !enable)
-            Pmsg2(000, "m_notifier Disabling notifier: %i %s\n", m_conn, m_console->m_dir->name());
-         else if (!prev_enabled && enable)
-            Pmsg2(000, "m_notifier Enabling notifier: %i %s\n", m_conn, m_console->m_dir->name());
+         Pmsg3(000, "conn=%i notify=%d prev=%d\n", m_conn, enable, prev_enabled);
       }
    } else if (mainWin->m_connDebug)
       Pmsg2(000, "m_notifier does not exist: %i %s\n", m_conn, m_console->m_dir->name());
@@ -453,8 +471,9 @@ bool DirComm::notify(bool enable)
 bool DirComm::is_notify_enabled() const
 {
    bool enabled = false;
-   if (m_notifier)
+   if (m_notifier) {
       enabled = m_notifier->isEnabled();   
+   }
    return enabled;
 }
 
@@ -468,7 +487,6 @@ static int tls_pem_callback(char *buf, int size, const void *userdata)
    (void)size;
    (void)userdata;
 #ifdef HAVE_TLS
-   const char *prompt = (const char *)userdata;
 # if defined(HAVE_WIN32)
    //sendit(prompt);
    if (win32_cgets(buf, size) == NULL) {
@@ -478,6 +496,7 @@ static int tls_pem_callback(char *buf, int size, const void *userdata)
       return strlen(buf);
    }
 # else
+   const char *prompt = (const char *)userdata;
    char *passwd;
 
    passwd = getpass(prompt);