First Commit

parents
#include "MainWindow.h"
#include "ui_MainWindow.h"
using namespace std;
MainWindow::MainWindow (QWidget * parent) : QMainWindow (parent)
{
_ui = QSharedPointer<Ui::MainWindow> (new Ui::MainWindow ());
_ui->setupUi (this);
setMaximumSize (width (), height ());
}
MainWindow::~MainWindow ()
{
}
#ifndef _MAIN_WINDOW_H_
#define _MAIN_WINDOW_H_
#include <QMainWindow>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow (QWidget * parent = 0);
~MainWindow ();
private:
QSharedPointer<Ui::MainWindow> _ui;
};
#endif /* _MAIN_WINDOW_H_ */
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<property name="minimumSize">
<size>
<width>400</width>
<height>300</height>
</size>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
QT += network
TARGET = PGAClient
TEMPLATE = app
VERSION = 0.0.2
Release:DESTDIR = $$_PRO_FILE_PWD_/../release
Debug:DESTDIR = $$_PRO_FILE_PWD_/../debug
SOURCES += main.cpp MainWindow.cpp
HEADERS += MainWindow.h
FORMS += MainWindow.ui
#include <QApplication>
#include "MainWindow.h"
int main (int argc, char * argv[])
{
QApplication app (argc, argv);
MainWindow window;
window.show ();
return app.exec ();
}
#include "Configuration.h"
using namespace std;
Configuration::Configuration ()
{
}
Configuration::~Configuration ()
{
}
const QString Configuration::lineSeparator = "\n";
const QString Configuration::loggingFilename = "logging.txt";
const QString Configuration::loggingFormat = "[%-7l] %t{yyyy-MM-dd, HH:mm:ss.zzz} <%C> %m" + Configuration::lineSeparator;
#ifndef _CONFIGURATION_H_
#define _CONFIGURATION_H_
#include <QString>
class Configuration
{
public:
static const QString loggingFilename;
static const QString loggingFormat;
static const QString lineSeparator;
private:
Configuration ();
~Configuration ();
};
#endif /* _CONFIGURATION_H_ */
#include "IServerAction.h"
using namespace std;
IServerAction::IServerAction ()
{
}
IServerAction::~IServerAction ()
{
}
#ifndef _ISERVER_ACTION_H_
#define _ISERVER_ACTION_H_
#include <QTcpSocket>
class Server;
class IServerAction
{
public:
virtual ~IServerAction ();
virtual bool execute (Server & server, const QTcpSocket & sender, const QString & message) = 0;
protected:
IServerAction ();
};
#endif /* _ISERVER_ACTION_H_ */
#include "IServerRequest.h"
using namespace std;
IServerRequest::IServerRequest ()
{
}
IServerRequest::~IServerRequest ()
{
}
#ifndef _ISERVER_REQUEST_H_
#define _ISERVER_REQUEST_H_
class IServerRequest
{
public:
virtual ~IServerRequest ();
protected:
IServerRequest ();
};
#endif /* _ISERVER_REQUEST_H_ */
#include "IServerState.h"
using namespace std;
IServerState::IServerState ()
{
}
IServerState::~IServerState ()
{
}
#ifndef _ISERVER_STATE_H_
#define _ISERVER_STATE_H_
class ServerManager;
class IServerState
{
public:
virtual ~IServerState ();
virtual bool stop (ServerManager & sm) = 0;
virtual bool start (ServerManager & sm) = 0;
protected:
IServerState ();
};
#endif /* _ISERVER_STATE_H_ */
#include <QDebug>
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "ServerManager.h"
#include "ServerLogger.h"
using namespace std;
MainWindow::MainWindow (QWidget * parent) : QMainWindow (parent)
{
_ui = QSharedPointer<Ui::MainWindow> (new Ui::MainWindow ());
_ui->setupUi (this);
setMaximumSize (width (), height ());
_sm = QSharedPointer<ServerManager> (new ServerManager ());
}
MainWindow::~MainWindow ()
{
}
void MainWindow::on_startButton_clicked ()
{
const QString host = _ui->hostEdit->text ();
const QString port = _ui->portEdit->text ();
try
{
_sm->setHost (host);
_sm->setPort (port);
_sm->start ();
}
catch (exception & e)
{
LOG_ERROR () << e.what ();
}
}
void MainWindow::on_stopButton_clicked ()
{
try
{
_sm->stop ();
}
catch (exception & e)
{
LOG_ERROR () << e.what ();
}
}
#ifndef _MAIN_WINDOW_H_
#define _MAIN_WINDOW_H_
#include <QMainWindow>
class ServerManager;
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow (QWidget * parent = 0);
~MainWindow ();
private slots:
void on_startButton_clicked ();
void on_stopButton_clicked ();
private:
QSharedPointer<Ui::MainWindow> _ui;
QSharedPointer<ServerManager> _sm;
};
#endif /* _MAIN_WINDOW_H_ */
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>315</width>
<height>142</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>315</width>
<height>142</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>315</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle">
<string>Server Application</string>
</property>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonIconOnly</enum>
</property>
<property name="unifiedTitleAndToolBarOnMac">
<bool>false</bool>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLabel" name="hostLabel">
<property name="geometry">
<rect>
<x>25</x>
<y>21</y>
<width>59</width>
<height>16</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>59</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Host</string>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="portLabel">
<property name="geometry">
<rect>
<x>160</x>
<y>21</y>
<width>59</width>
<height>16</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>59</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Port</string>
</property>
</widget>
<widget class="QLineEdit" name="hostEdit">
<property name="geometry">
<rect>
<x>25</x>
<y>42</y>
<width>129</width>
<height>25</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>113</width>
<height>0</height>
</size>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="echoMode">
<enum>QLineEdit::Normal</enum>
</property>
</widget>
<widget class="QLineEdit" name="portEdit">
<property name="geometry">
<rect>
<x>160</x>
<y>42</y>
<width>129</width>
<height>25</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>113</width>
<height>0</height>
</size>
</property>
</widget>
<widget class="QPushButton" name="startButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>69</x>
<y>90</y>
<width>85</width>
<height>27</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="toolTip">
<string/>
</property>
<property name="statusTip">
<string/>
</property>
<property name="whatsThis">
<string/>
</property>
<property name="text">
<string extracomment="This button start the server.">Start</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
<widget class="QPushButton" name="stopButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>161</x>
<y>90</y>
<width>85</width>
<height>27</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="toolTip">
<string/>
</property>
<property name="statusTip">
<string/>
</property>
<property name="whatsThis">
<string/>
</property>
<property name="text">
<string extracomment="This button start the server.">Stop</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
<property name="default">
<bool>true</bool>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
QT += network
TARGET = PGAServer
VERSION = 0.0.2
TEMPLATE = app
Release:DESTDIR = $$_PRO_FILE_PWD_/../release
Debug:DESTDIR = $$_PRO_FILE_PWD_/../debug
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../SharedSources/CuteLogger/release/ -lCuteLogger
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../SharedSources/CuteLogger/debug/ -lCuteLogger
else:unix:!symbian: LIBS += -L$$PWD/../../SharedSources/CuteLogger/release/ -lCuteLogger
INCLUDEPATH += $$PWD/../../SharedSources/CuteLogger/sources
DEPENDPATH += $$PWD/../../SharedSources/CuteLogger/sources
win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../../SharedSources/CuteLogger/release/CuteLogger.lib
else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../../SharedSources/CuteLogger/debug/CuteLogger.lib
else:unix:!symbian: PRE_TARGETDEPS += $$PWD/../../SharedSources/CuteLogger/release/libCuteLogger.a
SOURCES += main.cpp \
MainWindow.cpp \
ServerManager.cpp \
Server.cpp \
IServerRequest.cpp \
IServerAction.cpp \
ServerBestRequest.cpp \
ServerBestAction.cpp \
ServerRequestFactory.cpp \
ServerActionFactory.cpp \
IServerState.cpp \
ServerStartState.cpp \
ServerStopState.cpp \
Configuration.cpp \
ServerLogger.cpp
HEADERS += MainWindow.h \
ServerManager.h \
Server.h \
IServerRequest.h \
IServerAction.h \
ServerBestRequest.h \
ServerBestAction.h \
ServerRequestFactory.h \
ServerActionFactory.h \
IServerState.h \
ServerStartState.h \
ServerStopState.h \
Configuration.h \
ServerLogger.h
FORMS += MainWindow.ui
#include "ServerRequestFactory.h"
#include "ServerActionFactory.h"
#include "ServerBestRequest.h"
#include "ServerBestAction.h"
#include "Configuration.h"
#include "ServerLogger.h"
#include "Server.h"
using namespace std;
Server::Server () : QTcpServer ()
{
ServerRequestFactory::registerRequest (
QString ("^@best <dimensions='(.*)', values='(.*)'>$"),
QSharedPointer<IServerRequest> (new ServerBestRequest ())
);
ServerActionFactory::registerAction (
QSharedPointer<IServerRequest> (new ServerBestRequest ()),
QSharedPointer<IServerAction> (new ServerBestAction ())
);
}
Server::~Server ()
{
closeConnections ();
}
void Server::closeConnections ()
{
foreach (QTcpSocket * client, _clients)
{
client->disconnectFromHost();
}
}
unsigned short Server::getPort ()
{
return _port;
}
QHostAddress Server::getHost ()
{
return _host;
}
void Server::setPort (const unsigned short & port)
{
_port = port;
}
void Server::setHost (const QHostAddress & host)
{
_host = host;
}
void Server::broadcast (const QTcpSocket & sender, const QString & message)
{
foreach (QTcpSocket * client, _clients)
{
if (client->socketDescriptor() != sender.socketDescriptor())
{
client->write ((message + Configuration::lineSeparator).toUtf8 ());
}
}
}
void Server::incomingConnection (const int socketDescriptor)
{
QTcpSocket * client = new QTcpSocket (this);
client->setSocketDescriptor (socketDescriptor);
_clients.insert (client);
LOG_INFO () << "Client connection:" << client->peerAddress ().toString ();
connect (client, SIGNAL (readyRead ()), this, SLOT (readyRead ()));
connect (client, SIGNAL (disconnected ()), this, SLOT (disconnected ()));
connect (client, SIGNAL (disconnected ()), client, SLOT (deleteLater ()));
}
void Server::readyRead ()
{
QTcpSocket * client = dynamic_cast<QTcpSocket *> (sender ());
if (client->canReadLine ())
{
QString message = QString::fromUtf8 (client->readLine ()).trimmed ();
QSharedPointer<IServerRequest> request = ServerRequestFactory::getInstance (message);
if (!request.isNull ())
{
QSharedPointer<IServerAction> action = ServerActionFactory::getInstance (request);
if (!action.isNull ())
{
action->execute (*this, *client, message);
}
}
}
}
void Server::disconnected ()
{
QTcpSocket * client = dynamic_cast<QTcpSocket *> (sender ());
_clients.remove (client);
LOG_INFO () << "Client disconnection:" << client->peerAddress ().toString ();
}
#ifndef _SERVER_H_
#define _SERVER_H_
#include <QTcpSocket>
#include <QTcpServer>
#include <QSet>