void Test_AddCustomMessageType_Succeeds()
{
m_MessageFactory->AddMessageNewMethod(TYPE_TEST, (mitk::IGTLMessageFactory::PointerToMessageBaseNew)&mitk::IGTLDummyMessage::New);

igtl::MessageBase::Pointer messageBase = m_MessageFactory->CreateInstance(TYPE_TEST);
mitk::IGTLDummyMessage::Pointer message = dynamic_cast<mitk::IGTLDummyMessage*>(messageBase.GetPointer());

CPPUNIT_ASSERT_MESSAGE("The created message was not of type mitk::IGTLDummyMessage", message.IsNotNull());
}
  void Test_SendingMessageFromServerToOneClient_Successful()
  {
    CPPUNIT_ASSERT_MESSAGE("Server not connected to Client.", m_Server->OpenConnection());
    CPPUNIT_ASSERT_MESSAGE("Client 1 not connected to Server.", m_Client_One->OpenConnection());
    m_Server->StartCommunication();
    m_Client_One->StartCommunication();

    igtl::MessageBase::Pointer sentMessage = m_MessageFactory->CreateInstance("STATUS");
    dynamic_cast<igtl::StatusMessage*>(sentMessage.GetPointer())->SetStatusString(m_Message.c_str());
    m_Server->SendMessage(sentMessage);
    igtl::MessageBase::Pointer receivedMessage;
    int steps = 0;
    while ((receivedMessage = m_Client_One->GetMessageQueue()->PullMiscMessage()) == nullptr)
    {
      std::this_thread::sleep_for(std::chrono::milliseconds(5));
      if (++steps > 20)
        break;
    }
    CPPUNIT_ASSERT(receivedMessage != nullptr);

    CPPUNIT_ASSERT(m_Client_One->StopCommunication());
    CPPUNIT_ASSERT(m_Server->StopCommunication());

    CPPUNIT_ASSERT(m_Client_One->CloseConnection());
    CPPUNIT_ASSERT(m_Server->CloseConnection());

    testMessagesEqual(sentMessage, receivedMessage);
  }
void Test_CreateCapabilityMessage_NotNull()
{
igtl::MessageBase::Pointer messageBase = m_MessageFactory->CreateInstance(TYPE_CAPABILITY);
igtl::CapabilityMessage::Pointer message = dynamic_cast<igtl::CapabilityMessage*>(messageBase.GetPointer());

CPPUNIT_ASSERT_MESSAGE("The created message was not of type igtl::CapabilityMessage", message.IsNotNull());
}
void Test_CreateStatusMessage_NotNull()
{
igtl::MessageBase::Pointer messageBase = m_MessageFactory->CreateInstance(TYPE_STATUS);
igtl::StatusMessage::Pointer message = dynamic_cast<igtl::StatusMessage*>(messageBase.GetPointer());

CPPUNIT_ASSERT_MESSAGE("The created message was not of type igtl::StatusMessage", message.IsNotNull());
}
  void Test_SendingMessageFromServerToMultipleClients_Successful()
  {
    CPPUNIT_ASSERT_MESSAGE("Server not connected to Client.", m_Server->OpenConnection());
    m_Server->StartCommunication();
    CPPUNIT_ASSERT_MESSAGE("Client 1 not connected to Server.", m_Client_One->OpenConnection());
    m_Client_One->StartCommunication();
    CPPUNIT_ASSERT_MESSAGE("Client 2 not connected to Server.", m_Client_Two->OpenConnection());
    m_Client_Two->StartCommunication();

    std::this_thread::sleep_for(std::chrono::milliseconds(100));

    igtl::MessageBase::Pointer sentMessage = m_MessageFactory->CreateInstance("STATUS");
    dynamic_cast<igtl::StatusMessage*>(sentMessage.GetPointer())->SetStatusString(m_Message.c_str());
    m_Server->SendMessage(sentMessage);
    MITK_INFO << "SENT MESSAGE";

    igtl::MessageBase::Pointer receivedMessage1;
    igtl::MessageBase::Pointer receivedMessage2;
    int steps = 0;
    while (receivedMessage1 == nullptr || receivedMessage2 == nullptr)
    {
      std::this_thread::sleep_for(std::chrono::milliseconds(20));

      igtl::MessageBase::Pointer tmpMessage1 = m_Client_One->GetMessageQueue()->PullMiscMessage();
      if (tmpMessage1.IsNotNull())
        receivedMessage1 = tmpMessage1;

      igtl::MessageBase::Pointer tmpMessage2 = m_Client_Two->GetMessageQueue()->PullMiscMessage();
      if (tmpMessage2.IsNotNull())
        receivedMessage2 = tmpMessage2;

      if (++steps > 50)
        break;
    }

    CPPUNIT_ASSERT(m_Client_Two->StopCommunication());
    CPPUNIT_ASSERT(m_Client_One->StopCommunication());
    CPPUNIT_ASSERT(m_Server->StopCommunication());

    CPPUNIT_ASSERT(m_Client_Two->CloseConnection());
    CPPUNIT_ASSERT(m_Client_One->CloseConnection());
    CPPUNIT_ASSERT(m_Server->CloseConnection());

    CPPUNIT_ASSERT_MESSAGE("Message from first client was null..", receivedMessage1 != nullptr);
    CPPUNIT_ASSERT_MESSAGE("Message from first client was null..", receivedMessage1.IsNotNull());
    CPPUNIT_ASSERT_MESSAGE("Message from second client was null..", receivedMessage2 != nullptr);
    CPPUNIT_ASSERT_MESSAGE("Message from second client was null..", receivedMessage2.IsNotNull());

    testMessagesEqual(sentMessage, receivedMessage1);
    testMessagesEqual(sentMessage, receivedMessage2);
    testMessagesEqual(receivedMessage2, receivedMessage1);
  }