Obsolete
/
ipcd
Archived
3
0
Fork 0

-M to print messages.

master
Karchnu 2020-08-28 18:08:20 +02:00
parent b42e38eda8
commit a70b633c42
1 changed files with 43 additions and 3 deletions

View File

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