File: igtlCapabilityMessage.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 (155 lines) | stat: -rwxr-xr-x 3,163 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
/*=========================================================================

  Program:   The OpenIGTLink Library
  Module:
  Language:  C++
  Date:
  Version:

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

#include <igtlTypes.h>

#include <igtl_header.h>
#include <igtl_capability.h>
#include <igtlOSUtil.h>

#include <vector>
#include <string>

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

#include <string.h>

namespace igtl {

CapabilityMessage::CapabilityMessage():
    MessageBase()
{
  this->m_SendMessageType = "CAPABILITY";
  this->m_TypeNames.clear();
/// CapabilityMessage stay the same as previous versions, set m_Version = 1
/// to make the pack and unpack procedures the same as OpenIGTLink_PROTOCOL_VERSION 1
#if OpenIGTLink_HEADER_VERSION >= 2
  m_HeaderVersion = IGTL_HEADER_VERSION_1;
#endif
}


CapabilityMessage::~CapabilityMessage()
{
  this->m_TypeNames.clear();
}

void CapabilityMessage::SetTypes(std::vector<std::string> types)
{
  this->m_TypeNames.clear();
  this->m_TypeNames = types;
}


int CapabilityMessage::SetType(int id, const char* type)
{
  if (id < (int)this->m_TypeNames.size() && strlen(type) < IGTL_HEADER_TYPE_SIZE)
    {
    this->m_TypeNames[id] = type;
    return 1;
    }
  else
    {
    return 0;
    }
}


const char* CapabilityMessage::GetType(int id)
{
  if (id < (int)this->m_TypeNames.size())
    {
    return this->m_TypeNames[id].c_str();
    }
  else
    {
    return "";
    }
}


int CapabilityMessage::CalculateContentBufferSize()
{
  return (sizeof(char) * IGTL_HEADER_TYPE_SIZE * this->m_TypeNames.size());
}


int CapabilityMessage::PackContent()
{
  AllocateBuffer();

  if (this->m_TypeNames.size() == 0)
  {
    return 0;
  }

  igtl_capability_info info;
  int nTypes = this->m_TypeNames.size();

  igtl_capability_init_info(&info);
  info.ntypes = nTypes;
  igtl_capability_alloc_info(&info, nTypes);

  for(int i = 0; i < nTypes; i++ )
    {
    memcpy(info.typenames[i], this->m_TypeNames[i].c_str(), IGTL_HEADER_TYPE_SIZE);
    }

  igtl_capability_pack(&info, this->m_Content);

  return 1;
}


int CapabilityMessage::UnpackContent()
{

  igtl_capability_info info;

  igtl_capability_init_info(&info);
  igtl_capability_unpack(this->m_Content, &info, this->CalculateReceiveContentSize());

  int ntypes = info.ntypes;

  if(ntypes == 0)
    {
    return 0;
    }

  this->m_TypeNames.clear();
  for(int i = 0; i < ntypes; i++)
    {
    std::string buf;
    if (igtl::Strnlen((const char*)info.typenames[i], IGTL_HEADER_TYPE_SIZE) < IGTL_HEADER_TYPE_SIZE)
      {
      buf.append((const char*)info.typenames[i]);
      }
    else
      {
      buf.append((const char*)info.typenames[i], IGTL_HEADER_TYPE_SIZE);
      }
    this->m_TypeNames.push_back(buf);
    }
  

  return 1;
}


} // namespace igtl