~ubuntu-branches/ubuntu/raring/openwalnut/raring

« back to all changes in this revision

Viewing changes to src/modules/openIGTLink/WIGTLinkRemote.h

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2012-12-12 11:26:32 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20121212112632-xhiuwkxuz5h0idkh
Tags: 1.3.1+hg5849-1
* Minor changes compared to 1.3.0 but included several bug fixes.
* See http://www.openwalnut.org/versions/4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <vector>
 
29
#include <queue>
 
30
#include <string>
 
31
 
 
32
#include "core/common/WCondition.h"
 
33
 
 
34
#include "core/common/WThreadedRunner.h"
 
35
 
 
36
#include "core/common/math/WMatrix.h"
 
37
 
 
38
#include "igtlMessageHeader.h"
 
39
#include "igtlImageMessage.h"
 
40
#include "igtlClientSocket.h"
 
41
#include "core/dataHandler/WDataSetScalar.h"
 
42
 
 
43
class WDataSet;
 
44
class WDataSetScalar;
 
45
class WValueSetBase;
 
46
 
 
47
// there is no WDataSetScalar::SPtr definition, yet
 
48
// so I use this workaround here for better readability
 
49
typedef boost::shared_ptr<WDataSetScalar> WDataSetScalarSPtr;
 
50
 
 
51
/**
 
52
 * Common base class for a IGT Link connection.
 
53
 * Provides some helper functions to handle and convert OW data.
 
54
 */
 
55
class WIGTLinkRemote : public WThreadedRunner
 
56
{
 
57
public:
 
58
    WIGTLinkRemote();
 
59
 
 
60
    virtual ~WIGTLinkRemote();
 
61
 
 
62
    /**
 
63
     * Use this as a server that waits for client connections.
 
64
     *
 
65
     * \param port Port number for the connection
 
66
     */
 
67
    void createSocketAndWaitForConnection( uint32_t port );
 
68
 
 
69
    /**
 
70
     * Use this as a client that connects to a remote server
 
71
     *
 
72
     * \param server Name of the remote server.
 
73
     * \param port Port number for the connection.
 
74
     */
 
75
    void createSocketAndConnect( std::string server, uint32_t port );
 
76
 
 
77
    /**
 
78
     * main loop for reading
 
79
     */
 
80
    void readDataLoop();
 
81
 
 
82
    /**
 
83
     * Receive a transform
 
84
     *
 
85
     * \param headerMsg The message conatining the transform.
 
86
     */
 
87
    void receiveTransform( igtl::MessageHeader::Pointer headerMsg );
 
88
 
 
89
    /**
 
90
     * Receive an image
 
91
     *
 
92
     * \param headerMsg The image in this message is used to create a dataset
 
93
     * \return Dataset created from message.
 
94
     */
 
95
    WDataSetScalarSPtr receiveImage( igtl::MessageHeader::Pointer headerMsg );
 
96
 
 
97
    /**
 
98
     * TODO: inject a message in the send queue
 
99
     */
 
100
    void injectMessage();
 
101
 
 
102
    /**
 
103
     * Send metadata of a list of data sets
 
104
     *
 
105
     * \param dataSets A vector containing pointer to the datasets whose metadata we want to send.
 
106
     */
 
107
    void sendImageMetaData( const std::vector<WDataSetScalarSPtr>& dataSets );
 
108
 
 
109
    /**
 
110
     * Send image data of a single data set
 
111
     *
 
112
     * \param dataSetScalar Pointer to the dataset we want to send
 
113
     */
 
114
    void sendImageData( WDataSetScalarSPtr dataSetScalar );
 
115
 
 
116
    /**
 
117
     * send a matrix as an igtl transform
 
118
     * \param name: the name to use for the transform
 
119
     * \param matrix: the matrix that will be send
 
120
     * \pre: socket is valid. If not, nothing will be sent and no error will be reported.
 
121
     */
 
122
    void sendTransform( const std::string& name, const WMatrix <double> & matrix );
 
123
 
 
124
    /**
 
125
     * Internal helper to create a value set from a message
 
126
     *
 
127
     * \param imgMsg The image behind this pointer is used to create the dataset
 
128
     * \return Created value set.
 
129
     */
 
130
    boost::shared_ptr < WValueSetBase > createValueSet( const igtl::ImageMessage::Pointer& imgMsg );
 
131
 
 
132
    /**
 
133
     * Set whether we check the CRC. Otherwise, we just ignore it while unpacking the data.
 
134
     * The default for checkCRC is false, so there is no CRC checking when this function
 
135
     * has not been called.
 
136
     *
 
137
     * \param doCheckCRC Should CRC of the data be checked?
 
138
     */
 
139
    void setCheckCRC( bool doCheckCRC = true )
 
140
    {
 
141
        checkCRC = doCheckCRC;
 
142
    }
 
143
 
 
144
    /**
 
145
     * setup listening socket and listen
 
146
     */
 
147
    void listenLoop();
 
148
 
 
149
    /**
 
150
     * Send a message containing the metadata of the data sets
 
151
     *
 
152
     * \param dataSets A vector containing pointers to the datasets whose metadata we want to send.
 
153
     */
 
154
    void sendImageMetaData( std::vector < WDataSetScalarSPtr > dataSets );
 
155
 
 
156
    /**
 
157
     * mutex has to be locked during access to receiveQueue
 
158
     */
 
159
    boost::mutex receiversMutex;
 
160
 
 
161
    /**
 
162
     * condition to notify receivers that new data is waiting
 
163
     */
 
164
    boost::shared_ptr < WCondition > receiversCondition;
 
165
 
 
166
    /**
 
167
     * condition to notify a status change
 
168
     */
 
169
    boost::shared_ptr < WCondition > statusCondition;
 
170
 
 
171
    /**
 
172
     * queue of received data sets that should be read by the module
 
173
     */
 
174
    std::queue < WDataSetScalarSPtr > receiveQueue;
 
175
 
 
176
protected:
 
177
    /**
 
178
     * the main thread doing passive connections or receiving data
 
179
     */
 
180
    virtual void threadMain();
 
181
 
 
182
    /** true if we check CRC sums in incoming packets */
 
183
    bool checkCRC;
 
184
 
 
185
    /** the port for listening connections */
 
186
    uint32_t port;
 
187
private:
 
188
    /**
 
189
     * Points to the socket used by this connection.
 
190
     */
 
191
    igtl::ClientSocket::Pointer socket;
 
192
};
 
193
 
 
194
#endif  // WIGTLINKREMOTE_H