Skip to content

Commit c25f58a

Browse files
Qt: OpenIGTLink: handle client connection
No disconnect detection yet, though. It seems as if disconnects can only be detected by reading from the client and having that fail?
1 parent f71023f commit c25f58a

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
#include "openigtlink.h"
22

3+
SolumIGTL::SolumIGTL()
4+
{
5+
QObject::connect(&clientConnectTimer_, &QTimer::timeout, [this]()
6+
{
7+
client_ = server_->WaitForConnection(1); // 0 would wait forever.
8+
if (client_.IsNotNull())
9+
{
10+
emit clientConnected(true);
11+
clientConnectTimer_.stop();
12+
}
13+
});
14+
}
15+
316
bool SolumIGTL::serve(uint16_t port)
417
{
518
server_ = igtl::ServerSocket::New();
619
if (server_->CreateServer(port) < 0)
720
return false;
21+
reconnectClient();
822
return true;
923
}
1024

1125
void SolumIGTL::close()
1226
{
27+
disconnectClient();
1328
server_->CloseSocket();
1429
}
1530

31+
void SolumIGTL::disconnectClient()
32+
{
33+
clientConnectTimer_.stop();
34+
if (client_)
35+
client_->CloseSocket();
36+
emit clientConnected(false);
37+
}
38+
39+
void SolumIGTL::reconnectClient()
40+
{
41+
disconnectClient();
42+
clientConnectTimer_.start(100);
43+
}
44+
1645
bool SolumIGTL::isServing() const
1746
{
1847
return (server_ && server_->GetConnected());
1948
}
49+
50+
bool SolumIGTL::isClientConnected() const
51+
{
52+
return (client_ && client_->GetConnected());
53+
}

examples/solum_qt/solum/openigtlink.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@ class SolumIGTL : public QObject
88
Q_OBJECT;
99

1010
public:
11+
SolumIGTL();
12+
1113
bool serve(uint16_t port); /// Returns `false` on failure.
1214
void close();
1315

1416
bool isServing() const;
17+
bool isClientConnected() const;
18+
19+
signals:
20+
void clientConnected(bool);
1521

1622
private:
23+
void disconnectClient();
24+
void reconnectClient();
25+
1726
igtl::ServerSocket::Pointer server_;
27+
igtl::Socket::Pointer client_;
28+
QTimer clientConnectTimer_;
1829
};

examples/solum_qt/solum/solumqt.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ Solum::Solum(QWidget *parent) : QMainWindow(parent), imaging_(false)
301301
}
302302
});
303303

304+
connect(&igtl_, &SolumIGTL::clientConnected, [this](bool connected)
305+
{
306+
if(connected)
307+
ui_.igtlStatusClient->setText(tr("🟢 Client connected"));
308+
else
309+
ui_.igtlStatusClient->setText(tr("⌛ Waiting for client connection…"));
310+
});
311+
304312
imagingState(ImagingNotReady, false);
305313

306314
// Automatically trigger a BLE search at startup
@@ -1103,6 +1111,7 @@ void Solum::onIGTLServe()
11031111
{
11041112
igtl_.close();
11051113
ui_.igtlStatusServer->setText(tr("🔴 Server is stopped"));
1114+
ui_.igtlStatusClient->clear();
11061115
ui_.igtlport->setEnabled(true);
11071116
ui_.igtlserve->setText(tr("Serve"));
11081117
}

0 commit comments

Comments
 (0)