Thanks amieto!
What's happening now for me, is the SIGNAL from Analyzer appears to be emitted, but the SLOT in Logger never gets run.
#include <QtCore/QCoreApplication>
#include <QThread>
#include "logger.h"
#include "pollcontroller.h"
#include "analyzer.h"
int main(int argc, char *argv[])
{
int port = 85;
PollController *pollController = new PollController();
pollController->setup(1,1000);
Analyzer *analyzer = pollController->getAnalyzer();
pollController->moveToThread(&pollThread);
Logger *logger = new Logger();
logger->setup(host,port,analyzer);
logger->moveToThread(&loggerThread);
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QThread>
#include "logger.h"
#include "pollcontroller.h"
#include "analyzer.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString host = "web-dev";
int port = 85;
PollController *pollController = new PollController();
pollController->setup(1,1000);
Analyzer *analyzer = pollController->getAnalyzer();
QThread pollThread;
pollController->moveToThread(&pollThread);
Logger *logger = new Logger();
logger->setup(host,port,analyzer);
QThread loggerThread;
logger->moveToThread(&loggerThread);
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#ifndef POLLCONTROLLER_H
#define POLLCONTROLLER_H
#include <QObject>
#include <QThread>
#include "analyzer.h"
#include "sleep.h"
class PollController
: public QObject{
Q_OBJECT
public:
explicit PollController
(QObject *parent
= 0);
void setup(int,int);
Analyzer*& getAnalyzer();
signals:
public slots:
private:
void loopNumbers(int,int);
Analyzer *analyzer;
};
#endif // POLLCONTROLLER_H
#ifndef POLLCONTROLLER_H
#define POLLCONTROLLER_H
#include <QObject>
#include <QThread>
#include "analyzer.h"
#include "sleep.h"
class PollController : public QObject
{
Q_OBJECT
public:
explicit PollController(QObject *parent = 0);
void setup(int,int);
Analyzer*& getAnalyzer();
signals:
public slots:
private:
void loopNumbers(int,int);
Analyzer *analyzer;
};
#endif // POLLCONTROLLER_H
To copy to clipboard, switch view to plain text mode
#include "pollcontroller.h"
PollController
::PollController(QObject *parent
) :{
}
void PollController::setup(int begin, int end) {
analyzer = new Analyzer(this);
loopNumbers(begin,end);
}
void PollController::loopNumbers(int begin, int end) {
Sleep sleep;
QString skipMsg
= "99.00.121.01.03939021";
// Generate some data to check
for (int x=begin; x<end; x++) {
qDebug() << "poll received:" << x;
if (x % 4 == 0) {
analyzer->analyze(logMsg);
} else if (x % 2 == 0) {
analyzer->analyze(skipMsg);
}
sleep.msleep(1000);
}
}
Analyzer*& PollController::getAnalyzer() {
return analyzer;
}
#include "pollcontroller.h"
PollController::PollController(QObject *parent) :
QObject(parent)
{
}
void PollController::setup(int begin, int end) {
analyzer = new Analyzer(this);
loopNumbers(begin,end);
}
void PollController::loopNumbers(int begin, int end) {
Sleep sleep;
QString logMsg = "99.00.050.01.*";
QString skipMsg = "99.00.121.01.03939021";
// Generate some data to check
for (int x=begin; x<end; x++) {
qDebug() << "poll received:" << x;
if (x % 4 == 0) {
analyzer->analyze(logMsg);
} else if (x % 2 == 0) {
analyzer->analyze(skipMsg);
}
sleep.msleep(1000);
}
}
Analyzer*& PollController::getAnalyzer() {
return analyzer;
}
To copy to clipboard, switch view to plain text mode
#ifndef ANALYZER_H
#define ANALYZER_H
#include <QObject>
#include <QDebug>
{
Q_OBJECT
public:
explicit Analyzer
(QObject *parent
= 0);
void analyze(QString&);
signals:
void msgToLog(QString&);
public slots:
};
#endif // ANALYZER_H
#ifndef ANALYZER_H
#define ANALYZER_H
#include <QObject>
#include <QDebug>
class Analyzer : public QObject
{
Q_OBJECT
public:
explicit Analyzer(QObject *parent = 0);
void analyze(QString&);
signals:
void msgToLog(QString&);
public slots:
};
#endif // ANALYZER_H
To copy to clipboard, switch view to plain text mode
#include "analyzer.h"
#include <QStringList>
Analyzer
::Analyzer(QObject *parent
) :{
}
void Analyzer
::analyze(QString &msg
) {
if (msg.contains(".")) {
if (fields.at(2) == "050") {
emit(msgToLog(msg));
qDebug() << "emit(msgToLog(msg))";
return;
}
qDebug() << "msg" << msg << "didn't contain 050 command";
}
}
#include "analyzer.h"
#include <QStringList>
Analyzer::Analyzer(QObject *parent) :
QObject(parent)
{
}
void Analyzer::analyze(QString &msg)
{
if (msg.contains(".")) {
QStringList fields = msg.split(".");
if (fields.at(2) == "050") {
emit(msgToLog(msg));
qDebug() << "emit(msgToLog(msg))";
return;
}
qDebug() << "msg" << msg << "didn't contain 050 command";
}
}
To copy to clipboard, switch view to plain text mode
#ifndef LOGGER_H
#define LOGGER_H
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QDebug>
#include "analyzer.h"
{
Q_OBJECT
public:
explicit Logger
(QObject *parent
= 0);
void setup(QString&,int&,Analyzer*&);
signals:
public slots:
void connected();
void disconnected();
void bytesWritten(qint64);
void readyRead();
private:
int port;
};
#endif // LOGGER_H
#ifndef LOGGER_H
#define LOGGER_H
#include <QObject>
#include <QTcpSocket>
#include <QAbstractSocket>
#include <QDebug>
#include "analyzer.h"
class Logger : public QObject
{
Q_OBJECT
public:
explicit Logger(QObject *parent = 0);
void setup(QString&,int&,Analyzer*&);
signals:
public slots:
void connected();
void disconnected();
void bytesWritten(qint64);
void readyRead();
void write(QString &msg);
private:
QTcpSocket *socket;
QString host;
int port;
};
#endif // LOGGER_H
To copy to clipboard, switch view to plain text mode
#include "logger.h"
{
}
void Logger
::setup(QString &host,
int &port, Analyzer
*&analyzer
) {
connect(analyzer,SIGNAL(msgToLog(QString&)),this,SLOT(write(QString&)),Qt::AutoConnection);
connect(socket,SIGNAL(connected()),this,SLOT(connected()));
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWritten(qint64)));
this->host = host;
this->port = port;
}
void Logger::connected()
{
qDebug() << "Connected";
}
void Logger::disconnected()
{
qDebug() << "Disconnected";
}
void Logger::bytesWritten(qint64)
{
}
void Logger::readyRead()
{
qDebug() << "socket readAll:" << socket->readAll();
}
{
qDebug() << "write msg:" << msg;
socket->connectToHost(host,port);
if (!socket->waitForConnected(500)) {
qDebug() << "Error:" << socket->errorString();
return;
}
serverMsg.append("GET /log/test/msg/");
serverMsg.append(msg);
serverMsg.append("/time/1234567859 HTTP/1.0\r\n\r\n");
socket->write(serverMsg);
}
#include "logger.h"
Logger::Logger(QObject *parent) :
QObject(parent)
{
}
void Logger::setup(QString &host, int &port, Analyzer *&analyzer)
{
connect(analyzer,SIGNAL(msgToLog(QString&)),this,SLOT(write(QString&)),Qt::AutoConnection);
socket = new QTcpSocket(this->parent());
connect(socket,SIGNAL(connected()),this,SLOT(connected()));
connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
connect(socket,SIGNAL(bytesWritten(qint64)),this,SLOT(bytesWritten(qint64)));
this->host = host;
this->port = port;
}
void Logger::connected()
{
qDebug() << "Connected";
}
void Logger::disconnected()
{
qDebug() << "Disconnected";
}
void Logger::bytesWritten(qint64)
{
}
void Logger::readyRead()
{
qDebug() << "socket readAll:" << socket->readAll();
}
void Logger::write(QString &msg)
{
qDebug() << "write msg:" << msg;
socket->connectToHost(host,port);
if (!socket->waitForConnected(500)) {
qDebug() << "Error:" << socket->errorString();
return;
}
QByteArray serverMsg;
serverMsg.append("GET /log/test/msg/");
serverMsg.append(msg);
serverMsg.append("/time/1234567859 HTTP/1.0\r\n\r\n");
socket->write(serverMsg);
}
To copy to clipboard, switch view to plain text mode
#ifndef SLEEP_H
#define SLEEP_H
#include <QWaitCondition>
#include <QMutex>
class Sleep
{
public:
Sleep();
void msleep(unsigned long msecs);
};
#endif // SLEEP_H
#ifndef SLEEP_H
#define SLEEP_H
#include <QWaitCondition>
#include <QMutex>
class Sleep
{
public:
Sleep();
void msleep(unsigned long msecs);
};
#endif // SLEEP_H
To copy to clipboard, switch view to plain text mode
#include "sleep.h"
Sleep::Sleep()
{
}
void Sleep::msleep(unsigned long msecs) {
mutex.lock();
waitCondition.wait(&mutex,msecs);
mutex.unlock();
}
#include "sleep.h"
Sleep::Sleep()
{
}
void Sleep::msleep(unsigned long msecs) {
QMutex mutex;
mutex.lock();
QWaitCondition waitCondition;
waitCondition.wait(&mutex,msecs);
mutex.unlock();
}
To copy to clipboard, switch view to plain text mode
Bookmarks