author | displacer |
Tue, 26 Sep 2006 19:45:47 +0000 | |
changeset 169 | a78d4a552500 |
parent 168 | 679e9b8912f5 |
child 170 | ef1031ba44fc |
permissions | -rw-r--r-- |
164 | 1 |
#include "hwmap.h" |
2 |
||
3 |
#include "hwconsts.h" |
|
4 |
||
5 |
#include <QMessageBox> |
|
6 |
||
168 | 7 |
#include <QMutex> |
8 |
||
9 |
#include <QList> |
|
10 |
||
11 |
int HWMap::isBusy(0); // initialize static variable |
|
12 |
QList<HWMap*> srvsList; |
|
13 |
QMutex tcpSrvMut; |
|
14 |
||
164 | 15 |
HWMap::HWMap() : |
16 |
m_isStarted(false) |
|
17 |
{ |
|
169
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
18 |
IPCServer = new QTcpServer(this); |
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
19 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection())); |
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
20 |
IPCServer->setMaxPendingConnections(1); |
164 | 21 |
} |
22 |
||
168 | 23 |
HWMap::~HWMap() |
24 |
{ |
|
25 |
} |
|
26 |
||
164 | 27 |
void HWMap::getImage(std::string seed) |
28 |
{ |
|
29 |
m_seed=seed; |
|
30 |
Start(); |
|
31 |
} |
|
32 |
||
33 |
void HWMap::ClientDisconnect() |
|
34 |
{ |
|
35 |
QImage im((uchar*)(const char*)readbuffer, 256, 128, QImage::Format_Mono); |
|
36 |
im.setNumColors(2); |
|
37 |
||
38 |
IPCSocket->close(); |
|
169
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
39 |
//IPCSocket->deleteLater(); |
a78d4a552500
new more working, but still not completely working version :)
displacer
parents:
168
diff
changeset
|
40 |
//IPCSocket = 0; |
164 | 41 |
IPCServer->close(); |
168 | 42 |
//deleteLater(); |
43 |
||
44 |
||
45 |
tcpSrvMut.lock(); |
|
46 |
if(isBusy) --isBusy; |
|
47 |
tcpSrvMut.unlock(); |
|
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 |
tcpSrvMut.lock(); |
|
93 |
disconnect(srvsList.front(), SIGNAL(isReadyNow()), *(++srvsList.begin()), SLOT(tcpServerReady())); |
|
94 |
srvsList.pop_front(); |
|
95 |
tcpSrvMut.unlock(); |
|
96 |
||
97 |
RealStart(); |
|
98 |
} |
|
99 |
||
164 | 100 |
void HWMap::Start() |
101 |
{ |
|
168 | 102 |
tcpSrvMut.lock(); |
103 |
if(!isBusy) { |
|
104 |
++isBusy; |
|
105 |
srvsList.push_back(this); |
|
106 |
tcpSrvMut.unlock(); |
|
107 |
} else { |
|
108 |
connect(srvsList.back(), SIGNAL(isReadyNow()), this, SLOT(tcpServerReady())); |
|
109 |
srvsList.push_back(this); |
|
110 |
//deleteLater(); |
|
111 |
tcpSrvMut.unlock(); |
|
112 |
return; |
|
113 |
} |
|
114 |
||
115 |
RealStart(); |
|
116 |
} |
|
117 |
||
118 |
void HWMap::RealStart() |
|
119 |
{ |
|
164 | 120 |
IPCSocket = 0; |
121 |
if (!IPCServer->listen(QHostAddress::LocalHost, IPC_PORT)) { |
|
122 |
QMessageBox::critical(0, tr("Error"), |
|
123 |
tr("Unable to start the server: %1.") |
|
124 |
.arg(IPCServer->errorString())); |
|
125 |
} |
|
126 |
||
127 |
QProcess * process; |
|
128 |
QStringList arguments; |
|
129 |
process = new QProcess; |
|
130 |
connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(StartProcessError(QProcess::ProcessError))); |
|
131 |
arguments << "46631"; |
|
132 |
arguments << "landpreview"; |
|
133 |
process->start(bindir->absolutePath() + "/hwengine", arguments); |
|
134 |
} |