Initial commit

This commit is contained in:
Roland Winklmeier
2013-02-15 18:50:17 +01:00
parent 57214c1c1e
commit 584251e0a8
125 changed files with 10640 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
ADD_SUBDIRECTORY(blackcore)
ADD_SUBDIRECTORY(blackmisc)

View File

@@ -0,0 +1,55 @@
//! 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 CONSTANTS_H
#define CONSTANTS_H
#include "math.h"
namespace BlackCore
{
namespace Constants
{
//! Conversion from Degree to Radians
const double DegToRad = 4.0 * atan(1.0) / 180.0;
//! Conversion from Radians to Degree
const double RadToDeg = 180.0 / (4.0 * atan(1.0));
//! Mathematical constant Pi
const double PI = 4.0 * atan(1.0);
//! 2 * Pi
const double TwoPI = 2.0 * PI;
//! Conversion from feet to meter
const double FeetToMeter = 0.3048;
//! Conversion from meter to feed
const double MeterToFeet = 3.28084;
//! Conversion from knots to meter/second
const double KnotsToMeterPerSecond = 0.5144444444;
//! Equatorial radius of WGS84 ellipsoid (6378137 m)
const double EarthRadius = 6378137.0;
//! Flattening of WGS84 ellipsoid (1/298.257223563).
const double Flattening = 1/298.257223563;
//! First eccentricity squared
const double e2 = (Flattening * (2 - Flattening));
//! First eccentricity to the power of four
const double e4 = CMath::square(e2);
const double e2m = CMath::square(1 - Flattening);
const double e2absolute = abs(e2);
}
} // namespace BlackCore
#endif // CONSTANTS_H

View File

@@ -0,0 +1,42 @@
//! 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 ECEF_H
#define ECEF_H
#include "vector_3d.h"
namespace BlackCore
{
class CNed;
class CVectorGeo;
class CEcef : public CVector3D
{
public:
CEcef();
CEcef(double X, double Y, double Z);
//! Converts this velocity vector into the NED format
/*!
\param pos This position is needed for correct calculation
\return velocity in NED coordinate system
*/
CNed toNED(const CVectorGeo &pos);
//! Converts this position vector into the geodetic format
/*!
\return Position in latitude, longitude and altitude
*/
CVectorGeo toGeodetic();
//! Assignment operator
void operator= (const CVector3D &rhs);
};
} // namespace BlackCore
#endif // ECEF_H

View File

@@ -0,0 +1,126 @@
//! 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 FSD_CLIENT_H
#define FSD_CLIENT_H
#include <QObject>
#include <QList>
#include <QString>
#include <QTcpSocket>
#include <blackcore/fsd_protocol.h>
namespace FSD
{
typedef struct {
QString m_host;
quint16 m_port;
QString m_callsign;
QString m_userid;
QString m_password;
FSD::SimulatorType m_simType;
QString m_realName;
} TClientInfo;
class CFSDClient : public QObject
{
Q_OBJECT
public:
CFSDClient();
void connectTo(const QString &host, quint16 port);
void disconnectFrom();
void reconnect();
void updateClientInfo(TClientInfo &clientInfo );
TClientInfo& clientInfo();
bool isConnected();
//! Returns a human readable description of the last error that occurred.
QString getErrorMessage(QAbstractSocket::SocketError error);
bool sendMessage(const QString &message);
//////////////////////////////////
// FSD specific senders functions
//////////////////////////////////
void sendText ( QString &message);
void sendTextonRadios ( QList<qint16> &frequencies, QString &message);
void sendClientQuery ( TQueryType type, QString &callsign);
void sendClientQueryReponse ( TQueryType type, QString &data);
void sendPilotPosition ( TPositionMessage *posMessage );
void sendFlightPlan ();
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 .
/*!
\param data Reference to the raw byte data to be sent.
\return Returns true if sending was successfull, otherwise false.
*/
void onConnected();
void onDisconnected();
void onError(QAbstractSocket::SocketError error);
void onReceivingData();
protected:
void sendAddPilot();
private:
CFSDClient( const CFSDClient& other);
const CFSDClient& operator = ( const CFSDClient& other);
void init();
void registerMessages();
void processLine(QString &line);
QTcpSocket* m_tcp_socket;
QString m_host;
quint16 m_port;
QString m_callsign;
QString m_userid;
QString m_password;
FSD::SimulatorType m_simType;
QString m_realName;
QString m_last_error;
};
} // namespace FSD
#endif // FSD_CLIENT_H

View File

