Baguette::Log.

dev
Karchnu 4 years ago
parent b171f90e92
commit c57a7e8748

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

@ -13,6 +13,8 @@ require "base64"
require "digest"
require "./utils"
require "baguette-crystal-base"
require "./instance_storage.cr"
# All modifications to standard libraries go there.
@ -25,22 +27,6 @@ class Context
class_property host : String = "0.0.0.0"
class_property port_to_listen : UInt16 = 1234
class_property timer_delay : Int32 = 30_000 # 30 seconds
class_property verbosity : Int32 = 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
OptionParser.parse do |parser|
@ -61,7 +47,7 @@ OptionParser.parse do |parser|
end
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
Context.verbosity = opt.to_i
Baguette::Context.verbosity = opt.to_i
end
parser.on "-h", "--help", "Show this help" do
@ -75,11 +61,11 @@ def sending_ping_messages
begin
ws.ping "hello from #{fd}"
rescue e
Log.error "Exception: #{e}, already closed client #{fd}"
Baguette::Log.error "Exception: #{e}, already closed client #{fd}"
begin
Context.context.remove_fd fd
rescue e
Log.error "Cannot remove #{fd} from clients: #{e}"
Baguette::Log.error "Cannot remove #{fd} from clients: #{e}"
end
end
end
@ -103,7 +89,7 @@ def ws_http_upgrade(client)
# FIXME: check they actually wanted to upgrade to websocket
key = request.headers["Sec-WebSocket-Key"]
response_key = Digest::SHA1.base64digest key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
# Log.debug response_key
# Baguette::Log.debug response_key
# HTTP inside bru
headers_header = "HTTP/1.1 101 Switching Protocols"
@ -130,7 +116,7 @@ def ws_http_upgrade(client)
wsclient = WebSocket.new client
wsclient.on_pong do |m|
Log.debug "pong #{m}"
Baguette::Log.debug "pong #{m}"
end
# registering the client into storing structures to avoid being garbage collected
@ -139,7 +125,7 @@ def ws_http_upgrade(client)
return req_service, request
rescue e
Log.error "Exception in ws_http_upgrade: #{e}"
Baguette::Log.error "Exception in ws_http_upgrade: #{e}"
raise "DROP IT"
end
@ -178,7 +164,7 @@ def handle_new_clients(service, server)
# If not found, we use the client address from the socket.
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{serverfd}"
Baguette::Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{serverfd}"
# sfd = Context.context.switchtable[client.fd]
# message = IPC::Message.from_json(JSON).to_packet
# => JSON has to include these attributes: mtype, utype, payload
@ -190,16 +176,16 @@ def handle_new_clients(service, server)
# serv.send message.to_packet
end
Log.debug "new client: #{client.fd}"
Baguette::Log.debug "new client: #{client.fd}"
rescue e
Log.error "Exception in handle_new_client: #{e}"
Baguette::Log.error "Exception in handle_new_client: #{e}"
unless client.nil?
client.close
end
end
def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
# Log.info "OUT fd is #{fd}"
# Baguette::Log.info "OUT fd is #{fd}"
wsclient = Context.context.fd_to_websocket[fd]
message = IPC::Message.new pm
@ -215,13 +201,13 @@ def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
return LibIPC::IPCCB::NoError
rescue e
Log.error "Exception during message transfer: #{e}"
Baguette::Log.error "Exception during message transfer: #{e}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
end
def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
# Log.debug "IN fd is #{fd}"
# Baguette::Log.debug "IN fd is #{fd}"
wsclient = Context.context.fd_to_websocket[fd]
@ -229,7 +215,7 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
begin
message = wsclient.run_once
rescue e
Log.error "run_once FAILED: #{e}"
Baguette::Log.error "run_once FAILED: #{e}"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
end
@ -241,13 +227,13 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
end
if wsclient.closed?
Log.info "client closed the connection"
Baguette::Log.info "client closed the connection"
Context.context.remove_fd fd
return LibIPC::IPCCB::Closing
end
if message.nil?
Log.error "Reveiced a nil message"
Baguette::Log.error "Reveiced a nil message"
Context.context.remove_fd fd
return LibIPC::IPCCB::Closing
end
@ -263,39 +249,39 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
# every other option should be dropped
case message
when WebSocket::Error
Log.error "An error occured"
Baguette::Log.error "An error occured"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when WebSocket::Ping
Log.debug "Received a ping message"
Baguette::Log.debug "Received a ping message"
Context.context.remove_fd fd
return LibIPC::IPCCB::Ignore
when WebSocket::Pong
Log.debug "Received a pong message"
Baguette::Log.debug "Received a pong message"
return LibIPC::IPCCB::Ignore
when WebSocket::Close
Log.debug "Received a close message"
Baguette::Log.debug "Received a close message"
Context.context.remove_fd fd
return LibIPC::IPCCB::Closing
when WebSocket::NotFinal
Log.warning "Received only part of a message: NOT IMPLEMENTED"
Baguette::Log.warning "Received only part of a message: NOT IMPLEMENTED"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
when Bytes
# TODO: when receiving a binary message
# we should test the format and maybe its content
Log.error "Received a binary message: NOT IMPLEMENTED, YET"
Baguette::Log.error "Received a binary message: NOT IMPLEMENTED, YET"
Context.context.remove_fd fd
return LibIPC::IPCCB::Error
else
Log.error "Received a websocket message with unknown type"
Baguette::Log.error "Received a websocket message with unknown type"
end
end
return LibIPC::IPCCB::Error
rescue e
Log.error "Exception (receiving a message) #{e}"
Baguette::Log.error "Exception (receiving a message) #{e}"
# tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET)
# tcp.close
Context.context.remove_fd fd
@ -318,36 +304,36 @@ def main
service.timer = Context.timer_delay
service.loop do |event|
# Log.info "current state of the context:"
# Baguette::Log.info "current state of the context:"
# service.pp
case event
when IPC::Event::Timer
Log.debug "IPC::Event::Timer"
Baguette::Log.debug "IPC::Event::Timer"
sending_ping_messages
when IPC::Event::Connection
Log.debug "IPC::Event::Connection: fd #{event.fd}"
Baguette::Log.debug "IPC::Event::Connection: fd #{event.fd}"
when IPC::Event::Disconnection
Log.debug "IPC::Event::Disconnection: fd #{event.fd}"
Baguette::Log.debug "IPC::Event::Disconnection: fd #{event.fd}"
Context.context.remove_fd event.fd
when IPC::Event::ExtraSocket
Log.debug "IPC::Event::ExtraSocket: fd #{event.fd}"
Baguette::Log.debug "IPC::Event::ExtraSocket: fd #{event.fd}"
if server.fd != event.fd
raise "Error: the only extra socket should be the TCP/WS server"
end
handle_new_clients(service, server)
when IPC::Event::Switch
Log.debug "IPC::Event::Switch: fd #{event.fd}"
Baguette::Log.debug "IPC::Event::Switch: fd #{event.fd}"
# raise "Not implemented."
when IPC::Event::MessageSent
Log.debug "IPC::Event::MessageSent: fd #{event.fd}"
Baguette::Log.debug "IPC::Event::MessageSent: fd #{event.fd}"
when IPC::Event::MessageReceived
Log.debug "IPC::Event::Message: fd #{event.fd}"
Baguette::Log.debug "IPC::Event::Message: fd #{event.fd}"
raise "Not implemented."
end

Loading…
Cancel
Save