164
|
1 |
#include "hwmap.h"
|
|
2 |
|
|
3 |
#include "hwconsts.h"
|
|
4 |
|
|
5 |
#include <QMessageBox>
|
|
6 |
|
168
|
7 |
#include <QMutex>
|
|
8 |
#include <QDebug>
|
|
9 |
|
|
10 |
#include <QList>
|
|
11 |
|
|
12 |
int HWMap::isBusy(0); // initialize static variable
|
|
13 |
QList<HWMap*> srvsList;
|
|
14 |
QMutex tcpSrvMut;
|
|
15 |
|
164
|
16 |
HWMap::HWMap() :
|
|
17 |
m_isStarted(false)
|
|
18 |
{
|
|
19 |
}
|
|
20 |
|
168
|
21 |
HWMap::~HWMap()
|
|
22 |
{
|
|
23 |
}
|
|
24 |
|
164
|
25 |
void HWMap::getImage(std::string seed)
|
|
26 |
{
|
|
27 |
m_seed=seed;
|
|
28 |
Start();
|
|
29 |
}
|
|
30 |
|
|
31 |
void HWMap::ClientDisconnect()
|
|
32 |
{
|
|
33 |
QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono);
|
|
34 |
im.setNumColors(2);
|
|
35 |
|
|
36 |
IPCSocket->close();
|
|
37 |
IPCSocket->deleteLater();
|
|
38 |
IPCSocket = 0;
|
|
39 |
IPCServer->close();
|
168
|
40 |
//deleteLater();
|
|
41 |
|
|
42 |
|
|
43 |
tcpSrvMut.lock();
|
|
44 |
if(isBusy) --isBusy;
|
|
45 |
//if(!isBusy) srvsList.pop_front();//lastStarted=0;
|
|
46 |
tcpSrvMut.unlock();
|
|
47 |
qDebug() << "image emitted with seed " << QString(m_seed.c_str());
|
164
|
48 |
emit ImageReceived(im);
|
168
|
49 |
readbuffer.clear();
|
|
50 |
emit isReadyNow();
|
164
|
51 |
}
|
|
52 |
|
|
53 |
void HWMap::ClientRead()
|
|
54 |
{
|
|
55 |
readbuffer.append(IPCSocket->readAll());
|
|
56 |
}
|
|
57 |
|
|
58 |
void HWMap::SendToClientFirst()
|
|
59 |
{
|
|
60 |
std::string toSend=std::string("eseed ")+m_seed;
|
|
61 |
char ln=(char)toSend.length();
|
|
62 |
IPCSocket->write(&ln, 1);
|
|
63 |
IPCSocket->write(toSend.c_str(), ln);
|
|
64 |
|
|
65 |
IPCSocket->write("\x01!", 2);
|
|
66 |
}
|
|
67 |
|
|
68 |
void HWMap::NewConnection()
|
|
69 |
{
|
|
70 |
QTcpSocket * client = IPCServer->nextPendingConnection();
|
|
71 |
if(!IPCSocket) {
|
|
72 |
IPCServer->close();
|
|
73 |
IPCSocket = client;
|
|
74 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
75 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
76 |
SendToClientFirst();
|
|
77 |
} else {
|
|
78 |
qWarning("2nd IPC client?!");
|
|
79 |
client->disconnectFromHost();
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
void HWMap::StartProcessError(QProcess::ProcessError error)
|
|
84 |
{
|
|
85 |
QMessageBox::critical(0, tr("Error"),
|
|
86 |
tr("Unable to run engine: %1 (")
|
|
87 |
.arg(error) + bindir->absolutePath() + "/hwengine)");
|
|
88 |
}
|
|
89 |
|
168
|
90 |
void HWMap::tcpServerReady()
|
|
91 |
{
|
|
92 |
qDebug() << "received signal, i am " << this << ";";
|
|
93 |
qDebug() << srvsList.front() << " disconnected from " << *(++srvsList.begin());
|
|
94 |
tcpSrvMut.lock();
|
|
95 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady()));
|
|
96 |
srvsList.pop_front();
|
|
97 |
tcpSrvMut.unlock();
|
|
98 |
|
|
99 |
RealStart();
|
|
100 |
}
|
|
101 |
|
164
|
102 |
void HWMap::Start()
|
|
103 |
{
|
168
|
104 |
tcpSrvMut.lock();
|
|
105 |
if(!isBusy) {
|
|
106 |
qDebug() << "notBusy, i am " << this;
|
|
107 |
++isBusy;
|
|
108 |
srvsList.push_back(this);
|
|
109 |
tcpSrvMut.unlock();
|
|
110 |
} else {
|
|
111 |
qDebug() << "Busy, connected " << srvsList.back() << " to " << this;
|
|
112 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady()));
|
|
113 |
srvsList.push_back(this);
|
|
114 |
//deleteLater();
|
|
115 |
tcpSrvMut.unlock();
|
|
116 |
return;
|
|
117 |
}
|
|
118 |
|
|
119 |
RealStart();
|
|
120 |
}
|
|
121 |
|
|
122 |
void HWMap::RealStart()
|
|
123 |
{
|
164
|
124 |
IPCServer = new QTcpServer(this);
|
|
125 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
|
|
126 |
IPCServer->setMaxPendingConnections(1);
|
|
127 |
IPCSocket = 0;
|
|
128 |
if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) {
|
|
129 |
QMessageBox::critical(0, tr("Error"),
|
|
130 |
tr("Unable to start the server: %1.")
|
|
131 |
.arg(IPCServer->errorString()));
|
|
132 |
}
|
|
133 |
|
|
134 |
QProcess * process;
|
|
135 |
QStringList arguments;
|
|
136 |
process = new QProcess;
|
|
137 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError)));
|
|
138 |
arguments << "46631";
|
|
139 |
arguments << "landpreview";
|
|
140 |
process->start(bindir->absolutePath() + "/hwengine", arguments);
|
|
141 |
}
|