示例#1
0
void mitk::IGTLServer::Connect()
{
  igtl::Socket::Pointer socket;
  //check if another igtl device wants to connect to this socket
  socket =
    ((igtl::ServerSocket*)(this->m_Socket.GetPointer()))->WaitForConnection(1);
  //if there is a new connection the socket is not null
  if (socket.IsNotNull())
  {
    //add the new client socket to the list of registered clients
    m_SentListMutex->Lock();
    m_ReceiveListMutex->Lock();
    this->m_RegisteredClients.push_back(socket);
    m_SentListMutex->Unlock();
    m_ReceiveListMutex->Unlock();
    //inform observers about this new client
    this->InvokeEvent(NewClientConnectionEvent());
    MITK_INFO("IGTLServer") << "Connected to a new client: " << socket;
  }
}
bool mitk::IGTLClient::OpenConnection()
{
  if (this->GetState() != Setup)
  {
    mitkThrowException(mitk::Exception) <<
      "Can only try to open the connection if in setup mode";
    return false;
  }

  std::string hostname = this->GetHostname();
  int portNumber = this->GetPortNumber();

  if (portNumber == -1 || hostname.size() <= 0)
  {
    //port number or hostname was not correct
    return false;
  }

  //create a new client socket
  m_Socket = igtl::ClientSocket::New();

  //try to connect to the igtl server
  int response = dynamic_cast<igtl::ClientSocket*>(m_Socket.GetPointer())->
    ConnectToServer(hostname.c_str(), portNumber);

  //check the response
  if ( response != 0 )
  {
    mitkThrowException(mitk::Exception) <<
      "The client could not connect to " << hostname << " port: " << portNumber;
    return false;
  }

  // everything is initialized and connected so the communication can be started
  this->SetState(Ready);

  //inform observers about this new client
  this->InvokeEvent(NewClientConnectionEvent());

  return true;
}