mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-02 23:25:53 +08:00
committed by
Mathew Sutcliffe
parent
978f3c88e5
commit
c6da7b0d35
@@ -1,111 +0,0 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / authors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
/*!
|
||||
\file
|
||||
*/
|
||||
|
||||
#ifndef BLACKMISC_ATCLIST_H
|
||||
#define BLACKMISC_ATCLIST_H
|
||||
|
||||
#include "pqfrequency.h"
|
||||
#include "pqlength.h"
|
||||
#include "coordinategeodetic.h"
|
||||
#include "context.h"
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QMap>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* Value object encapsulating information about an ATC station.
|
||||
*/
|
||||
class CAtcListEntry
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* Default constructor.
|
||||
*/
|
||||
CAtcListEntry() {}
|
||||
|
||||
/*!
|
||||
* Constructor.
|
||||
*/
|
||||
CAtcListEntry(const QString &callsign, const BlackMisc::PhysicalQuantities::CFrequency &freq,
|
||||
const BlackMisc::Geo::CCoordinateGeodetic &pos, const BlackMisc::PhysicalQuantities::CLength &range)
|
||||
: m_callsign(callsign), m_freq(freq), m_pos(pos), m_range(range)
|
||||
{}
|
||||
|
||||
/*!
|
||||
* Get callsign.
|
||||
* \return
|
||||
*/
|
||||
const QString &getCallsign() const { return m_callsign; }
|
||||
|
||||
/*!
|
||||
* Get frequency.
|
||||
* \return
|
||||
*/
|
||||
const BlackMisc::PhysicalQuantities::CFrequency &getFrequency() const { return m_freq; }
|
||||
|
||||
/*!
|
||||
* Get the position of the center of the controller's area of visibility.
|
||||
* \return
|
||||
*/
|
||||
const BlackMisc::Geo::CCoordinateGeodetic &getPosition() const { return m_pos; }
|
||||
|
||||
/*!
|
||||
* Get the radius of the controller's area of visibility.
|
||||
* \return
|
||||
*/
|
||||
const BlackMisc::PhysicalQuantities::CLength &getAtcRange() const { return m_range; }
|
||||
|
||||
private:
|
||||
QString m_callsign;
|
||||
BlackMisc::PhysicalQuantities::CFrequency m_freq;
|
||||
BlackMisc::Geo::CCoordinateGeodetic m_pos;
|
||||
BlackMisc::PhysicalQuantities::CLength m_range;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Value object encapsulating a list of ATC stations.
|
||||
*/
|
||||
class CAtcList
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* Immutable getter for the internal map.
|
||||
* \return
|
||||
*/
|
||||
const QMap<QString, CAtcListEntry> &constMap() const { return m_map; }
|
||||
|
||||
/*!
|
||||
* Insert an ATC station into the list.
|
||||
* \param entry
|
||||
*/
|
||||
void insert(const CAtcListEntry &entry) { m_map.insert(entry.getCallsign(), entry); }
|
||||
|
||||
/*!
|
||||
* Remove an ATC station from the list.
|
||||
* \param callsign
|
||||
*/
|
||||
void remove(const QString &callsign) { m_map.remove(callsign); }
|
||||
|
||||
/*!
|
||||
* Remove all ATC stations from the list.
|
||||
*/
|
||||
void clear() { m_map.clear(); }
|
||||
|
||||
private:
|
||||
QMap<QString, CAtcListEntry> m_map;
|
||||
};
|
||||
|
||||
} //namespace BlackMisc
|
||||
|
||||
Q_DECLARE_METATYPE(BlackMisc::CAtcList)
|
||||
|
||||
#endif //BLACKMISC_ATCLIST_H
|
||||
@@ -1,186 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#include "blackmisc/com_client.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include <QTcpSocket>
|
||||
#include <QDataStream>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
CComClient::CComClient(IContext &context, QObject *parent)
|
||||
: IComHandler(context, parent), m_context(context), m_tcp_socket(NULL), m_port(0)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
CComClient::~CComClient()
|
||||
{
|
||||
if ( isConnected() )
|
||||
disconnect();
|
||||
|
||||
if ( m_tcp_socket != NULL )
|
||||
delete m_tcp_socket;
|
||||
|
||||
m_tcp_socket = NULL;
|
||||
}
|
||||
|
||||
bool CComClient::init()
|
||||
{
|
||||
m_tcp_socket = new QTcpSocket(this);
|
||||
Q_ASSERT(m_tcp_socket);
|
||||
|
||||
Q_ASSERT ( QObject::connect( m_tcp_socket, SIGNAL( connected() ), this, SLOT( onConnected() ) ) );
|
||||
Q_ASSERT ( QObject::connect( m_tcp_socket, SIGNAL( disconnected() ), this, SLOT( onDisconnected() ) ) );
|
||||
Q_ASSERT ( QObject::connect( m_tcp_socket, SIGNAL( error(QAbstractSocket::SocketError) ), this, SLOT( onError(QAbstractSocket::SocketError) ) ) );
|
||||
|
||||
Q_ASSERT ( QObject::connect( m_tcp_socket, SIGNAL( readyRead() ), this, SLOT( onReceivingData() ) ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CComClient::connectTo( const QString& hostName, quint16 port)
|
||||
{
|
||||
Q_ASSERT ( ! hostName.isEmpty() );
|
||||
Q_ASSERT ( port > 0 );
|
||||
|
||||
m_hostName = hostName;
|
||||
m_port = port;
|
||||
|
||||
if ( isConnected() )
|
||||
return;
|
||||
|
||||
bDebug(m_context) << "Connecting to host: " << hostName << ":" << port;
|
||||
|
||||
m_receive_buffer.clear();
|
||||
|
||||
m_tcp_socket->connectToHost(hostName, port);
|
||||
|
||||
}
|
||||
|
||||
bool CComClient::isConnected() const
|
||||
{
|
||||
return m_tcp_socket->state() == QAbstractSocket::ConnectedState;
|
||||
}
|
||||
|
||||
void CComClient::disconnectFrom()
|
||||
{
|
||||
bDebug(m_context) << "Disconnecting from host.";
|
||||
|
||||
m_tcp_socket->disconnectFromHost();
|
||||
m_port = 0;
|
||||
m_hostName.clear();
|
||||
}
|
||||
|
||||
bool CComClient::sendMessage(const QString& messageID, const QByteArray &message)
|
||||
{
|
||||
if (!isConnected())
|
||||
{
|
||||
bError(m_context) << "Cannot send data in disconnected state!";
|
||||
return false;
|
||||
}
|
||||
|
||||
createFrame(messageID, message);
|
||||
|
||||
qint64 sender_buffer_size = m_sender_buffer.size();
|
||||
|
||||
qint64 bytes = m_tcp_socket->write(m_sender_buffer);
|
||||
if (bytes < 0 || bytes != sender_buffer_size)
|
||||
{
|
||||
bWarning(m_context) << "Error writing to socket!";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QString CComClient::getErrorMessage(QAbstractSocket::SocketError error)
|
||||
{
|
||||
QString message;
|
||||
|
||||
/*
|
||||
ConnectionRefusedError,
|
||||
RemoteHostClosedError,
|
||||
HostNotFoundError,
|
||||
SocketAccessError,
|
||||
SocketResourceError,
|
||||
SocketTimeoutError,
|
||||
DatagramTooLargeError,
|
||||
NetworkError,
|
||||
AddressInUseError,
|
||||
SocketAddressNotAvailableError,
|
||||
UnsupportedSocketOperationError,
|
||||
UnfinishedSocketOperationError,
|
||||
ProxyAuthenticationRequiredError,
|
||||
SslHandshakeFailedError,
|
||||
ProxyConnectionRefusedError,
|
||||
ProxyConnectionClosedError,
|
||||
ProxyConnectionTimeoutError,
|
||||
ProxyNotFoundError,
|
||||
ProxyProtocolError,
|
||||
UnknownSocketError = -1 */
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case QAbstractSocket::ConnectionRefusedError:
|
||||
message = "Connection refused by host!";
|
||||
break;
|
||||
|
||||
case QAbstractSocket::RemoteHostClosedError:
|
||||
message = "Connection closed by host!";
|
||||
break;
|
||||
|
||||
case QAbstractSocket::HostNotFoundError:
|
||||
message = "Host not found!";
|
||||
break;
|
||||
|
||||
default:
|
||||
case QAbstractSocket::UnknownSocketError:
|
||||
message = "Unknown error!";
|
||||
break;
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
void CComClient::onConnected()
|
||||
{
|
||||
bDebug(m_context) << "Connected successfully to remote host.";
|
||||
emit doConnected();
|
||||
}
|
||||
|
||||
void CComClient::onDisconnected()
|
||||
{
|
||||
bDebug(m_context) << "Disconnected successfully from remote host.";
|
||||
emit doDisconnected();
|
||||
}
|
||||
|
||||
void CComClient::onError(QAbstractSocket::SocketError error)
|
||||
{
|
||||
if ( error != 0 )
|
||||
{
|
||||
bError(m_context) << "Received socket error: " << error << " - Disconnecting...";
|
||||
}
|
||||
|
||||
disconnect();
|
||||
emit doError(error, getErrorMessage(error) );
|
||||
}
|
||||
|
||||
void CComClient::onReceivingData()
|
||||
{
|
||||
QString messageID;
|
||||
QByteArray message;
|
||||
m_receive_buffer.append(m_tcp_socket->readAll());
|
||||
while (parseFrame(messageID, message))
|
||||
{
|
||||
emit doReceivedMessage(messageID, message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef COM_CLIENT_H
|
||||
#define COM_CLIENT_H
|
||||
|
||||
#include "blackmisc/com_handler.h"
|
||||
#include <QString>
|
||||
#include <QAbstractSocket>
|
||||
|
||||
class QTcpSocket;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class IContext;
|
||||
|
||||
class CComClient : public IComHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
//! Constructor
|
||||
/*!
|
||||
\param parent Pointer to the parent QObject
|
||||
*/
|
||||
CComClient(IContext &context, QObject *parent = 0);
|
||||
|
||||
//! Destructor
|
||||
~CComClient();
|
||||
|
||||
//! This method initializes the client
|
||||
virtual bool init();
|
||||
|
||||
//! Connects to the host address.
|
||||
/*!
|
||||
\param host Host address of the remote host.
|
||||
\param port Port number of the remote host.
|
||||
\return Returns true if connecting was successfull, otherwise false.
|
||||
*/
|
||||
void connectTo(const QString &hostName, quint16 port);
|
||||
|
||||
//! Returns the connection status
|
||||
/*!
|
||||
\return Returns true if connected, otherwise false
|
||||
*/
|
||||
bool isConnected() const;
|
||||
|
||||
//! Disconnects from the current server
|
||||
void disconnectFrom();
|
||||
|
||||
//! Sends a message to the remote host.
|
||||
/*!
|
||||
\param data Reference to the raw byte data to be sent.
|
||||
\return Returns true if sending was successfull, otherwise false.
|
||||
*/
|
||||
bool sendMessage(const QString &messageID, const QByteArray &message);
|
||||
|
||||
//! Returns a human readable description of the last error that occurred.
|
||||
QString getErrorMessage(QAbstractSocket::SocketError error);
|
||||
|
||||
signals:
|
||||
|
||||
//! emitted when new data were received
|
||||
void doReceivedMessage(QString &messageID, QByteArray &message);
|
||||
|
||||
//! emitted when cliebt connected
|
||||
void doConnected();
|
||||
|
||||
//! emitted when client disconnected
|
||||
void doDisconnected();
|
||||
|
||||
//! emitted when an error has occured
|
||||
void doError(QAbstractSocket::SocketError error, const QString &error_message);
|
||||
|
||||
protected slots:
|
||||
|
||||
//! Call this slot, when connected succesfully
|
||||
void onConnected();
|
||||
|
||||
//! Call this slot, when disconnected succesfully
|
||||
void onDisconnected();
|
||||
|
||||
//! Call this slot, when an error appeared
|
||||
void onError(QAbstractSocket::SocketError error);
|
||||
|
||||
//! Call this slot, when data has been received
|
||||
void onReceivingData();
|
||||
|
||||
private:
|
||||
IContext &m_context;
|
||||
|
||||
protected:
|
||||
|
||||
//! TCP Socket
|
||||
/*!
|
||||
Pointer to the tcp socket.
|
||||
*/
|
||||
QTcpSocket *m_tcp_socket;
|
||||
|
||||
//! Remote hostname
|
||||
QString m_hostName;
|
||||
|
||||
//! Remote host port
|
||||
quint16 m_port;
|
||||
|
||||
//! This variable holds the last appeared error
|
||||
QString m_last_error;
|
||||
|
||||
private:
|
||||
|
||||
CComClient(const CComClient &other);
|
||||
const CComClient &operator = (const CComClient &other);
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // COM_CLIENT_H
|
||||
@@ -1,59 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#include "blackmisc/com_client_buffer.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include <QTcpSocket>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
CComClientBuffer::CComClientBuffer(IContext &context, uint clientID, QTcpSocket *socket, QObject *parent)
|
||||
: IComHandler(context, parent), m_context(context), m_tcp_socket(socket), m_client_id(clientID)
|
||||
{
|
||||
connect(m_tcp_socket, SIGNAL(readyRead()), this, SLOT(onReceivingData()));
|
||||
connect(m_tcp_socket, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
|
||||
}
|
||||
CComClientBuffer::~CComClientBuffer()
|
||||
{
|
||||
m_tcp_socket->deleteLater();
|
||||
}
|
||||
|
||||
bool CComClientBuffer::sendMessage(const QString &id, const QByteArray &message)
|
||||
{
|
||||
createFrame(id, message);
|
||||
|
||||
qint64 sender_buffer_size = m_sender_buffer.size();
|
||||
|
||||
qint64 bytes = m_tcp_socket->write(m_sender_buffer);
|
||||
if (bytes < 0 || bytes != sender_buffer_size)
|
||||
{
|
||||
bWarning(m_context) << "Error writing to socket!";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CComClientBuffer::onReceivingData()
|
||||
{
|
||||
QByteArray message;
|
||||
QString messageID;
|
||||
|
||||
m_receive_buffer.append(m_tcp_socket->readAll());
|
||||
while (parseFrame(messageID, message))
|
||||
{
|
||||
emit doReceivedMessage(m_client_id, messageID, message);
|
||||
}
|
||||
}
|
||||
|
||||
void CComClientBuffer::onClientDisconnected()
|
||||
{
|
||||
bInfo(m_context) << "Client disconnected!";
|
||||
emit doDisconnected(m_client_id);
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,52 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef COM_CLIENT_BUFFER_H
|
||||
#define COM_CLIENT_BUFFER_H
|
||||
|
||||
#include "blackmisc/com_handler.h"
|
||||
#include <QObject>
|
||||
|
||||
class QTcpSocket;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class IContext;
|
||||
|
||||
class CComClientBuffer : public IComHandler
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CComClientBuffer(IContext &context, uint clientID, QTcpSocket *socket, QObject *parent = 0);
|
||||
|
||||
virtual ~CComClientBuffer();
|
||||
|
||||
//! Sends a message to the remote host.
|
||||
/*!
|
||||
\param data Reference to the raw byte data to be sent.
|
||||
\return Returns true if sending was successfull, otherwise false.
|
||||
*/
|
||||
bool sendMessage(const QString &id, const QByteArray &message);
|
||||
|
||||
signals:
|
||||
|
||||
void doReceivedMessage(uint clientID, QString &messageID, QByteArray &message);
|
||||
void doDisconnected(uint clientID);
|
||||
|
||||
protected slots:
|
||||
void onReceivingData();
|
||||
void onClientDisconnected();
|
||||
|
||||
protected:
|
||||
IContext &m_context;
|
||||
QTcpSocket *m_tcp_socket;
|
||||
uint m_client_id;
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // COM_CLIENT_BUFFER_H
|
||||
@@ -1,127 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#include "blackmisc/com_handler.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include <QTcpSocket>
|
||||
#include <QDataStream>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
IComHandler::IComHandler(IContext &context, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_context(context)
|
||||
{
|
||||
}
|
||||
|
||||
void IComHandler::createFrame(const QString& messageID, const QByteArray &data)
|
||||
{
|
||||
m_sender_buffer.clear();
|
||||
|
||||
QDataStream stream(&m_sender_buffer, QIODevice::WriteOnly);
|
||||
|
||||
qint32 message_size = data.size();
|
||||
quint16 crc = qChecksum(data.constData(), message_size);
|
||||
|
||||
stream << (qint32)Sync_Marker
|
||||
<< (qint32)message_size
|
||||
<< (QString)messageID;
|
||||
stream.writeRawData(data.constData(), message_size);
|
||||
stream << (quint16)crc;
|
||||
}
|
||||
|
||||
bool IComHandler::parseFrame(QString &messageID, QByteArray &data)
|
||||
{
|
||||
if (m_receive_buffer.isEmpty())
|
||||
return false;
|
||||
|
||||
qint32 total_length = 0;
|
||||
qint32 min_size = 3 * (int)sizeof(qint32);
|
||||
|
||||
if (m_receive_buffer.size() < min_size)
|
||||
{
|
||||
bDebug(m_context) << "Received data is to small to read SYNC and frame length!";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool found_sync = false;
|
||||
QDataStream stream (m_receive_buffer);
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Read the Sync
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
do
|
||||
{
|
||||
qint32 sync = 0;
|
||||
stream >> sync;
|
||||
if (sync == Sync_Marker)
|
||||
found_sync = true;
|
||||
}
|
||||
while (!found_sync && !stream.atEnd());
|
||||
|
||||
if (!found_sync)
|
||||
{
|
||||
bWarning(m_context) << "Could not find sync pattern in the stream. Discarding all data!";
|
||||
m_receive_buffer.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
total_length += (int)sizeof(qint32); // Sync
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Read size of the message itself
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
qint32 message_length = 0;
|
||||
stream >> message_length;
|
||||
|
||||
total_length += (int)sizeof(qint32); // Length
|
||||
total_length += message_length; // Data
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Read the message id
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
stream >> messageID;
|
||||
total_length += sizeof(QChar) * messageID.length() + (int)sizeof(quint32); // ID
|
||||
|
||||
if (m_receive_buffer.size() < (total_length + (int)sizeof (quint16)))
|
||||
{
|
||||
bDebug(m_context) << "Received data is to small to read data block!";
|
||||
return false;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
// Read the data
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
data.resize(message_length);
|
||||
// creates warning when compiled as RELEASE
|
||||
qint32 bytes = stream.readRawData(data.data(), message_length);
|
||||
Q_ASSERT (bytes == message_length);
|
||||
Q_ASSERT (data.size() == message_length);
|
||||
|
||||
quint16 crc_calc = qChecksum (data.constData(), data.size());
|
||||
quint16 crc_recv = 0;
|
||||
stream >> crc_recv;
|
||||
|
||||
total_length += (int)sizeof(quint16);
|
||||
|
||||
m_receive_buffer.remove(0, total_length);
|
||||
|
||||
if (crc_calc != crc_recv)
|
||||
{
|
||||
bWarning(m_context) << "Message CRC error!";
|
||||
data.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,77 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef COM_HANDLER_H
|
||||
#define COM_HANDLER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QTcpSocket;
|
||||
|
||||
const qint32 Sync_Marker = 0x1ACFFC1D;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
class IContext;
|
||||
|
||||
//! IComHandler Interface.
|
||||
/*!
|
||||
This interface implements the basic class for every InterCommunikation
|
||||
objects. It creates the frames in which the data is packed and
|
||||
deframes it, when it receives something from the TCP socket.
|
||||
\sa CComClient CComServer
|
||||
*/
|
||||
class IComHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Constructor
|
||||
/*!
|
||||
\param parent Pointer to the parent QObject
|
||||
*/
|
||||
IComHandler(IContext &context, QObject *parent = 0);
|
||||
|
||||
//! Virtual destructor
|
||||
virtual ~IComHandler() {}
|
||||
|
||||
protected:
|
||||
|
||||
//! Creates a sendable frame containing some data.
|
||||
/*!
|
||||
\param messageID QString with the unique messageID
|
||||
\param message The actual data
|
||||
\sa IMessage
|
||||
*/
|
||||
void createFrame (const QString &messageID, const QByteArray &data);
|
||||
|
||||
//! Parses a new frame and constructs messageID and data out of it
|
||||
/*!
|
||||
\param messageID Reference to the QString messageID
|
||||
\param message Reference where the data will be stored in.
|
||||
\sa IMessage
|
||||
*/
|
||||
bool parseFrame(QString &messageID, QByteArray &data);
|
||||
|
||||
//! Context.
|
||||
IContext &m_context;
|
||||
|
||||
//! Receive Buffer
|
||||
/*!
|
||||
Data received from the TCP socket, will stored in here.
|
||||
*/
|
||||
QByteArray m_receive_buffer;
|
||||
|
||||
//! Sender Buffer
|
||||
/*!
|
||||
Sending data via the TCP socket, should be stored in here.
|
||||
*/
|
||||
QByteArray m_sender_buffer;
|
||||
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // COM_HANDLER_H
|
||||
@@ -1,136 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#include "blackmisc/com_server.h"
|
||||
#include "blackmisc/com_client_buffer.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
CComServer::CComServer(IContext &context, QObject *parent)
|
||||
: QObject(parent), m_context(context), m_tcp_server(NULL), m_port(0)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
CComServer::~CComServer()
|
||||
{
|
||||
//QObject::disconnect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(onIncomingConnection()) );
|
||||
|
||||
TClientBufferHash::iterator it = m_client_buffers.begin();
|
||||
while (it != m_client_buffers.end())
|
||||
{
|
||||
it.value()->deleteLater();
|
||||
++it;
|
||||
}
|
||||
|
||||
m_tcp_server->close();
|
||||
m_tcp_server->deleteLater();
|
||||
}
|
||||
|
||||
bool CComServer::init()
|
||||
{
|
||||
m_tcp_server = new QTcpServer(this);
|
||||
Q_ASSERT(m_tcp_server);
|
||||
Q_ASSERT(QObject::connect(m_tcp_server, SIGNAL(newConnection()), this, SLOT(onIncomingConnection())));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CComServer::Host(const QHostAddress &address, const quint16 port)
|
||||
{
|
||||
if (isHosting()) return;
|
||||
|
||||
Q_ASSERT(! address.isNull());
|
||||
Q_ASSERT(port > 0);
|
||||
|
||||
if (!m_tcp_server->listen(address, port))
|
||||
{
|
||||
bError(m_context) << "Hosting failed";
|
||||
emit doHostClosed();
|
||||
}
|
||||
else
|
||||
{
|
||||
bDebug(m_context) << "Hosting successfull";
|
||||
emit doHosting();
|
||||
}
|
||||
}
|
||||
|
||||
bool CComServer::isHosting() const
|
||||
{
|
||||
return m_tcp_server->isListening();
|
||||
}
|
||||
|
||||
void CComServer::close()
|
||||
{
|
||||
m_tcp_server->close();
|
||||
}
|
||||
|
||||
void CComServer::sendToClient(const uint clientID, const QString &messageID, const QByteArray &data)
|
||||
{
|
||||
if (!m_client_buffers.contains(clientID))
|
||||
{
|
||||
bWarning(m_context) << "Cannot send data to client - unknown client!";
|
||||
return;
|
||||
}
|
||||
m_client_buffers.value(clientID)->sendMessage(messageID, data);
|
||||
}
|
||||
|
||||
void CComServer::sendToAll(const QString &messageID, const QByteArray &data)
|
||||
{
|
||||
TClientBufferHash::const_iterator it = m_client_buffers.constBegin();
|
||||
while (it != m_client_buffers.constEnd())
|
||||
{
|
||||
it.value()->sendMessage(messageID, data);
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
QString CComServer::getErrorMessage(const QAbstractSocket::SocketError /** error **/)
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
|
||||
void CComServer::onIncomingConnection()
|
||||
{
|
||||
while (m_tcp_server->hasPendingConnections())
|
||||
{
|
||||
QTcpSocket *socket = m_tcp_server->nextPendingConnection();
|
||||
uint clientID = qHash(socket);
|
||||
|
||||
// Create new ClientBuffer object. This new object gets the owner of the socket
|
||||
CComClientBuffer *clientbuf = new CComClientBuffer(m_context, clientID, socket, this);
|
||||
Q_ASSERT(clientbuf);
|
||||
|
||||
connect(clientbuf, SIGNAL(doDisconnected(uint)), this, SLOT(onClientDisconnected(uint)));
|
||||
connect(clientbuf, SIGNAL(doReceivedMessage(uint, QString &, QByteArray &)), this, SLOT(onClientMessageReceived(uint, QString &, QByteArray &)));
|
||||
|
||||
m_client_buffers.insert(clientID, clientbuf);
|
||||
|
||||
emit doClientConnected();
|
||||
}
|
||||
}
|
||||
|
||||
void CComServer::onClientDisconnected(uint clientID)
|
||||
{
|
||||
if (!m_client_buffers.contains(clientID))
|
||||
{
|
||||
bWarning(m_context) << "Disconnected unknown client!";
|
||||
return;
|
||||
}
|
||||
|
||||
m_client_buffers.take(clientID)->deleteLater();
|
||||
}
|
||||
|
||||
void CComServer::onClientMessageReceived(uint /** clientID **/, QString &messageID, QByteArray &message)
|
||||
{
|
||||
emit doMessageReceived(messageID, message);
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,105 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef TCP_SERVER_H
|
||||
#define TCP_SERVER_H
|
||||
|
||||
#include <QHash>
|
||||
#include <QHostAddress>
|
||||
#include <QTcpSocket>
|
||||
|
||||
class QTcpServer;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class CComClientBuffer;
|
||||
class IContext;
|
||||
|
||||
class CComServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
//! Constructor
|
||||
/*!
|
||||
\param context
|
||||
\param parent Pointer to the parent QObject
|
||||
*/
|
||||
CComServer(IContext &context, QObject *parent = 0);
|
||||
|
||||
//! Destructor
|
||||
~CComServer();
|
||||
|
||||
//! This method initializes the server
|
||||
/*!
|
||||
\return Returns true, if successfull, otherwise false
|
||||
*/
|
||||
bool init ();
|
||||
|
||||
//! Call this method to start hosting
|
||||
void Host ( const QHostAddress & address, const quint16 port);
|
||||
|
||||
//! Hosting status
|
||||
/*!
|
||||
\return Returns true if hosting, otherwise false
|
||||
*/
|
||||
bool isHosting () const;
|
||||
|
||||
//! Closes the opened hosting session
|
||||
void close();
|
||||
|
||||
//! Sends data to a client
|
||||
/*!
|
||||
\param clientID ID of the target client
|
||||
\param messageID ID of the message, to be sent to the client
|
||||
\param data The actual data/message
|
||||
*/
|
||||
void sendToClient(const quint32 clientID, const QString &messageID, const QByteArray& data);
|
||||
|
||||
//! Sends data to all clients
|
||||
/*!
|
||||
\param messageID ID of the message, to be sent to the client
|
||||
\param data The actual data/message
|
||||
*/
|
||||
void sendToAll(const QString &messageID, const QByteArray& data);
|
||||
|
||||
//! Call this method to get last error
|
||||
/*!
|
||||
\return Returns a string representation of the last error.
|
||||
*/
|
||||
QString getErrorMessage( const QAbstractSocket::SocketError error);
|
||||
|
||||
signals:
|
||||
|
||||
void doHostClosed();
|
||||
void doHosting();
|
||||
void doClientConnected();
|
||||
void doClientDisconnected();
|
||||
void doMessageReceived(QString &, QByteArray &);
|
||||
void doMessageReceived();
|
||||
|
||||
protected slots:
|
||||
|
||||
void onIncomingConnection();
|
||||
|
||||
void onClientDisconnected(uint clientID);
|
||||
|
||||
void onClientMessageReceived(uint clientHash, QString &messageID, QByteArray &message);
|
||||
|
||||
protected:
|
||||
|
||||
IContext& m_context;
|
||||
QTcpServer* m_tcp_server;
|
||||
QHostAddress m_address;
|
||||
quint16 m_port;
|
||||
typedef QHash<uint, CComClientBuffer*> TClientBufferHash;
|
||||
TClientBufferHash m_client_buffers;
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // TCP_SERVER_H
|
||||
@@ -1,239 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#include "blackmisc/config.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include <QFile>
|
||||
#include <QStringList>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
CValue::CValue() : m_bool_value(false), m_double_value(0.0), m_int_value(0),
|
||||
m_type(CONFIG_UNKOWN), m_value("")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CValue::CValue(const QString &value) : m_bool_value(false), m_double_value(0.0), m_int_value(0),
|
||||
m_type(CONFIG_UNKOWN), m_value(value)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
void CValue::init()
|
||||
{
|
||||
bool result = false;
|
||||
qint32 int_value = 0;
|
||||
double double_value = 0.0;
|
||||
|
||||
int_value = m_value.toInt(&result);
|
||||
if (result)
|
||||
{
|
||||
m_type = CONFIG_INT;
|
||||
m_int_value = int_value;
|
||||
m_double_value = int_value;
|
||||
m_bool_value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
double_value = m_value.toDouble(&result);
|
||||
if (result)
|
||||
{
|
||||
m_type = CONFIG_DOUBLE;
|
||||
m_int_value = 0;
|
||||
m_double_value = double_value;
|
||||
m_bool_value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_value.compare("false", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
m_type = CONFIG_BOOL;
|
||||
m_int_value = 0;
|
||||
m_double_value = 0.0;
|
||||
m_bool_value = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_value.compare("true", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
m_type = CONFIG_BOOL;
|
||||
m_int_value = 0;
|
||||
m_double_value = 0.0;
|
||||
m_bool_value = true;
|
||||
return;
|
||||
}
|
||||
|
||||
m_type = CONFIG_STRING;
|
||||
m_int_value = 0;
|
||||
m_double_value = 0.0;
|
||||
m_bool_value = false;
|
||||
}
|
||||
|
||||
qint32 CValue::asInt(bool *ok)
|
||||
{
|
||||
bool result = true;
|
||||
if (m_type != CONFIG_INT)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
if (ok != NULL)
|
||||
*ok = result;
|
||||
return m_int_value;
|
||||
}
|
||||
|
||||
double CValue::asDouble(bool *ok)
|
||||
{
|
||||
bool result = true;
|
||||
if (m_type != CONFIG_DOUBLE)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
if (ok != NULL)
|
||||
*ok = result;
|
||||
return m_double_value;
|
||||
}
|
||||
|
||||
bool CValue::asBool(bool *ok)
|
||||
{
|
||||
bool result = true;
|
||||
if (m_type != CONFIG_BOOL)
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
if (ok != NULL)
|
||||
*ok = result;
|
||||
return m_bool_value;
|
||||
}
|
||||
|
||||
CConfig::CConfig(IContext &context, const QString &filename, const QString &separator, bool /*isRelative*/)
|
||||
: m_context(context), m_configfile(filename), m_separator(separator)
|
||||
{
|
||||
}
|
||||
|
||||
bool CConfig::load()
|
||||
{
|
||||
return load(m_configfile);
|
||||
}
|
||||
|
||||
bool CConfig::load(const QString &filename)
|
||||
{
|
||||
m_configfile = filename;
|
||||
|
||||
m_value_map.clear();
|
||||
|
||||
if (m_configfile.isEmpty())
|
||||
{
|
||||
bError(m_context) << "Can't open emtpy config file!";
|
||||
return false;
|
||||
}
|
||||
QFile input(m_configfile);
|
||||
if (!input.open(QIODevice::ReadOnly))
|
||||
{
|
||||
bError(m_context) << "Failed to open config file !" << m_configfile;
|
||||
input.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool error = false;
|
||||
quint32 no_line = 0;
|
||||
|
||||
QTextStream instream(&input);
|
||||
|
||||
while (!instream.atEnd())
|
||||
{
|
||||
++no_line;
|
||||
|
||||
QString line = instream.readLine();
|
||||
|
||||
// Remove any whitespace from the start and end
|
||||
line = line.trimmed();
|
||||
|
||||
// Search for the comment operator and discard it
|
||||
int pos = line.indexOf(QChar('#'));
|
||||
if (pos != -1)
|
||||
line = line.left(pos).trimmed();
|
||||
|
||||
// Check if we have a empty line
|
||||
if (line.isEmpty())
|
||||
continue;
|
||||
|
||||
// Separate between key and value
|
||||
QStringList tags = line.split(m_separator);
|
||||
if (tags.count() != 2)
|
||||
{
|
||||
bWarning(m_context) << "Could not parse line " << no_line << " in file " << m_configfile << "!";
|
||||
error = true;
|
||||
continue;
|
||||
}
|
||||
QString key = tags[0].trimmed();
|
||||
CValue value = CValue(tags[1].trimmed());
|
||||
|
||||
setValue(key, value);
|
||||
}
|
||||
|
||||
input.close();
|
||||
return !error;
|
||||
}
|
||||
|
||||
void CConfig::setValue(const QString &key, const CValue &value)
|
||||
{
|
||||
if (contains(key))
|
||||
update(key, value);
|
||||
else
|
||||
add(key, value);
|
||||
}
|
||||
|
||||
CValue CConfig::value(const QString &key) const
|
||||
{
|
||||
if (m_value_map.contains(key))
|
||||
return m_value_map.value(key);
|
||||
else
|
||||
return CValue();
|
||||
}
|
||||
|
||||
bool CConfig::contains(const QString &key) const
|
||||
{
|
||||
return m_value_map.contains(key);
|
||||
}
|
||||
|
||||
void CConfig::remove(const QString &key)
|
||||
{
|
||||
m_value_map.remove(key);
|
||||
}
|
||||
|
||||
void CConfig::add(const QString &key, const CValue &value)
|
||||
{
|
||||
// Paranoid...
|
||||
if (contains(key))
|
||||
{
|
||||
update(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_value_map.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
void CConfig::update(const QString &key, const CValue &value)
|
||||
{
|
||||
m_value_map[key] = value;
|
||||
}
|
||||
|
||||
void CConfig::display()
|
||||
{
|
||||
TValueMap::const_iterator it;
|
||||
for (it = m_value_map.begin(); it != m_value_map.end(); ++it)
|
||||
{
|
||||
CValue value = it.value();
|
||||
bDebug(m_context) << "Key: " << it.key() << " - Value: " << value.asString();
|
||||
}
|
||||
}
|
||||
|
||||
} //! namespace BlackMisc
|
||||
@@ -1,168 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
#include <QDebug>
|
||||
#include <QMap>
|
||||
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class IContext;
|
||||
|
||||
class CValue
|
||||
{
|
||||
public:
|
||||
|
||||
//! Configuration type enum.
|
||||
/*! This enum lists the three different values, used in the config file. */
|
||||
typedef enum { CONFIG_STRING = 0, /*!< Type String. */
|
||||
CONFIG_INT, /*!< Type Integer. */
|
||||
CONFIG_DOUBLE, /*!< Type Double. */
|
||||
CONFIG_BOOL, /*!< Type Bool. */
|
||||
CONFIG_UNKOWN /*!< Type Unknown. */
|
||||
} TConfigType;
|
||||
|
||||
CValue();
|
||||
|
||||
CValue(const QString &value);
|
||||
|
||||
void init();
|
||||
|
||||
QString &asString() { return m_value; }
|
||||
|
||||
qint32 asInt(bool *ok = NULL);
|
||||
|
||||
bool asBool(bool *ok = NULL);
|
||||
|
||||
double asDouble(bool *result);
|
||||
|
||||
inline bool isValid() {return m_type != CONFIG_UNKOWN;}
|
||||
|
||||
inline bool isInt() { return m_type == CONFIG_INT; }
|
||||
inline bool isDouble() { return m_type == CONFIG_DOUBLE; }
|
||||
inline bool isBool() { return m_type == CONFIG_BOOL; }
|
||||
|
||||
protected:
|
||||
|
||||
bool m_bool_value;
|
||||
double m_double_value;
|
||||
qint32 m_int_value;
|
||||
TConfigType m_type;
|
||||
QString m_value;
|
||||
|
||||
};
|
||||
|
||||
//! Configuration class.
|
||||
/*!
|
||||
This class implements the configuration part of the library.
|
||||
\warning it is not safe to use this from within
|
||||
*/
|
||||
class CConfig
|
||||
{
|
||||
public:
|
||||
|
||||
CConfig(IContext &context, const QString &filename, const QString &separator = "=", bool isRelative = false);
|
||||
|
||||
//! Sets the value of the specified key.
|
||||
/*!
|
||||
\param key Key, which value should be set.
|
||||
\param value The actual value as T.
|
||||
*/
|
||||
void setValue(const QString &key, const CValue &value);
|
||||
|
||||
//! Returns the value from key.
|
||||
/*!
|
||||
\param key Specified key.
|
||||
\return The value to key 'key' as T&
|
||||
*/
|
||||
CValue value(const QString &key) const;
|
||||
|
||||
//! Function to check if the key is in the config.
|
||||
/*!
|
||||
\param key Specified key.
|
||||
\return Returns true, if key is in the config.
|
||||
*/
|
||||
void add(const QString &key, const CValue &value);
|
||||
|
||||
//! Function to check if the key is in the config.
|
||||
/*!
|
||||
\param key Specified key.
|
||||
\return Returns true, if key is in the config.
|
||||
*/
|
||||
void update(const QString &key, const CValue &value);
|
||||
|
||||
//! Function to check if the key is in the config.
|
||||
/*!
|
||||
\param key Specified key.
|
||||
\return Returns true, if key is in the config.
|
||||
*/
|
||||
bool contains(const QString &key) const;
|
||||
|
||||
//! Removes the key and its value from the config.
|
||||
/*!
|
||||
\param key Key, which has to be removed.
|
||||
*/
|
||||
void remove(const QString &key);
|
||||
|
||||
//! Displays the configuration to the debug output.
|
||||
/*!
|
||||
More info.
|
||||
*/
|
||||
void display();
|
||||
|
||||
/*!
|
||||
* Config File section
|
||||
*/
|
||||
|
||||
//! Returns the config filename of this config object.
|
||||
/*!
|
||||
\return filename of this config.
|
||||
*/
|
||||
const QString &configFile() const {return m_configfile; }
|
||||
|
||||
//! Loads the config file.
|
||||
/*!
|
||||
\return Returns true if loading went well.
|
||||
*/
|
||||
bool load();
|
||||
|
||||
//! Loads the config file.
|
||||
/*!
|
||||
\param filename Specify a different filename.
|
||||
\return Returns true if loading went well.
|
||||
*/
|
||||
bool load(const QString &filename);
|
||||
|
||||
//! Saves the config to file.
|
||||
/*!
|
||||
\return Returns true if loading went well.
|
||||
*/
|
||||
bool save() { return false; } // TODO
|
||||
|
||||
//! Saves the config to file.
|
||||
/*!
|
||||
\param filename Specify a different filename.
|
||||
\return Returns true if loading went well.
|
||||
*/
|
||||
bool save(const QString &filename) { qDebug() << filename; return false; } // TODO
|
||||
|
||||
protected:
|
||||
|
||||
IContext &m_context;
|
||||
QString m_configfile;
|
||||
QString m_separator;
|
||||
typedef QMap<QString, CValue> TValueMap;
|
||||
TValueMap m_value_map;
|
||||
|
||||
};
|
||||
} //! namespace BlackMisc
|
||||
|
||||
#endif // CONFIG_H
|
||||
@@ -1,98 +0,0 @@
|
||||
#include "blackmisc/config_manager.h"
|
||||
#include "blackmisc/config.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include <QDir>
|
||||
#include <QStringList>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
SINGLETON_CLASS_IMPLEMENTATION(CConfigManager)
|
||||
|
||||
CConfigManager::CConfigManager()
|
||||
: m_context(IContext::getInstance())
|
||||
{
|
||||
}
|
||||
|
||||
CConfigManager::CConfigManager(IContext &context)
|
||||
: m_context(context)
|
||||
{
|
||||
}
|
||||
|
||||
void CConfigManager::setConfigPath(QString path)
|
||||
{
|
||||
m_config_path = QDir(path).absolutePath();
|
||||
}
|
||||
|
||||
int CConfigManager::readConfig(bool forceReload)
|
||||
{
|
||||
/*!
|
||||
Foreach *.cfg file in the config path,
|
||||
create a new CConfig object and parse it into.
|
||||
Then append the pointer to this object into
|
||||
the config map.
|
||||
*/
|
||||
|
||||
if (forceReload)
|
||||
clear();
|
||||
|
||||
QDir directory(m_config_path);
|
||||
QStringList filters;
|
||||
filters << "*.cfg";
|
||||
directory.setNameFilters(filters);
|
||||
|
||||
QStringList files = directory.entryList();
|
||||
|
||||
QStringList::const_iterator constIterator;
|
||||
for (constIterator = files.constBegin(); constIterator != files.constEnd(); ++constIterator)
|
||||
{
|
||||
int index = (*constIterator).indexOf(".");
|
||||
QString section = (*constIterator).left(index);
|
||||
|
||||
if (!m_config_map.contains(section))
|
||||
{
|
||||
QString filePath = m_config_path + QDir::separator() + (*constIterator);
|
||||
CConfig *config = new CConfig(m_context, filePath);
|
||||
config->load();
|
||||
|
||||
m_config_map.insert(section, config);
|
||||
}
|
||||
}
|
||||
|
||||
return m_config_map.size();
|
||||
}
|
||||
|
||||
int CConfigManager::writeConfig()
|
||||
{
|
||||
/*!
|
||||
Foreach config object in the map,
|
||||
create a file with all values, from
|
||||
the object. Filename is created
|
||||
from the section name
|
||||
*/
|
||||
return m_config_map.size();
|
||||
}
|
||||
|
||||
void CConfigManager::clear()
|
||||
{
|
||||
TConfigMap::iterator iterator;
|
||||
for (iterator = m_config_map.begin(); iterator != m_config_map.end(); ++iterator)
|
||||
{
|
||||
delete iterator.value();
|
||||
}
|
||||
|
||||
m_config_map.clear();
|
||||
}
|
||||
|
||||
CConfig *CConfigManager::getConfig(const QString §ion)
|
||||
{
|
||||
if (m_config_map.contains(section))
|
||||
return m_config_map.value(section);
|
||||
else
|
||||
{
|
||||
//@@@ should we throw an exception here?
|
||||
bAppError << "Could not find config section: " << section;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,74 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef BLACKMISC_CONFIGMANAGER_H
|
||||
#define BLACKMISC_CONFIGMANAGER_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QString>
|
||||
|
||||
#include "blackmisc/context.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class CConfig;
|
||||
|
||||
class CConfigManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
// safe singleton declaration
|
||||
SINGLETON_CLASS_DECLARATION(CConfigManager)
|
||||
|
||||
public:
|
||||
/*!
|
||||
Default constructor.
|
||||
\deprecated Do not use.
|
||||
\todo Remove this when removing SINGLETON_CLASS_DECLARATION.
|
||||
*/
|
||||
CConfigManager();
|
||||
|
||||
/*!
|
||||
Constructor.
|
||||
\param context
|
||||
*/
|
||||
CConfigManager(IContext &context);
|
||||
|
||||
//! Configuration Manager error codes.
|
||||
/*! This enum lists all errors, which can appear. If you need
|
||||
* a readable output then consider using /sa getErrorString()
|
||||
*/
|
||||
typedef enum { UNKNOWN_PATH = 0, /*!< Type String. */
|
||||
|
||||
} TErrorCodes;
|
||||
|
||||
//! Sets the global path, where all the config files are located
|
||||
/*!
|
||||
\param path absolute pathname
|
||||
*/
|
||||
void setConfigPath(QString path);
|
||||
|
||||
int readConfig(bool forceReload = false);
|
||||
|
||||
int writeConfig();
|
||||
|
||||
void clear();
|
||||
|
||||
CConfig *getConfig(const QString §ion);
|
||||
|
||||
private:
|
||||
|
||||
IContext &m_context;
|
||||
|
||||
typedef QMap<QString, CConfig*> TConfigMap;
|
||||
TConfigMap m_config_map;
|
||||
|
||||
QString m_config_path;
|
||||
|
||||
};
|
||||
} // namespace BlackLib
|
||||
|
||||
#endif // BLACKMISC_CONFIGMANAGER_H
|
||||
@@ -1,30 +0,0 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / contributors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "blackcore/dbus_server.h"
|
||||
#include "blackmisc/dbus_handler.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
IDBusHandler::IDBusHandler(QObject *parent) :
|
||||
QObject(parent), m_dbusserver(0), m_parent(parent)
|
||||
{
|
||||
}
|
||||
|
||||
void IDBusHandler::setDBusServer(BlackCore::CDBusServer *dbusServer)
|
||||
{
|
||||
m_dbusserver = dbusServer;
|
||||
|
||||
if (m_dbusserver)
|
||||
m_dbusserver->addObject(m_dbusPath, this);
|
||||
}
|
||||
|
||||
void IDBusHandler::setDBusObjectPath(const QString &dbusPath)
|
||||
{
|
||||
m_dbusPath = dbusPath;
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,59 +0,0 @@
|
||||
/* Copyright (C) 2013 VATSIM Community / contributors
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef DBUS_HANDLER_BASE_H
|
||||
#define DBUS_HANDLER_BASE_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
namespace BlackCore {
|
||||
class CDBusServer;
|
||||
}
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* \brief DBus Handler Base class
|
||||
* \details This class implements the basic methods any DBus handler class needs to use. If you want
|
||||
* to implement your own DBus handler derive it from this one.
|
||||
*/
|
||||
|
||||
class IDBusHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
protected:
|
||||
|
||||
BlackCore::CDBusServer *m_dbusserver; //!< Our DBusServer
|
||||
QString m_dbusPath; //!< DBus object path
|
||||
|
||||
QObject *m_parent; //!< Pointer to the parent plane manager object
|
||||
|
||||
public:
|
||||
|
||||
/*!
|
||||
* \brief Default constructor
|
||||
* \param parent
|
||||
*/
|
||||
IDBusHandler(QObject *parent = 0);
|
||||
|
||||
/*!
|
||||
* \brief Sets the DBusServer
|
||||
* \param dbusServer
|
||||
*/
|
||||
void setDBusServer(BlackCore::CDBusServer *dbusServer);
|
||||
|
||||
void setDBusObjectPath( const QString &dbusPath);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // DBUS_HANDLER_BASE_H
|
||||
@@ -1,115 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef GUI_MESSAGES_H
|
||||
#define GUI_MESSAGES_H
|
||||
|
||||
#include "blackmisc/message.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
class MSG_CONNECT_TO_VATSIM : public IMessage
|
||||
{
|
||||
public:
|
||||
MSG_CONNECT_TO_VATSIM() : IMessage(QString("MSG_ID_CONNECT_TO_VATSIM"))
|
||||
{
|
||||
}
|
||||
|
||||
QString getHost () const { return m_host; }
|
||||
quint16 getPort () const { return m_port; }
|
||||
QString getCallsign () const { return m_callsign; }
|
||||
QString getUserID () const { return m_userid; }
|
||||
QString getPassword () const { return m_password; }
|
||||
QString getRealName () const { return m_realName; }
|
||||
|
||||
void setHost (const QString &host) { m_host = host; }
|
||||
void setPort (const quint16 &port) { m_port = port; }
|
||||
void setCallsign (const QString &callsign) { m_callsign = callsign; }
|
||||
void setUserID (const QString &id) { m_userid = id; }
|
||||
void setPassword (const QString &password) { m_password = password; }
|
||||
void setRealName (const QString &realname) { m_realName = realname; }
|
||||
|
||||
virtual QDataStream& operator<< ( QDataStream& in)
|
||||
{
|
||||
in >> m_message_id;
|
||||
in >> m_host;
|
||||
in >> m_port;
|
||||
in >> m_callsign;
|
||||
in >> m_userid;
|
||||
in >> m_password;
|
||||
in >> m_realName;
|
||||
return in;
|
||||
}
|
||||
|
||||
virtual QDataStream& operator>> (QDataStream& out) const
|
||||
{
|
||||
out << m_message_id;
|
||||
out << m_host;
|
||||
out << m_port;
|
||||
out << m_callsign;
|
||||
out << m_userid;
|
||||
out << m_password;
|
||||
out << m_realName;
|
||||
return out;
|
||||
}
|
||||
|
||||
virtual QTextStream& operator<< ( QTextStream& in) { return in; }
|
||||
virtual QTextStream& operator>> (QTextStream& out) const { return out; }
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
QString m_host;
|
||||
quint16 m_port;
|
||||
QString m_callsign;
|
||||
QString m_userid;
|
||||
QString m_password;
|
||||
QString m_realName;
|
||||
};
|
||||
|
||||
class MSG_CHAT_MESSAGE : public IMessage
|
||||
{
|
||||
public:
|
||||
MSG_CHAT_MESSAGE() : IMessage(QString("MSG_ID_CHAT_MESSAGE"))
|
||||
{
|
||||
}
|
||||
|
||||
void setSource (const QString &source) { m_source = source; }
|
||||
void setDestination (const QString &destination) { m_destination = destination; }
|
||||
void setText (const QString &text) { m_source = text; }
|
||||
|
||||
QString getSource() const {return m_source;}
|
||||
QString getDestination() const {return m_destination;}
|
||||
QString getText() const {return m_text;}
|
||||
|
||||
virtual QDataStream& operator<< ( QDataStream& in)
|
||||
{
|
||||
in >> m_message_id;
|
||||
in >> m_source;
|
||||
in >> m_destination;
|
||||
return in;
|
||||
}
|
||||
|
||||
virtual QDataStream& operator>> (QDataStream& out) const
|
||||
{
|
||||
out << m_message_id;
|
||||
out << m_source;
|
||||
out << m_destination;
|
||||
return out;
|
||||
}
|
||||
|
||||
virtual QTextStream& operator<< ( QTextStream& in) { return in; }
|
||||
virtual QTextStream& operator>> (QTextStream& out) const { return out; }
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
QString m_source;
|
||||
QString m_destination;
|
||||
QString m_text;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GUI_MESSAGES_H
|
||||
@@ -1,15 +0,0 @@
|
||||
#include "blackmisc/message.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
IMessage::IMessage(QString id)
|
||||
{
|
||||
m_message_id = id;
|
||||
}
|
||||
|
||||
QString IMessage::getID() const
|
||||
{
|
||||
return m_message_id;
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,73 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef MESSAGE_H
|
||||
#define MESSAGE_H
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QDataStream>
|
||||
#include <QTextStream>
|
||||
#include "blackmisc/serialize.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class IMessage : public ISerialize
|
||||
{
|
||||
public:
|
||||
IMessage(QString id);
|
||||
|
||||
QString getID() const;
|
||||
|
||||
virtual QDataStream& operator<< ( QDataStream& in) = 0;
|
||||
virtual QDataStream& operator>> (QDataStream& out) const = 0;
|
||||
|
||||
virtual QTextStream& operator<< ( QTextStream& in) = 0;
|
||||
virtual QTextStream& operator>> (QTextStream& out) const = 0;
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
QString m_message_id;
|
||||
};
|
||||
|
||||
class TestMessage : public IMessage
|
||||
{
|
||||
public:
|
||||
TestMessage() : IMessage("MSG_ID_TestMessage")
|
||||
{
|
||||
testString = "This is a test message!";
|
||||
}
|
||||
|
||||
QString getTestString () const { return testString; }
|
||||
|
||||
//QDataStream &operator>>(qint8 &i);
|
||||
|
||||
virtual QDataStream& operator<< ( QDataStream& in)
|
||||
{
|
||||
in >> m_message_id;
|
||||
in >> testString;
|
||||
return in;
|
||||
}
|
||||
|
||||
virtual QDataStream& operator>> (QDataStream& out) const
|
||||
{
|
||||
out << m_message_id;
|
||||
out << testString;
|
||||
return out;
|
||||
}
|
||||
|
||||
virtual QTextStream& operator<< ( QTextStream& in) { return in; }
|
||||
virtual QTextStream& operator>> (QTextStream& out) const { return out; }
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
QString testString;
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // MESSAGE_H
|
||||
@@ -1,32 +0,0 @@
|
||||
#include "blackmisc/message_handler.h"
|
||||
#include "blackmisc/message_dispatcher.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
SINGLETON_CLASS_IMPLEMENTATION(CMessageDispatcher)
|
||||
|
||||
void CMessageDispatcher::append(IMessage *message)
|
||||
{
|
||||
m_messageQueue.enqueue(message);
|
||||
}
|
||||
|
||||
void CMessageDispatcher::dispatch()
|
||||
{
|
||||
IMessage *message = NULL;
|
||||
|
||||
if (m_messageQueue.isEmpty())
|
||||
return;
|
||||
|
||||
message = m_messageQueue.dequeue();
|
||||
|
||||
if (message != NULL)
|
||||
{
|
||||
CTypeInfo typeinfo = CTypeInfo(typeid(*message));
|
||||
QList<CMessageHandler *> neededHandlers = m_messageHander.values(typeinfo);
|
||||
int handlerSize = neededHandlers.size();
|
||||
for (int i = 0; i < handlerSize; ++i)
|
||||
neededHandlers.at(i)->handleMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,55 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef BLACKMISC_MESSAGE_DISPATCHER_H
|
||||
#define BLACKMISC_MESSAGE_DISPATCHER_H
|
||||
|
||||
#include "blackmisc/message.h"
|
||||
#include "blackmisc/context.h"
|
||||
#include "blackmisc/type_info.h"
|
||||
#include <QQueue>
|
||||
#include <QMultiMap>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
class CMessageHandler;
|
||||
|
||||
class CMessageDispatcher : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
// safe singleton declaration
|
||||
SINGLETON_CLASS_DECLARATION(CMessageDispatcher)
|
||||
|
||||
CMessageDispatcher() {}
|
||||
|
||||
public:
|
||||
virtual ~CMessageDispatcher() {}
|
||||
|
||||
void dispatch ();
|
||||
void append ( IMessage* message);
|
||||
|
||||
bool empty ()
|
||||
{
|
||||
return m_messageQueue.size() == 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void registerClass(T* object, CTypeInfo typeinfo)
|
||||
{
|
||||
m_messageHander.insert(typeinfo, object);
|
||||
}
|
||||
|
||||
private:
|
||||
typedef QQueue<IMessage*> TMessageQueue;
|
||||
TMessageQueue m_messageQueue;
|
||||
|
||||
typedef QMultiMap<CTypeInfo, CMessageHandler*> TMessageHandlerMap;
|
||||
TMessageHandlerMap m_messageHander;
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // BLACKMISC_MESSAGE_DISPATCHER_H
|
||||
@@ -1,48 +0,0 @@
|
||||
#include "blackmisc/message_factory.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
SINGLETON_CLASS_IMPLEMENTATION(CMessageFactory)
|
||||
|
||||
IMessageCreator::IMessageCreator(const QString &messageID)
|
||||
{
|
||||
CMessageFactory::getInstance().registerMessage(messageID, this);
|
||||
}
|
||||
|
||||
IMessage* CMessageFactory::create(const QString &messageID)
|
||||
{
|
||||
if (m_creators.contains(messageID))
|
||||
{
|
||||
IMessageCreator* creator = m_creators.value(messageID);
|
||||
IMessage* msg = creator->create();
|
||||
return msg;
|
||||
}
|
||||
else
|
||||
return (IMessage*)NULL;
|
||||
}
|
||||
|
||||
CMessageFactory::~CMessageFactory()
|
||||
{
|
||||
TMessageCreatorHash::iterator it = m_creators.begin();
|
||||
while (it != m_creators.end())
|
||||
{
|
||||
IMessageCreator* creator = it.value();
|
||||
if (creator)
|
||||
delete creator;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void CMessageFactory::registerMessage(const QString &messageID, IMessageCreator *creator)
|
||||
{
|
||||
m_creators.insert(messageID, creator);
|
||||
}
|
||||
|
||||
void CMessageFactory::registerMessages()
|
||||
{
|
||||
REGISTER_MESSAGE(TestMessage, MSG_ID_TestMessage);
|
||||
REGISTER_MESSAGE(MSG_CONNECT_TO_VATSIM, MSG_ID_CONNECT_TO_VATSIM);
|
||||
REGISTER_MESSAGE(MSG_CHAT_MESSAGE, MSG_ID_CHAT_MESSAGE);
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,61 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef BLACKMISC_MESSAGE_FACTORY_H
|
||||
#define BLACKMISC_MESSAGE_FACTORY_H
|
||||
|
||||
#include <QHash>
|
||||
#include "blackmisc/message.h"
|
||||
#include "blackmisc/gui_messages.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/context.h"
|
||||
|
||||
#define REGISTER_MESSAGE(classname, msgName) new MessageCreatorImpl<classname>(#msgName);
|
||||
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
class IMessageCreator
|
||||
{
|
||||
public:
|
||||
IMessageCreator(const QString &messageID);
|
||||
virtual IMessage *create() = 0;
|
||||
virtual ~IMessageCreator() {}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class MessageCreatorImpl : public IMessageCreator
|
||||
{
|
||||
public:
|
||||
MessageCreatorImpl(const QString &messageID) : IMessageCreator(messageID) {}
|
||||
virtual IMessage *create() { return new T; }
|
||||
};
|
||||
|
||||
class CMessageFactory : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
// safe singleton declaration
|
||||
SINGLETON_CLASS_DECLARATION(CMessageFactory)
|
||||
|
||||
CMessageFactory() { }
|
||||
public:
|
||||
|
||||
virtual ~CMessageFactory();
|
||||
|
||||
IMessage *create(const QString &messageID);
|
||||
|
||||
void registerMessage(const QString &messageID, IMessageCreator *creator);
|
||||
static void registerMessages();
|
||||
|
||||
private:
|
||||
|
||||
typedef QHash<QString, IMessageCreator *> TMessageCreatorHash;
|
||||
TMessageCreatorHash m_creators;
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // BLACKMISC_MESSAGE_FACTORY_H
|
||||
@@ -1,25 +0,0 @@
|
||||
#include "blackmisc/message_handler.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
CMessageHandler::~CMessageHandler()
|
||||
{
|
||||
TFunctionHandlerMap::iterator it = m_messagehandler.begin();
|
||||
while (it != m_messagehandler.end())
|
||||
{
|
||||
delete it.value();
|
||||
++it;
|
||||
}
|
||||
m_messagehandler.clear();
|
||||
}
|
||||
|
||||
void CMessageHandler::handleMessage(const IMessage * message)
|
||||
{
|
||||
TFunctionHandlerMap::iterator it = m_messagehandler.find(CTypeInfo(typeid(*message)));
|
||||
if (it != m_messagehandler.end())
|
||||
{
|
||||
it.value()->exec(message);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,75 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef MESSAGE_HANDLER_H
|
||||
#define MESSAGE_HANDLER_H
|
||||
|
||||
#include "blackmisc/message_dispatcher.h"
|
||||
#include "blackmisc/message.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/type_info.h"
|
||||
#include <QMap>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class IFunctionHandler
|
||||
{
|
||||
public:
|
||||
virtual ~IFunctionHandler() {}
|
||||
void exec (const IMessage* message)
|
||||
{
|
||||
call (message);
|
||||
}
|
||||
|
||||
private:
|
||||
virtual void call (const IMessage*) = 0;
|
||||
};
|
||||
|
||||
template <class T, class MessageT>
|
||||
class MemberFunctionHandler : public IFunctionHandler
|
||||
{
|
||||
public:
|
||||
typedef void (T::*MemberFunction)(MessageT*);
|
||||
MemberFunctionHandler(T* instance, MemberFunction memfunc) : m_instance(instance), m_function(memfunc) {}
|
||||
|
||||
void call(const IMessage* message)
|
||||
{
|
||||
(m_instance->*m_function)(static_cast<MessageT*>(message));
|
||||
}
|
||||
|
||||
private:
|
||||
T* m_instance;
|
||||
MemberFunction m_function;
|
||||
};
|
||||
|
||||
|
||||
class CMessageHandler
|
||||
{
|
||||
public:
|
||||
~CMessageHandler();
|
||||
|
||||
void handleMessage(const IMessage* message);
|
||||
|
||||
template <class T, class MessageT>
|
||||
void registerMessageFunction(T*, void (T::*memfunc)(MessageT*));
|
||||
|
||||
private:
|
||||
typedef QMap<CTypeInfo, IFunctionHandler*> TFunctionHandlerMap;
|
||||
TFunctionHandlerMap m_messagehandler;
|
||||
};
|
||||
|
||||
template <class T, class MessageT>
|
||||
void CMessageHandler::registerMessageFunction(T* obj, void (T::*memfunc)(MessageT*))
|
||||
{
|
||||
CTypeInfo typeinfo = CTypeInfo(typeid(MessageT));
|
||||
m_messagehandler[typeinfo]= new MemberFunctionHandler<T, MessageT>(obj, memfunc);
|
||||
CMessageDispatcher::getInstance().registerClass(obj, typeinfo);
|
||||
}
|
||||
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // MESSAGE_HANDLER_H
|
||||
@@ -1,17 +0,0 @@
|
||||
#include "blackmisc/message_system.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
|
||||
CMessageSystem::CMessageSystem()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
void CMessageSystem::init()
|
||||
{
|
||||
CMessageFactory::registerMessages();
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,26 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef MESSAGE_SYSTEM_H
|
||||
#define MESSAGE_SYSTEM_H
|
||||
|
||||
#include "blackmisc/message_handler.h"
|
||||
#include "blackmisc/message_dispatcher.h"
|
||||
#include "blackmisc/message_factory.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class CMessageSystem
|
||||
{
|
||||
public:
|
||||
CMessageSystem();
|
||||
|
||||
static void init();
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // MESSAGE_SYSTEM_H
|
||||
@@ -1,26 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef SERIALIZE_H
|
||||
#define SERIALIZE_H
|
||||
#include <QDataStream>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* \brief Serialize interface
|
||||
*/
|
||||
class ISerialize
|
||||
{
|
||||
public:
|
||||
ISerialize() {}
|
||||
virtual ~ISerialize() {}
|
||||
virtual QDataStream &operator<< (QDataStream &in) = 0;
|
||||
virtual QDataStream &operator>> (QDataStream &out) const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // SERIALIZE_H
|
||||
@@ -1,21 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#include "blackmisc/type_info.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
CTypeInfo::CTypeInfo(const std::type_info& info)
|
||||
: m_typeinfo(info)
|
||||
{
|
||||
}
|
||||
|
||||
bool CTypeInfo::operator <(const CTypeInfo& rhs) const
|
||||
{
|
||||
return m_typeinfo.before(rhs.m_typeinfo) != 0;
|
||||
}
|
||||
|
||||
} // namespace BlackMisc
|
||||
@@ -1,27 +0,0 @@
|
||||
//! Copyright (C) 2013 Roland Winklmeier
|
||||
//! This Source Code Form is subject to the terms of the Mozilla Public
|
||||
//! License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
//! file, You can obtain one at http://mozilla.org/MPL/2.0/
|
||||
|
||||
#ifndef TYPE_INFO_H
|
||||
#define TYPE_INFO_H
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
class CTypeInfo
|
||||
{
|
||||
public:
|
||||
explicit CTypeInfo(const std::type_info& info);
|
||||
|
||||
bool operator < (const CTypeInfo& rhs) const;
|
||||
|
||||
private:
|
||||
const std::type_info& m_typeinfo;
|
||||
};
|
||||
|
||||
} // namespace BlackMisc
|
||||
|
||||
#endif // TYPE_INFO_H
|
||||
Reference in New Issue
Block a user