File: igtlQueryMessage.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 (143 lines) | stat: -rw-r--r-- 3,584 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
/*=========================================================================

  Program:   The OpenIGTLink Library
  Language:  C++
  Web page:  http://openigtlink.org/

  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 "igtlQueryMessage.h"

#include "igtl_header.h"

// Disable warning C4996 (strncpy() may be unsafe) in Windows. 
#define _CRT_SECURE_NO_WARNINGS

#include <string.h>

namespace igtl {

QueryMessage::QueryMessage()
{
  this->m_SendMessageType = "QUERY";
}

QueryMessage::~QueryMessage()
{
}


int QueryMessage::SetDeviceUID(const char* string)
{
  if (strlen(string) > 0xFFFF) /* If the length is beyond the range of unsigned short */
    {
    return 0;
    }
  m_DeviceUID = string;
  return (int) this->m_DeviceUID.length();
}


int QueryMessage::SetDeviceUID(const std::string & string)
{
  if (string.length() > 0xFFFF) /* If the length is beyond the range of unsigned short */
  {
    return 0;
  }
  this->m_DeviceUID = string;
  return (int) this->m_DeviceUID.length();
}


std::string QueryMessage::GetDeviceUID()
{
  return this->m_DeviceUID;
}
  
  
  
int QueryMessage::SetDataType(const char* dataType)
{
  if (strlen(dataType) > IGTL_QUERY_DATE_TYPE_SIZE) /* If the length is beyond the range specified by the spec */
  {
    return 0;
  }
  strcpy((char*)m_DataType, dataType);
  return 1;
}


int QueryMessage::SetDataType(const std::string& dataType)
{
  return this->SetDataType(dataType.c_str());
}



int QueryMessage::CalculateContentBufferSize()
{
  // Body pack size is the sum of DeviceUID and data type fields
  return IGTL_QUERY_HEADER_SIZE + this->m_DeviceUID.length();
}

  
int QueryMessage::PackContent()
{
  // Allocate buffer
  AllocateBuffer();
  igtl_query_header * query_header;
  char * deviceName;
  
  // Set pointers
#if OpenIGTLink_HEADER_VERSION >= 2
  query_header = (igtl_query_header*) this->m_Content;
  deviceName        = (char *) this->m_Content + sizeof(igtl_query_header);
#else
  query_header = (igtl_query_header*) this->m_Body;
  deviceName        = (char *) this->m_Body + sizeof(igtl_query_header);
#endif
  
  // Copy data
  query_header->deviceUIDLength   = static_cast<igtlUint16>(this->m_DeviceUID.length());
  query_header->queryID = this->m_QueryID;
  memcpy(query_header->queryDataType, this->m_DataType, IGTL_QUERY_DATE_TYPE_SIZE);
  strncpy(deviceName, this->m_DeviceUID.c_str(), query_header->deviceUIDLength);
  
  // Convert byte order from host to network
  igtl_query_convert_byte_order(query_header);
  
  return 1;
}

int QueryMessage::UnpackContent()
{
  igtl_query_header * query_header;
  char * deviceName;
  
#if OpenIGTLink_HEADER_VERSION >= 2
  query_header = (igtl_query_header*) this->m_Content;
  deviceName        = (char *) this->m_Content + sizeof(igtl_query_header);
#else
  query_header = (igtl_query_header*) this->m_Body;
  deviceName        = (char *) this->m_Body + sizeof(igtl_query_header);
#endif
  
  // Convert byte order from network to host
  igtl_query_convert_byte_order(query_header);
  
  // Copy data
  this->m_QueryID = query_header->queryID;
  memcpy(m_DataType, query_header->queryDataType, IGTL_QUERY_DATE_TYPE_SIZE);
  this->m_DeviceUID.clear();
  this->m_DeviceUID.append(deviceName, query_header->deviceUIDLength);
  
  return 1;
}

} // namespace igtl