diff options
Diffstat (limited to 'lib/libtqtrla/src/tqtrla.cpp')
| -rw-r--r-- | lib/libtqtrla/src/tqtrla.cpp | 166 |
1 files changed, 165 insertions, 1 deletions
diff --git a/lib/libtqtrla/src/tqtrla.cpp b/lib/libtqtrla/src/tqtrla.cpp index 376a9d9..4e024d3 100644 --- a/lib/libtqtrla/src/tqtrla.cpp +++ b/lib/libtqtrla/src/tqtrla.cpp @@ -23,6 +23,13 @@ #include <tqwidget.h> #include <klocale.h> +#include <kmessagebox.h> + +// RAJA UNCOMMENT ME +//#define SERVER_TIMEOUT_MS 10000 +// RAJA DEBUG ONLY +#define SERVER_TIMEOUT_MS 100000 +#define FPGA_DATA_PROCESSING_TIMEOUT_MS 2500 namespace KParts { @@ -39,9 +46,14 @@ namespace KParts }; RemoteInstrumentPart::RemoteInstrumentPart(TQObject *parent, const char *name) - : Part( parent, name ) + : Part( parent, name ), + m_socket(NULL), connToServerConnecting(false), connToServerState(-1), connToServerTimeoutTimer(NULL) { d = new RemoteInstrumentPartPrivate; + + // Create timers + m_connectionTimer = new TQTimer(this); + connect(m_connectionTimer, SIGNAL(timeout()), this, SLOT(finishConnectingToServer())); } RemoteInstrumentPart::~RemoteInstrumentPart() { @@ -108,6 +120,158 @@ namespace KParts return ret; } + + int RemoteInstrumentPart::connectToServer(TQString server) { + if (m_socket) { + return -1; + } + if (!m_socket) { + m_socket = new TDEKerberosClientSocket(this); + connect(m_socket, TQT_SIGNAL(statusMessageUpdated(const TQString&)), this, TQT_SLOT(setStatusMessage(const TQString&) )); + } + m_socket->setServiceName("remotefpga"); + m_socket->setServerFQDN(server); + m_socket->connectToHost(server, 4004); + + // Finish connecting when appropriate + connToServerState = 0; + connToServerConnecting = true; + m_connectionTimer->start(100, TRUE); + + return 0; + } + + void RemoteInstrumentPart::disconnectFromServer() { + disconnectFromServerCallback(); + m_connectionTimer->stop(); + if (m_socket) { + m_socket->clearPendingData(); + m_socket->close(); + delete m_socket; + m_socket = NULL; + } + connectionStatusChangedCallback(); + } + + void RemoteInstrumentPart::finishConnectingToServer() { + if (!m_socket) { + connToServerState = -1; + connToServerConnecting = false; + connectionStatusChangedCallback(); + return; + } + + if (connToServerConnecting) { + switch(connToServerState) { + case 0: + if (!connToServerTimeoutTimer) { + connToServerTimeoutTimer = new TQTimer; + connToServerTimeoutTimer->start(SERVER_TIMEOUT_MS, TRUE); + } + if ((m_socket->state() == TQSocket::Connecting) || (m_socket->state() == TQSocket::HostLookup)) { + if (!connToServerTimeoutTimer->isActive()) { + connToServerState = -3; + connToServerConnecting = false; + disconnectFromServer(); + KMessageBox::error(0, i18n("<qt>Unable to establish connection to remote server</qt>"), i18n("Connection Failed")); + } + } + else { + if (m_socket->state() == TQSocket::Connected) { + printf("[DEBUG] Initial connection established...\n\r"); fflush(stdout); + m_socket->setDataTimeout(SERVER_TIMEOUT_MS); + m_socket->setUsingKerberos(true); + connToServerState = 1; + } + else { + connToServerState = -1; + connToServerConnecting = false; + disconnectFromServer(); + KMessageBox::error(0, i18n("<qt>Unable to establish connection to remote server</qt>"), i18n("Connection Failed")); + } + } + break; + case 1: + if (m_socket->kerberosStatus() == TDEKerberosClientSocket::KerberosInitializing) { + // Do nothing + } + else { + if (m_socket->kerberosStatus() != TDEKerberosClientSocket::KerberosInUse) { + connToServerState = -1; + connToServerConnecting = false; + disconnectFromServer(); + KMessageBox::error(0, i18n("<qt>Unable to establish Kerberos protocol with remote server<p>Please verify that you currently hold a valid Kerberos ticket</qt>"), i18n("Connection Failed")); + } + else { + connToServerState = 2; + } + } + break; + case 2: + // Connection established! + // Read magic number and proto version from server + m_socket->processPendingData(); + if (m_socket->canReadFrame()) { + TQDataStream ds(m_socket); + ds.setPrintableData(true); + TQ_UINT32 magicnum; + TQ_UINT32 protover; + ds >> magicnum; + ds >> protover; + m_socket->clearFrameTail(); + printf("[DEBUG] Got magic number %d and protocol version %d\n\r", magicnum, protover); fflush(stdout); + + // Request connection to backend server + ds << TQString("SERV"); + m_socket->writeEndOfFrame(); + ds << m_clientLibraryName; + m_socket->writeEndOfFrame(); + connToServerState = 3; + } + break; + case 3: + // Read response from server + m_socket->processPendingData(); + if (m_socket->bytesAvailable() > 0) { + TQDataStream ds(m_socket); + ds.setPrintableData(true); + TQString response; + ds >> response; + m_socket->clearFrameTail(); + if (response == "OK") { + connToServerState = 4; + connToServerConnecting = false; + connectionFinishedCallback(); + return; + } + else { + TQStringList errorStrings = textForServerError(response); + connToServerState = -1; + connToServerConnecting = false; + disconnectFromServer(); + KMessageBox::error(0, errorStrings[0], errorStrings[1]); + close(); + return; + } + } + break; + } + + m_connectionTimer->start(100, TRUE); + } + } + + void RemoteInstrumentPart::connectionFinishedCallback() { + // + } + + void RemoteInstrumentPart::disconnectFromServerCallback() { + // + } + + void RemoteInstrumentPart::connectionStatusChangedCallback() { + // + } } bool operator==( const ServiceType &s1, const ServiceType &s2 ) { |
