Baguette::Log.

master
Karchnu 2020-08-28 02:02:19 +02:00
parent 185a87431c
commit b42e38eda8
2 changed files with 20 additions and 31 deletions

View File

@ -11,6 +11,9 @@ dependencies:
ipc: ipc:
git: https://git.baguette.netlib.re/Baguette/ipc.cr git: https://git.baguette.netlib.re/Baguette/ipc.cr
branch: master branch: master
baguette-crystal-base:
git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base
branch: master
targets: targets:
pongc: pongc:

View File

@ -11,6 +11,8 @@ require "base64"
require "digest" require "digest"
require "./utils" require "./utils"
require "baguette-crystal-base"
require "colorize" require "colorize"
# All modifications to standard libraries go there. # All modifications to standard libraries go there.
@ -23,22 +25,6 @@ class Context
class_property host = "0.0.0.0" class_property host = "0.0.0.0"
class_property port_to_listen : UInt16 = 1234 class_property port_to_listen : UInt16 = 1234
class_property timer_delay : Int32 = 30_000.to_i32 class_property timer_delay : Int32 = 30_000.to_i32
class_property verbosity = 1
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 end
OptionParser.parse do |parser| OptionParser.parse do |parser|
@ -59,7 +45,7 @@ OptionParser.parse do |parser|
end end
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt| parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
Context.verbosity = opt.to_i Baguette::Context.verbosity = opt.to_i
end end
parser.on "-h", "--help", "Show this help" do parser.on "-h", "--help", "Show this help" do
@ -90,7 +76,7 @@ class InstanceStorage
end end
def remove_fd (fdclient : Int32) def remove_fd (fdclient : Int32)
Log.info "closing the client:#{CRESET} #{fdclient}" Baguette::Log.info "closing the client:#{CRESET} #{fdclient}"
# 1. closing both the client and the service # 1. closing both the client and the service
fdservice = @switchtable[fdclient]? fdservice = @switchtable[fdclient]?
tcpfdc = @fd_to_tcpsocket[fdclient] tcpfdc = @fd_to_tcpsocket[fdclient]
@ -197,7 +183,7 @@ def websocket_client_connection(client)
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
sfd = Context.context.switchtable[client.fd] sfd = Context.context.switchtable[client.fd]
Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}" Baguette::Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}"
# 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
# message = IPC::Message.new mtype, utype, payload # message = IPC::Message.new mtype, utype, payload
@ -212,7 +198,7 @@ def websocket_client_connection(client)
wsclient = WebSocket.new client wsclient = WebSocket.new client
wsclient.on_pong do |m| wsclient.on_pong do |m|
Log.debug "pong #{m}" Baguette::Log.debug "pong #{m}"
end end
Context.context.is_client[client.fd] = true Context.context.is_client[client.fd] = true
@ -385,25 +371,25 @@ Context.service.loop do |event|
begin begin
case event case event
when IPC::Event::Timer when IPC::Event::Timer
Log.info "IPC::Event::Timer" Baguette::Log.info "IPC::Event::Timer"
sending_ping_messages sending_ping_messages
when IPC::Event::Connection when IPC::Event::Connection
Log.debug "IPC::Event::Connection: #{event.fd}" Baguette::Log.debug "IPC::Event::Connection: #{event.fd}"
when IPC::Event::Disconnection when IPC::Event::Disconnection
Log.debug "IPC::Event::Disconnection: #{event.fd}" Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}"
when IPC::Event::ExtraSocket when IPC::Event::ExtraSocket
Log.debug "IPC::Event::ExtraSocket: #{event.fd}" Baguette::Log.debug "IPC::Event::ExtraSocket: #{event.fd}"
# 1. accept new websocket clients # 1. accept new websocket clients
if server.fd == event.fd if server.fd == event.fd
client = server.accept client = server.accept
begin begin
websocket_client_connection client websocket_client_connection client
Log.info "new client: #{client.fd}" Baguette::Log.info "new client: #{client.fd}"
rescue e rescue e
Log.error "Exception: #{e}" Baguette::Log.error "Exception: #{e}"
client.close client.close
end end
next next
@ -412,24 +398,24 @@ Context.service.loop do |event|
# 2. active fd != server fd # 2. active fd != server fd
activefd = event.fd activefd = event.fd
if activefd <= 0 if activefd <= 0
Log.error "faulty activefd: #{activefd}" Baguette::Log.error "faulty activefd: #{activefd}"
end end
websocket_switching_procedure activefd websocket_switching_procedure activefd
when IPC::Event::Switch when IPC::Event::Switch
Log.debug "IPC::Event::Switch: from fd #{event.fd}" Baguette::Log.debug "IPC::Event::Switch: from fd #{event.fd}"
raise "Not implemented." raise "Not implemented."
# IPC::Event::Message has to be the last entry # IPC::Event::Message has to be the last entry
# because ExtraSocket and Switch inherit from Message class # because ExtraSocket and Switch inherit from Message class
when IPC::Event::MessageSent when IPC::Event::MessageSent
Log.error "IPC::Event::MessageSent: #{event.fd}" Baguette::Log.error "IPC::Event::MessageSent: #{event.fd}"
when IPC::Event::MessageReceived when IPC::Event::MessageReceived
Log.debug "IPC::Event::MessageReceived: #{event.fd}" Baguette::Log.debug "IPC::Event::MessageReceived: #{event.fd}"
raise "Not implemented." raise "Not implemented."
end end
rescue e rescue e
Log.error "IPC loop final catch: #{e}" Baguette::Log.error "IPC loop final catch: #{e}"
end end
end end