Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  
igtlMessageBase.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Open IGT Link Library
4  Module: $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlMessageBase.h $
5  Language: C++
6  Date: $Date: 2009-01-13 14:11:16 -0500 (Tue, 13 Jan 2009) $
7  Version: $Revision: 3544 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10 
11  This software is distributed WITHOUT ANY WARRANTY; without even
12  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the above copyright notices for more information.
14 
15 =========================================================================*/
16 
17 #ifndef __igtlMessageBase_h
18 #define __igtlMessageBase_h
19 
20 #include "igtlObject.h"
21 #include "igtlObjectFactory.h"
22 //#include "igtlMacros.h"
23 #include "igtlMacro.h"
24 #include "igtlMath.h"
25 #include "igtlTimeStamp.h"
26 
27 #include "igtlMessageHeader.h"
28 
29 #include <string>
30 
31 //-------------------------------------------------------------------------
32 // The MessageBase class is the base class of all message type classes
33 // used in the Open IGT Link Library. The message classes can be used
34 // both for serializing (packing) Open IGT Link message byte streams.
35 // The class can also deserializing (unpacking) Open IGT Link.
36 // For the deserialization example, please refer igtlMessageHeader.h.
37 //
38 // The typical packing procedures using sub-classes of
39 // MessageBase look like the followings
40 //
41 // // Create instance and set Device Name
42 // igtl::TransformMessage::Pointer transMsg;
43 // transMsg = igtl::TransformMessage::New();
44 // transMsg->SetDeviceName("Tracker");
45 //
46 // // Create matrix and substitute values
47 // igtl::Matrix4x4 matrix;
48 // GetRandomTestMatrix(matrix);
49 //
50 // // Set matrix data, serialize, and send it.
51 // transMsg->SetMatrix(matrix);
52 // transMsg->Pack();
53 // socket->Send(transMsg->GetPackPointer(), transMsg->GetPackSize());
54 //
55 //-------------------------------------------------------------------------
56 
57 namespace igtl
58 {
59 
61 {
62 public:
63 
64  typedef MessageBase Self;
65  typedef Object Superclass;
68 
71 
72  enum {
73  UNPACK_UNDEF = 0x0000,
74  UNPACK_HEADER = 0x0001,
75  UNPACK_BODY = 0x0002
76  };
77 
78 public:
79 
80  void SetDeviceName(const char* name);
81  const char* GetDeviceName();
82  const char* GetDeviceType();
83 
84  int SetTimeStamp(unsigned int sec, unsigned int frac);
85  int GetTimeStamp(unsigned int* sec, unsigned int* frac);
86 
87  void SetTimeStamp(igtl::TimeStamp::Pointer& ts);
88  void GetTimeStamp(igtl::TimeStamp::Pointer& ts);
89 
90  // Pack() serializes the header and body based on the member variables.
91  // PackBody() must be implemented in the child class.
92  int Pack();
93 
94  // Unpack() deserializes the header and/or body, extracting data from
95  // the byte stream. If the header has already been deserilized, Unpack()
96  // deserializes only the body part. UnpackBody() must be implemented to
97  // deserialize the body part. Unpack() performs 64-bit CRC check,
98  // when crccheck = 1. It returns:
99  // UNPACK_UNDEF : Nothing deserialized
100  // UNPACK_HEADER : The header has been deserialized.
101  // UNPACK_BODY : The body has been deserialized.
102  // If CRC check fails, Unpack() doesn't
103  // deserialize the body, thus it doesn't
104  // return UNPACK_BODY flag.
105  // UNPACK_HEADER|UNPACK_BODY: Both the header and body have been
106  // deserialized
107  int Unpack(int crccheck = 0);
108 
109  void* GetPackPointer();
110  void* GetPackBodyPointer();
111  int GetPackSize();
112  int GetPackBodySize();
113 
114  const char* GetBodyType() { return this->m_BodyType.c_str(); };
115 
116  // Allocate memory for packing / receiving buffer
117  void AllocatePack();
118 
119  // Call InitPack() before receive header.
120  // This function simply resets the Unpacked flag for both
121  // the header and body pack.
122  void InitPack();
123 
124  // Copy contents from the specified Massage class.
125  // If the type of the specified class is the same as this class,
126  // both general header and body are copied. Otherwise, only
127  // general header is copied.
128  int Copy(const MessageBase* mb);
129 
130  virtual int SetMessageHeader(const MessageHeader* mb) { return Copy(mb); };
131  int GetBodySizeToRead() { return m_BodySizeToRead; };
132 
133 protected:
134  MessageBase();
135  ~MessageBase();
136 
137 protected:
138 
139  // Pack body (must be implemented in a child class)
140  virtual int GetBodyPackSize() { return 0; };
141  virtual int PackBody() { return 0; };
142  virtual int UnpackBody() { return 0; };
143 
144  // Allocate memory specifying the body size
145  // (used when create a brank package to receive data)
146  void AllocatePack(int bodySize);
147 
148  // Copy data
149  int CopyHeader(const MessageBase *mb);
150  int CopyBody(const MessageBase *mb);
151 
152  // Pointers to header and image
153  // To prevent large copy of byte array in Pack() function,
154  // header byte array is concatinated to that of image.
155  // Consequently,
156  // body = header + sizeof (igtl_header)
157  // after these areas are allocated.
158  //
160  unsigned char* m_Header;
161  unsigned char* m_Body;
162 
164 
165  //BTX
166  std::string m_DefaultBodyType;
167  std::string m_BodyType;
168  std::string m_DeviceName;
169  //ETX
170  unsigned int m_TimeStampSec;
172 
173  // Unpacking status (0: -- 1: unpacked)
176 
177 };
178 
179 
180 } // namespace igtl
181 
182 #endif // _igtlMessageBase_h
183 
184 
185 

Generated at Thu Mar 20 2014 16:32:06 for OpenIGTLink by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2000