Baguette::Log.
parent
b171f90e92
commit
c57a7e8748
|
@ -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:
|
||||||
websocketd:
|
websocketd:
|
||||||
|
|
|
@ -13,6 +13,8 @@ require "base64"
|
||||||
require "digest"
|
require "digest"
|
||||||
require "./utils"
|
require "./utils"
|
||||||
|
|
||||||
|
require "baguette-crystal-base"
|
||||||
|
|
||||||
require "./instance_storage.cr"
|
require "./instance_storage.cr"
|
||||||
|
|
||||||
# All modifications to standard libraries go there.
|
# All modifications to standard libraries go there.
|
||||||
|
@ -25,22 +27,6 @@ class Context
|
||||||
class_property host : String = "0.0.0.0"
|
class_property host : String = "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 # 30 seconds
|
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
|
end
|
||||||
|
|
||||||
OptionParser.parse do |parser|
|
OptionParser.parse do |parser|
|
||||||
|
@ -61,7 +47,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
|
||||||
|
@ -75,11 +61,11 @@ def sending_ping_messages
|
||||||
begin
|
begin
|
||||||
ws.ping "hello from #{fd}"
|
ws.ping "hello from #{fd}"
|
||||||
rescue e
|
rescue e
|
||||||
Log.error "Exception: #{e}, already closed client #{fd}"
|
Baguette::Log.error "Exception: #{e}, already closed client #{fd}"
|
||||||
begin
|
begin
|
||||||
Context.context.remove_fd fd
|
Context.context.remove_fd fd
|
||||||
rescue e
|
rescue e
|
||||||
Log.error "Cannot remove #{fd} from clients: #{e}"
|
Baguette::Log.error "Cannot remove #{fd} from clients: #{e}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -103,7 +89,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"
|
||||||
# Log.debug response_key
|
# Baguette::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"
|
||||||
|
@ -130,7 +116,7 @@ def ws_http_upgrade(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
|
||||||
|
|
||||||
# registering the client into storing structures to avoid being garbage collected
|
# registering the client into storing structures to avoid being garbage collected
|
||||||
|
@ -139,7 +125,7 @@ def ws_http_upgrade(client)
|
||||||
|
|
||||||
return req_service, request
|
return req_service, request
|
||||||
rescue e
|
rescue e
|
||||||
Log.error "Exception in ws_http_upgrade: #{e}"
|
Baguette::Log.error "Exception in ws_http_upgrade: #{e}"
|
||||||
raise "DROP IT"
|
raise "DROP IT"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -178,7 +164,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
|
||||||
|
|
||||||
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]
|
# 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
|
||||||
|
@ -190,16 +176,16 @@ def handle_new_clients(service, server)
|
||||||
# serv.send message.to_packet
|
# serv.send message.to_packet
|
||||||
end
|
end
|
||||||
|
|
||||||
Log.debug "new client: #{client.fd}"
|
Baguette::Log.debug "new client: #{client.fd}"
|
||||||
rescue e
|
rescue e
|
||||||
Log.error "Exception in handle_new_client: #{e}"
|
Baguette::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))
|
||||||
# Log.info "OUT fd is #{fd}"
|
# Baguette::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
|
||||||
|
@ -215,13 +201,13 @@ def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
|
||||||
|
|
||||||
return LibIPC::IPCCB::NoError
|
return LibIPC::IPCCB::NoError
|
||||||
rescue e
|
rescue e
|
||||||
Log.error "Exception during message transfer: #{e}"
|
Baguette::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*)
|
||||||
# Log.debug "IN fd is #{fd}"
|
# Baguette::Log.debug "IN fd is #{fd}"
|
||||||
|
|
||||||
wsclient = Context.context.fd_to_websocket[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
|
begin
|
||||||
message = wsclient.run_once
|
message = wsclient.run_once
|
||||||
rescue e
|
rescue e
|
||||||
Log.error "run_once FAILED: #{e}"
|
Baguette::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
|
||||||
|
@ -241,13 +227,13 @@ def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
|
||||||
end
|
end
|
||||||
|
|
||||||
if wsclient.closed?
|
if wsclient.closed?
|
||||||
Log.info "client closed the connection"
|
Baguette::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?
|
||||||
Log.error "Reveiced a nil message"
|
Baguette::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
|
||||||
|
@ -263,39 +249,39 @@ 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
|
||||||
Log.error "An error occured"
|
Baguette::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
|
||||||
Log.debug "Received a ping message"
|
Baguette::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
|
||||||
Log.debug "Received a pong message"
|
Baguette::Log.debug "Received a pong message"
|
||||||
return LibIPC::IPCCB::Ignore
|
return LibIPC::IPCCB::Ignore
|
||||||
when WebSocket::Close
|
when WebSocket::Close
|
||||||
Log.debug "Received a close message"
|
Baguette::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
|
||||||
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
|
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
|
||||||
Log.error "Received a binary message: NOT IMPLEMENTED, YET"
|
Baguette::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
|
||||||
Log.error "Received a websocket message with unknown type"
|
Baguette::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
|
||||||
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 = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET)
|
||||||
# tcp.close
|
# tcp.close
|
||||||
Context.context.remove_fd fd
|
Context.context.remove_fd fd
|
||||||
|
@ -318,36 +304,36 @@ def main
|
||||||
service.timer = Context.timer_delay
|
service.timer = Context.timer_delay
|
||||||
|
|
||||||
service.loop do |event|
|
service.loop do |event|
|
||||||
# Log.info "current state of the context:"
|
# Baguette::Log.info "current state of the context:"
|
||||||
# service.pp
|
# service.pp
|
||||||
case event
|
case event
|
||||||
when IPC::Event::Timer
|
when IPC::Event::Timer
|
||||||
Log.debug "IPC::Event::Timer"
|
Baguette::Log.debug "IPC::Event::Timer"
|
||||||
sending_ping_messages
|
sending_ping_messages
|
||||||
|
|
||||||
when IPC::Event::Connection
|
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
|
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
|
Context.context.remove_fd event.fd
|
||||||
|
|
||||||
when IPC::Event::ExtraSocket
|
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
|
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
|
||||||
Log.debug "IPC::Event::Switch: fd #{event.fd}"
|
Baguette::Log.debug "IPC::Event::Switch: fd #{event.fd}"
|
||||||
# raise "Not implemented."
|
# raise "Not implemented."
|
||||||
|
|
||||||
when IPC::Event::MessageSent
|
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
|
when IPC::Event::MessageReceived
|
||||||
Log.debug "IPC::Event::Message: fd #{event.fd}"
|
Baguette::Log.debug "IPC::Event::Message: fd #{event.fd}"
|
||||||
|
|
||||||
raise "Not implemented."
|
raise "Not implemented."
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue