OpenWalnut  1.5.0dev
WIGTLinkRemote.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WIGTLINKREMOTE_H
26 #define WIGTLINKREMOTE_H
27 
28 #include <memory>
29 #include <queue>
30 #include <string>
31 #include <vector>
32 
33 #include "core/common/WCondition.h"
34 #include "core/common/WThreadedRunner.h"
35 #include "core/common/math/WMatrix.h"
36 #include "core/dataHandler/WDataSetScalar.h"
37 #include "igtlClientSocket.h"
38 #include "igtlImageMessage.h"
39 #include "igtlMessageHeader.h"
40 
41 class WDataSet;
42 class WDataSetScalar;
43 class WValueSetBase;
44 
45 // there is no WDataSetScalar::SPtr definition, yet
46 // so I use this workaround here for better readability
47 typedef std::shared_ptr<WDataSetScalar> WDataSetScalarSPtr;
48 
49 /**
50  * Common base class for a IGT Link connection.
51  * Provides some helper functions to handle and convert OW data.
52  */
54 {
55 public:
57 
58  virtual ~WIGTLinkRemote();
59 
60  /**
61  * Use this as a server that waits for client connections.
62  *
63  * \param port Port number for the connection
64  */
65  void createSocketAndWaitForConnection( uint32_t port );
66 
67  /**
68  * Use this as a client that connects to a remote server
69  *
70  * \param server Name of the remote server.
71  * \param port Port number for the connection.
72  */
73  void createSocketAndConnect( std::string server, uint32_t port );
74 
75  /**
76  * main loop for reading
77  */
78  void readDataLoop();
79 
80  /**
81  * Receive a transform
82  *
83  * \param headerMsg The message conatining the transform.
84  */
85  void receiveTransform( igtl::MessageHeader::Pointer headerMsg );
86 
87  /**
88  * Receive an image
89  *
90  * \param headerMsg The image in this message is used to create a dataset
91  * \return Dataset created from message.
92  */
93  WDataSetScalarSPtr receiveImage( igtl::MessageHeader::Pointer headerMsg );
94 
95  /**
96  * TODO: inject a message in the send queue
97  */
98  void injectMessage();
99 
100  /**
101  * Send metadata of a list of data sets
102  *
103  * \param dataSets A vector containing pointer to the datasets whose metadata we want to send.
104  */
105  void sendImageMetaData( const std::vector<WDataSetScalarSPtr>& dataSets );
106 
107  /**
108  * Send image data of a single data set
109  *
110  * \param dataSetScalar Pointer to the dataset we want to send
111  */
112  void sendImageData( WDataSetScalarSPtr dataSetScalar );
113 
114  /**
115  * send a matrix as an igtl transform
116  * \param name: the name to use for the transform
117  * \param matrix: the matrix that will be send
118  * \pre: socket is valid. If not, nothing will be sent and no error will be reported.
119  */
120  void sendTransform( const std::string& name, const WMatrix <double> & matrix );
121 
122  /**
123  * Internal helper to create a value set from a message
124  *
125  * \param imgMsg The image behind this pointer is used to create the dataset
126  * \return Created value set.
127  */
128  std::shared_ptr < WValueSetBase > createValueSet( const igtl::ImageMessage::Pointer& imgMsg );
129 
130  /**
131  * Set whether we check the CRC. Otherwise, we just ignore it while unpacking the data.
132  * The default for checkCRC is false, so there is no CRC checking when this function
133  * has not been called.
134  *
135  * \param doCheckCRC Should CRC of the data be checked?
136  */
137  void setCheckCRC( bool doCheckCRC = true )
138  {
139  checkCRC = doCheckCRC;
140  }
141 
142  /**
143  * setup listening socket and listen
144  */
145  void listenLoop();
146 
147  /**
148  * Send a message containing the metadata of the data sets
149  *
150  * \param dataSets A vector containing pointers to the datasets whose metadata we want to send.
151  */
152  void sendImageMetaData( std::vector < WDataSetScalarSPtr > dataSets );
153 
154  /**
155  * mutex has to be locked during access to receiveQueue
156  */
157  boost::mutex receiversMutex;
158 
159  /**
160  * condition to notify receivers that new data is waiting
161  */
162  std::shared_ptr < WCondition > receiversCondition;
163 
164  /**
165  * condition to notify a status change
166  */
167  std::shared_ptr < WCondition > statusCondition;
168 
169  /**
170  * queue of received data sets that should be read by the module
171  */
172  std::queue < WDataSetScalarSPtr > receiveQueue;
173 
174 protected:
175  /**
176  * the main thread doing passive connections or receiving data
177  */
178  virtual void threadMain();
179 
180  /** true if we check CRC sums in incoming packets */
181  bool checkCRC;
182 
183  /** the port for listening connections */
184  uint32_t port;
185 private:
186  /**
187  * Points to the socket used by this connection.
188  */
189  igtl::ClientSocket::Pointer socket;
190 };
191 
192 #endif // WIGTLINKREMOTE_H
This data set type contains scalars as values.
Base class for all data set types.
Definition: WDataSet.h:50
Common base class for a IGT Link connection.
void createSocketAndWaitForConnection(uint32_t port)
Use this as a server that waits for client connections.
igtl::ClientSocket::Pointer socket
Points to the socket used by this connection.
void receiveTransform(igtl::MessageHeader::Pointer headerMsg)
Receive a transform.
std::shared_ptr< WCondition > receiversCondition
condition to notify receivers that new data is waiting
bool checkCRC
true if we check CRC sums in incoming packets
void sendTransform(const std::string &name, const WMatrix< double > &matrix)
send a matrix as an igtl transform
std::shared_ptr< WValueSetBase > createValueSet(const igtl::ImageMessage::Pointer &imgMsg)
Internal helper to create a value set from a message.
void readDataLoop()
main loop for reading
std::shared_ptr< WCondition > statusCondition
condition to notify a status change
boost::mutex receiversMutex
mutex has to be locked during access to receiveQueue
void sendImageMetaData(std::vector< WDataSetScalarSPtr > dataSets)
Send a message containing the metadata of the data sets.
uint32_t port
the port for listening connections
void setCheckCRC(bool doCheckCRC=true)
Set whether we check the CRC.
WDataSetScalarSPtr receiveImage(igtl::MessageHeader::Pointer headerMsg)
Receive an image.
void injectMessage()
TODO: inject a message in the send queue.
void createSocketAndConnect(std::string server, uint32_t port)
Use this as a client that connects to a remote server.
virtual void threadMain()
the main thread doing passive connections or receiving data
void sendImageMetaData(const std::vector< WDataSetScalarSPtr > &dataSets)
Send metadata of a list of data sets.
void sendImageData(WDataSetScalarSPtr dataSetScalar)
Send image data of a single data set.
void listenLoop()
setup listening socket and listen
std::queue< WDataSetScalarSPtr > receiveQueue
queue of received data sets that should be read by the module
Base class for all classes needing to be executed in a separate thread.
Abstract base class to all WValueSets.
Definition: WValueSetBase.h:60