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.
00001 /*=========================================================================
00002 
00003   Program:   Open IGT Link Library
00004   Module:    $HeadURL: http://svn.na-mic.org/NAMICSandBox/trunk/OpenIGTLink/Source/igtlMessageBase.h $
00005   Language:  C++
00006   Date:      $Date: 2009-01-13 14:11:16 -0500 (Tue, 13 Jan 2009) $
00007   Version:   $Revision: 3544 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010 
00011   This software is distributed WITHOUT ANY WARRANTY; without even
00012   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00013   PURPOSE.  See the above copyright notices for more information.
00014 
00015 =========================================================================*/
00016 
00017 #ifndef __igtlMessageBase_h
00018 #define __igtlMessageBase_h
00019 
00020 #include "igtlObject.h"
00021 #include "igtlObjectFactory.h"
00022 //#include "igtlMacros.h"
00023 #include "igtlMacro.h"
00024 #include "igtlMath.h"
00025 #include "igtlTimeStamp.h"
00026 
00027 #include "igtlMessageHeader.h"
00028 
00029 #include <string>
00030 
00031 //-------------------------------------------------------------------------
00032 // The MessageBase class is the base class of all message type classes
00033 // used in the Open IGT Link Library. The message classes can be used
00034 // both for serializing (packing) Open IGT Link message byte streams.
00035 // The class can also deserializing (unpacking) Open IGT Link.
00036 // For the deserialization example, please refer igtlMessageHeader.h.
00037 // 
00038 // The typical packing procedures using sub-classes of
00039 // MessageBase look like the followings
00040 //
00041 //     // Create instance and set Device Name
00042 //     igtl::TransformMessage::Pointer transMsg;
00043 //     transMsg = igtl::TransformMessage::New();
00044 //     transMsg->SetDeviceName("Tracker");
00045 //
00046 //     // Create matrix and substitute values
00047 //     igtl::Matrix4x4 matrix;
00048 //     GetRandomTestMatrix(matrix);
00049 //
00050 //     // Set matrix data, serialize, and send it.
00051 //     transMsg->SetMatrix(matrix);
00052 //     transMsg->Pack();
00053 //     socket->Send(transMsg->GetPackPointer(), transMsg->GetPackSize());
00054 //
00055 //-------------------------------------------------------------------------
00056 
00057 namespace igtl
00058 {
00059 
00060 class IGTLCommon_EXPORT MessageBase: public Object
00061 {
00062 public:
00063 
00064   typedef MessageBase               Self;
00065   typedef Object                    Superclass;
00066   typedef SmartPointer<Self>        Pointer;
00067   typedef SmartPointer<const Self>  ConstPointer;
00068 
00069   igtlTypeMacro(igtl::MessageBase, igtl::Object)
00070   igtlNewMacro(igtl::MessageBase);
00071 
00072   enum {
00073     UNPACK_UNDEF   = 0x0000,
00074     UNPACK_HEADER  = 0x0001,
00075     UNPACK_BODY    = 0x0002
00076   };
00077 
00078 public:
00079 
00080   void  SetDeviceName(const char* name);
00081   const char* GetDeviceName();
00082   const char* GetDeviceType();
00083   
00084   int   SetTimeStamp(unsigned int sec, unsigned int frac);
00085   int   GetTimeStamp(unsigned int* sec, unsigned int* frac);
00086 
00087   void  SetTimeStamp(igtl::TimeStamp::Pointer& ts);
00088   void  GetTimeStamp(igtl::TimeStamp::Pointer& ts);
00089 
00090   // Pack() serializes the header and body based on the member variables.
00091   // PackBody() must be implemented in the child class.
00092   int   Pack();
00093 
00094   // Unpack() deserializes the header and/or body, extracting data from
00095   // the byte stream. If the header has already been deserilized, Unpack()
00096   // deserializes only the body part. UnpackBody() must be implemented to
00097   // deserialize the body part. Unpack() performs 64-bit CRC check,
00098   // when crccheck = 1. It returns:
00099   //      UNPACK_UNDEF             : Nothing deserialized
00100   //      UNPACK_HEADER            : The header has been deserialized.
00101   //      UNPACK_BODY              : The body has been deserialized.
00102   //                                 If CRC check fails, Unpack() doesn't
00103   //                                 deserialize the body, thus it doesn't
00104   //                                 return UNPACK_BODY flag.
00105   //      UNPACK_HEADER|UNPACK_BODY: Both the header and body have been
00106   //                                 deserialized
00107   int   Unpack(int crccheck = 0);
00108 
00109   void* GetPackPointer();
00110   void* GetPackBodyPointer();
00111   int   GetPackSize();
00112   int   GetPackBodySize();
00113 
00114   const char* GetBodyType() { return this->m_BodyType.c_str(); };
00115 
00116   // Allocate memory for packing / receiving buffer
00117   void AllocatePack();
00118 
00119   // Call InitPack() before receive header. 
00120   // This function simply resets the Unpacked flag for both
00121   // the header and body pack.
00122   void InitPack();
00123 
00124   // Copy contents from the specified Massage class.
00125   // If the type of the specified class is the same as this class,
00126   // both general header and body are copied. Otherwise, only
00127   // general header is copied.
00128   int Copy(const MessageBase* mb);
00129 
00130   virtual int SetMessageHeader(const MessageHeader* mb) { return Copy(mb); };
00131   int GetBodySizeToRead()                       { return m_BodySizeToRead; };
00132   
00133 protected:
00134   MessageBase();
00135   ~MessageBase();
00136 
00137 protected:
00138 
00139   // Pack body (must be implemented in a child class)
00140   virtual int  GetBodyPackSize() { return 0; };
00141   virtual int  PackBody()        { return 0; };
00142   virtual int  UnpackBody()      { return 0; };
00143 
00144   // Allocate memory specifying the body size
00145   // (used when create a brank package to receive data)
00146   void AllocatePack(int bodySize);
00147 
00148   // Copy data 
00149   int CopyHeader(const MessageBase *mb);
00150   int CopyBody(const MessageBase *mb);
00151 
00152   // Pointers to header and image
00153   //  To prevent large copy of byte array in Pack() function,
00154   //  header byte array is concatinated to that of image.
00155   //  Consequently,
00156   //    body = header + sizeof (igtl_header)
00157   //  after these areas are allocated.
00158   //
00159   int            m_PackSize;
00160   unsigned char* m_Header;
00161   unsigned char* m_Body;
00162 
00163   int            m_BodySizeToRead;
00164 
00165   //BTX
00166   std::string    m_DefaultBodyType;
00167   std::string    m_BodyType;
00168   std::string    m_DeviceName;
00169   //ETX
00170   unsigned int   m_TimeStampSec;
00171   unsigned int   m_TimeStampSecFraction;
00172 
00173   // Unpacking status (0: --   1: unpacked)
00174   int            m_IsHeaderUnpacked;
00175   int            m_IsBodyUnpacked;
00176 
00177 };
00178 
00179 
00180 } // namespace igtl
00181 
00182 #endif // _igtlMessageBase_h
00183 
00184 
00185 

Generated at Tue May 11 19:42:18 2010 for OpenIGTLink by doxygen 1.6.3 written by Dimitri van Heesch, © 1997-2000