Skip to content

Commit

Permalink
Shell|GUI: Opening a connection
Browse files Browse the repository at this point in the history
Added a dialog for specifying which server to connect to. Log entries
and other packets received from the server are handled in the context
of the connection window (rather than application-wide).
  • Loading branch information
skyjake committed Feb 8, 2013
1 parent 6756f61 commit 4ec0c8e
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 4 deletions.
10 changes: 9 additions & 1 deletion doomsday/libshell/include/de/shell/protocol.h
Expand Up @@ -33,6 +33,9 @@ namespace shell {
*/
class LIBSHELL_PUBLIC LogEntryPacket : public Packet
{
public:
typedef QList<LogEntry *> Entries;

public:
LogEntryPacket();
~LogEntryPacket();
Expand All @@ -46,6 +49,11 @@ class LIBSHELL_PUBLIC LogEntryPacket : public Packet
*/
void add(LogEntry const &entry);

Entries const &entries() const;

/**
* Adds all the entries into the application's log buffer.
*/
void execute() const;

// Implements ISerializable.
Expand All @@ -55,7 +63,7 @@ class LIBSHELL_PUBLIC LogEntryPacket : public Packet
static Packet *fromBlock(Block const &block);

private:
QList<LogEntry *> _entries;
Entries _entries;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libshell/src/protocol.cpp
Expand Up @@ -57,6 +57,11 @@ void LogEntryPacket::add(LogEntry const &entry)
_entries.append(new LogEntry(entry));
}

LogEntryPacket::Entries const &LogEntryPacket::entries() const
{
return _entries;
}

void LogEntryPacket::execute() const
{
// Copies of all entries in the packet are added to the LogBuffer.
Expand Down
6 changes: 4 additions & 2 deletions doomsday/tools/shell/shell-gui/shell-gui.pro
Expand Up @@ -32,15 +32,17 @@ HEADERS += \
src/qtguiapp.h \
src/qttextcanvas.h \
src/qtrootwidget.h \
src/guishellapp.h
src/guishellapp.h \
src/opendialog.h

SOURCES += \
src/main.cpp \
src/mainwindow.cpp \
src/qtguiapp.cpp \
src/qttextcanvas.cpp \
src/qtrootwidget.cpp \
src/guishellapp.cpp
src/guishellapp.cpp \
src/opendialog.cpp

# Deployment ----------------------------------------------------------------

Expand Down
23 changes: 22 additions & 1 deletion doomsday/tools/shell/shell-gui/src/guishellapp.cpp
Expand Up @@ -18,6 +18,7 @@

#include "guishellapp.h"
#include "mainwindow.h"
#include "opendialog.h"
#include <QMenuBar>
#include <de/shell/ServerFinder>

Expand Down Expand Up @@ -104,14 +105,34 @@ MainWindow *GuiShellApp::newOrReusedConnectionWindow()
if(!found)
{
found = new MainWindow;
d->windows.prepend(found);
}

d->windows.prepend(found);
found->show();
return found;
}

GuiShellApp &GuiShellApp::app()
{
return *static_cast<GuiShellApp *>(qApp);
}

ServerFinder &GuiShellApp::serverFinder()
{
return d->finder;
}

void GuiShellApp::connectToServer()
{
MainWindow *win = newOrReusedConnectionWindow();

QScopedPointer<OpenDialog> dlg(new OpenDialog(win));
dlg->setWindowModality(Qt::WindowModal);

if(dlg->exec() == OpenDialog::Accepted)
{
win->openConnection(dlg->address());
}
}

void GuiShellApp::connectToLocalServer()
Expand Down
5 changes: 5 additions & 0 deletions doomsday/tools/shell/shell-gui/src/guishellapp.h
Expand Up @@ -20,6 +20,7 @@
#define GUISHELLAPP_H

#include "qtguiapp.h"
#include <de/shell/ServerFinder>

class MainWindow;

Expand All @@ -34,6 +35,10 @@ class GuiShellApp : public QtGuiApp
void openNewConnectionWindow();
MainWindow *newOrReusedConnectionWindow();

static GuiShellApp &app();

de::shell::ServerFinder &serverFinder();

public slots:
void connectToServer();
void connectToLocalServer();
Expand Down
85 changes: 85 additions & 0 deletions doomsday/tools/shell/shell-gui/src/mainwindow.cpp
Expand Up @@ -29,6 +29,8 @@ struct MainWindow::Instance

cli = new CommandLineWidget;
log = new LogWidget;

logBuffer.addSink(log->logSink());
}
};

Expand Down Expand Up @@ -104,5 +106,88 @@ void MainWindow::closeEvent(QCloseEvent *event)
return;
}
}

closeConnection();
event->accept();
}

void MainWindow::openConnection(QString address)
{
closeConnection();

qDebug() << "Opening connection to" << address;

// Keep trying to connect to 30 seconds.
d->link = new Link(address, 30);
//d->status->setShellLink(d->link);

connect(d->link, SIGNAL(packetsReady()), this, SLOT(handleIncomingPackets()));
connect(d->link, SIGNAL(disconnected()), this, SLOT(disconnected()));

setTitle(address);
}

void MainWindow::closeConnection()
{
if(d->link)
{
qDebug() << "Closing existing connection to" << d->link->address().asText();

// Get rid of the old connection.
disconnect(d->link, SIGNAL(packetsReady()), this, SLOT(handleIncomingPackets()));
disconnect(d->link, SIGNAL(disconnected()), this, SLOT(disconnected()));

delete d->link;
d->link = 0;
//d->status->setShellLink(0);

setTitle(tr("Disconnected"));
}
}

void MainWindow::handleIncomingPackets()
{
forever
{
DENG2_ASSERT(d->link != 0);

QScopedPointer<Packet> packet(d->link->nextPacket());
if(packet.isNull()) break;

// Process packet contents.
shell::Protocol &protocol = d->link->protocol();
switch(protocol.recognize(packet.data()))
{
case shell::Protocol::LogEntries: {
// Add the entries into the local log buffer.
LogEntryPacket *entries = static_cast<LogEntryPacket *>(packet.data());
foreach(LogEntry *e, entries->entries())
{
d->logBuffer.add(new LogEntry(*e));
}
break; }

case shell::Protocol::ConsoleLexicon:
// Terms for auto-completion.
//d->cli->setLexicon(protocol.lexicon(*packet));
break;

default:
break;
}
}
}

void MainWindow::disconnected()
{
if(!d->link) return;

// The link was disconnected.
disconnect(d->link, SIGNAL(packetsReady()), this, SLOT(handleIncomingPackets()));

d->link->deleteLater();
d->link = 0;
//d->status->setShellLink(0);

setTitle(tr("Disconnected"));
}
8 changes: 8 additions & 0 deletions doomsday/tools/shell/shell-gui/src/mainwindow.h
Expand Up @@ -16,6 +16,14 @@ class MainWindow : public QMainWindow
bool isConnected() const;
void closeEvent(QCloseEvent *);

public slots:
void openConnection(QString address);
void closeConnection();

protected slots:
void handleIncomingPackets();
void disconnected();

private:
struct Instance;
Instance *d;
Expand Down

0 comments on commit 4ec0c8e

Please sign in to comment.