-M to print messages.
parent
b42e38eda8
commit
a70b633c42
|
@ -25,6 +25,7 @@ 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 print_messages = false
|
||||
end
|
||||
|
||||
OptionParser.parse do |parser|
|
||||
|
@ -48,6 +49,10 @@ OptionParser.parse do |parser|
|
|||
Baguette::Context.verbosity = opt.to_i
|
||||
end
|
||||
|
||||
parser.on "-M", "--print-messages", "Print messages received and sent." do
|
||||
Context.print_messages = true
|
||||
end
|
||||
|
||||
parser.on "-h", "--help", "Show this help" do
|
||||
puts parser
|
||||
exit 0
|
||||
|
@ -79,13 +84,25 @@ class InstanceStorage
|
|||
Baguette::Log.info "closing the client:#{CRESET} #{fdclient}"
|
||||
# 1. closing both the client and the service
|
||||
fdservice = @switchtable[fdclient]?
|
||||
if fdservice.nil?
|
||||
Baguette::Log.error "client #{fdclient} already removed!"
|
||||
return
|
||||
end
|
||||
begin
|
||||
tcpfdc = @fd_to_tcpsocket[fdclient]
|
||||
|
||||
# 2. closing the TCP connections
|
||||
tcpfdc.close unless tcpfdc.closed?
|
||||
rescue e
|
||||
Baguette::Log.error "1"
|
||||
end
|
||||
|
||||
# 3. removing the client and the service fds from the loop check
|
||||
begin
|
||||
@service.remove_fd (fdclient)
|
||||
rescue e
|
||||
Baguette::Log.error "2"
|
||||
end
|
||||
|
||||
# 5. removing both the client and the service from the switchtable
|
||||
@switchtable = @switchtable.select do |fdc, fds|
|
||||
|
@ -100,6 +117,7 @@ class InstanceStorage
|
|||
fd != fdclient
|
||||
end
|
||||
|
||||
begin
|
||||
unless fdservice.nil?
|
||||
service = @fd_to_ipcclient[fdservice]
|
||||
service.close
|
||||
|
@ -110,6 +128,11 @@ class InstanceStorage
|
|||
|
||||
@is_client = @is_client.select do |fd,v| fd != fdservice end
|
||||
end
|
||||
rescue e
|
||||
Baguette::Log.error "3"
|
||||
end
|
||||
rescue e
|
||||
Baguette::Log.error "in InstanceStorage#remove_fd: #{e}"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -124,7 +147,9 @@ Context.service << server.fd
|
|||
|
||||
def websocket_client_connection(client)
|
||||
request = HTTP::Request.from_io client
|
||||
# pp! request
|
||||
if Context.print_messages
|
||||
pp! request
|
||||
end
|
||||
|
||||
if request.nil?
|
||||
raise "#REQUEST IS NIL"
|
||||
|
@ -180,7 +205,7 @@ def websocket_client_connection(client)
|
|||
|
||||
# If the requested service is trackingd, send the IP address of the client
|
||||
if req_service == "tracking"
|
||||
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]
|
||||
Baguette::Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}"
|
||||
|
@ -189,11 +214,16 @@ def websocket_client_connection(client)
|
|||
# message = IPC::Message.new mtype, utype, payload
|
||||
remote_address = client.remote_address.address
|
||||
message = IPC::Message.new sfd, 1, 1.to_u8, "{\"ipaddress\": \"#{remote_address}\"}"
|
||||
if Context.print_messages
|
||||
Baguette::Log.info "to trackingd: #{message.to_s}"
|
||||
end
|
||||
serv = WrappedTCPFileDescriptor.new(fd: sfd, family: Socket::Family::INET)
|
||||
serv.send message.to_packet
|
||||
end
|
||||
|
||||
# puts "#{headers_header}\n#{headers.to_s}\r\n"
|
||||
if Context.print_messages
|
||||
Baguette::Log.info "to client: #{headers_header}\n#{headers.to_s}"
|
||||
end
|
||||
client.send "#{headers_header}\n#{headers.to_s}\r\n"
|
||||
|
||||
wsclient = WebSocket.new client
|
||||
|
@ -305,6 +335,11 @@ def websocket_switching_procedure (activefd : Int)
|
|||
end
|
||||
|
||||
if Context.context.is_json[activefd] && message.is_a?(String)
|
||||
if Context.print_messages
|
||||
j = JSON.parse message
|
||||
Baguette::Log.info "received from client #{activefd}"
|
||||
pp! j["payload"]
|
||||
end
|
||||
message = IPC::Message.from_json(message).to_packet
|
||||
end
|
||||
|
||||
|
@ -331,6 +366,11 @@ def websocket_switching_procedure (activefd : Int)
|
|||
|
||||
if Context.context.is_json[fdclient]
|
||||
buf = message.to_json
|
||||
if Context.print_messages
|
||||
j = JSON.parse buf
|
||||
Baguette::Log.info "received from service #{activefd}"
|
||||
pp! j["payload"]
|
||||
end
|
||||
else
|
||||
buf = message.to_packet
|
||||
end
|
||||
|
|
Reference in New Issue