This repository has been archived by the owner on May 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fldigi.cpp
135 lines (109 loc) · 3.33 KB
/
fldigi.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <QTcpSocket>
#include <QtXml>
#include <QtDebug>
#include "fldigi.h"
#include "logformat/adiformat.h"
Fldigi::Fldigi(QObject *parent) :
QTcpServer(parent)
{
listen(QHostAddress::Any, 8421);
}
void Fldigi::incomingConnection(int socket) {
QTcpSocket* sock = new QTcpSocket(this);
connect(sock, SIGNAL(readyRead()), this, SLOT(readClient()));
connect(sock, SIGNAL(disconnected()), this, SLOT(discardClient()));
sock->setSocketDescriptor(socket);
}
void Fldigi::readClient() {
QTcpSocket* sock = (QTcpSocket*)sender();
QString data = QString(sock->readAll());
int split = data.indexOf("\r\n\r\n", 0);
data.remove(0, split+4);
QXmlStreamReader xml(data);
processMethodCall(sock, xml);
sock->close();
if (sock->state() == QTcpSocket::UnconnectedState) {
delete sock;
}
}
void Fldigi::discardClient() {
QTcpSocket* sock = (QTcpSocket*)sender();
sock->deleteLater();
}
void Fldigi::processMethodCall(QTcpSocket* sock, QXmlStreamReader& xml) {
QByteArray response;
while (!xml.atEnd() && !xml.hasError()) {
xml.readNextStartElement();
if (xml.name() == "methodCall") {
qDebug() << "method call";
}
if (xml.name() == "methodName") {
QString method = xml.readElementText();
if (method == "log.add_record") {
QString param = parseParam(xml);
if (!param.isEmpty()) {
response = addRecord(param);
}
}
else if (method == "system.listMethods") {
response = listMethods();
}
}
}
QTextStream out(sock);
if (!response.isEmpty()) {
out << "HTTP/1.1 200 OK\r\n";
out << "Content-Type: text/xml; charset=utf-8\r\n";
out << "Content-Length: " << response.length() << "\r\n";
out << "\r\n";
out << response;
}
else {
out << "HTTP/1.1 400 Internal Error\r\n";
}
}
QString Fldigi::parseParam(QXmlStreamReader& xml) {
while (!xml.atEnd() && !xml.hasError()) {
xml.readNextStartElement();
if (xml.name() == "value") {
return xml.readElementText();
}
}
return QString();
}
QByteArray Fldigi::listMethods() {
QByteArray out;
QXmlStreamWriter xml(&out);
xml.writeStartDocument();
xml.writeStartElement("methodResponse");
xml.writeStartElement("params");
xml.writeStartElement("param");
xml.writeStartElement("value");
xml.writeStartElement("array");
xml.writeStartElement("data");
xml.writeTextElement("value", "log.add_record");
xml.writeTextElement("value", "system.listMethods");
xml.writeEndDocument();
return out;
}
QByteArray Fldigi::addRecord(QString data) {
QByteArray out;
QSettings settings;
QXmlStreamWriter xml(&out);
xml.writeStartDocument();
xml.writeStartElement("methodResponse");
xml.writeStartElement("params");
xml.writeStartElement("param");
xml.writeStartElement("value");
xml.writeEndDocument();
QMap<QString, QString> defaults;
defaults["my_grid"] = settings.value("operator/grid").toString();
QTextStream in(&data);
AdiFormat adif(in);
adif.setDefaults(defaults);
int count = adif.runImport();
if (count) {
emit contactAdded();
}
return out;
}