-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.cc
68 lines (57 loc) · 1.43 KB
/
App.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "App.h"
#include <QTimer>
#include <QStringList>
#include <QCoreApplication>
#include <QtDebug>
#include <QDateTime>
#include "ZmqSocket.h"
#include "ZmqMessage.h"
App::App()
{
QTimer::singleShot(0, this, SLOT(start()));
}
void App::start()
{
QStringList args = QCoreApplication::arguments();
if(args.size() < 3) {
qWarning() << "Usage: " << args.front() << " client|server <key>";
QCoreApplication::exit();
return;
}
key = args.at(2).toLocal8Bit();
QTimer *ndyt = new QTimer(this);
connect(ndyt, SIGNAL(timeout()), this, SLOT(ndy()));
ndyt->setInterval(5000);
ndyt->start();
if(args.at(1) == "client") {
sock = new ZmqSocket(ZMQ_SUB, this);
connect(sock, SIGNAL(readyRead()), this, SLOT(heard()));
sock->subscribe(key);
sock->connectTo("tcp://127.0.0.1:3782");
}
if(args.at(1) == "server") {
sock = new ZmqSocket(ZMQ_PUB, this);
sock->bind("tcp://127.0.0.1:3782");
QTimer *t = new QTimer(this);
connect(t, SIGNAL(timeout()), this, SLOT(squawk()));
t->setInterval(1000);
t->start();
}
}
void App::squawk()
{
QString s = QDateTime::currentDateTime().toString();
qDebug() << "Squawking " << s;
sock->send(key + s.toLocal8Bit());
}
void App::heard()
{
QList<QByteArray> r = sock->recv();
for(QList<QByteArray>::const_iterator i=r.constBegin(); i!=r.constEnd(); ++i) {
qDebug() << "Received " << *i;
}
}
void App::ndy()
{
qDebug() << "Not dead yet";
}