14 #include "igtlOSUtil.h" 15 #include "igtlMessageHeader.h" 16 #include "igtlTransformMessage.h" 17 #include "igtlPositionMessage.h" 18 #include "igtlImageMessage.h" 19 #include "igtlClientSocket.h" 20 #include "igtlStatusMessage.h" 31 #include "vtkImageData.h" 37 mHeadingReceived(false),
64 mSocket.reset(
new QTcpSocket());
65 connect(mSocket.get(), SIGNAL(readyRead()),
this, SLOT(readyReadSlot()), Qt::DirectConnection);
66 connect(mSocket.get(), SIGNAL(hostFound()),
this, SLOT(hostFoundSlot()), Qt::DirectConnection);
67 connect(mSocket.get(), SIGNAL(connected()),
this, SLOT(connectedSlot()), Qt::DirectConnection);
68 connect(mSocket.get(), SIGNAL(disconnected()),
this, SLOT(disconnectedSlot()), Qt::DirectConnection);
69 connect(mSocket.get(), SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(errorSlot(QAbstractSocket::SocketError)),
70 Qt::DirectConnection);
72 if (!this->multipleTryConnectToHost())
74 reportError(
"IGTLinkClientStreamer: Failed to start streaming");
80 mHeaderMsg = igtl::MessageHeader::New();
83 bool IGTLinkClientStreamer::multipleTryConnectToHost()
86 int numberOfConnectionAttempts = 5;
88 for (
int i=0; i<numberOfConnectionAttempts; ++i)
91 report(QString(
"[%2] Attempt %1 to connect to host").arg(i+1).arg(this->hostDescription()));
92 if (this->tryConnectToHost())
96 reportError(QString(
"[%1] Timeout connecting to host").arg(this->hostDescription()));
100 bool IGTLinkClientStreamer::tryConnectToHost()
102 mSocket->connectToHost(mAddress, mPort);
105 if (!mSocket->waitForConnected(timeout))
107 mSocket->disconnectFromHost();
117 mSocket->disconnectFromHost();
125 return (mSocket && mSocket->isValid());
128 QString IGTLinkClientStreamer::hostDescription()
const 133 void IGTLinkClientStreamer::hostFoundSlot()
135 report(QString(
"[%1] Found host").arg(this->hostDescription()));
138 void IGTLinkClientStreamer::connectedSlot()
140 reportSuccess(QString(
"[%1] Connected to host").arg(this->hostDescription()));
142 void IGTLinkClientStreamer::disconnectedSlot()
144 report(QString(
"[%1] Disconnected from host").arg(this->hostDescription()));
147 void IGTLinkClientStreamer::errorSlot(QAbstractSocket::SocketError socketError)
149 report(QString(
"[%1] Socket error [code=%2]: %3")
150 .arg(this->hostDescription())
151 .arg(QString::number(socketError))
152 .arg(mSocket->errorString()));
155 void IGTLinkClientStreamer::readyReadSlot()
158 while (this->readOneMessage());
165 bool IGTLinkClientStreamer::readOneMessage()
170 if (!mHeadingReceived)
174 mHeaderMsg->InitPack();
177 if (mSocket->bytesAvailable() < mHeaderMsg->GetPackSize())
185 mSocket->read(reinterpret_cast<char*>(mHeaderMsg->GetPackPointer()), mHeaderMsg->GetPackSize());
186 mHeadingReceived =
true;
189 mHeaderMsg->Unpack();
192 if (mHeadingReceived)
195 bool success =
false;
205 if (QString(mHeaderMsg->GetDeviceType()) ==
"IMAGE")
207 success = this->ReceiveImage(mSocket.get(), mHeaderMsg);
209 else if (QString(mHeaderMsg->GetDeviceType()) ==
"CX_US_ST")
211 success = this->ReceiveSonixStatus(mSocket.get(), mHeaderMsg);
219 std::cerr <<
"Receiving : " << mHeaderMsg->GetDeviceType() << std::endl;
220 mSocket->read(mHeaderMsg->GetBodySizeToRead());
224 mHeadingReceived =
false;
232 bool IGTLinkClientStreamer::ReceiveSonixStatus(QTcpSocket* socket, igtl::MessageHeader::Pointer& header)
235 msg = IGTLinkUSStatusMessage::New();
236 msg->SetMessageHeader(header);
239 if (socket->bytesAvailable() < msg->GetPackBodySize())
244 socket->read(reinterpret_cast<char*>(msg->GetPackBodyPointer()), msg->GetPackBodySize());
248 int c = msg->Unpack();
249 if (c & (igtl::MessageHeader::UNPACK_BODY | igtl::MessageHeader::UNPACK_UNDEF))
251 this->addToQueue(msg);
256 std::cout <<
"body crc failed!" << std::endl;
262 QDateTime my_decode_timestamp(igtl::MessageBase* msg)
265 igtl::TimeStamp::Pointer timestamp = igtl::TimeStamp::New();
266 msg->GetTimeStamp(timestamp);
267 double timestampMS = timestamp->GetTimeStamp() * 1000;
268 return QDateTime::fromMSecsSinceEpoch(timestampMS);
271 void write_time_info(igtl::ImageMessage::Pointer imgMsg)
273 int kb = imgMsg->GetPackSize()/1024;
275 QDateTime org_ts = my_decode_timestamp(imgMsg.GetPointer());
276 QDateTime now_ts = QDateTime::currentDateTime();
279 <<
", time=(" << org_ts.toString(format) <<
"->" << now_ts.toString(format) <<
")" 280 <<
", lag=" << org_ts.msecsTo(now_ts) <<
"ms";
284 bool IGTLinkClientStreamer::ReceiveImage(QTcpSocket* socket, igtl::MessageHeader::Pointer& header)
287 igtl::ImageMessage::Pointer imgMsg = igtl::ImageMessage::New();
288 imgMsg->SetMessageHeader(header);
289 imgMsg->AllocatePack();
293 if (socket->bytesAvailable() < imgMsg->GetPackBodySize())
299 socket->read(reinterpret_cast<char*>(imgMsg->GetPackBodyPointer()), imgMsg->GetPackBodySize());
303 int c = imgMsg->Unpack();
307 if (c & (igtl::MessageHeader::UNPACK_BODY | igtl::MessageHeader::UNPACK_UNDEF))
309 this->addToQueue(imgMsg);
313 std::cout <<
"body crc failed!" << std::endl;
320 mUnsentUSStatusMessage = msg;
323 void IGTLinkClientStreamer::addToQueue(igtl::ImageMessage::Pointer msg)
333 package->mImage = cxconverter.
decode(msg);
337 package->mImage = imageconverter.
decode(msg);
341 if (mUnsentUSStatusMessage)
347 package->mProbe = cxconverter.
decode(package->mProbe);
358 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)
ImagePtr decode(igtl::ImageMessage *in)
virtual void stopStreaming()
QString decode(igtl::StringMessage::Pointer msg)
virtual bool isStreaming()
#define CX_LOG_CHANNEL_DEBUG(channel)
bool guessIsSonixLegacyFormat(QString deviceName)
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
ImagePtr decode(igtl::ImageMessage::Pointer msg)
Namespace for all CustusX production code.