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"
52 #include "vtkImageData.h"
58 mHeadingReceived(false),
85 mSocket.reset(
new QTcpSocket());
86 connect(mSocket.get(), SIGNAL(readyRead()),
this, SLOT(readyReadSlot()), Qt::DirectConnection);
87 connect(mSocket.get(), SIGNAL(hostFound()),
this, SLOT(hostFoundSlot()), Qt::DirectConnection);
88 connect(mSocket.get(), SIGNAL(connected()),
this, SLOT(connectedSlot()), Qt::DirectConnection);
89 connect(mSocket.get(), SIGNAL(disconnected()),
this, SLOT(disconnectedSlot()), Qt::DirectConnection);
90 connect(mSocket.get(), SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(errorSlot(QAbstractSocket::SocketError)),
91 Qt::DirectConnection);
93 if (!this->multipleTryConnectToHost())
95 reportError(
"IGTLinkClientStreamer: Failed to start streaming");
101 mHeaderMsg = igtl::MessageHeader::New();
104 bool IGTLinkClientStreamer::multipleTryConnectToHost()
107 int numberOfConnectionAttempts = 5;
109 for (
int i=0; i<numberOfConnectionAttempts; ++i)
112 report(QString(
"[%2] Attempt %1 to connect to host").arg(i+1).arg(this->hostDescription()));
113 if (this->tryConnectToHost())
117 reportError(QString(
"[%1] Timeout connecting to host").arg(this->hostDescription()));
121 bool IGTLinkClientStreamer::tryConnectToHost()
123 mSocket->connectToHost(mAddress, mPort);
126 if (!mSocket->waitForConnected(timeout))
128 mSocket->disconnectFromHost();
138 mSocket->disconnectFromHost();
146 return (mSocket && mSocket->isValid());
149 QString IGTLinkClientStreamer::hostDescription()
const
154 void IGTLinkClientStreamer::hostFoundSlot()
156 report(QString(
"[%1] Found host").arg(this->hostDescription()));
159 void IGTLinkClientStreamer::connectedSlot()
161 reportSuccess(QString(
"[%1] Connected to host").arg(this->hostDescription()));
163 void IGTLinkClientStreamer::disconnectedSlot()
165 report(QString(
"[%1] Disconnected from host").arg(this->hostDescription()));
168 void IGTLinkClientStreamer::errorSlot(QAbstractSocket::SocketError socketError)
170 report(QString(
"[%1] Socket error [code=%2]: %3")
171 .arg(this->hostDescription())
172 .arg(QString::number(socketError))
173 .arg(mSocket->errorString()));
176 void IGTLinkClientStreamer::readyReadSlot()
179 while (this->readOneMessage());
186 bool IGTLinkClientStreamer::readOneMessage()
191 if (!mHeadingReceived)
195 mHeaderMsg->InitPack();
198 if (mSocket->bytesAvailable() < mHeaderMsg->GetPackSize())
206 mSocket->read(reinterpret_cast<char*>(mHeaderMsg->GetPackPointer()), mHeaderMsg->GetPackSize());
207 mHeadingReceived =
true;
210 mHeaderMsg->Unpack();
213 if (mHeadingReceived)
216 bool success =
false;
226 if (QString(mHeaderMsg->GetDeviceType()) ==
"IMAGE")
228 success = this->ReceiveImage(mSocket.get(), mHeaderMsg);
230 else if (QString(mHeaderMsg->GetDeviceType()) ==
"CX_US_ST")
232 success = this->ReceiveSonixStatus(mSocket.get(), mHeaderMsg);
240 std::cerr <<
"Receiving : " << mHeaderMsg->GetDeviceType() << std::endl;
241 mSocket->read(mHeaderMsg->GetBodySizeToRead());
245 mHeadingReceived =
false;
253 bool IGTLinkClientStreamer::ReceiveSonixStatus(QTcpSocket* socket, igtl::MessageHeader::Pointer& header)
256 msg = IGTLinkUSStatusMessage::New();
257 msg->SetMessageHeader(header);
260 if (socket->bytesAvailable() < msg->GetPackBodySize())
265 socket->read(reinterpret_cast<char*>(msg->GetPackBodyPointer()), msg->GetPackBodySize());
269 int c = msg->Unpack();
270 if (c & (igtl::MessageHeader::UNPACK_BODY | igtl::MessageHeader::UNPACK_UNDEF))
272 this->addToQueue(msg);
277 std::cout <<
"body crc failed!" << std::endl;
283 QDateTime my_decode_timestamp(igtl::MessageBase* msg)
286 igtl::TimeStamp::Pointer timestamp = igtl::TimeStamp::New();
287 msg->GetTimeStamp(timestamp);
288 double timestampMS = timestamp->GetTimeStamp() * 1000;
289 return QDateTime::fromMSecsSinceEpoch(timestampMS);
292 void write_time_info(igtl::ImageMessage::Pointer imgMsg)
294 int kb = imgMsg->GetPackSize()/1024;
296 QDateTime org_ts = my_decode_timestamp(imgMsg.GetPointer());
297 QDateTime now_ts = QDateTime::currentDateTime();
300 <<
", time=(" << org_ts.toString(format) <<
"->" << now_ts.toString(format) <<
")"
301 <<
", lag=" << org_ts.msecsTo(now_ts) <<
"ms";
305 bool IGTLinkClientStreamer::ReceiveImage(QTcpSocket* socket, igtl::MessageHeader::Pointer& header)
308 igtl::ImageMessage::Pointer imgMsg = igtl::ImageMessage::New();
309 imgMsg->SetMessageHeader(header);
310 imgMsg->AllocatePack();
314 if (socket->bytesAvailable() < imgMsg->GetPackBodySize())
320 socket->read(reinterpret_cast<char*>(imgMsg->GetPackBodyPointer()), imgMsg->GetPackBodySize());
324 int c = imgMsg->Unpack();
328 if (c & (igtl::MessageHeader::UNPACK_BODY | igtl::MessageHeader::UNPACK_UNDEF))
330 this->addToQueue(imgMsg);
334 std::cout <<
"body crc failed!" << std::endl;
341 mUnsentUSStatusMessage = msg;
344 void IGTLinkClientStreamer::addToQueue(igtl::ImageMessage::Pointer msg)
346 IGTLinkConversion converter;
347 IGTLinkConversionImage imageconverter;
348 IGTLinkConversionSonixCXLegacy cxconverter;
352 if (cxconverter.guessIsSonixLegacyFormat(msg->GetDeviceName()))
354 package->mImage = cxconverter.decode(msg);
358 package->mImage = imageconverter.decode(msg);
362 if (mUnsentUSStatusMessage)
364 package->mProbe = converter.decode(mUnsentUSStatusMessage, msg,
ProbeDefinitionPtr());
366 if (cxconverter.guessIsSonixLegacyFormat(mUnsentUSStatusMessage->GetDeviceName()))
368 package->mProbe = cxconverter.decode(package->mProbe);
378 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()
#define CX_LOG_CHANNEL_DEBUG(channel)
void reportSuccess(QString msg)
void syncToCurrentTime(ImagePtr imgMsg)
boost::shared_ptr< struct Package > PackagePtr
igtl::SmartPointer< Self > Pointer
boost::shared_ptr< Sender > SenderPtr
QString timestampMilliSecondsFormatNice()
boost::shared_ptr< class ProbeDefinition > ProbeDefinitionPtr