forked from ciphrex/mSIGNA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandserver.cpp
147 lines (123 loc) · 3.23 KB
/
commandserver.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
136
137
138
139
140
141
142
143
144
145
146
147
///////////////////////////////////////////////////////////////////////////////
//
// mSIGNA
//
// commandserver.cpp
//
// Copyright (c) 2013 Eric Lombrozo
//
// All Rights Reserved.
#include "commandserver.h"
#include <QLocalSocket>
#include <QLocalServer>
#include <QDataStream>
#include <QUrl>
#include <QFile>
#include "settings.h"
#include "severitylogger.h"
const int COMMAND_SERVER_TIMEOUT = 500;
const QString COMMAND_SERVER_NAME("VaultCommandServer");
const QString COMMAND_SERVER_URL_PREFIX("bitcoin:");
CommandServer::CommandServer(QObject* parent)
: QObject(parent), server(NULL)
{
}
CommandServer::~CommandServer()
{
stop();
}
bool CommandServer::start()
{
if (server) stop();
server = new QLocalServer(this);
if (!server) return false;
server->removeServer(COMMAND_SERVER_NAME);
if (!server->listen(COMMAND_SERVER_NAME)) {
server->close();
delete server;
server = NULL;
return false;
}
connect(server, SIGNAL(newConnection()), this, SLOT(handleConnection()));
return true;
}
bool CommandServer::stop()
{
if (!server) return false;
server->close();
delete server;
server = NULL;
return true;
}
bool CommandServer::processArgs(int argc, char* argv[])
{
QLocalSocket socket;
socket.connectToServer(COMMAND_SERVER_NAME, QIODevice::WriteOnly);
if (!socket.waitForConnected(COMMAND_SERVER_TIMEOUT)) {
// This is the only instance running
if (argc > 1) { command = QString(argv[1]); }
for (int i = 2; i < argc; i++) { args.push_back(QString(argv[i])); }
return false;
}
if (argc > 1) {
// Send command and args to main instance
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
// Send each arg
for (int i = 1; i < argc; i++) {
QString arg(argv[i]);
if (arg.isEmpty()) continue; // Not sure this is necessary, but it can't hurt.
out << arg;
}
socket.write(block);
if (!socket.waitForBytesWritten(COMMAND_SERVER_TIMEOUT)) {
LOGGER(error) << "CommandServer::processArgs - " + socket.errorString().toStdString() << std::endl;
}
}
socket.disconnectFromServer();
return true;
}
void CommandServer::uiReady()
{
emitCommand(command, args);
/*
for (auto& arg: args) {
processArg(arg);
}
*/
}
void CommandServer::handleConnection()
{
QLocalSocket* client = server->nextPendingConnection();
QDataStream in(client);
bool bReadArgs = false;
QString command;
std::vector<QString> args;
while (client->waitForReadyRead(COMMAND_SERVER_TIMEOUT))
{
if (bReadArgs)
{
QString arg;
in >> arg;
args.push_back(arg);
}
else
{
in >> command;
bReadArgs = true;
}
}
emitCommand(command, args);
}
void CommandServer::emitCommand(const QString& command, const std::vector<QString>& args)
{
if (command.startsWith(COMMAND_SERVER_URL_PREFIX, Qt::CaseInsensitive)) {
emit gotUrl(QUrl(command));
}
else if (QFile::exists(command)) {
emit gotFile(command);
}
else {
emit gotCommand(command, args);
}
}