1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*=========================================================================
Program: OpenIGTLink -- Example for Label Meta Data Server
Language: C++
Copyright (c) Insight Software Consortium. All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <cstring>
#include "igtlOSUtil.h"
#include "igtlMessageHeader.h"
#include "igtlImageMessage.h"
#include "igtlServerSocket.h"
#include "igtlLabelMetaMessage.h"
int SendLabelMeta(igtl::Socket::Pointer& socket);
int main(int argc, char* argv[])
{
//------------------------------------------------------------
// Parse Arguments
if (argc != 2) // check number of arguments
{
// If not correct, print usage
std::cerr << "Usage: " << argv[0] << " <port>" << std::endl;
std::cerr << " <port> : Port # (18944 in Slicer default)" << std::endl;
exit(0);
}
int port = atoi(argv[1]);
igtl::ServerSocket::Pointer serverSocket;
serverSocket = igtl::ServerSocket::New();
int r = serverSocket->CreateServer(port);
if (r < 0)
{
std::cerr << "Cannot create a server socket." << std::endl;
exit(0);
}
igtl::Socket::Pointer socket;
while (1)
{
//------------------------------------------------------------
// Waiting for Connection
socket = serverSocket->WaitForConnection(1000);
if (socket.IsNotNull()) // if client connected
{
std::cerr << "A client is connected." << std::endl;
// Create a message buffer to receive header
igtl::MessageHeader::Pointer headerMsg;
headerMsg = igtl::MessageHeader::New();
//------------------------------------------------------------
// loop
for (int i = 0; i < 100; i ++)
{
// Initialize receive buffer
headerMsg->InitPack();
// Receive generic header from the socket
int rs = socket->Receive(headerMsg->GetPackPointer(), headerMsg->GetPackSize());
if (rs == 0)
{
socket->CloseSocket();
}
if (rs != headerMsg->GetPackSize())
{
continue;
}
// Deserialize the header
headerMsg->Unpack();
// Check data type and receive data body
if (strcmp(headerMsg->GetDeviceType(), "GET_LBMETA") == 0)
{
std::cerr << "Received a GET_LBMETA message." << std::endl;
//socket->Skip(headerMsg->GetBodySizeToRead(), 0);
SendLabelMeta(socket);
}
else
{
// if the data type is unknown, skip reading.
std::cerr << "Receiving : " << headerMsg->GetDeviceType() << std::endl;
socket->Skip(headerMsg->GetBodySizeToRead(), 0);
}
}
}
}
//------------------------------------------------------------
// Close connection (The example code never reaches to this section ...)
socket->CloseSocket();
}
int SendLabelMeta(igtl::Socket::Pointer& socket)
{
//------------------------------------------------------------
// Allocate Status Message Class
igtl::LabelMetaMessage::Pointer lbMetaMsg;
lbMetaMsg = igtl::LabelMetaMessage::New();
lbMetaMsg->SetDeviceName("MetaServer");
//---------------------------
// Create 1st meta data
igtl::LabelMetaElement::Pointer lbMeta0;
lbMeta0 = igtl::LabelMetaElement::New();
lbMeta0->SetName("LABEL_DESCRIPTION_0");
lbMeta0->SetDeviceName("LABEL_0");
lbMeta0->SetLabel(1);
lbMeta0->SetRGBA(0xFF, 0x00, 0x00, 0xFF);
lbMeta0->SetSize(512, 512, 64);
lbMeta0->SetOwner("IMAGE_0");
//---------------------------
// Create 2nd meta data
igtl::LabelMetaElement::Pointer lbMeta1;
lbMeta1 = igtl::LabelMetaElement::New();
lbMeta1->SetName("LABEL_DESCRIPTION_1");
lbMeta1->SetDeviceName("LABEL_1");
lbMeta1->SetLabel(2);
lbMeta1->SetRGBA(0x00, 0xFF, 0, 0xFF);
lbMeta1->SetSize(256, 128, 32);
lbMeta1->SetOwner("IMAGE_1");
//---------------------------
// Create 3rd meta data
igtl::LabelMetaElement::Pointer lbMeta2;
lbMeta2 = igtl::LabelMetaElement::New();
lbMeta2->SetName("LABEL_DESCRIPTION_2");
lbMeta2->SetDeviceName("LABEL_2");
lbMeta2->SetLabel(3);
lbMeta2->SetRGBA(0, 0, 0xFF, 0xFF);
lbMeta2->SetSize(256, 256, 32);
lbMeta2->SetOwner("IMAGE_2");
lbMetaMsg->AddLabelMetaElement(lbMeta0);
lbMetaMsg->AddLabelMetaElement(lbMeta1);
lbMetaMsg->AddLabelMetaElement(lbMeta2);
lbMetaMsg->Pack();
std::cerr << "Size of pack: " << lbMetaMsg->GetPackSize() << std::endl;
std::cerr << "Name of type: " << lbMetaMsg->GetDeviceType() << std::endl;
std::cerr << "Sending a LBMETA message..." << std::endl;
socket->Send(lbMetaMsg->GetPackPointer(), lbMetaMsg->GetPackSize());
return 1;
}
|