Obsolete
/
ipcd
Archived
3
0
Fork 0

More exceptions.

master
Karchnu 2020-11-04 14:24:42 +01:00
parent d9c53342dc
commit 5497011a1d
1 changed files with 45 additions and 13 deletions

View File

@ -31,6 +31,17 @@ class Baguette::Configuration
end end
module IPCd module IPCd
class ServiceUnavailable < ::Exception
end
class NoScheme < ::Exception
end
class NoFileDescriptor < ::Exception
end
class ServiceError < ::Exception
end
class SharingFileDescriptor < ::Exception
end
class Service < IPC::Server class Service < IPC::Server
@rules = RuleSet.new @rules = RuleSet.new
@ -139,7 +150,7 @@ module IPCd
network_service_name = requested_service.scheme network_service_name = requested_service.scheme
if network_service_name.nil? if network_service_name.nil?
raise "no scheme" raise NoScheme.new
end end
# In case of "unix", then it's a simple redirection to do, no network required. # In case of "unix", then it's a simple redirection to do, no network required.
@ -155,12 +166,12 @@ module IPCd
IPC::Client.new network_service_name IPC::Client.new network_service_name
rescue e rescue e
# Better raise message. # Better raise message.
raise "cannot contact #{network_service_name}" raise ServiceUnavailable.new network_service_name
end end
# Will probably never happen in practice. # Will probably never happen in practice.
if service.fd.nil? if service.fd.nil?
raise "no fd for #{network_service_name}" raise NoFileDescriptor.new network_service_name
end end
service_fd = service.fd.not_nil! service_fd = service.fd.not_nil!
@ -175,17 +186,20 @@ module IPCd
response = service.read response = service.read
payload = String.new response.payload payload = String.new response.payload
if payload.chomp != "OK" if payload.chomp != "OK"
raise "service #{network_service_name} response was #{payload.chomp}" raise ServiceError.new "service #{network_service_name} response was #{payload.chomp}"
end end
end end
Baguette::Log.debug "providing the fd from #{network_service_name} to the client #{client_fd}" Baguette::Log.debug "providing the fd from #{network_service_name} to the client #{client_fd}"
# Inform the client that we successfully contacted the service.
send_now client_fd, 3.to_u8, "OK"
# Provide the file descriptor to the client. # Provide the file descriptor to the client.
r = LibIPC.ipc_provide_fd(client_fd, service_fd) r = LibIPC.ipc_provide_fd(client_fd, service_fd)
if r.error_code != 0 if r.error_code != 0
m = String.new r.error_message.to_slice m = String.new r.error_message.to_slice
raise Exception.new "cannot send the file descriptor of the requested service: #{m}" raise SharingFileDescriptor.new "cannot send the file descriptor of the requested service: #{m}"
end end
Baguette::Log.debug "everything went well, closing the service fd" Baguette::Log.debug "everything went well, closing the service fd"
@ -247,19 +261,37 @@ def main
when IPC::Event::MessageReceived when IPC::Event::MessageReceived
Baguette::Log.debug "Message received: #{event.fd}" if config.print_ipc_message_received Baguette::Log.debug "Message received: #{event.fd}" if config.print_ipc_message_received
Baguette::Log.debug event.message.to_s Baguette::Log.debug event.message.to_s
begin was_ok = begin
ipcd.service_lookup event.message, event.fd ipcd.service_lookup event.message, event.fd
true
rescue e : IPCd::ServiceUnavailable
Baguette::Log.error "cannot contact #{e}"
false
rescue e : IPCd::NoScheme
Baguette::Log.error "#{e}"
false
rescue e : IPCd::NoFileDescriptor
Baguette::Log.error "#{e}"
false
rescue e : IPCd::ServiceError
Baguette::Log.error "#{e}"
false
rescue e : IPCd::SharingFileDescriptor
Baguette::Log.error "#{e}"
false
rescue e rescue e
Baguette::Log.error "#{e}" Baguette::Log.error "#{e}"
# when a problem occurs, close the client connection false
begin
# LibIPC.ipc_connections_print pointerof(@connections)
ipcd.remove_fd event.fd
rescue e
Baguette::Log.error "Exception during a client removal: #{e}"
end end
unless was_ok
# Inform the client that we successfully contacted the service.
ipcd.send_now event.fd, 3.to_u8, "NOT OK"
end end
# # Job done: disconnection.
# ipcd.remove_fd event.fd
when IPC::Event::MessageSent when IPC::Event::MessageSent
Baguette::Log.debug "Message sent: #{event.fd}" if config.print_ipc_message_sent Baguette::Log.debug "Message sent: #{event.fd}" if config.print_ipc_message_sent