35 #include "igtlOSUtil.h"
36 #include "igtlMessageHeader.h"
37 #include "igtlTransformMessage.h"
38 #include "igtlPositionMessage.h"
39 #include "igtlImageMessage.h"
40 #include "igtlClientSocket.h"
41 #include "igtlStatusMessage.h"
54 mHeadingReceived(false),
81 mSocket.reset(
new QTcpSocket());
82 connect(mSocket.get(), SIGNAL(readyRead()),
this, SLOT(readyReadSlot()), Qt::DirectConnection);
83 connect(mSocket.get(), SIGNAL(hostFound()),
this, SLOT(hostFoundSlot()), Qt::DirectConnection);
84 connect(mSocket.get(), SIGNAL(connected()),
this, SLOT(connectedSlot()), Qt::DirectConnection);
85 connect(mSocket.get(), SIGNAL(disconnected()),
this, SLOT(disconnectedSlot()), Qt::DirectConnection);
86 connect(mSocket.get(), SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(errorSlot(QAbstractSocket::SocketError)),
87 Qt::DirectConnection);
89 if (!this->multipleTryConnectToHost())
91 reportError(
"IGTLinkClientStreamer: Failed to start streaming");
97 mHeaderMsg = igtl::MessageHeader::New();
100 bool IGTLinkClientStreamer::multipleTryConnectToHost()
103 int numberOfConnectionAttempts = 5;
105 for (
int i=0; i<numberOfConnectionAttempts; ++i)
108 report(QString(
"[%2] Attempt %1 to connect to host").arg(i+1).arg(this->hostDescription()));
109 if (this->tryConnectToHost())
113 reportError(QString(
"[%1] Timeout connecting to host").arg(this->hostDescription()));
117 bool IGTLinkClientStreamer::tryConnectToHost()
119 mSocket->connectToHost(mAddress, mPort);
122 if (!mSocket->waitForConnected(timeout))
124 mSocket->disconnectFromHost();
134 mSocket->disconnectFromHost();
142 return (mSocket && mSocket->isValid());
145 QString IGTLinkClientStreamer::hostDescription()
const
150 void IGTLinkClientStreamer::hostFoundSlot()
152 report(QString(
"[%1] Found host").arg(this->hostDescription()));
155 void IGTLinkClientStreamer::connectedSlot()
157 reportSuccess(QString(
"[%1] Connected to host").arg(this->hostDescription()));
159 void IGTLinkClientStreamer::disconnectedSlot()
161 report(QString(
"[%1] Disconnected from host").arg(this->hostDescription()));
164 void IGTLinkClientStreamer::errorSlot(QAbstractSocket::SocketError socketError)
166 report(QString(
"[%1] Socket error [code=%2]: %3")
167 .arg(this->hostDescription())
168 .arg(QString::number(socketError))
169 .arg(mSocket->errorString()));
172 void IGTLinkClientStreamer::readyReadSlot()
175 while (this->readOneMessage());
182 bool IGTLinkClientStreamer::readOneMessage()
187 if (!mHeadingReceived)
191 mHeaderMsg->InitPack();
194 if (mSocket->bytesAvailable() < mHeaderMsg->GetPackSize())
202 mSocket->read(reinterpret_cast<char*>(mHeaderMsg->GetPackPointer()), mHeaderMsg->GetPackSize());
203 mHeadingReceived =
true;
206 mHeaderMsg->Unpack();
209 if (mHeadingReceived)
212 bool success =
false;
222 if (QString(mHeaderMsg->GetDeviceType()) ==
"IMAGE")
224 success = this->ReceiveImage(mSocket.get(), mHeaderMsg);
226 else if (QString(mHeaderMsg->GetDeviceType()) ==
"CX_US_ST")
228 success = this->ReceiveSonixStatus(mSocket.get(), mHeaderMsg);
236 std::cerr <<
"Receiving : " << mHeaderMsg->GetDeviceType() << std::endl;
237 mSocket->read(mHeaderMsg->GetBodySizeToRead());
241 mHeadingReceived =
false;
249 bool IGTLinkClientStreamer::ReceiveSonixStatus(QTcpSocket* socket, igtl::MessageHeader::Pointer& header)
252 msg = IGTLinkUSStatusMessage::New();
253 msg->SetMessageHeader(header);
256 if (socket->bytesAvailable() < msg->GetPackBodySize())
261 socket->read(reinterpret_cast<char*>(msg->GetPackBodyPointer()), msg->GetPackBodySize());
265 int c = msg->Unpack();
266 if (c & (igtl::MessageHeader::UNPACK_BODY | igtl::MessageHeader::UNPACK_UNDEF))
268 this->addToQueue(msg);
273 std::cout <<
"body crc failed!" << std::endl;
277 bool IGTLinkClientStreamer::ReceiveImage(QTcpSocket* socket, igtl::MessageHeader::Pointer& header)
281 imgMsg = IGTLinkImageMessage::New();
282 imgMsg->SetMessageHeader(header);
283 imgMsg->AllocatePack();
287 if (socket->bytesAvailable() < imgMsg->GetPackBodySize())
293 socket->read(reinterpret_cast<char*>(imgMsg->GetPackBodyPointer()), imgMsg->GetPackBodySize());
297 int c = imgMsg->Unpack();
299 if (c & (igtl::MessageHeader::UNPACK_BODY | igtl::MessageHeader::UNPACK_UNDEF))
301 this->addToQueue(imgMsg);
305 std::cout <<
"body crc failed!" << std::endl;
312 mUnsentUSStatusMessage = msg;
317 IGTLinkConversion converter;
320 package->mIgtLinkImageMessage = msg;
323 if (mUnsentUSStatusMessage)
325 package->mIgtLinkUSStatusMessage = mUnsentUSStatusMessage;
329 mSender->send(package);
QString qstring_cast(const T &val)
virtual ~IGTLinkClientStreamer()
virtual void startStreaming(SenderPtr sender)
void reportError(QString msg)
void setAddress(QString address, int port)
virtual void stopStreaming()
virtual bool isStreaming()
igtl::SmartPointer< Self > Pointer
void reportSuccess(QString msg)
boost::shared_ptr< struct Package > PackagePtr
igtl::SmartPointer< Self > Pointer
boost::shared_ptr< Sender > SenderPtr