File: server.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (198 lines) | stat: -rw-r--r-- 5,246 bytes parent folder | download | duplicates (2)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/************************************************************************
 *
 * Copyright (C) 2014-2024 IRCAD France
 * Copyright (C) 2014-2019 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#pragma once

#include "io/igtl/client.hpp"
#include <sight/io/igtl/config.hpp>
#include "io/igtl/network.hpp"
// Patched header.
#include "io/igtl/patch/igtlSocket.h"

#include <core/exception.hpp>
#include <core/mt/types.hpp>

#include <igtlServerSocket.h>

#include <list>
#include <string>
#include <vector>
#include <optional>

namespace sight::io::igtl
{

/**
 *
 * @brief a network server class use igtl::ServerSocket
 */
class SIGHT_IO_IGTL_CLASS_API server : public io::igtl::network
{
public:

    using sptr = std::shared_ptr<server>;

    /**
     * @brief constructor
     */
    SIGHT_IO_IGTL_API server();

    /**
     * @brief destructor
     */
    SIGHT_IO_IGTL_API ~server() override;

    /**
     * @brief method to start the server on a port
     *
     * @param[in] _port the port to listen
     */
    SIGHT_IO_IGTL_API void start(std::uint16_t _port);

    /**
     * @brief method to wait a connection
     *
     * @param[in] _msec time to wait in millisecond
     */
    SIGHT_IO_IGTL_API client::sptr wait_for_connection(int _msec = 0);

    /**
     * @brief method to stop the server
     */
    SIGHT_IO_IGTL_API void stop();

    /**
     * @brief method to test if the server started
     */
    SIGHT_IO_IGTL_API bool started() const;

    /**
     * @brief method to broadcast to all client the obj
     */
    SIGHT_IO_IGTL_API void broadcast(const data::object::csptr& _obj);

    /**
     * @brief method to broadcast to all client a msg
     */
    SIGHT_IO_IGTL_API void broadcast(::igtl::MessageBase::Pointer _msg);

    /**
     * @brief get the port
     *
     * @return the port listened by server instance
     */
    SIGHT_IO_IGTL_API std::uint16_t get_port() const;

    /**
     * @brief method to run server and start event loop of server
     */
    SIGHT_IO_IGTL_API void run_server();

    /**
     * @brief method to have the current number of clients
     */
    SIGHT_IO_IGTL_API std::size_t num_clients() const;

    /**
     * @brief method to receive all headers of all connected clients
     *
     * @return vector of igl::MessageHeader::Pointer
     */
    SIGHT_IO_IGTL_API std::vector< ::igtl::MessageHeader::Pointer> receive_headers();

    /** @brief receive body pack of a specific connected client
     *
     *  @param[in] _header msg header
     *  @param[in] _client num
     *
     *  @return Message
     */
    SIGHT_IO_IGTL_API ::igtl::MessageBase::Pointer receive_body(
        ::igtl::MessageHeader::Pointer _header,
        unsigned int _client
    );

    /**
     * @brief method to retrieve vector of received object from all connected clients
     *
     * @return a vector of smart pointer of fwData object
     */
    SIGHT_IO_IGTL_API std::vector<data::object::sptr> receive_objects(std::vector<std::string>& _device_names);

    /**
     * @brief set the device name when a message is sent
     */
    SIGHT_IO_IGTL_API void set_message_device_name(const std::string& _device_name);

    /// Sets the receive timeout in ms.
    inline void set_receive_timeout(std::optional<unsigned int> _timeout);

    /// Gets the current receive timeout.
    inline std::optional<int> get_receive_timeout() const;

private:

    /// Patched version of igtlServer::CreateServer.
    int create_server(std::uint16_t _port);

    static void remove_client(client::sptr _client);

    /// server socket
    ::igtl::ServerSocket::Pointer m_server_socket;

    ///mutex
    mutable core::mt::mutex m_mutex;

    /// state of server
    bool m_is_started {false};

    /// vector of clients
    std::vector<client::sptr> m_clients;

    /// Server port
    std::uint16_t m_port {0};

    /// integer constant for success
    static const int SUCCESS = 0;

    /// device name in the sended message
    std::string m_device_name_out;

    /// Optional timeout for receiving message from clients
    std::optional<unsigned int> m_receive_timeout;
};

//------------------------------------------------------------------------------

inline void server::set_receive_timeout(std::optional<unsigned int> _timeout)
{
    m_receive_timeout = _timeout;
}

//------------------------------------------------------------------------------

inline std::optional<int> server::get_receive_timeout() const
{
    return m_receive_timeout;
}

} // namespace sight::io::igtl