|
|
|
@ -11,6 +11,8 @@ require "base64"
|
|
|
|
|
require "digest"
|
|
|
|
|
require "./utils"
|
|
|
|
|
|
|
|
|
|
require "baguette-crystal-base"
|
|
|
|
|
|
|
|
|
|
require "colorize"
|
|
|
|
|
|
|
|
|
|
# All modifications to standard libraries go there.
|
|
|
|
@ -23,22 +25,6 @@ class Context
|
|
|
|
|
class_property host = "0.0.0.0"
|
|
|
|
|
class_property port_to_listen : UInt16 = 1234
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
OptionParser.parse do |parser|
|
|
|
|
@ -59,7 +45,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
|
|
|
|
@ -90,7 +76,7 @@ class InstanceStorage
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
fdservice = @switchtable[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
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
# => JSON has to include these attributes: mtype, utype, payload
|
|
|
|
|
# message = IPC::Message.new mtype, utype, payload
|
|
|
|
@ -212,7 +198,7 @@ def websocket_client_connection(client)
|
|
|
|
|
|
|
|
|
|
wsclient = WebSocket.new client
|
|
|
|
|
wsclient.on_pong do |m|
|
|
|
|
|
Log.debug "pong #{m}"
|
|
|
|
|
Baguette::Log.debug "pong #{m}"
|
|
|
|
|
end
|
|
|
|
|
Context.context.is_client[client.fd] = true
|
|
|
|
|
|
|
|
|
@ -385,25 +371,25 @@ Context.service.loop do |event|
|
|
|
|
|
begin
|
|
|
|
|
case event
|
|
|
|
|
when IPC::Event::Timer
|
|
|
|
|
Log.info "IPC::Event::Timer"
|
|
|
|
|
Baguette::Log.info "IPC::Event::Timer"
|
|
|
|
|
sending_ping_messages
|
|
|
|
|
when IPC::Event::Connection
|
|
|
|
|
Log.debug "IPC::Event::Connection: #{event.fd}"
|
|
|
|
|
Baguette::Log.debug "IPC::Event::Connection: #{event.fd}"
|
|
|
|
|
|
|
|
|
|
when IPC::Event::Disconnection
|
|
|
|
|
Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
|
|
|
|
Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
|
|
|
|
|
|
|
|
|
when IPC::Event::ExtraSocket
|
|
|
|
|
Log.debug "IPC::Event::ExtraSocket: #{event.fd}"
|
|
|
|
|
Baguette::Log.debug "IPC::Event::ExtraSocket: #{event.fd}"
|
|
|
|
|
|
|
|
|
|
# 1. accept new websocket clients
|
|
|
|
|
if server.fd == event.fd
|
|
|
|
|
client = server.accept
|
|
|
|
|
begin
|
|
|
|
|
websocket_client_connection client
|
|
|
|
|
Log.info "new client: #{client.fd}"
|
|
|
|
|
Baguette::Log.info "new client: #{client.fd}"
|
|
|
|
|
rescue e
|
|
|
|
|
Log.error "Exception: #{e}"
|
|
|
|
|
Baguette::Log.error "Exception: #{e}"
|
|
|
|
|
client.close
|
|
|
|
|
end
|
|
|
|
|
next
|
|
|
|
@ -412,24 +398,24 @@ Context.service.loop do |event|
|
|
|
|
|
# 2. active fd != server fd
|
|
|
|
|
activefd = event.fd
|
|
|
|
|
if activefd <= 0
|
|
|
|
|
Log.error "faulty activefd: #{activefd}"
|
|
|
|
|
Baguette::Log.error "faulty activefd: #{activefd}"
|
|
|
|
|
end
|
|
|
|
|
websocket_switching_procedure activefd
|
|
|
|
|
|
|
|
|
|
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."
|
|
|
|
|
|
|
|
|
|
# IPC::Event::Message has to be the last entry
|
|
|
|
|
# because ExtraSocket and Switch inherit from Message class
|
|
|
|
|
when IPC::Event::MessageSent
|
|
|
|
|
Log.error "IPC::Event::MessageSent: #{event.fd}"
|
|
|
|
|
Baguette::Log.error "IPC::Event::MessageSent: #{event.fd}"
|
|
|
|
|
|
|
|
|
|
when IPC::Event::MessageReceived
|
|
|
|
|
Log.debug "IPC::Event::MessageReceived: #{event.fd}"
|
|
|
|
|
Baguette::Log.debug "IPC::Event::MessageReceived: #{event.fd}"
|
|
|
|
|
raise "Not implemented."
|
|
|
|
|
end
|
|
|
|
|
rescue e
|
|
|
|
|
Log.error "IPC loop final catch: #{e}"
|
|
|
|
|
Baguette::Log.error "IPC loop final catch: #{e}"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|