File: SessionManagerServer.cxx

package info (click to toggle)
openigtlink 3.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,080 kB
  • sloc: cpp: 20,076; ansic: 6,704; sh: 227; perl: 74; makefile: 46
file content (196 lines) | stat: -rw-r--r-- 6,437 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*=========================================================================

  Program:   OpenIGTLink -- Example for Data Receiving Server Program
  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 <iomanip>
#include <math.h>
#include <cstdlib>
#include <cstring>

#include "igtlOSUtil.h"
#include "igtlMessageHeader.h"
#include "igtlMessageHandler.h"
#include "igtlMessageHandlerMacro.h"
#include "igtlSessionManager.h"
#include "igtlTransformMessage.h"
#include "igtlPositionMessage.h"
#include "igtlImageMessage.h"



//------------------------------------------------------------
// Define a structure type to share data between message
// handler classes and main() function. 
// It can be any types e.g. C++ class, array, etc.
// In this example, the shared structure is only used for
// passing the message type and the device name from the
// handler to main() function.

typedef struct {
  std::string messagetype;
  std::string devicename;
} MyData;


//------------------------------------------------------------
// Define message handler classes for TransformMessage,
// PositionMessage and ImageMessage.
// igtlMessageHandlerClassMacro() defines a child class of
// igtl::MessageHandler to handle OpenIGTLink messages for
// the message type specified as the first argument. The
// second argument will be used for the name of this 
// message handler class, while the third argument specifies
// a type of data that will be shared with the message functions
// of this handler class. 

igtlMessageHandlerClassMacro(igtl::TransformMessage, TransformHandler, MyData);
igtlMessageHandlerClassMacro(igtl::PositionMessage,  PositionHandler,  MyData);
igtlMessageHandlerClassMacro(igtl::ImageMessage,     ImageHandler,     MyData);


//------------------------------------------------------------
// You need to describe how the received message is processed
// in Process() function of the message handler class.
// When Process() is called, pointers to the received message
// and the shared data are passed as the arguments.

// -- Transform message
int TransformHandler::Process(igtl::TransformMessage * transMsg, MyData* data)
{
  // Retrive the transform data
  igtl::Matrix4x4 matrix;
  transMsg->GetMatrix(matrix);
  igtl::PrintMatrix(matrix);

  data->messagetype = transMsg->GetDeviceType();
  data->devicename  = transMsg->GetDeviceName();

  return 1;
}

// -- Position message
int PositionHandler::Process(igtl::PositionMessage * positionMsg, MyData* data)
{
  // Retrive the transform data
  float position[3];
  float quaternion[4];
  
  positionMsg->GetPosition(position);
  positionMsg->GetQuaternion(quaternion);
  
  std::cerr << "position   = (" << position[0] << ", " << position[1] << ", " << position[2] << ")" << std::endl;
  std::cerr << "quaternion = (" << quaternion[0] << ", " << quaternion[1] << ", "
            << quaternion[2] << ", " << quaternion[3] << ")" << std::endl << std::endl;

  data->messagetype = positionMsg->GetDeviceType();
  data->devicename  = positionMsg->GetDeviceName();

  return 1;
}

// -- Image message
int ImageHandler::Process(igtl::ImageMessage * imgMsg, MyData *data)
{
  // Retrive the image data
  int   size[3];          // image dimension
  float spacing[3];       // spacing (mm/pixel)
  int   svsize[3];        // sub-volume size
  int   svoffset[3];      // sub-volume offset
  int   scalarType;       // scalar type
  int   endian;           // endian
  
  scalarType = imgMsg->GetScalarType();
  endian = imgMsg->GetEndian();
  imgMsg->GetDimensions(size);
  imgMsg->GetSpacing(spacing);
  imgMsg->GetSubVolume(svsize, svoffset);
  
  std::cerr << "Device Name           : " << imgMsg->GetDeviceName() << std::endl;
  std::cerr << "Scalar Type           : " << scalarType << std::endl;
  std::cerr << "Endian                : " << endian << std::endl;
  std::cerr << "Dimensions            : ("
            << size[0] << ", " << size[1] << ", " << size[2] << ")" << std::endl;
  std::cerr << "Spacing               : ("
            << spacing[0] << ", " << spacing[1] << ", " << spacing[2] << ")" << std::endl;
  std::cerr << "Sub-Volume dimensions : ("
            << svsize[0] << ", " << svsize[1] << ", " << svsize[2] << ")" << std::endl;
  std::cerr << "Sub-Volume offset     : ("
            << svoffset[0] << ", " << svoffset[1] << ", " << svoffset[2] << ")" << std::endl;

  data->messagetype = imgMsg->GetDeviceType();
  data->devicename  = imgMsg->GetDeviceName();

  return 1;
}


int main(int argc, char* argv[])
{

  //------------------------------------------------------------
  // Parse Arguments
  if (argc != 2) // check number of arguments
    {
    // If not correct, print usage
    std::cerr << "    <port>     : Port # (18944 in Slicer default)"   << std::endl;
    exit(0);
    }

  int    port     = atoi(argv[1]);

  //------------------------------------------------------------
  // Create a session manager
  igtl::SessionManager::Pointer sm;
  sm = igtl::SessionManager::New();
  sm->SetMode(igtl::SessionManager::MODE_SERVER);
  sm->SetPort(port);

  //------------------------------------------------------------
  // Create message handlers
  TransformHandler::Pointer tmh = TransformHandler::New();
  PositionHandler::Pointer pmh  = PositionHandler::New();
  ImageHandler::Pointer imh     = ImageHandler::New();

  MyData mydata;
  tmh->SetData(&mydata);
  pmh->SetData(&mydata);
  imh->SetData(&mydata);

  //------------------------------------------------------------
  // Register the message handlers to the session manager
  sm->AddMessageHandler(tmh);
  sm->AddMessageHandler(pmh);
  sm->AddMessageHandler(imh);

  //------------------------------------------------------------
  // Start session
  if (sm->Connect())
    {
    while (1)
      {
      int r = sm->ProcessMessage();
      if (r == 0) // Disconnected
        {
        break;
        }
      std::cerr << "Message Type: " << tmh->GetData()->messagetype << std::endl;
      std::cerr << "Device Name: " << tmh->GetData()->devicename << std::endl;
      }
    // Stop session
    sm->Disconnect();
    }
  
}