3
0
Fork 0

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