@@ -0,0 +1,156 @@
//! 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 FSD_MESSAGES_H
#define FSD_MESSAGES_H
#include "blackmisc/message_system.h"
#define VATSIM_PROTOCOL_REV 9
// Qt includes
#include <QtGlobal>
#include <QVector>
typedef QVector<QString> QStringVector;
namespace FSD
{
class FSD_MSG : public BlackMisc::IMessage
{
public:
FSD_MSG(QString& id) : IMessage(id)
{
}
void setSource(const QString &source) {m_source = source; }
void setDest(const QString &destination) {m_destination = destination; }
protected:
qint32 unpack(const QString &line, QStringVector &tokens);
QString pack(const QStringVector &tokens) const;
QString m_destination;
QString m_source;
};
class FSD_MSG_AddPilot : public FSD_MSG
{
public:
FSD_MSG_AddPilot() : FSD_MSG(QString("#AP")), m_revision(VATSIM_PROTOCOL_REV),
m_rating(1)
{
}
void setUserID(const QString &userID) { m_userID = userID; }
void setPassword(const QString &password) { m_password = password; }
void setSimulator(const quint16 &simulator) { m_simulator = simulator; }
void setRealName(const QString &name) { m_realName = name; }
virtual QDataStream& operator<< (QDataStream& in) { return in; }
virtual QDataStream& operator>> (QDataStream& out) const { return out; }
virtual QTextStream& operator<< ( QTextStream& in)
{ return in; }
virtual QTextStream& operator>> (QTextStream& out) const
{
QStringVector tokens;
tokens << m_source << m_destination << m_userID << m_password;
tokens << QString("%1").arg(m_rating) << QString("%1").arg(m_revision);
tokens << QString("%1").arg(m_simulator) << m_realName;
out << pack(tokens);
return out;
}
private:
QString m_userID;
QString m_password;
quint16 m_rating;
quint16 m_revision;
quint16 m_simulator;
QString m_realName;
};
class FSD_MSG_TextMessage : public FSD_MSG
{
public:
FSD_MSG_TextMessage() : FSD_MSG(QString("#TM"))
{
}
virtual QDataStream& operator<< (QDataStream& in) { return in; }
virtual QDataStream& operator>> (QDataStream& out) const { return out;}
virtual QTextStream& operator<< ( QTextStream& in)
{
QString message = in.readAll();
QStringVector tokens;
//tokens.resize(3);
unpack(message, tokens);
m_source = tokens.at(0);
m_destination = tokens.at(1);
int size = tokens.size();
QString m_textmessage;
for ( int ii = 2; ii < tokens.size(); ++ii)
m_textmessage += tokens.at(ii);
bInfo << m_textmessage;
return in;
}
virtual QTextStream& operator>> (QTextStream& out) const
{
QStringVector tokens;
tokens << m_source << m_destination;
out << pack(tokens);
return out;
}
};
class FSD_MSG_Plane_Position : public FSD_MSG
{
public:
FSD_MSG_Plane_Position() : FSD_MSG(QString("@"))
{
}
inline QString SquawkMode() const { return m_message_tokens.at(0); }
inline QString Callsign() const { return m_message_tokens.at(1); }
inline QString Squawk() const { return m_message_tokens.at(2); }
inline quint16 Rating() const { return m_message_tokens.at(3).toUInt(); }
inline double Latitude() const { return m_message_tokens.at(4).toDouble(); }
inline double Longitude() const { return m_message_tokens.at(5).toDouble(); }
inline qint32 Altitude() const { return m_message_tokens.at(6).toInt(); }
inline qint32 Speed() const { return m_message_tokens.at(7).toInt(); }
inline quint32 PBH() const { return m_message_tokens.at(8).toUInt(); }
inline qint32 AltDiff() const { return m_message_tokens.at(9).toInt(); }
inline void setSquawkMode( const QString &squawk_mode) { m_message_tokens.replace(0, squawk_mode); }
inline void setCallsign ( const QString &callsign) { m_message_tokens.replace(1, callsign); }
inline void setSquawk ( const QString &squawk) { m_message_tokens.replace(2, squawk); }
inline void setRating ( const quint16 rating) { m_message_tokens.replace(3, QString("%1").arg(rating)); }
inline void setLatitude ( const double latitude) { m_message_tokens.replace(4, QString("%1").arg(latitude)); }
inline void setLongitude ( const double longitude) { m_message_tokens.replace(5, QString("%1").arg(longitude)); }
inline void setAltitude ( const qint32 altitude) { m_message_tokens.replace(6, QString("%1").arg(altitude)); }
inline void setSpeed ( const qint32 speed) { m_message_tokens.replace(7, QString("%1").arg(speed)); }
inline void setPBH ( const quint32 pbh) { m_message_tokens.replace(8, QString("%1").arg(pbh)); }
inline void setAltDiff ( const qint32 altdiff) { m_message_tokens.replace(9, QString("%1").arg(altdiff)); }
virtual QTextStream& operator<< ( QTextStream& in);
virtual QTextStream& operator>> (QTextStream& out) const;
virtual QDataStream& operator<< (QDataStream& in) { return in; }
virtual QDataStream& operator>> (QDataStream& out) const { return out;}
private:
QStringVector m_message_tokens;
};
} // namespace FSD
#endif // FSD_MESSAGES_H

View File

@@ -0,0 +1,61 @@
//! 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 FSD_PROTOCOL_H
#define FSD_PROTOCOL_H
#include <QString>
namespace FSD
{
enum SimulatorType {
SIM_UNKNOWN = -1,
};
typedef enum {
Query_FP,
Query_Frequency,
Query_Server,
Query_RealName,
Query_isATC,
Query_Capabilities,
Query_IP
} TQueryType;
typedef enum {
TMode_Standby = 'S',
TMode_Charly = 'N',
TMode_Ident = 'Y'
} TTransponderMode;
typedef struct {
TTransponderMode transponderMode;
quint16 squawk;
quint8 rating;
double latitude;
double longitude;
qint32 altitude;
qint32 groundSpeed;
double pitch;
double bank;
double heading;
qint32 diffPressureTrueAlt;
} TPositionMessage;
const QString Headers[] = {
"@",
"%",
"#AA",
"#AP",
"#DA",
"#DP",
"#TM"
};
#define MAX_FSD_HEADERS 7
}
#endif // FSD_PROTOCOL_H

View File

@@ -0,0 +1,78 @@
//! 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 INTERPOLATOR_H
#define INTERPOLATOR_H
#include <QElapsedTimer>
#include "blackcore/vector_geo.h"
#include <blackcore/ecef.h>
#include "blackcore/vector_3d.h"
#include "blackcore/ned.h"
#include "blackcore/ecef.h"
#include "blackcore/constants.h"
namespace BlackCore
{
typedef struct
{
double heading;
double pitch;
double bank;
} TOrientation;
typedef struct
{
void reset()
{
}
qint64 timestamp;
CEcef position;
TOrientation orientation;
double groundspeed;
CVector3D velocity;
CNed velNED;
} TPlaneState;
class CInterpolator
{
public:
CInterpolator();
void initialize();
void pushUpdate(CVectorGeo pos, double groundVelocity, double heading, double pitch, double bank);
bool isValid();
bool stateNow(TPlaneState *state);
private:
double normalizeRadians(double radian);
QElapsedTimer m_time;
TPlaneState *m_state_begin;
TPlaneState *m_state_end;
bool m_valid;
CVector3D m_a;
CVector3D m_b;
double m_timeEnd;
double m_timeBegin;
};
} // namespace BlackCore
#endif // INTERPOLATOR_H

View File

