From b6eed13d4e98bb0c10e717b7ae4f52964aa8176f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attilio=20Don=C3=A0?= Date: Fri, 19 Apr 2024 18:36:59 +0200 Subject: [PATCH] Better error handling in case of not unique component name --- src/Rembus.jl | 16 +++++++++++++--- src/broker.jl | 5 ++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Rembus.jl b/src/Rembus.jl index 06c42a5..1a6568d 100644 --- a/src/Rembus.jl +++ b/src/Rembus.jl @@ -111,6 +111,10 @@ Base.@kwdef struct RembusError <: RembusException reason::Union{String,Nothing} = nothing end +struct AlreadyConnected <: RembusException + cid::String +end + """ `RpcMethodNotFound` is thrown from a rpc request when the called method is unknown. @@ -1044,6 +1048,12 @@ function rembus_task(pd, rb, protocol=:ws) end end catch e + if isa(e, AlreadyConnected) + @error "[$(e.cid)] already connected" + Rembus.CONFIG.cid = "rembus" + return + end + if isa(e, HTTP.Exceptions.ConnectError) msg = "[$pd]: $(e.url) connection error" else @@ -1065,8 +1075,7 @@ end mutable struct NullProcess <: Visor.Supervised id::String - inbo - x::Channel + inbox::Channel NullProcess(id) = new(id, Channel(1)) end @@ -1472,13 +1481,14 @@ function authenticate(rb) if isa(response, RembusTimeout) close(rb.socket) throw(response) + elseif (response.status == STS_GENERIC_ERROR) + throw(AlreadyConnected(rb.client.id)) elseif (response.status == STS_CHALLENGE) msg = attestate(rb, response) response = wait_response(rb, msg, request_timeout()) end if (response.status != STS_SUCCESS) - # Avoid DOS attack: this has to be the server work!! close(rb.socket) rembuserror(code=response.status, reason=reason) end diff --git a/src/broker.jl b/src/broker.jl index c1049e2..4994ba4 100644 --- a/src/broker.jl +++ b/src/broker.jl @@ -687,7 +687,10 @@ function anonymous_twin_receiver(router, twin) named = named_twin(msg.cid, router) if named !== nothing && !offline(named) @warn "a component with id [$(msg.cid)] is already connected" - close(ws) + ## close(ws) + transport_send(twin, ws, ResMsg( + msg.id, STS_GENERIC_ERROR, "already connected" + )) else # check if cid is registered rembus_login = isfile(joinpath(CONFIG.db, "apps", msg.cid))