File: igtlSessionManager.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 (317 lines) | stat: -rw-r--r-- 8,126 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*=========================================================================

  Program:   OpenIGTLink Library
  Module:    git@github.com:openigtlink/OpenIGTLink.git
  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 "igtlSessionManager.h"
#include "igtlMessageHandler.h"
#include "igtlClientSocket.h"
#include "igtlServerSocket.h"

#include "igtl_header.h"

namespace igtl
{
  

SessionManager::SessionManager()
{
  this->m_MessageHandlerList.clear();
  this->m_Mode = MODE_SERVER;

  this->m_Header = igtl::MessageHeader::New();
  this->m_TimeStamp = igtl::TimeStamp::New();

  this->m_CurrentReadIndex = 0;
  this->m_HeaderDeserialized = 0;
}


SessionManager::~SessionManager()
{
}


int SessionManager::AddMessageHandler(MessageHandler* handler)
{
  // Check if there is any handler for the same message type
  std::vector< MessageHandler* >::iterator iter;
  for (iter = this->m_MessageHandlerList.begin(); iter != this->m_MessageHandlerList.end(); iter ++)
    {
    if ( (*iter)->GetMessageType() == handler->GetMessageType() )
      {
      return 0;
      }
    }

  // If not, add the handler to the list.
  this->m_MessageHandlerList.push_back(handler);

  return 1;
}


int SessionManager::RemoveMessageHandler(MessageHandler* handler)
{
  // Check if there is any handler for the same message type
  std::vector< MessageHandler* >::iterator iter;
  for (iter = this->m_MessageHandlerList.begin(); iter != this->m_MessageHandlerList.end(); iter ++)
    {
    if (*iter == handler)
      {
      this->m_MessageHandlerList.erase(iter);
      return 1;
      }
    }
  return 0;
}


int SessionManager::Connect()
{
  
  this->m_Socket = NULL;

  if (this->m_Mode == MODE_CLIENT)
    {
    ClientSocket::Pointer clientSocket;
    clientSocket = ClientSocket::New();
    //this->DebugOff();
    if (this->m_Hostname.length() == 0)
      {
      return 0;
      }
    //this->m_Socket->SetConnectTimeout(1000);
    int r = clientSocket->ConnectToServer(this->m_Hostname.c_str(), this->m_Port);
    if (r == 0) // if connected to server
      {
      //clientSocket->SetReceiveTimeout(0);
      this->m_Socket = clientSocket;
      }
    }
  else
    {
    ServerSocket::Pointer serverSocket;
    serverSocket = ServerSocket::New();
    int r = serverSocket->CreateServer(this->m_Port);
    if (r < 0)
      {
      return 0;
      }

    if (serverSocket.IsNotNull())
      {
      //this->ServerSocket->CreateServer(this->m_Port);
      this->m_Socket = serverSocket->WaitForConnection(10000);
      }
    
    if (this->m_Socket.IsNotNull() && this->m_Socket->GetConnected()) // if client connected
      {
      this->m_Socket->DebugOff();
      }
    else
      {
      return 0;
      }
    }

  this->m_Socket->SetReceiveBlocking(0); // Psuedo non-blocking
  this->m_CurrentReadIndex = 0;
  this->m_HeaderDeserialized = 0;
  return 1;
}


int SessionManager::Disconnect()
{
  if (this->m_Socket.IsNotNull())
    {
    this->m_Socket->CloseSocket();
    }

  return 0;
}

int SessionManager::ProcessMessage()
{
  // The workflow of this function is as follows:
  //
  //  HEADER:
  //   IF the message is (a) a new message:
  //       start reading the header;
  //   ELSE IF the message is a message in progress:
  //       if the process is reading the header:
  //           continue to read the header;
  //   ELSE:
  //       GOTO BODY;
  //   
  //   IF the header has not been received:
  //       GOTO BODY;
  //   ELSE
  //       RETURN;
  //   
  //  BODY:
  //   IF the process has not started reading the body:
  //       check the body type;
  //       find an appropriate handler;
  //       start reading the body
  //   ELSE:
  //       continue to read the body
  //

  //--------------------------------------------------
  // Header
  if (this->m_CurrentReadIndex == 0)
    {
    // Initialize receive buffer
    this->m_Header->InitBuffer();
  
    // Receive generic header from the socket
    int r = this->m_Socket->Receive(this->m_Header->GetBufferPointer(), this->m_Header->GetBufferSize(), 0);
    if (r == 0)
      {
      this->m_CurrentReadIndex = 0;
      this->m_HeaderDeserialized = 0;
      return 0; // Disconnected
      }
    if (r != this->m_Header->GetBufferSize())
      {
      // Only a part of header has arrived.
      if (r < 0) // timeout
        {
        this->m_CurrentReadIndex = 0;
        }
      else
        {
        this->m_CurrentReadIndex = r;
        }
      return -1;
      }
    // The header has been received.
    this->m_CurrentReadIndex = IGTL_HEADER_SIZE;
    }
  else if (this->m_CurrentReadIndex < IGTL_HEADER_SIZE)
    {
    // Message transfer was interrupted in the header
    int r = this->m_Socket->Receive((void*)((char*)this->m_Header->GetBufferPointer()+this->m_CurrentReadIndex),
                                    this->m_Header->GetBufferSize()-this->m_CurrentReadIndex, 0);
    if (r == 0)
      {
      this->m_CurrentReadIndex = 0;
      this->m_HeaderDeserialized = 0;
      return 0; // Disconnected
      }
    if (r != this->m_Header->GetBufferSize()-this->m_CurrentReadIndex)
      {
      // Only a part of header has arrived.
      if (r > 0) // exclude a case of timeout 
        {
        this->m_CurrentReadIndex += r;
        }
      return -1;
      }
    // The header has been received.
    this->m_CurrentReadIndex = IGTL_HEADER_SIZE;
    }
  

  //--------------------------------------------------
  // Body
  if (this->m_HeaderDeserialized == 0)
    {
    // Deserialize the header
    this->m_Header->Unpack();
    
    // Get time stamp
    igtlUint32 sec;
    igtlUint32 nanosec;
   
    this->m_Header->GetTimeStamp(this->m_TimeStamp);
    this->m_TimeStamp->GetTimeStamp(&sec, &nanosec);
    //std::cerr << "Time stamp: "
    //          << sec << "." << std::setw(9) << std::setfill('0') 
    //          << nanosec << std::endl;

    // Look for a message handler that matches to the message type.
    int found = 0;
    std::vector< MessageHandler* >::iterator iter;
    for (iter = this->m_MessageHandlerList.begin(); iter != this->m_MessageHandlerList.end(); iter ++)
      {
#if OpenIGTLink_HEADER_VERSION >= 2
      if ( this->m_Header->GetMessageType() == (*iter)->GetMessageType() )
#else
      if (strcmp(this->m_Header->GetDeviceType(), (*iter)->GetMessageType()) == 0)
#endif
        {
        this->m_CurrentMessageHandler = *iter;
        found = 1;
        break;
        }
      }
      
    // If there is no message handler, skip the message
    if (!found)
      {
#if OpenIGTLink_HEADER_VERSION >= 2
      std::cerr << "Receiving: " << this->m_Header->GetMessageType() << std::endl;
#else
      std::cerr << "Receiving: " << this->m_Header->GetDeviceType() << std::endl;
#endif
      this->m_Socket->Skip(this->m_Header->GetBodySizeToRead(), 0);
      // Reset the index counter to be ready for the next message
      this->m_CurrentReadIndex = 0;
      this->m_HeaderDeserialized = 0;
      return 1;
      }

    this->m_HeaderDeserialized = 1;
    }

  int r = this->m_CurrentMessageHandler->ReceiveMessage(this->m_Socket, this->m_Header,
                                                        this->m_CurrentReadIndex-IGTL_HEADER_SIZE);
  if (r == this->m_Header->GetBodySizeToRead())
    {
    this->m_CurrentReadIndex = 0;
    this->m_HeaderDeserialized = 0;
    }
  else
    {
    this->m_CurrentReadIndex += IGTL_HEADER_SIZE + r;
    }

  return 1;
}  
  

int SessionManager::PushMessage(MessageBase* message)
{
  
  if (message && this->m_Socket.IsNotNull() && this->m_Socket->GetConnected()) // if client connected
    {
    return this->m_Socket->Send(message->GetBufferPointer(), message->GetBufferSize());
    }
  else
    {
    return 0;
    }
}


}