@@ -0,0 +1,56 @@
//! 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 MATH_H
#define MATH_H
namespace BlackCore
{
class CMath
{
public:
//! Calculates the hypotenuse of x and y without overflow
/*!
\param x
\param y
*/
static double hypot(double x, double y);
//! Calculates the square of x
/*!
\param x
\return x^2
*/
static inline double square(const double x)
{
return x*x;
}
//! Calculates x to the power of three
/*!
\param x
\return x^3
*/
static inline double cubic(const double x)
{
return x*x*x;
}
//! Calculates the real cubic root
/*!
\param x
\param y
\return Returns the real part of the solution
*/
static double cubicRootReal(const double x);
private:
CMath() {}
};
} // namespace BlackCore
#endif // MATH_H

View File

@@ -0,0 +1,86 @@
//! 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 MATRIX_3D_H
#define MATRIX_3D_H
#include "blackcore/vector_3d.h"
#include "blackcore/ecef.h"
namespace BlackCore
{
class CMatrix3D
{
public:
CMatrix3D();
CMatrix3D(const CMatrix3D & other);
/*!
Basic Matrix functions
*/
//! Fills the matrix with random elements
void random();
//! Calculates the determinant of the matrix
double determinant();
//! Returns the inverse matrix
CMatrix3D inverse();
//! Sets all elements to zero
void zeros();
//! Prints the matrix to stdout
void print();
//! Returns an element
/*!
\param row Specifies elements row
\param column Specifies elements column
\return Returns element of [row, column]
*/
double getElement(qint8 row, qint8 column) const;
//! Sets a matrix element
/*!
\param row Specifies elements row
\param column Specifies elements column
\param value Specifies the new elements value
*/
void setElement(qint8 row, qint8 column, double value);
/*!
Operators
*/
CMatrix3D & operator +=(const CMatrix3D &rhs);
CMatrix3D & operator -=(const CMatrix3D &rhs);
CMatrix3D & operator = (const CMatrix3D &rhs);
CMatrix3D operator +(const CMatrix3D &rhs);
CMatrix3D operator -(const CMatrix3D &rhs);
bool operator ==(const CMatrix3D &rhs);
bool operator !=(const CMatrix3D &rhs);
CMatrix3D & operator *=(const CMatrix3D &rhs);
CMatrix3D operator *(const CMatrix3D &rhs);
CVector3D operator * ( const CVector3D &rhs);
CEcef operator * ( const CEcef &rhs);
double& operator() (const qint8 row, const qint8 column);
private:
double m[3][3];
};
} // namespace BlackCore
#endif // MATRIX_3D_H

View File

@@ -0,0 +1,100 @@
//! 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 MULTIPLAYER_H
#define MULTIPLAYER_H
#include <QHash>
#include <QString>
#include <qglobal.h>
#include <blackmisc/message_system.h>
#include <blackcore/fsd_messages.h>
namespace BlackCore {
class CPlane;
class ISimulator;
union FS_PBH {
quint32 pbh;
struct {
quint32 unused : 1;
quint32 onground : 1;
quint32 hdg : 10;
quint32 bank : 10;
quint32 pitch : 10;
};
};
class CMultiPlayer : public BlackMisc::CMessageHandler
{
public:
CMultiPlayer();
//! Starts the multiplayer mode.
void start ();
//! Stops the multiplayer mode.
void stop ();
//! Returns true if the multiplayer mode has been started. Otherwise false. \sa start()
/*!
\return Return true if running, otherwise false.
*/
inline bool isRunning() const { return m_isRunning; }
//! This is the trigger, to do all frequent calculations and send everything to the simulator.
//! CMultiPlayer does nothing by itself, the parent has to call this.
void run();
//! Returns true the plane is known, othwewise false.
/*!
\return Return true if the given plane is in the hash map, otherwise false.
*/
bool isKnown(const QString &callsign) const;
//! Enables or disables the injection of AI planes
/*!
\param enable If enable is true, injection is enabled, otherwise it will be disabled
*/
void enableAIPlanes(bool enable);
//! Returns true if rendering of AI planes is enabled
/*!
\return Return true if enabled, otherwise false.
*/
bool areAIPlanesEnabled() const;
//! Use this method if you need a specific plane object.
/*!
\param callsign Callsign of the players plane
\return Returns the pointer to the Plane object.
*/
CPlane *getPlane ( const QString &callsign);
private:
void onPositionUpdate(const FSD::FSD_MSG_Plane_Position *plane_position);
void addPlane(CPlane *plane);
void removePlane(CPlane *plane);
bool needsToRemoved(CPlane *plane);
typedef QHash<QString, CPlane*> TPlaneManager;
TPlaneManager m_multiplayer_planes;
bool m_isRunning;
bool m_enableAIPlanes;
ISimulator *m_simulator;
};
} //! namespace BlackCore
#endif // MULTIPLAYER_H

View File

@@ -0,0 +1,47 @@
//! 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 VECTOR_NED_H
#define VECTOR_NED_H
#include "vector_3d.h"
#include "vector_geo.h"
namespace BlackCore
{
class CEcef;
class CNed : public CVector3D
{
public:
CNed();
CNed(CVectorGeo &pos, double N, double E, double D);
double North() const {return v[0];}
double East() const {return v[1];}
double Down() const {return v[2];}
CVectorGeo position() const { return m_position; }
void setNorth(const double num) { v[0] = num; }
void setEast(const double num) { v[1] = num; }
void setDown(const double num) { v[2] = num; }
void setPosition(const CVectorGeo &pos ) { m_position = pos; }
CEcef toECEF();
private:
CVectorGeo m_position;
};
} // namespace BlackCore
#endif // VECTOR_NED_H

View File

@@ -0,0 +1,52 @@
//! 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 PLANE_H
#define PLANE_H
#include <QString>
namespace BlackCore {
class ISimulator;
class CInterpolator;
class CPlane
{
public:
enum ESquawkMode {Standby = 'S',
Charlie = 'N',
Ident = 'Y'};
CPlane();
CPlane(const QString &callsign, ISimulator *driver);
void addPosition(const CVectorGeo &position, double groundVelocity, double heading, double pitch, double bank);
//! Returns the callsign of the multiplayer plane
/*!
\return callsign.
*/
inline QString& Callsign() { return m_callsign; }
void render();
double getLastUpdateTime();
private:
QString m_callsign;
CInterpolator *m_interpolator;
ISimulator *m_driver;
};
} // namespace BlackCore
#endif // PLANE_H

View File

@@ -0,0 +1,16 @@
//! 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 SIM_CALLBACKS_H
#define SIM_CALLBACKS_H
typedef void (* cbChangedRadioFreq)(const int radioNumber, const QString &frequency);
typedef void (* cbChangedGearPosition)(const int percentage);
typedef void (* cbChangedLights)(const int map);
typedef void (* cbSimStarted)(const bool status);
typedef void (* cbChangedAircraftType)(const QString &type);
typedef void (* cbChangedFlaps)(const int percentage);
#endif // CALLBACKS_H

View File

@@ -0,0 +1,135 @@
//! 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 SIMULATOR_H
#define SIMULATOR_H
#include <QString>
#include <blackcore/sim_callbacks.h>
#include "blackcore/vector_geo.h"
class CLibraryContext;
#define SHARED_LIBRARY_NAME_FS9 "bb_driver_fs9"
#define SHARED_LIBRARY_NAME_FSX "bb_driver_fsx"
#define SHARED_LIBRARY_NAME_XPLANE "bb_driver_xplane"
namespace BlackCore {
class CVector3D;
class CVectorGEO;
class IContext;
class ISimulator
{
public:
/// Version of the driver interface. To increment when the interface change.
static const quint32 InterfaceVersionMajor;
static const quint32 InterfaceVersionMinor;
enum ESimulator { FS9 = 0,
FSX,
XPLANE};
ISimulator(void) {}
virtual ~ISimulator() {}
virtual void setLibraryContext(IContext *context) = 0;
////////////////////////////////
// Global section
////////////////////////////////
virtual int init() = 0;
virtual int connectTo() = 0;
// Callback when the Simulation starts
virtual void setcbSimStarted(cbSimStarted func, void *context);
virtual bool isRunning() = 0;
virtual QString getLastErrorMessage() = 0;
virtual qint32 sendTextMessage(const QString &text) = 0;
////////////////////////////////
// Remote plane section
////////////////////////////////
virtual qint32 addPlane(const QString& callsign, const QString &type, const CVectorGEO * const pos, const double groundSpeed) = 0;
virtual bool removePlane(const qint32 id) = 0;
virtual bool updatePositionAndSpeed(const qint32 id, const CVectorGEO * const pos, const double groundSpeed) = 0;
virtual bool setGear(const qint32 id, const qint32 percentage) = 0;
virtual bool setFlaps(const qint32 id, const qint32 percentage) = 0;
virtual bool setLights(const qint32 id, const qint32 map) = 0;
////////////////////////////////
// User plane section
////////////////////////////////
// Callback frequency tuner
virtual void setChangedRadioFreq(cbChangedRadioFreq func, void * context);
// Callback when the gear is moving
virtual void setcbChangedGearPosition(cbChangedGearPosition func, void *context);
// Callback if the user switched on/off a light
virtual void setcbChangedLights(cbChangedLights func, void *context);
// Callback, when the Aircraft is set or gets changed
virtual void setcbChangedAircraftType(cbChangedAircraftType func, void *context);
// Callback, when the Flaps are moving
virtual void setcbChangedFlaps(cbChangedFlaps func, void *context);
// This one is called regular and when we actually need it, therefor not a callback
virtual double getUserSpeed() const = 0;
// This one is called regular and when we actually need it, therefor not a callback
virtual CVectorGEO getUserPosition() const = 0;
// This one is called regular and when we actually need it, therefor not a callback
virtual qint32 getUserPitch() const = 0;
// This one is called regular and when we actually need it, therefor not a callback
virtual qint32 getUserBank() const = 0;
// This one is called regular and when we actually need it, therefor not a callback
virtual qint32 getUserHeading() const = 0;
// This one is called regular and when we actually need it, therefor not a callback
virtual qint32 getUserPitchBankHeading() const = 0;
virtual bool isOnGround() const = 0;
static ISimulator *createDriver(ESimulator sim);
protected:
CLibraryContext * mLibraryContext;
cbSimStarted mSimStarted;
cbChangedRadioFreq mChangedRadioFreq;
cbChangedGearPosition mChangedGearPosition;
cbChangedLights mChangedLights;
cbChangedAircraftType mChangedAircraftType;
cbChangedFlaps mChangedFlaps;
void *m_CallbackContext;
};
} //! namespace BlackCore
#endif // SIMULATOR_H

View File

@@ -0,0 +1,73 @@
//! 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 <qglobal.h>
#ifndef VECTOR_3D_H
#define VECTOR_3D_H
namespace BlackCore
{
class CMatrix3D;
class CVector3D
{
public:
CVector3D();
CVector3D(double x, double y, double z);
CVector3D( const CVector3D & other);
double X() const {return v[0];}
double Y() const {return v[1];}
double Z() const {return v[2];}
void setX(const double num) { v[0] = num; }
void setY(const double num) { v[1] = num; }
void setZ(const double num) { v[2] = num; }
double getElement(qint8 row) const;
void print();
void zeros();
CVector3D & operator +=(const CVector3D &rhs);
CVector3D & operator -=(const CVector3D &rhs);
CVector3D & operator = (const CVector3D &rhs);
CVector3D operator +(const CVector3D &rhs);
CVector3D operator -(const CVector3D &rhs);
bool operator ==(const CVector3D &rhs);
bool operator !=(const CVector3D &rhs);
//double crossProduct(qint32 );
CVector3D & operator *=(const CVector3D &rhs);
CVector3D operator *( const CVector3D &rhs);
CVector3D & operator *=( const double rhs);
CVector3D operator *( const double rhs);
CVector3D & operator /=( const double rhs);
CVector3D operator /( const double rhs);
double magnitude();
protected:
double v[3];
friend class CMatrix3D;
};
} //! namespace BlackCore
#endif // VECTOR_3D_H

View File

@@ -0,0 +1,56 @@
//! 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 VECTOR_GEO_H
#define VECTOR_GEO_H
namespace BlackCore
{
class CEcef;
class CVectorGeo
{
public:
CVectorGeo();
CVectorGeo(double lat, double lon, double alt);
CVectorGeo( const CVectorGeo &other );
void setLatitude( double latitude)
{
m_latitude = latitude;
}
void setLongitude( double longitude)
{
m_longitude = longitude;
}
void setAltitude( double altitude)
{
m_altitude = altitude;
}
double latitude() const {return m_latitude;}
double longitude() const {return m_longitude;}
double altitude() const {return m_altitude;}
CEcef toCartesian();
void zeros();
void print();
CVectorGeo & operator = (const CVectorGeo &rhs);
private:
double m_latitude;
double m_longitude;
double m_altitude;
};
} // namespace BlackCore
#endif // VECTOR_GEO_H

