Line |
Branch |
Exec |
Source |
1 |
|
|
/************************************************************************ |
2 |
|
|
* |
3 |
|
|
* Copyright (C) 2009-2023 IRCAD France |
4 |
|
|
* Copyright (C) 2012-2020 IHU Strasbourg |
5 |
|
|
* |
6 |
|
|
* This file is part of Sight. |
7 |
|
|
* |
8 |
|
|
* Sight is free software: you can redistribute it and/or modify it under |
9 |
|
|
* the terms of the GNU Lesser General Public License as published by |
10 |
|
|
* the Free Software Foundation, either version 3 of the License, or |
11 |
|
|
* (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* Sight is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU Lesser General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU Lesser General Public |
19 |
|
|
* License along with Sight. If not, see <https://www.gnu.org/licenses/>. |
20 |
|
|
* |
21 |
|
|
***********************************************************************/ |
22 |
|
|
|
23 |
|
|
#include "client_sender.hpp" |
24 |
|
|
|
25 |
|
|
#include <core/com/signal.hxx> |
26 |
|
|
#include <core/tools/failed.hpp> |
27 |
|
|
|
28 |
|
|
#include <service/macros.hpp> |
29 |
|
|
|
30 |
|
|
#include <ui/__/dialog/message.hpp> |
31 |
|
|
#include <ui/__/preferences.hpp> |
32 |
|
|
|
33 |
|
|
namespace sight::module::io::igtl |
34 |
|
|
{ |
35 |
|
|
|
36 |
|
|
//----------------------------------------------------------------------------- |
37 |
|
|
|
38 |
|
✗ |
client_sender::client_sender() |
39 |
|
|
= default; |
40 |
|
|
|
41 |
|
|
//----------------------------------------------------------------------------- |
42 |
|
|
|
43 |
|
✗ |
client_sender::~client_sender() |
44 |
|
|
= default; |
45 |
|
|
|
46 |
|
|
//----------------------------------------------------------------------------- |
47 |
|
|
|
48 |
|
✗ |
void client_sender::configuring() |
49 |
|
|
{ |
50 |
|
✗ |
service::config_t config = this->get_config(); |
51 |
|
|
|
52 |
|
✗ |
const config_t config_in = config.get_child("in"); |
53 |
|
|
|
54 |
|
|
SIGHT_ASSERT( |
55 |
|
|
"configured group must be 'objects'", |
56 |
|
|
config_in.get<std::string>("<xmlattr>.group", "") == "objects" |
57 |
|
✗ |
); |
58 |
|
|
|
59 |
|
✗ |
const auto key_cfg = config_in.equal_range("key"); |
60 |
|
✗ |
for(auto it_cfg = key_cfg.first ; it_cfg != key_cfg.second ; ++it_cfg) |
61 |
|
|
{ |
62 |
|
✗ |
const service::config_t& attr = it_cfg->second.get_child("<xmlattr>"); |
63 |
|
✗ |
const std::string name = attr.get("deviceName", "Sight"); |
64 |
|
✗ |
m_device_names.push_back(name); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
✗ |
const std::string server_info = config.get("server", ""); |
68 |
|
✗ |
if(!server_info.empty()) |
69 |
|
|
{ |
70 |
|
✗ |
const std::string::size_type split_position = server_info.find(':'); |
71 |
|
✗ |
SIGHT_ASSERT("Server info not formatted correctly", split_position != std::string::npos); |
72 |
|
|
|
73 |
|
✗ |
m_hostname_config = server_info.substr(0, split_position); |
74 |
|
✗ |
m_port_config = server_info.substr(split_position + 1, server_info.size()); |
75 |
|
|
} |
76 |
|
|
else |
77 |
|
|
{ |
78 |
|
✗ |
throw core::tools::failed("Server element not found"); |
79 |
|
|
} |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
//----------------------------------------------------------------------------- |
83 |
|
|
|
84 |
|
✗ |
void client_sender::starting() |
85 |
|
|
{ |
86 |
|
✗ |
if(!m_client.is_connected()) |
87 |
|
|
{ |
88 |
|
✗ |
try |
89 |
|
|
{ |
90 |
|
✗ |
ui::preferences preferences; |
91 |
|
✗ |
const auto port = preferences.delimited_get<std::uint16_t>(m_port_config); |
92 |
|
✗ |
const auto hostname = preferences.delimited_get<std::string>(m_hostname_config); |
93 |
|
|
|
94 |
|
✗ |
m_client.connect(hostname, port); |
95 |
|
✗ |
m_sig_connected->async_emit(); |
96 |
|
|
} |
97 |
|
✗ |
catch(core::exception& ex) |
98 |
|
|
{ |
99 |
|
✗ |
sight::ui::dialog::message::show("Connection error", ex.what()); |
100 |
|
✗ |
SIGHT_ERROR(ex.what()); |
101 |
|
✗ |
this->slot(service::slots::STOP)->async_run(); |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
//----------------------------------------------------------------------------- |
107 |
|
|
|
108 |
|
✗ |
void client_sender::stopping() |
109 |
|
|
{ |
110 |
|
✗ |
try |
111 |
|
|
{ |
112 |
|
✗ |
if(m_client.is_connected()) |
113 |
|
|
{ |
114 |
|
✗ |
m_client.disconnect(); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
✗ |
m_sig_disconnected->async_emit(); |
118 |
|
|
} |
119 |
|
✗ |
catch(core::exception& e) |
120 |
|
|
{ |
121 |
|
✗ |
sight::ui::dialog::message::show("Error", e.what()); |
122 |
|
✗ |
SIGHT_ERROR(e.what()); |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
//----------------------------------------------------------------------------- |
127 |
|
|
|
128 |
|
✗ |
void client_sender::send_object(const data::object::csptr& _obj, const std::size_t _index) |
129 |
|
|
{ |
130 |
|
✗ |
SIGHT_ASSERT("No device name associated with object index " << _index, _index < m_device_names.size()); |
131 |
|
|
|
132 |
|
✗ |
if(m_client.is_connected()) |
133 |
|
|
{ |
134 |
|
✗ |
m_client.set_device_name_out(m_device_names[_index]); |
135 |
|
✗ |
m_client.send_object(_obj); |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
//----------------------------------------------------------------------------- |
140 |
|
|
|
141 |
|
|
} // namespace sight::module::io::igtl |
142 |
|
|
|