3
0
Fork 0

New logging system.

dev
Karchnu 2020-08-24 16:57:02 +02:00
parent 4ba0b805a2
commit b171f90e92
1 changed files with 57 additions and 46 deletions

View File

@ -3,6 +3,8 @@ require "ipc"
require "socket" require "socket"
require "./colors" require "./colors"
require "colorize"
require "json" require "json"
require "socket" require "socket"
@ -16,7 +18,7 @@ require "./instance_storage.cr"
# All modifications to standard libraries go there. # All modifications to standard libraries go there.
require "./lib_modifications.cr" require "./lib_modifications.cr"
class CLI class Context
# service instance parameters # service instance parameters
# they can be changed via the cli # they can be changed via the cli
class_property service_name : String = "websocket" class_property service_name : String = "websocket"
@ -26,25 +28,40 @@ class CLI
class_property verbosity : Int32 = 1 class_property verbosity : Int32 = 1
end end
class Log
def self.debug(message)
STDOUT << ":: ".colorize(:green) << message.colorize(:white) << "\n" if ::Context.verbosity > 2
end
def self.info(message)
STDOUT << ":: ".colorize(:blue) << message.colorize(:white) << "\n" if ::Context.verbosity > 1
end
def self.warning(message)
STDERR << "?? ".colorize(:yellow) << message.colorize(:yellow) << "\n" if ::Context.verbosity > 0
end
def self.error(message)
STDERR << "!! ".colorize(:red) << message.colorize(:red) << "\n" if ::Context.verbosity > 0
end
end
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.on "-l host", "--l host", "IP address to listen on." do |h| parser.on "-l host", "--l host", "IP address to listen on." do |h|
CLI.host = h Context.host = h
end end
parser.on "-p port", "--port port", "Port to listen on." do |port| parser.on "-p port", "--port port", "Port to listen on." do |port|
CLI.port_to_listen = port.to_u16 Context.port_to_listen = port.to_u16
end end
parser.on "-s service-name", "--service-name service-name", "Service name." do |name| parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
CLI.service_name = name Context.service_name = name
end end
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t| parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
CLI.timer_delay = t.to_i32 * 1000 # stored in ms Context.timer_delay = t.to_i32 * 1000 # stored in ms
end end
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt| parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
CLI.verbosity = opt.to_i Context.verbosity = opt.to_i
end end
parser.on "-h", "--help", "Show this help" do parser.on "-h", "--help", "Show this help" do
@ -58,11 +75,11 @@ def sending_ping_messages
begin begin
ws.ping "hello from #{fd}" ws.ping "hello from #{fd}"
rescue e rescue e
puts "#{CRED}Exception: #{e}#{CRESET}, already closed client #{fd}" Log.error "Exception: #{e}, already closed client #{fd}"
begin begin
Context.context.remove_fd fd Context.context.remove_fd fd
rescue e rescue e
puts "#{CRED}Cannot remove #{fd} from clients: #{e}#{CRESET}" Log.error "Cannot remove #{fd} from clients: #{e}"
end end
end end
end end
@ -86,7 +103,7 @@ def ws_http_upgrade(client)
# FIXME: check they actually wanted to upgrade to websocket # FIXME: check they actually wanted to upgrade to websocket
key = request.headers["Sec-WebSocket-Key"] key = request.headers["Sec-WebSocket-Key"]
response_key = Digest::SHA1.base64digest key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" response_key = Digest::SHA1.base64digest key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
# puts response_key # Log.debug response_key
# HTTP inside bru # HTTP inside bru
headers_header = "HTTP/1.1 101 Switching Protocols" headers_header = "HTTP/1.1 101 Switching Protocols"
@ -113,7 +130,7 @@ def ws_http_upgrade(client)
wsclient = WebSocket.new client wsclient = WebSocket.new client
wsclient.on_pong do |m| wsclient.on_pong do |m|
puts "pong #{m}" Log.debug "pong #{m}"
end end
# registering the client into storing structures to avoid being garbage collected # registering the client into storing structures to avoid being garbage collected
@ -122,7 +139,7 @@ def ws_http_upgrade(client)
return req_service, request return req_service, request
rescue e rescue e
puts "#{CRED}Exception in ws_http_upgrade#{CRESET}: #{CBLUE}#{e}#{CRESET}" Log.error "Exception in ws_http_upgrade: #{e}"
raise "DROP IT" raise "DROP IT"
end end
@ -161,7 +178,7 @@ def handle_new_clients(service, server)
# If not found, we use the client address from the socket. # If not found, we use the client address from the socket.
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
puts "trackingd - sending the IP address #{real_ip_address} to fd #{serverfd}" Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{serverfd}"
# sfd = Context.context.switchtable[client.fd] # sfd = Context.context.switchtable[client.fd]
# message = IPC::Message.from_json(JSON).to_packet # message = IPC::Message.from_json(JSON).to_packet
# => JSON has to include these attributes: mtype, utype, payload # => JSON has to include these attributes: mtype, utype, payload
@ -173,16 +190,16 @@ def handle_new_clients(service, server)
# serv.send message.to_packet # serv.send message.to_packet
end end
info "#{CBLUE}new client:#{CRESET} #{client.fd}" Log.debug "new client: #{client.fd}"
rescue e rescue e
puts "Exception in handle_new_client: #{CRED}#{e}#{CRESET}" Log.error "Exception in handle_new_client: #{e}"
unless client.nil? unless client.nil?
client.close client.close
end end
end end
def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message)) def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
# info "OUT fd is #{fd}" # Log.info "OUT fd is #{fd}"
wsclient = Context.context.fd_to_websocket[fd] wsclient = Context.context.fd_to_websocket[fd]
message = IPC::Message.new pm message = IPC::Message.new pm
@ -198,13 +215,13 @@ def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
return LibIPC::IPCCB::NoError return LibIPC::IPCCB::NoError
rescue e rescue e
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}" Log.error "Exception during message transfer: #{e}"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
end end
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*)
# info "IN fd is #{fd}" # Log.debug "IN fd is #{fd}"
wsclient = Context.context.fd_to_websocket[fd] wsclient = Context.context.fd_to_websocket[fd]
@ -212,7 +229,7 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
begin begin
message = wsclient.run_once message = wsclient.run_once
rescue e rescue e
puts "#{CRED}run_once FAILED#{CRESET}: #{e}" Log.error "run_once FAILED: #{e}"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
end end
@ -224,13 +241,13 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
end end
if wsclient.closed? if wsclient.closed?
info "#{CBLUE}client is closed#{CRESET}" Log.info "client closed the connection"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Closing return LibIPC::IPCCB::Closing
end end
if message.nil? if message.nil?
puts "#{CRED}message is nil#{CRESET}" Log.error "Reveiced a nil message"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Closing return LibIPC::IPCCB::Closing
end end
@ -246,97 +263,91 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
# every other option should be dropped # every other option should be dropped
case message case message
when WebSocket::Error when WebSocket::Error
puts "#{CRED}An error occured#{CRESET}" Log.error "An error occured"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
when WebSocket::Ping when WebSocket::Ping
puts "#{CBLUE}Received a ping message#{CRESET}" Log.debug "Received a ping message"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Ignore return LibIPC::IPCCB::Ignore
when WebSocket::Pong when WebSocket::Pong
puts "#{CBLUE}Received a pong message#{CRESET}" Log.debug "Received a pong message"
return LibIPC::IPCCB::Ignore return LibIPC::IPCCB::Ignore
when WebSocket::Close when WebSocket::Close
puts "#{CBLUE}Received a close message#{CRESET}" Log.debug "Received a close message"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Closing return LibIPC::IPCCB::Closing
when WebSocket::NotFinal when WebSocket::NotFinal
puts "#{CBLUE}Received only part of a message: NOT IMPLEMENTED#{CRESET}" Log.warning "Received only part of a message: NOT IMPLEMENTED"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
when Bytes when Bytes
# TODO: when receiving a binary message # TODO: when receiving a binary message
# we should test the format and maybe its content # we should test the format and maybe its content
puts "#{CBLUE}Received a binary message: NOT IMPLEMENTED, YET#{CRESET}" Log.error "Received a binary message: NOT IMPLEMENTED, YET"
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
else else
puts "#{CRED}Received a websocket message with unknown type#{CRESET}" Log.error "Received a websocket message with unknown type"
end end
end end
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
rescue e rescue e
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}" Log.error "Exception (receiving a message) #{e}"
# tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET) # tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET)
# tcp.close # tcp.close
Context.context.remove_fd fd Context.context.remove_fd fd
return LibIPC::IPCCB::Error return LibIPC::IPCCB::Error
end end
def info(str : String)
if CLI.verbosity > 0
puts str
end
end
class Context class Context
class_property context = InstanceStorage.new class_property context = InstanceStorage.new
end end
def main def main
# by default, listen on any IP address # by default, listen on any IP address
server = TCPServer.new(CLI.host, CLI.port_to_listen) server = TCPServer.new(Context.host, Context.port_to_listen)
service = IPC::Server.new CLI.service_name service = IPC::Server.new Context.service_name
service << server.fd service << server.fd
# Every few seconds, the service should trigger the timer # Every few seconds, the service should trigger the timer
# Allowing the sending of Ping messages to clients # Allowing the sending of Ping messages to clients
service.base_timer = CLI.timer_delay service.base_timer = Context.timer_delay
service.timer = CLI.timer_delay service.timer = Context.timer_delay
service.loop do |event| service.loop do |event|
# info "current state of the context:" # Log.info "current state of the context:"
# service.pp # service.pp
case event case event
when IPC::Event::Timer when IPC::Event::Timer
info "#{CORANGE}IPC::Event::Timer#{CRESET}" Log.debug "IPC::Event::Timer"
sending_ping_messages sending_ping_messages
when IPC::Event::Connection when IPC::Event::Connection
info "#{CBLUE}IPC::Event::Connection#{CRESET}: fd #{event.fd}" Log.debug "IPC::Event::Connection: fd #{event.fd}"
when IPC::Event::Disconnection when IPC::Event::Disconnection
info "#{CBLUE}IPC::Event::Disconnection#{CRESET}: fd #{event.fd}" Log.debug "IPC::Event::Disconnection: fd #{event.fd}"
Context.context.remove_fd event.fd Context.context.remove_fd event.fd
when IPC::Event::ExtraSocket when IPC::Event::ExtraSocket
info "#{CBLUE}IPC::Event::ExtraSocket#{CRESET}: fd #{event.fd}" Log.debug "IPC::Event::ExtraSocket: fd #{event.fd}"
if server.fd != event.fd if server.fd != event.fd
raise "Error: the only extra socket should be the TCP/WS server" raise "Error: the only extra socket should be the TCP/WS server"
end end
handle_new_clients(service, server) handle_new_clients(service, server)
when IPC::Event::Switch when IPC::Event::Switch
info "\033[36mIPC::Event::Switch#{CRESET}: fd #{event.fd}" Log.debug "IPC::Event::Switch: fd #{event.fd}"
# raise "Not implemented." # raise "Not implemented."
when IPC::Event::MessageSent when IPC::Event::MessageSent
info "#{CBLUE}IPC::Event::MessageSent#{CRESET}: fd #{event.fd}" Log.debug "IPC::Event::MessageSent: fd #{event.fd}"
when IPC::Event::MessageReceived when IPC::Event::MessageReceived
info "#{CBLUE}IPC::Event::Message#{CRESET}: fd #{event.fd}" Log.debug "IPC::Event::Message: fd #{event.fd}"
raise "Not implemented." raise "Not implemented."
end end