1111#include " pipe/server_pipe.hpp"
1212
1313#include " ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp"
14- #include " ocvsmd/common/ipc/RouteConnect_1_0 .hpp"
14+ #include " ocvsmd/common/ipc/RouteConnect_0_1 .hpp"
1515#include " ocvsmd/common/ipc/Route_1_0.hpp"
1616#include " uavcan/primitive/Empty_1_0.hpp"
1717
@@ -137,7 +137,7 @@ class ServerRouterImpl final : public ServerRouter
137137 , endpoint_{endpoint}
138138 , next_sequence_{0 }
139139 {
140- ::syslog (LOG_DEBUG, " Gateway(cl=%zu, tag=%zu)." , endpoint.getClientId(), endpoint.getTag());
140+ ::syslog (LOG_DEBUG, " Gateway(cl=%zu, tag=%zu)." , endpoint.getClientId(), endpoint.getTag()); // NOLINT
141141 }
142142
143143 GatewayImpl (const GatewayImpl&) = delete ;
@@ -147,7 +147,7 @@ class ServerRouterImpl final : public ServerRouter
147147
148148 ~GatewayImpl ()
149149 {
150- ::syslog (LOG_DEBUG, " ~Gateway(cl=%zu, tag=%zu)." , endpoint_.getClientId(), endpoint_.getTag());
150+ ::syslog (LOG_DEBUG, " ~Gateway(cl=%zu, tag=%zu)." , endpoint_.getClientId(), endpoint_.getTag()); // NOLINT
151151
152152 router_.onGatewayDisposal (endpoint_);
153153 }
@@ -258,18 +258,22 @@ class ServerRouterImpl final : public ServerRouter
258258 channel_end.tag = endpoint.getTag ();
259259 channel_end.error_code = 0 ; // No error b/c it's a normal channel completion.
260260
261- const int result = tryPerformOnSerialized (route, [this , &endpoint](const auto payload) {
261+ const int error = tryPerformOnSerialized (route, [this , &endpoint](const auto payload) {
262262 //
263263 return server_pipe_->send (endpoint.getClientId (), {{payload}});
264264 });
265265 // Best efforts strategy - gateway anyway is gone, so nowhere to report.
266- (void ) result ;
266+ (void ) error ;
267267 }
268268 }
269269
270- CETL_NODISCARD int handlePipeEvent (const pipe::ServerPipe::Event::Connected conn )
270+ CETL_NODISCARD static int handlePipeEvent (const pipe::ServerPipe::Event::Connected& pipe_conn )
271271 {
272- connected_client_ids_.insert (conn.client_id );
272+ ::syslog (LOG_DEBUG, " Pipe is connected (cl=%zu)." , pipe_conn.client_id); // NOLINT
273+
274+ // It's not enough to consider the client router connected by the pipe event.
275+ // We gonna wait for `RouteConnect` negotiation (see `handleRouteConnect`).
276+ // But for now everything is fine.
273277 return 0 ;
274278 }
275279
@@ -288,7 +292,7 @@ class ServerRouterImpl final : public ServerRouter
288292 cetl::visit (cetl::make_overloaded (
289293 //
290294 [this ](const uavcan::primitive::Empty_1_0&) {},
291- [this , &msg](const RouteConnect_1_0 & route_conn) {
295+ [this , &msg](const RouteConnect_0_1 & route_conn) {
292296 //
293297 handleRouteConnect (msg.client_id , route_conn);
294298 },
@@ -307,27 +311,39 @@ class ServerRouterImpl final : public ServerRouter
307311 return 0 ;
308312 }
309313
310- CETL_NODISCARD static int handlePipeEvent (const pipe::ServerPipe::Event::Disconnected)
314+ CETL_NODISCARD static int handlePipeEvent (const pipe::ServerPipe::Event::Disconnected& disconn )
311315 {
316+ ::syslog (LOG_DEBUG, " Pipe is disconnected (cl=%zu)." , disconn.client_id); // NOLINT
317+
312318 // TODO: Implement! disconnected for all gateways which belong to the corresponding client id
313319 return 0 ;
314320 }
315321
316- void handleRouteConnect (const pipe::ServerPipe::ClientId client_id, const RouteConnect_1_0&) const
322+ void handleRouteConnect (const pipe::ServerPipe::ClientId client_id, const RouteConnect_0_1& rt_conn)
317323 {
318- // TODO: log client route connection
324+ ::syslog (LOG_DEBUG, // NOLINT
325+ " Route connect request (cl=%zu, ver='%d.%d', err=%d)." ,
326+ client_id,
327+ static_cast <int >(rt_conn.version.major),
328+ static_cast<int>(rt_conn.version.minor),
329+ static_cast<int>(rt_conn.error_code));
319330
320331 Route_1_0 route{&memory_};
321-
322- auto & route_conn = route.set_connect ();
332+ auto & route_conn = route.set_connect ();
323333 route_conn.version .major = VERSION_MAJOR;
324334 route_conn.version .minor = VERSION_MINOR;
325-
326- const int result = tryPerformOnSerialized (route, [this , client_id](const auto payload) {
335+ // In the future, we might have version comparison logic here,
336+ // and potentially refuse the connection if the versions are incompatible.
337+ route_conn.error_code = 0 ;
338+ //
339+ const int error = tryPerformOnSerialized (route, [this , client_id](const auto payload) {
327340 //
328341 return server_pipe_->send (client_id, {{payload}});
329342 });
330- (void ) result;
343+ if (0 != error)
344+ {
345+ connected_client_ids_.insert (client_id);
346+ }
331347 }
332348
333349 void handleRouteChannelMsg (const pipe::ServerPipe::ClientId client_id,
@@ -362,7 +378,19 @@ class ServerRouterImpl final : public ServerRouter
362378 // TODO: log unsolicited message
363379 }
364380
365- void handleRouteChannelEnd (const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end) {}
381+ void handleRouteChannelEnd (const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end)
382+ {
383+ ::syslog (LOG_DEBUG, " Route Ch End (tag=%zu, err=%d)." , route_ch_end.tag, route_ch_end.error_code); // NOLINT
384+
385+ const Endpoint endpoint{route_ch_end.tag , client_id};
386+ const auto error_code = static_cast <ErrorCode>(route_ch_end.error_code );
387+
388+ findAndActOnRegisteredGateway (endpoint, [this , error_code](auto & gateway, auto found_it) {
389+ //
390+ endpoint_to_gateway_.erase (found_it);
391+ gateway.event (detail::Gateway::Event::Completed{error_code});
392+ });
393+ }
366394
367395 cetl::pmr::memory_resource& memory_;
368396 pipe::ServerPipe::Ptr server_pipe_;
0 commit comments