websocketc: working and without annoying debug messages.

master
Karchnu 2020-11-08 06:27:07 +01:00
parent 6d4db9a8ac
commit 1deee0f7f1
1 changed files with 34 additions and 39 deletions

View File

@ -30,9 +30,17 @@ end
class Baguette::Configuration
class Websocketc < Base
property verbosity : Int32 = 2
property print_ipc_timer : Bool = false
property ipc_timer : Int32 = 30_000
property verbosity : Int32 = 2
property ipc_timer : Int32 = 30_000
property print_ipc_timer : Bool = false
property print_ipc_connection : Bool = false
property print_ipc_disconnection : Bool = false
property print_ipc_extra_socket : Bool = false
property print_ipc_message_received : Bool = false
property print_ipc_message_sent : Bool = false
property print_ipc_switch : Bool = false
def initialize
end
@ -70,10 +78,6 @@ class Relation
end
io << "client #{c} service #{s} #{j}"
end
def finalize
Baguette::Log.warning "TADAAAAA"
end
end
# Hide the complexity of managing relations.
@ -91,12 +95,12 @@ class Relations < Array(Relation)
end
def remove(fd : Int32)
Baguette::Log.warning "context before removing #{fd}:"
Context.service.not_nil!.print_self
# Baguette::Log.warning "context before removing #{fd}:"
# Context.service.not_nil!.print_self
each do |r|
if r.related? fd
Baguette::Log.debug "== Closing this relation: #{r}"
Baguette::Log.debug "Closing this relation: #{r}"
# Removing relations and file descriptors from C structures.
pointer_ctx = Context.service.not_nil!.pointer
@ -105,7 +109,7 @@ class Relations < Array(Relation)
LibIPC.ipc_del_fd pointer_ctx, r.fd_service
# Close these sockets.
Baguette::Log.debug "Closing both #{r.fd_client} and #{r.fd_service} (ws)"
# Baguette::Log.debug "Closing both #{r.fd_client} and #{r.fd_service} (ws)"
begin
s = Socket.new r.fd_client, Socket::Family::UNIX, Socket::Type::RAW
s.close
@ -130,8 +134,8 @@ class Relations < Array(Relation)
end
select! {|r| ! r.related? fd }
Baguette::Log.warning "context after remove"
Context.service.not_nil!.print_self
# Baguette::Log.warning "context after removing #{fd}"
# Context.service.not_nil!.print_self
end
end
@ -139,8 +143,8 @@ require "./network.cr"
def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
Context.service.not_nil!.relations.search?(fd).try do |relation|
Baguette::Log.info "IN fd is #{fd} in relation #{relation}"
Context.service.not_nil!.print_self
# Baguette::Log.info "IN fd is #{fd} in relation #{relation}"
# Context.service.not_nil!.print_self
message = nil
begin
message = relation.ws.run_once
@ -180,26 +184,24 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
ipc_message = IPC::Message.from_json m
ipc_message.copy_to_message_pointer pm
rescue e
Baguette::Log.error "cannot send message coming from #{fd}"
Baguette::Log.error "message: #{m}"
Baguette::Log.error "cannot send message coming from #{fd}, message: #{m}"
Context.service.not_nil!.relations.remove fd
Context.service.not_nil!.print_self
Baguette::Log.error "error: #{e}"
return LibIPC::IPCCB::Error
end
Baguette::Log.warning "no error reassembling the message"
Context.service.not_nil!.print_self
Baguette::Log.debug "no error reassembling the message"
return LibIPC::IPCCB::NoError
end
Baguette::Log.error "cannot handle non-json messages!"
return LibIPC::IPCCB::Error
when WebSocket::Ping
Baguette::Log.debug "TODO: Received a ping message"
return LibIPC::IPCCB::Ignore
when WebSocket::NotFinal
Baguette::Log.warning "Received only part of a message."
Baguette::Log.debug "Received only part of a message"
relation.buffer_client += message.message
return LibIPC::IPCCB::Ignore
@ -240,10 +242,10 @@ end
def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
Context.service.not_nil!.relations.search?(fd).try do |relation|
Baguette::Log.info "OUT fd is #{fd} in relation #{relation}"
Context.service.not_nil!.print_self
# Baguette::Log.info "OUT fd is #{fd} in relation #{relation}"
# Context.service.not_nil!.print_self
message = IPC::Message.new pm
Baguette::Log.info "message to send: #{message}"
# Baguette::Log.info "message to send: #{message}"
if relation.is_json
buf = message.to_json
@ -339,39 +341,30 @@ class Websocketc::Service < IPC::Server
case event
when IPC::Event::Timer
Baguette::Log.debug "Timer" if @config.print_ipc_timer
Baguette::Log.debug "Timer: see the context"
print_self
# print_self
when IPC::Event::Connection
Baguette::Log.info "connection from #{event.fd}"
Baguette::Log.info "connection from #{event.fd}" if @config.print_ipc_connection
when IPC::Event::Disconnection
Baguette::Log.info "disconnection from #{event.fd}"
Baguette::Log.info "disconnection from #{event.fd}" if @config.print_ipc_disconnection
@relations.remove event.fd
# begin
# s = Socket.new event.fd, Socket::Family::UNIX, Socket::Type::RAW
# s.close
# rescue e
# Baguette::Log.warning "cannot close the socket #{event.fd}: #{e} (ignoring the problem)"
# end
when IPC::Event::MessageSent
Baguette::Log.info "message sent to #{event.fd}"
Baguette::Log.info "message sent to #{event.fd}" if @config.print_ipc_message_sent
when IPC::Event::MessageReceived
Baguette::Log.info "message received from #{event.fd}"
Baguette::Log.info "message received from #{event.fd}" if @config.print_ipc_message_received
if r = @relations.search? event.fd
Baguette::Log.error "MessageReceived but from an already existent relation"
Baguette::Log.error "relation: #{r}"
exit 1
else
first_connection event
Baguette::Log.warning "context currently is"
print_self
end
when IPC::Event::Switch
# Baguette::Log.debug "switched message from #{event.fd}"
Baguette::Log.debug "switched message from #{event.fd}" if @config.print_ipc_switch
when IPC::Event::EventNotSet
Baguette::Log.error "Event not set: #{event.fd}"
@ -432,6 +425,8 @@ class Websocketc::Service < IPC::Server
end
end
Baguette::Context.verbosity = configuration.verbosity
if simulation
pp! configuration
exit 0