8 #include "PlusConfigure.h" 22 , IgtlMessageCrcCheckEnabled(0)
23 , ReceiveTimeoutSec(0.5)
25 , NumberOfRetryAttempts(3)
26 , DelayBetweenRetryAttemptsSec(0.100)
28 , SocketMutex(vtkSmartPointer<vtkIGSIORecursiveCriticalSection>::New())
29 , ClientSocket(
igtl::ClientSocket::New())
30 , ReconnectOnReceiveTimeout(true)
31 , UseReceivedTimestamps(true)
45 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
55 os << indent <<
"Server address: " << this->
ServerAddress <<
"\n";
57 os << indent <<
"Server port: " << this->
ServerPort <<
"\n";
60 os << indent <<
"Message type: " << this->
MessageType <<
"\n";
70 std::ostringstream version;
71 version <<
"OpenIGTLink v" << OPENIGTLINK_VERSION;
78 LOG_TRACE(
"vtkPlusOpenIGTLinkDevice::InternalConnect");
83 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
95 LOG_TRACE(
"vtkPlusOpenIGTLinkDevice::Disconnect");
97 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
105 LOG_TRACE(
"vtkPlusOpenIGTLinkDevice::Probe");
120 LOG_DEBUG(
"Attempt to connect to client socket in device " << this->
GetDeviceId());
123 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
132 LOG_ERROR(
"Unable to connect OpenIGTLink server - server address is undefined");
138 LOG_ERROR(
"Unable to connect OpenIGTLink server - server port is invalid: " << this->
ServerPort);
144 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
157 LOG_DEBUG(
"Client successfully connected to server (" << this->
ServerAddress <<
":" << this->
ServerPort <<
").");
160 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
194 igsioTransformName tName(it->second->GetId());
199 auto clientInfoMsg = igtl::PlusClientInfoMessage::New();
200 clientInfoMsg->SetClientInfo(clientInfo);
201 clientInfoMsg->Pack();
206 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
208 (retValue = this->
ClientSocket->Send(clientInfoMsg->GetBufferPointer(), clientInfoMsg->GetBufferSize())) != 0,
214 LOG_ERROR(
"Failed to send PlusClientInfo message to server!");
226 LOG_WARNING(
"No OpenIGTLink message has been received in device " << this->
GetDeviceId() <<
": failed to receive OpenIGTLink transforms. Attempt to reconnect.");
234 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
241 LOG_ERROR(
"Socket error in device " << this->
GetDeviceId() <<
": failed to receive OpenIGTLink transforms. Attempt to reconnect.");
246 LOG_ERROR(
"Socket error in device " << this->
GetDeviceId() <<
": failed to receive OpenIGTLink transforms");
254 headerMsg = this->
MessageFactory->CreateHeaderMessage(IGTL_HEADER_VERSION_1);
256 int numOfBytesReceived = 0;
259 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
261 (numOfBytesReceived = this->
ClientSocket->Receive(headerMsg->GetBufferPointer(), headerMsg->GetBufferSize(), timeout)) != 0,
265 if (numOfBytesReceived > 0)
268 if (numOfBytesReceived != headerMsg->GetBufferSize())
271 LOG_ERROR(
"Couldn't receive data from OpenIGTLink device " << this->
GetDeviceId() <<
": (unexpected header size)");
281 bool socketError = numOfBytesReceived < 0;
286 int socketErrorCode = WSAGetLastError();
287 if (socketErrorCode == WSAETIMEDOUT)
290 LOG_TRACE(
"No data coming from OpenIGTLink device " << this->
GetDeviceId() <<
": (timeout)");
295 LPTSTR errorMsgPtr = 0;
296 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, socketErrorCode, 0, (LPTSTR)&errorMsgPtr, 0, NULL) != 0)
298 LOG_DEBUG(
"No data coming from OpenIGTLink device " << this->
GetDeviceId() <<
": (socket error: " << errorMsgPtr <<
")");
302 LOG_DEBUG(
"No data coming from OpenIGTLink device " << this->
GetDeviceId() <<
": (unknown socket error)");
308 LOG_DEBUG(
"No data coming from OpenIGTLink device " << this->
GetDeviceId());
311 LOG_DEBUG(
"No data coming from OpenIGTLink device " << this->
GetDeviceId());
321 XML_READ_STRING_ATTRIBUTE_REQUIRED(
ServerAddress, deviceConfig);
322 XML_READ_SCALAR_ATTRIBUTE_REQUIRED(
int,
ServerPort, deviceConfig);
323 XML_READ_STRING_ATTRIBUTE_OPTIONAL(
MessageType, deviceConfig);
325 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
double,
SendTimeoutSec, deviceConfig);
336 XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(
ServerAddress, rootConfigElement);
337 deviceConfig->SetIntAttribute(
"ServerPort", this->
ServerPort);
338 XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(
MessageType, rootConfigElement);
340 deviceConfig->SetDoubleAttribute(
"SendTimeoutSec", this->
SendTimeoutSec);
352 igsioLockGuard<vtkIGSIORecursiveCriticalSection> socketGuard(this->
SocketMutex);
353 success = this->
ClientSocket->Send(packedMessage->GetBufferPointer(), packedMessage->GetBufferSize());
357 LOG_ERROR(
"OpenIGTLink client couldn't send message to server.");
DataSourceContainer::const_iterator DataSourceContainerConstIterator
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
vtkPlusOpenIGTLinkDevice()
virtual PlusStatus Probe()
void ReceiveMessageHeaderWithErrorHandling(igtl::MessageHeader::Pointer &headerMsg)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
std::string EmbeddedTransformToFrame
std::vector< ImageStream > ImageStreams
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
std::vector< std::string > IgtlMessageTypes
vtkSmartPointer< vtkPlusIgtlMessageFactory > MessageFactory
virtual std::string GetDeviceId() const
virtual PlusStatus InternalDisconnect()
std::string ServerAddress
vtkSmartPointer< vtkIGSIORecursiveCriticalSection > SocketMutex
std::vector< igsioTransformName > TransformNames
igsioTransformName ImageMessageEmbeddedTransformName
bool SendMessage(igtl::MessageBase::Pointer packedMessage)
int NumberOfRetryAttempts
virtual PlusStatus Disconnect()
virtual PlusStatus SendRequestedMessageTypes()
virtual std::string GetSdkVersion()
virtual PlusStatus Connect()
virtual ~vtkPlusOpenIGTLinkDevice()
virtual PlusStatus InternalConnect()
virtual PlusStatus ReceiveMessageHeader(igtl::MessageHeader::Pointer &headerMsg)
Factory class of supported OpenIGTLink message types.
virtual PlusStatus StopRecording()
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
bool UseReceivedTimestamps
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
bool StartThreadForInternalUpdates
igtl::ClientSocket::Pointer ClientSocket
DataSourceContainerConstIterator GetToolIteratorBegin() const
virtual PlusStatus ClientSocketReconnect()
This class provides client information for vtkPlusOpenIGTLinkServer.
virtual bool GetReconnectOnReceiveTimeout()
DataSourceContainerConstIterator GetToolIteratorEnd() const
double DelayBetweenRetryAttemptsSec
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *config)
int IgtlMessageCrcCheckEnabled
bool ReconnectOnReceiveTimeout