More exceptions.
parent
d9c53342dc
commit
5497011a1d
56
src/ipcd.cr
56
src/ipcd.cr
|
@ -31,6 +31,17 @@ class Baguette::Configuration
|
|||
end
|
||||
|
||||
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
|
||||
|
||||
@rules = RuleSet.new
|
||||
|
@ -139,7 +150,7 @@ module IPCd
|
|||
network_service_name = requested_service.scheme
|
||||
|
||||
if network_service_name.nil?
|
||||
raise "no scheme"
|
||||
raise NoScheme.new
|
||||
end
|
||||
|
||||
# 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
|
||||
rescue e
|
||||
# Better raise message.
|
||||
raise "cannot contact #{network_service_name}"
|
||||
raise ServiceUnavailable.new network_service_name
|
||||
end
|
||||
|
||||
# Will probably never happen in practice.
|
||||
if service.fd.nil?
|
||||
raise "no fd for #{network_service_name}"
|
||||
raise NoFileDescriptor.new network_service_name
|
||||
end
|
||||
|
||||
service_fd = service.fd.not_nil!
|
||||
|
@ -175,17 +186,20 @@ module IPCd
|
|||
response = service.read
|
||||
payload = String.new response.payload
|
||||
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
|
||||
|
||||
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.
|
||||
r = LibIPC.ipc_provide_fd(client_fd, service_fd)
|
||||
if r.error_code != 0
|
||||
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
|
||||
|
||||
Baguette::Log.debug "everything went well, closing the service fd"
|
||||
|
@ -247,19 +261,37 @@ def main
|
|||
when IPC::Event::MessageReceived
|
||||
Baguette::Log.debug "Message received: #{event.fd}" if config.print_ipc_message_received
|
||||
Baguette::Log.debug event.message.to_s
|
||||
begin
|
||||
was_ok = begin
|
||||
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
|
||||
Baguette::Log.error "#{e}"
|
||||
# when a problem occurs, close the client connection
|
||||
begin
|
||||
# LibIPC.ipc_connections_print pointerof(@connections)
|
||||
ipcd.remove_fd event.fd
|
||||
rescue e
|
||||
Baguette::Log.error "Exception during a client removal: #{e}"
|
||||
false
|
||||
end
|
||||
|
||||
unless was_ok
|
||||
# Inform the client that we successfully contacted the service.
|
||||
ipcd.send_now event.fd, 3.to_u8, "NOT OK"
|
||||
end
|
||||
|
||||
# # Job done: disconnection.
|
||||
# ipcd.remove_fd event.fd
|
||||
|
||||
when IPC::Event::MessageSent
|
||||
Baguette::Log.debug "Message sent: #{event.fd}" if config.print_ipc_message_sent
|
||||
|
||||
|
|
Reference in New Issue