View File

@@ -0,0 +1 @@
FILE(GLOB HEADERS *.h)

View File

@@ -0,0 +1,117 @@
//! 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 <QString>
#include <QAbstractSocket>
#include "blackmisc/com_handler.h"
class QTcpSocket;
namespace BlackMisc
{
class CComClient : public IComHandler
{
Q_OBJECT
public:
//! Constructor
/*!
\param parent Pointer to the parent QObject
*/
CComClient(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();
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

View File

@@ -0,0 +1,53 @@
//! 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 <QObject>
#include "blackmisc/com_handler.h"
class QTcpSocket;
namespace BlackMisc
{
class CComClientBuffer : public IComHandler
{
Q_OBJECT
public:
CComClientBuffer(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:
QTcpSocket* m_tcp_socket;
uint m_client_id;
};
} // namespace BlackMisc
#endif // COM_CLIENT_BUFFER_H

View File

@@ -0,0 +1,72 @@
//! 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
{
//! 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
*/
explicit IComHandler(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);
//! 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

View File

@@ -0,0 +1,104 @@
//! 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;
class CComClientBuffer;
namespace BlackMisc
{
class CComServer : public QObject
{
Q_OBJECT
public:
//! Constructor
/*!
\param parent Pointer to the parent QObject
*/
CComServer(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:
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

View File

@@ -0,0 +1,162 @@
//! 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 <QtGlobal>
#include <QMap>
namespace BlackMisc
{
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 _value; }
qint32 asInt( bool* ok = NULL );
bool asBool(bool* ok = NULL );
double asDouble( bool* result );
inline bool isValid() {return _type != CONFIG_UNKOWN;}
inline bool isInt() { return _type == CONFIG_INT; }
inline bool isDouble() { return _type == CONFIG_DOUBLE; }
inline bool isBool() { return _type == CONFIG_BOOL; }
protected:
QString _value;
TConfigType _type;
qint32 _int_value;
double _double_value;
bool _bool_value;
};
//! Configuration class.
/*!
This class implements the configuration part of the library.
*/
class CConfig
{
public:
CConfig(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() {} // TODO
//! Saves the config to file.
/*!
\param filename Specify a different filename.
\return Returns true if loading went well.
*/
bool save(const QString& filename) {} // TODO
protected:
QString m_configfile;
QString m_separator;
typedef QMap<QString, CValue> TValueMap;
TValueMap m_value_map;
};
} //! namespace BlackMisc
#endif // CONFIG_H

View File

@@ -0,0 +1,59 @@
//! 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 CONFIGMANAGER_H
#define CONFIGMANAGER_H
#include <QMap>
#include <QString>
#include "blackmisc/context.h"
namespace BlackMisc
{
class CConfig;
class CConfigManager
{
// safe singleton declaration
SINGLETON_CLASS_DECLARATION(CConfigManager)
CConfigManager();
public:
//! 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 &section);
private:
typedef QMap<QString, CConfig*> TConfigMap;
TConfigMap m_config_map;
QString m_config_path;
};
} // namespace BlackLib
#endif // CONFIGMANAGER_H

View File

@@ -0,0 +1,249 @@
//! 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 CONTEXT_H
#define CONTEXT_H
#include <QMap>
namespace BlackMisc
{
class CLog;
class CDebug;
//! IContext Interface.
/*!
This interface abstracts any application and library context. Use it to
add singleton classes and for application and library wide logging.
*/
class IContext
{
public:
//! Reference to IConext.
/*!
This static function returns a reference to the singleton object.
Use always this method to retrieve the context, since you cannot
instantiate your own one. Before it can be used you have to create
either a CApplicationContext or a CLibraryContext in the very
beginning of the executable/libary.
*/
static IContext &getInstance();
//! Returns true if the context is initialized and ready.
/*!
\return Returns true if context is initialized, otherwise false.
*/
static bool isInitialised();
//! Destructor.
/*!
Destroys the context.
*/
virtual ~IContext();
//! Pure virtual method returns the pointer to singletone object by its name.
/*!
\param singletonName a reference to the singletones name.
\return Returns a pointer to the singleton object.
*/
virtual void *singletonPointer(const QString &singletonName) = NULL;
//! Pure virtual method sets the singletone pointer, given by its name.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void setSingletonPointer(const QString &singletonName, void *object) = NULL;
//! Deallocates the object and removes the singletone pointer from the context map.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void releaseSingletonPointer(const QString &singletonName, void *object) = NULL;
//! Pure virtual method returns the pointer to debug object
/*!
\return Pointer to CDebug object
*/
virtual CDebug *getDebug() = NULL;
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CDebug object
*/
virtual void setDebug(CDebug *debug) = NULL;
protected:
//! Method to register the context.
/*! This method should be called by the derived class. It sets the
context variable to the calling object.*/
/*!
\param Pointer to CLog object
*/
void registerContext();
//! Pointer to the global context.
/*!
This variable holds a pointer to the application context.
*/
static IContext *m_context;
//! Pointer to the global Debug object.
/*!
This member variable contains all logging types, not only debug.
*/
CDebug *m_debug;
};
//! CApplicationContext.
/*!
This class implements the IContext interface for applications.
*/
class CApplicationContext : public IContext
{
public:
CApplicationContext();
//! This method returns the pointer to singletone object by its name.
/*!
\param singletonName a reference to the singletones name.
\return Returns a pointer to the singleton object.
*/
virtual void *singletonPointer(const QString &singletonName);
//! Sets the singletone pointer, given by its name.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void setSingletonPointer(const QString &singletonName, void *object);
//! Deallocates the object and removes the singletone pointer from the context map.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void releaseSingletonPointer(const QString &singletonName, void *ptr);
//! Pure virtual method returns the pointer to debug object
/*!
\return Pointer to CDebug object
*/
virtual CDebug *getDebug();
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CDebug object
*/
virtual void setDebug(CDebug *debug);
private:
//! Typedef for the singleton map
typedef QMap<QString, void*> TSingletonMap;
//! Map of all registered objects inside the application.
/*!
This map holds associates all registered singleton objects to their names.
*/
TSingletonMap m_singletonObjects;
};
//! CApplicationContext.
/*!
This class implements the IContext interface for libraries.
*/
class CLibraryContext : public IContext
{
public:
CLibraryContext (IContext &application);
//! This method returns the pointer to singletone object by its name.
/*!
\param singletonName a reference to the singletones name.
\return Returns a pointer to the singleton object.
*/
virtual void *singletonPointer(const QString &singletonName);
//! Sets the singletone pointer, given by its name.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void setSingletonPointer(const QString &singletonName, void *ptr);
//! Deallocates the object and removes the singletone pointer from the context map.
/*!
\param singletonName a reference to the singletones name.
\param object a pointer to the singletone object.
*/
virtual void releaseSingletonPointer(const QString &singletonName, void *ptr);
//! Pure virtual method returns the pointer to debug object
/*!
\return Pointer to CDebug object
*/
virtual CDebug *getDebug();
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CDebug object
*/
virtual void setDebug(CDebug *debug);
private:
//! Pointer the application context.
/*!
Libraries do not have their own context, because they are using
the one from the linked application.
*/
IContext *m_applicationContext;
};
/*!
This macros helps to create the body of a singletone class,
which registers itself with the application context.
*/
#define SINGLETON_CLASS_DECLARATION(className) \
private:\
/* Singletone class constructor */ \
className (const className &) {}\
/* the local static pointer to the singleton instance */ \
static className *m_instance; \
public:\
static className &getInstance() \
{ \
if (m_instance == NULL) \
{ \
/* We need a valid context.*/ \
bAssertstr(BlackMisc::IContext::isInitialised(), QString("You are trying to access a singleton without having initialized a context. The simplest correction is to add 'BlackMisc::CApplicationContext myApplicationContext;' at the very beginning of your application.")); \
\
/* Get the singleton object from the context, if there is one */ \
void *object = BlackMisc::IContext::getInstance().singletonPointer(#className); \
if (object == NULL) \
{ \
/* We have no allocated object yet, so do it now. */ \
m_instance = new className; \
BlackMisc::IContext::getInstance().setSingletonPointer(#className, m_instance); \
} \
else \
{ \
/* Always use the c++ methods instead of casting the C way. */ \
m_instance = reinterpret_cast<className*>(object); \
} \
} \
return *m_instance; \
} \
private:
/* Put your class constructor directly after this makro and make sure it is private! */
#define SINGLETON_CLASS_IMPLEMENTATION(className) className *className::m_instance = NULL;
} // namespace BlackMisc
#endif CONTEXT_H

View File

@@ -0,0 +1,147 @@
//! 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 DEBUG_H
#define DEBUG_H
#include "blackmisc/context.h"
#include "blackmisc/log.h"
#include "blackmisc/display.h"
#include "blackmisc/logmessage.h"
// This header contains debug macros
namespace BlackMisc
{
//! class CDebug
/*! This class implements the logging of the library. It holds a list of displays
takes care of any additional information needed, e.g. timestamps, filename etc.
To use the logging use the default displayer or implement your own one and
register it with this class.
*/
class CDebug
{
public:
CDebug();
// internal use only
void create (QString &logPath = QString(""), bool logInFile = true, bool eraseLastLog = false);
// init Debug
void init(bool logInFile);
/// Do not call this, unless you know what you're trying to do (it kills debug)!
void destroy() {}
void changeLogDirectory(const QString &dir) {}
QString getLogDirectory();
// Assert function
//! Method returns the pointer to the global assert log object
/*!
\return Pointer to CLog object
*/
CLog *getAssertLog();
//! Pure virtual function to set the global assert log object
/*!
\param Pointer to CLog object
*/
void setAssertLog(CLog *assertLog);
//! Pure virtual method returns the pointer to the global error log object
/*!
\return Pointer to CLog object
*/
CLog *getErrorLog();
//! Pure virtual function to set the global error log object
/*!
\param Pointer to CLog object
*/
void setErrorLog(CLog *errorLog);
//! Pure virtual method returns the pointer to the global warning log object
/*!
\return Pointer to CLog object
*/
CLog *getWarningLog();
//! Pure virtual function to set the global warning log object
/*!
\param Pointer to CLog object
*/
void setWarningLog(CLog *warningLog);
//! Pure virtual method returns the pointer to the global info log object
/*!
\return Pointer to CLog object
*/
CLog *getInfoLog();
//! Pure virtual function to set the global info log object
/*!
\param Pointer to CLog object
*/
void setInfoLog(CLog *infoLog);
//! Pure virtual method returns the pointer to the global debug log object
/*!
\return Pointer to CLog object
*/
CLog *getDebugLog();
//! Pure virtual function to set the global debug log object
/*!
\param Pointer to CLog object
*/
void setDebugLog(CLog *debugLog);
void assertFailed(int line, const char *file, const char* function, const char *exp);
void assertFailedString(int line, const char *fileName, const char *methodName, const char* exp, const char* string);
CLogMessage blackInfo(int line, const char *fileName, const char *methodName);
CLogMessage blackWarning(int line, const char *fileName, const char *methodName);
CLogMessage blackDebug(int line, const char *fileName, const char *methodName);
CLogMessage blackError(int line, const char *fileName, const char *methodName);
CLogMessage blackAssert(int line, const char *fileName, const char *methodName);
CLogMessage blackAssertqstr(int line, const char *fileName, const char *methodName);
private:
bool m_isInitialized;
CStdDisplay *stdDisplayer;
CFileDisplay *fileDisplayer;
QString m_logPath;
CLog *m_errorLog;
CLog *m_warningLog;
CLog *m_infoLog;
CLog *m_debugLog;
CLog *m_assertLog;
};
/*!
Macro is not defined in VC6
*/
#if _MSC_VER <= 1200
# define __FUNCTION__ NULL
#endif
#define bInfo ( BlackMisc::IContext::getInstance().getDebug()->blackInfo(__LINE__, __FILE__, __FUNCTION__ ) )
#define bWarning ( BlackMisc::IContext::getInstance().getDebug()->blackWarning(__LINE__, __FILE__, __FUNCTION__ ) )
#define bDebug ( BlackMisc::IContext::getInstance().getDebug()->blackDebug(__LINE__, __FILE__, __FUNCTION__ ) )
#define bError ( BlackMisc::IContext::getInstance().getDebug()->blackError(__LINE__, __FILE__, __FUNCTION__ ) )
#define bAssert(exp) if (!(exp)) BlackMisc::IContext::getInstance().getDebug()->assertFailed(__LINE__, __FILE__, __FUNCTION__, #exp)
#define bAssertstr(exp, str) if (!(exp)) BlackMisc::IContext::getInstance().getDebug()->assertFailedString(__LINE__, __FILE__, __FUNCTION__, #exp, #str)
} // namespace BlackMisc
#endif DEBUG_H // DEBUG_H

View File

@@ -0,0 +1,95 @@
//! 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 DISPLAY_H
#define DISPLAY_H
#include <QFile>
#include <QMutex>
#include "blackmisc/log.h"
namespace BlackMisc
{
class ILogDisplay
{
public:
/// Constructor
ILogDisplay(const QString &displayName );
/// Destructor
virtual ~ILogDisplay();
/// Display the string where it does.
void print( const CLog::SLogInformation &logInformation, const QString &message );
/// This is the identifier for a displayer, it is used to find or remove a displayer
QString DisplayName;
protected:
/// Method to implement in the derived class
virtual void doPrint(const CLog::SLogInformation &logInformation, const QString &message) = NULL;
// Return the header string with date (for the first line of the log)
static QString headLine ();
public:
/// Convert log type to string
static const char *logTypeToString (CLog::TLogType logType);
/// Convert the current date to human string
static QString currentDateToString ();
/// Convert date to "2000/01/14 10:05:17" string
static QString dateToString (const QDateTime &time);
private:
QMutex *m_mutex;
};
class CStdDisplay : virtual public ILogDisplay
{
public:
CStdDisplay ( const QString &displayerName = QString("")) : ILogDisplay (displayerName) {}
protected:
/// Display the string to stdout and OutputDebugString on Windows
virtual void doPrint (const CLog::SLogInformation &logInformation, const QString &message);
};
class CFileDisplay : virtual public ILogDisplay
{
public:
/// Constructor
CFileDisplay (const QString &filename, bool eraseLastLog = false, const QString &displayName = "");
CFileDisplay ();
~CFileDisplay ();
/// Set Parameter of the displayer if not set at the ctor time
void setParam (const QString &filename, bool eraseLastLog = false);
protected:
/// Put the string into the file.
virtual void doPrint (const CLog::SLogInformation &logInformation, const QString &message);
private:
QString m_fileName;
QFile *m_file;
bool m_needHeader;
};
} // BlackMisc
#endif // DISPLAY_H

View File

@@ -0,0 +1,115 @@
//! 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

View File

@@ -0,0 +1,204 @@
//! 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 LOG_H
#define LOG_H
#include <QMutex>
#include <QList>
#include <QString>
#include <QDateTime>
namespace BlackMisc {
class ILogDisplay;
//! Logging class
/*! This class implements the logging of the library. It holds a list of displays
takes care of any additional information needed, e.g. timestamps, filename etc.
To use the logging use the default displayer or implement your own one and
register it with this class.
*/
class CLog
{
public:
//! Logging type.
/*! This enum holds all different available log types. */
typedef enum { OFF = 0,
ERROR,
WARNING,
INFO,
DEBUG,
ASSERT,
UNKNOWN } TLogType;
//! SLogParameter
/*!
This structs capsulates all information needed to output a log line.
*/
struct SLogInformation
{
SLogInformation() : m_logType(CLog::OFF), m_threadId(0), m_line(-1), m_sourceFile(NULL), m_methodName(NULL) {}
QDateTime m_dateTime;
TLogType m_logType;
QString m_applicationName;
quint32 m_threadId;
const char *m_sourceFile;
qint32 m_line;
const char *m_methodName;
};
CLog (TLogType logType = OFF);
//! CLog destructor.
virtual ~CLog(void);
/*!
* Display
*/
//! This method adds a Displayer to the Log object
/*! CLog does not own the pointer. It is the callers responsibility
to allocate the displayer and remove it after using. */
/*!
\param display A pointer to a display.
\sa ILogDisplay
*/
void attachDisplay (BlackMisc::ILogDisplay *display);
//! This method returns the pointer to a registered display.
/*!
\return display A pointer to a display.
*/
ILogDisplay *getDisplay (const QString &displayName);
//! Removes a display
/*!
\param logDisplay Pointer to the display.
*/
void dettachDisplay (ILogDisplay *logDisplay);
//! Removes a display by its name.
/*!
\param displayName Name of the display.
*/
void dettachDisplay (const QString& displayName);
//! Checks if the displayer is added
/*!
\param logDisplay Pointer to a display.
\return Returns true if display is attached, otherwise false.
*/
bool isAttached (ILogDisplay *logDisplay) const;
//! Checks if the object has any attached displays
/*!
\return Returns true if no display is attached, otherwise false.
*/
bool hasNoDisplays () const { return m_logDisplays.empty(); }
//! Sets the name of the application process
/*!
\param displayName Name of the application.
*/
static void setApplicationName (const QString &displayName);
//! Sets the default application name. This is the name
//! of the executable.
static void setDefaultApplicationName ();
//! Sets any additional information as prefix to the log message
/*!
\param fileName This is the name of the corresponding source file.
\param methodName This is the name of calling method.
*/
void setLogInformation (int line, const char *fileName, const char *methodName = NULL);
//! Prints the message and adds an new line character at the end
/*!
\param message Message string, which has to be logged
*/
void printWithNewLine( QString &message );
//! Prints the message as is and appends no additional characters
/*!
\param message Message string, which has to be logged
*/
void print( QString &message );
void printString ( QString &message );
protected:
/// Display a string in decorated form to all attached displayers.
void displayString (const char *str);
/// Symetric to setPosition(). Automatically called by display...(). Do not call if noDisplayer().
void resetLogInformation();
//! Logging Type.
/*!
Specifies which logging type should be used.
Possible are: Error, Warning, Info, Debug and Assert.
*/
TLogType m_logType;
//! Application name.
/*!
Specifies the name of the application to put it into the log line
*/
static QString *m_applicationName;
//! Source file name.
/*!
Specifies the name of the source file, log message was called from.
*/
const char *m_sourceFile;
//! Code line.
/*!
Specifies the line in the source file, log message was called from.
*/
qint32 m_line;
//! Method name.
/*!
Specifies the method in the source file, log message was called from.
*/
const char *m_methodName;
/*!
\typedef TLogDisplayerMap
QList container holding pointers to ILogDisplay objects
\sa ArrayList::GetEnumerator, \sa List::GetEnumerator
*/
typedef QList<ILogDisplay *> TLogDisplayList;
//! List of all attached displays.
TLogDisplayList m_logDisplays;
//! Mutex object
/*!
This makes our class thread safe.
*/
QMutex m_mutex;
//! Position
quint32 m_posSet;
//! Log message string
/*!
This variable is used, if a log line consists of two or more
calls. The first ones are then stored temporary in this variable
until one message ends with a line feed.
*/
QString m_logLine;
SLogInformation m_logInformation;
};
} // namespace BlackMisc
#endif LOG_H

View File

@@ -0,0 +1,83 @@
//! 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 LOGMESSAGE_H
#define LOGMESSAGE_H
// Qt includes
#include <QTextStream>
#include "blackmisc/context.h"
#include "blackmisc/log.h"
namespace BlackMisc
{
class CLogMessage
{
struct LogStream {
//! Constructor
LogStream(CLog::TLogType type);
QTextStream output;
//! Message Buffer
QString buffer;
//! Logging type
CLog::TLogType type;
bool needSpace;
bool enableOutput;
//! Reference count
quint32 reference;
} *logStream;
public:
CLogMessage( CLog::TLogType type );
CLogMessage(const CLogMessage &other);
~CLogMessage();
inline CLogMessage &maybeSpace() { if (logStream->needSpace) logStream->output << ' '; return *this; }
inline CLogMessage &operator<<(QChar t) { logStream->output << '\'' << t << '\''; return maybeSpace(); }
inline CLogMessage &operator<<(QBool t) { logStream->output << (bool(t != 0) ? "true" : "false"); return maybeSpace(); }
inline CLogMessage &operator<<(bool t) { logStream->output << (t ? "true" : "false"); return maybeSpace(); }
inline CLogMessage &operator<<(char t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(signed short t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(unsigned short t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(signed int t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(unsigned int t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(signed long t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(unsigned long t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(qint64 t)
{ logStream->output << QString::number(t); return maybeSpace(); }
inline CLogMessage &operator<<(quint64 t)
{ logStream->output << QString::number(t); return maybeSpace(); }
inline CLogMessage &operator<<(float t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(double t) { logStream->output << t; return maybeSpace(); }
inline CLogMessage &operator<<(const char* t) { logStream->output << QString::fromAscii(t); return maybeSpace(); }
inline CLogMessage &operator<<(const QString & t) { logStream->output << '\"' << t << '\"'; return maybeSpace(); }
inline CLogMessage &operator<<(const QByteArray & t) { logStream->output << '\"' << t << '\"'; return maybeSpace(); }
static CLogMessage getWarningMsgObj() { return CLogMessage(CLog::WARNING); }
static CLogMessage getInfoMsgObj() { return CLogMessage(CLog::INFO); }
static CLogMessage getDebugMsgObj() { return CLogMessage(CLog::DEBUG); }
static CLogMessage getErrorMsgObj() { return CLogMessage(CLog::ERROR); }
static CLogMessage getAssertMsgObj() { return CLogMessage(CLog::ASSERT); }
inline CLogMessage &operator<<(QTextStreamManipulator m)
{ logStream->output << m; return *this; }
};
} // BlackMisc
#endif LOGMESSAGE_H// LOGMESSAGE_H

View File

@@ -0,0 +1,73 @@
//! 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(QString("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

View File

@@ -0,0 +1,59 @@
//! 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_DISPATCHER_H
#define MESSAGE_DISPATCHER_H
#include <QQueue>
#include <QMultiMap>
#include "blackmisc/message.h"
#include "blackmisc/context.h"
#include "blackmisc/type_info.h"
namespace BlackMisc
{
class CMessageHandler;
class CMessageDispatcher
{
// 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*, CTypeInfo);
private:
typedef QQueue<IMessage*> TMessageQueue;
TMessageQueue m_messageQueue;
typedef QMultiMap<CTypeInfo, CMessageHandler*> TMessageHandlerMap;
TMessageHandlerMap m_messageHander;
};
template <class T>
void CMessageDispatcher::registerClass( T* object, CTypeInfo typeinfo)
{
m_messageHander.insert(typeinfo, object);
}
} // namespace BlackMisc
#endif // MESSAGE_DISPATCHER_H

View File

@@ -0,0 +1,59 @@
//! 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_FACTORY_H
#define 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;
};
template <class T>
class MessageCreatorImpl : public IMessageCreator
{
public:
MessageCreatorImpl(const QString& messageID) : IMessageCreator(messageID) {}
virtual IMessage* create() { return new T; }
};
class CMessageFactory
{
// 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 // MESSAGE_FACTORY_H

View File

@@ -0,0 +1,75 @@
//! 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 <QMap>
#include "blackmisc/message.h"
#include "blackmisc/debug.h"
#include "blackmisc/type_info.h"
#include "blackmisc/message_dispatcher.h"
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

View File

@@ -0,0 +1,26 @@
//! 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

View File

@@ -0,0 +1,21 @@
//! 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
class QDataStream;
class ISerialize
{
public:
ISerialize();
virtual ~ISerialize() {};
virtual QDataStream& operator<< (QDataStream& in) = 0;
virtual QDataStream& operator>> (QDataStream& out) const = 0;
};
#endif // SERIALIZE_H

View File

@@ -0,0 +1,27 @@
//! 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.h>
namespace BlackMisc
{
class CTypeInfo
{
public:
explicit CTypeInfo(const type_info& info);
bool operator < (const CTypeInfo& rhs) const;
private:
const type_info& m_typeinfo;
};
} // namespace BlackMisc
#endif // TYPE_INFO_H