File: server_sender.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (141 lines) | stat: -rw-r--r-- 4,113 bytes parent folder | download | duplicates (3)
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
/************************************************************************
 *
 * Copyright (C) 2009-2023 IRCAD France
 * Copyright (C) 2012-2020 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/>.
 *
 ***********************************************************************/

#include "server_sender.hpp"

#include <core/com/signal.hxx>
#include <core/com/slots.hpp>
#include <core/com/slots.hxx>

#include <service/macros.hpp>

#include <ui/__/dialog/message.hpp>
#include <ui/__/preferences.hpp>

#include <functional>

namespace sight::module::io::igtl
{

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

server_sender::server_sender() :
    m_server(std::make_shared<sight::io::igtl::server>())
{
}

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

server_sender::~server_sender()
= default;

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

void server_sender::configuring()
{
    service::config_t config = this->get_config();

    m_port_config = config.get("port", "4242");

    const config_t config_in = config.get_child("in");

    SIGHT_ASSERT(
        "configured group must be 'objects'",
        config_in.get<std::string>("<xmlattr>.group", "") == "objects"
    );

    const auto key_cfg = config_in.equal_range("key");
    for(auto it_cfg = key_cfg.first ; it_cfg != key_cfg.second ; ++it_cfg)
    {
        const service::config_t& attr = it_cfg->second.get_child("<xmlattr>");
        const std::string device_name = attr.get("deviceName", "Sight");
        m_device_names.push_back(device_name);
    }
}

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

void server_sender::starting()
{
    try
    {
        ui::preferences preferences;
        const auto port = preferences.delimited_get<std::uint16_t>(m_port_config);

        m_server->start(port);

        m_server_future = std::async(std::launch::async, [this](auto&& ...){m_server->run_server();});
        m_sig_connected->async_emit();
    }
    catch(core::exception& e)
    {
        sight::ui::dialog::message::show(
            "Error",
            "Cannot start the server: "
            + std::string(e.what()),
            sight::ui::dialog::message::critical
        );
        // Only report the error on console (this normally happens only if we have requested the disconnection)
        SIGHT_ERROR(e.what());
        this->slot(service::slots::STOP)->async_run();
    }
}

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

void server_sender::stopping()
{
    try
    {
        if(m_server->started())
        {
            m_server->stop();
        }

        m_server_future.wait();
        m_sig_disconnected->async_emit();
    }
    catch(core::exception& e)
    {
        sight::ui::dialog::message::show("Error", e.what());
    }
    catch(std::future_error&)
    {
        // This happens when the server failed to start, so we just ignore it silently.
    }
}

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

void server_sender::send_object(const data::object::csptr& _obj, const std::size_t _index)
{
    if(!m_device_names[_index].empty())
    {
        m_server->set_message_device_name(m_device_names[_index]);
    }

    m_server->broadcast(_obj);
}

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

} // namespace sight::module::io::igtl