File: client_sender.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • 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,315 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 "client_sender.hpp"

#include <core/com/signal.hxx>
#include <core/tools/failed.hpp>

#include <service/macros.hpp>

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

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

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

client_sender::client_sender()
= default;

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

client_sender::~client_sender()
= default;

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

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

    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 name        = attr.get("deviceName", "Sight");
        m_device_names.push_back(name);
    }

    const std::string server_info = config.get("server", "");
    if(!server_info.empty())
    {
        const std::string::size_type split_position = server_info.find(':');
        SIGHT_ASSERT("Server info not formatted correctly", split_position != std::string::npos);

        m_hostname_config = server_info.substr(0, split_position);
        m_port_config     = server_info.substr(split_position + 1, server_info.size());
    }
    else
    {
        throw core::tools::failed("Server element not found");
    }
}

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

void client_sender::starting()
{
    if(!m_client.is_connected())
    {
        try
        {
            ui::preferences preferences;
            const auto port     = preferences.delimited_get<std::uint16_t>(m_port_config);
            const auto hostname = preferences.delimited_get<std::string>(m_hostname_config);

            m_client.connect(hostname, port);
            m_sig_connected->async_emit();
        }
        catch(core::exception& ex)
        {
            sight::ui::dialog::message::show("Connection error", ex.what());
            SIGHT_ERROR(ex.what());
            this->slot(service::slots::STOP)->async_run();
        }
    }
}

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

void client_sender::stopping()
{
    try
    {
        if(m_client.is_connected())
        {
            m_client.disconnect();
        }

        m_sig_disconnected->async_emit();
    }
    catch(core::exception& e)
    {
        sight::ui::dialog::message::show("Error", e.what());
        SIGHT_ERROR(e.what());
    }
}

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

void client_sender::send_object(const data::object::csptr& _obj, const std::size_t _index)
{
    SIGHT_ASSERT("No device name associated with object index " << _index, _index < m_device_names.size());

    if(m_client.is_connected())
    {
        m_client.set_device_name_out(m_device_names[_index]);
        m_client.send_object(_obj);
    }
}

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

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