Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
Karchnu | b76701a275 | |
Karchnu | f2edaa5301 | |
Karchnu | 640284c067 | |
Karchnu | 1deee0f7f1 | |
Karchnu | 6d4db9a8ac | |
Karchnu | 4d7a7d3742 | |
Karchnu | 5e5de4a9f1 | |
Karchnu | e41c6878cf | |
Karchnu | 59b701c03e | |
Karchnu | 772389ab41 | |
Karchnu | a74bed308b | |
Karchnu | cad5a776cc | |
Karchnu | 9f6f711187 | |
Karchnu | 58fec6f966 |
11
shard.yml
11
shard.yml
|
@ -1,5 +1,5 @@
|
||||||
name: ipcd
|
name: ipcd
|
||||||
version: 0.6.0 # should follow libipc and ipc.cr bindings versions.
|
version: 0.7.0 # should follow libipc and ipc.cr bindings versions.
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
- karchnu <karchnu@karchnu.fr>
|
- karchnu <karchnu@karchnu.fr>
|
||||||
|
@ -10,13 +10,20 @@ description: |
|
||||||
dependencies:
|
dependencies:
|
||||||
ipc:
|
ipc:
|
||||||
git: https://git.baguette.netlib.re/Baguette/ipc.cr
|
git: https://git.baguette.netlib.re/Baguette/ipc.cr
|
||||||
branch: poll
|
branch: master
|
||||||
|
baguette-crystal-base:
|
||||||
|
git: https://git.baguette.netlib.re/Baguette/baguette-crystal-base
|
||||||
|
branch: master
|
||||||
|
|
||||||
targets:
|
targets:
|
||||||
websocketd:
|
websocketd:
|
||||||
main: src/websocketd.cr
|
main: src/websocketd.cr
|
||||||
|
websocketd-dev:
|
||||||
|
main: src/websocketd-dev.cr
|
||||||
websocketc:
|
websocketc:
|
||||||
main: src/websocketc.cr
|
main: src/websocketc.cr
|
||||||
|
oldwsc:
|
||||||
|
main: src/old-websocketc.cr
|
||||||
|
|
||||||
# tests
|
# tests
|
||||||
test-ws-pong:
|
test-ws-pong:
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
# Link between fd and TCPSocket, WebSocket and IPC::Connection instances.
|
||||||
|
class InstanceStorage
|
||||||
|
property is_json : Hash(Int32, Bool)
|
||||||
|
property fd_to_websocket : Hash(Int32, WebSocket)
|
||||||
|
|
||||||
|
# So the GC won't close it.
|
||||||
|
property fd_to_socket : Hash(Int32, TCPSocket)
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@is_json = Hash(Int32,Bool).new
|
||||||
|
@fd_to_websocket = Hash(Int32, WebSocket).new
|
||||||
|
@fd_to_socket = Hash(Int32, TCPSocket).new
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_fd (fdclient : Int32)
|
||||||
|
puts "#{CBLUE}closing the client:#{CRESET} #{fdclient}"
|
||||||
|
# 1. remove the client and the service from is_json
|
||||||
|
@is_json.select! do |fd,v| fd != fdclient end
|
||||||
|
|
||||||
|
# 2. remove the client from fd_to_websocket
|
||||||
|
@fd_to_websocket.select! do |fd, ws| fd != fdclient end
|
||||||
|
|
||||||
|
# 3. remove the client from fd_to_socket
|
||||||
|
@fd_to_socket.select! do |fd, ws| fd != fdclient end
|
||||||
|
end
|
||||||
|
end
|
|
@ -38,7 +38,14 @@ class HTTP::WebSocket
|
||||||
record Pong
|
record Pong
|
||||||
record Close
|
record Close
|
||||||
record Error
|
record Error
|
||||||
record NotFinal
|
# record NotFinal
|
||||||
|
|
||||||
|
struct NotFinal
|
||||||
|
property message : String
|
||||||
|
property info : Protocol::PacketInfo
|
||||||
|
def initialize(@message, @info)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :nodoc:
|
||||||
def run_once : Bytes | String | Close | Ping | Pong | NotFinal | Error
|
def run_once : Bytes | String | Close | Ping | Pong | NotFinal | Error
|
||||||
|
@ -73,6 +80,8 @@ class HTTP::WebSocket
|
||||||
@on_message.try &.call(@current_message.to_s)
|
@on_message.try &.call(@current_message.to_s)
|
||||||
@current_message.clear
|
@current_message.clear
|
||||||
return String.new message
|
return String.new message
|
||||||
|
else
|
||||||
|
return NotFinal.new String.new(message), info
|
||||||
end
|
end
|
||||||
when Protocol::Opcode::BINARY
|
when Protocol::Opcode::BINARY
|
||||||
message = @buffer[0, info.size]
|
message = @buffer[0, info.size]
|
||||||
|
@ -93,7 +102,7 @@ class HTTP::WebSocket
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return NotFinal.new
|
raise "what the fuck?"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the websocket instance.
|
# Returns the websocket instance.
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
require "ipc"
|
||||||
|
require "json"
|
||||||
|
require "ipc/json"
|
||||||
|
|
||||||
|
class IPC::JSON
|
||||||
|
def handle(service : IPC::Server, event : IPC::Event::Events)
|
||||||
|
raise "unimplemented"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Websocketc
|
||||||
|
class_getter requests = [] of IPC::JSON.class
|
||||||
|
class_getter responses = [] of IPC::JSON.class
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
class Websocketc::Response
|
||||||
|
IPC::JSON.message Error, 0 do
|
||||||
|
property reason : String | Array(String)
|
||||||
|
|
||||||
|
def initialize(@reason)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
IPC::JSON.message Success, 1 do
|
||||||
|
def initialize
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
require "./requests/*"
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
require "http/web_socket"
|
||||||
|
require "option_parser"
|
||||||
|
|
||||||
|
require "ipc"
|
||||||
|
require "./colors"
|
||||||
|
require "./utils"
|
||||||
|
require "./lib_modifications.cr"
|
||||||
|
|
||||||
|
class CLI
|
||||||
|
class_property uri = "ws://localhost:1234/pong"
|
||||||
|
class_property rounds = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-u uri", "--uri uri", "URI" do |opturi|
|
||||||
|
CLI.uri = opturi
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-r rounds", "--rounds nb-messages", "Nb messages to send." do |opt|
|
||||||
|
CLI.rounds = opt.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
puts parser
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_then_print(ws : WebSocket)
|
||||||
|
m = read ws
|
||||||
|
puts "message: #{String.new(m)}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_then_print_hexa(ws : WebSocket)
|
||||||
|
m = read ws
|
||||||
|
print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def read(ws : WebSocket)
|
||||||
|
puts "reading a message"
|
||||||
|
m = ws.run_once
|
||||||
|
if m.nil?
|
||||||
|
raise "empty message"
|
||||||
|
end
|
||||||
|
|
||||||
|
m
|
||||||
|
end
|
||||||
|
|
||||||
|
def send_with_announce(ws : WebSocket, m : String)
|
||||||
|
puts "sending #{m}"
|
||||||
|
send ws, m
|
||||||
|
end
|
||||||
|
|
||||||
|
def send(ws : WebSocket, m : String | Slice)
|
||||||
|
ws.send m
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
ws = WebSocket.new(URI.parse(CLI.uri))
|
||||||
|
puts "connection done: sending pong"
|
||||||
|
|
||||||
|
ws.on_close do |socket|
|
||||||
|
puts "socket is closing"
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
|
||||||
|
message = if CLI.uri.ends_with? ".JSON"
|
||||||
|
IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_json
|
||||||
|
else
|
||||||
|
IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_packet
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "final message: #{message}"
|
||||||
|
CLI.rounds.times do |i|
|
||||||
|
send ws, message
|
||||||
|
pp! read ws
|
||||||
|
end
|
||||||
|
|
||||||
|
ws.close
|
||||||
|
# pp! ws.run_once
|
||||||
|
rescue e
|
||||||
|
puts "Exception: #{e}"
|
||||||
|
end
|
|
@ -0,0 +1,81 @@
|
||||||
|
|
||||||
|
class Websocketc::Request
|
||||||
|
IPC::JSON.message Test, 2 do
|
||||||
|
def initialize
|
||||||
|
end
|
||||||
|
|
||||||
|
# def read_then_print(ws : WebSocket)
|
||||||
|
# m = read ws
|
||||||
|
# puts "message: #{String.new(m)}"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def read_then_print_hexa(ws : WebSocket)
|
||||||
|
# m = read ws
|
||||||
|
# print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}")
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def read(ws : WebSocket)
|
||||||
|
# puts "reading a message"
|
||||||
|
# m = ws.run_once
|
||||||
|
# if m.nil?
|
||||||
|
# raise "empty message"
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# m
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def send_with_announce(ws : WebSocket, m : String)
|
||||||
|
# puts "sending #{m}"
|
||||||
|
# send ws, m
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# def send(ws : WebSocket, m : String | Slice)
|
||||||
|
# ws.send m
|
||||||
|
# end
|
||||||
|
|
||||||
|
|
||||||
|
def handle(websocketc : Websocketc::Service, event : IPC::Event::Events)
|
||||||
|
Baguette::Log.warning "within the test class"
|
||||||
|
|
||||||
|
# ws = WebSocket.new(URI.parse(Websocketc::Context.uri))
|
||||||
|
# puts "connection done: sending pong"
|
||||||
|
#
|
||||||
|
# ws.on_close do |socket|
|
||||||
|
# puts "socket is closing"
|
||||||
|
# exit 0
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# message = if Websocketc::Context.uri.ends_with? ".JSON"
|
||||||
|
# IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_json
|
||||||
|
# else
|
||||||
|
# IPC::Message.new(0, 2.to_u8, 3.to_u8, STDIN.gets_to_end).to_packet
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# puts "final message: #{message}"
|
||||||
|
# Websocketc::Context.rounds.times do |i|
|
||||||
|
# send ws, message
|
||||||
|
# pp! read ws
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# ws.close
|
||||||
|
## pp! ws.run_once
|
||||||
|
|
||||||
|
# At the end of this function, we should get something like this.
|
||||||
|
# connections[event.fd] = ws
|
||||||
|
|
||||||
|
Response::Success.new
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "during ws init #{e}"
|
||||||
|
Response::Error.new "not implemented"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
Websocketc.requests << Test
|
||||||
|
end
|
||||||
|
|
||||||
|
class Websocketc::Response
|
||||||
|
#IPC::JSON.message TestResponse, 2 do
|
||||||
|
# def initialize
|
||||||
|
# end
|
||||||
|
#end
|
||||||
|
end
|
||||||
|
|
|
@ -1,76 +1,448 @@
|
||||||
require "http/web_socket"
|
require "http/web_socket"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
|
|
||||||
require "./colors"
|
require "ipc"
|
||||||
|
require "baguette-crystal-base"
|
||||||
require "./utils"
|
require "./utils"
|
||||||
|
require "./lib_modifications.cr"
|
||||||
|
|
||||||
uri = "ws://localhost:1234/pong"
|
|
||||||
|
|
||||||
OptionParser.parse do |parser|
|
# Websocketc
|
||||||
parser.on "-u uri", "--uri uri", "URI" do |opturi|
|
# - is the application allowing libipc communications through Web Socket
|
||||||
uri = opturi
|
# - is designed to connect to a Websocketd server, ask for a service, then exchange data
|
||||||
end
|
# - is WIP
|
||||||
|
|
||||||
parser.on "-h", "--help", "Show this help" do
|
# Currently, the exchange format is JSON, encapsulated in JSON:
|
||||||
puts parser
|
# type: Int32 # type of the message
|
||||||
exit 0
|
# payload: JSON::Any? # encapsulated payload
|
||||||
|
#
|
||||||
|
# Websocketd deserialize the type and the payload before sending it to the service.
|
||||||
|
|
||||||
|
|
||||||
|
module Websocketc
|
||||||
|
class Exception < ::Exception
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_then_print(ws : WebSocket)
|
class Context
|
||||||
m = read ws
|
class_property service : Websocketc::Service?
|
||||||
puts "message: #{String.new(m)}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_then_print_hexa(ws : WebSocket)
|
class Baguette::Configuration
|
||||||
m = read ws
|
class Websocketc < IPC
|
||||||
print_hexa(String.new(m), "#{CBLUE}Received message hexa#{CRESET}")
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def read(ws : WebSocket)
|
class Relation
|
||||||
m = ws.read
|
property fd_client : Int32
|
||||||
if m.nil?
|
property fd_service : Int32
|
||||||
raise "empty message"
|
property ws : WebSocket
|
||||||
|
# ws can send JSON messages instead of libipc-formated messages.
|
||||||
|
property is_json : Bool
|
||||||
|
|
||||||
|
property buffer_client : String = String.new
|
||||||
|
property buffer_service : String = String.new
|
||||||
|
|
||||||
|
def related? (fd : Int32)
|
||||||
|
fd == @fd_client || fd == @fd_service
|
||||||
end
|
end
|
||||||
|
|
||||||
m
|
def initialize(@fd_client, @fd_service, @ws, @is_json)
|
||||||
end
|
|
||||||
|
|
||||||
def send_with_announce(ws : WebSocket, m : String)
|
|
||||||
puts "sending #{m}"
|
|
||||||
send ws, m
|
|
||||||
end
|
|
||||||
|
|
||||||
def send(ws : WebSocket, m : String | Slice)
|
|
||||||
ws.send m
|
|
||||||
end
|
|
||||||
|
|
||||||
begin
|
|
||||||
ws = WebSocket.new(URI.parse(uri))
|
|
||||||
|
|
||||||
puts "connection done: sending pong"
|
|
||||||
|
|
||||||
ws.on_close do |socket|
|
|
||||||
puts "socket is closing"
|
|
||||||
exit 0
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if uri.ends_with? ".JSON"
|
def inspect(io) : Nil
|
||||||
json_message = STDIN.gets_to_end
|
to_s io
|
||||||
json_message = json_message.chomp
|
|
||||||
puts "json_message: #{json_message}"
|
|
||||||
final_json_message = "{ \"mtype\": 2, \"payload\": #{json_message} }"
|
|
||||||
puts "final json message: #{final_json_message}"
|
|
||||||
send ws, final_json_message
|
|
||||||
# send ws, "{ \"mtype\": 2, \"payload\": \"coucou\" }"
|
|
||||||
read ws
|
|
||||||
else
|
|
||||||
send ws, to_message(2, STDIN.gets_to_end)
|
|
||||||
read ws
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ws.close
|
def to_s(io)
|
||||||
ws.read
|
c = "%4d" % @fd_client
|
||||||
|
s = "%4d" % @fd_service
|
||||||
|
j = if @is_json
|
||||||
|
"JSON"
|
||||||
|
else
|
||||||
|
"Not JSON"
|
||||||
|
end
|
||||||
|
io << "client #{c} service #{s} #{j}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Hide the complexity of managing relations.
|
||||||
|
class Relations < Array(Relation)
|
||||||
|
property all_fd : Array(Int32) = Array(Int32).new
|
||||||
|
def <<(relation : Relation)
|
||||||
|
Baguette::Log.debug "Adding a new relation: #{relation}"
|
||||||
|
@all_fd << relation.fd_client
|
||||||
|
@all_fd << relation.fd_service
|
||||||
|
super relation
|
||||||
|
end
|
||||||
|
|
||||||
|
def search?(fd : Int32) : Relation?
|
||||||
|
find {|r| r.fd_client == fd || r.fd_service == fd }
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove(fd : Int32)
|
||||||
|
# Baguette::Log.warning "context before removing #{fd}:"
|
||||||
|
# Context.service.not_nil!.print_self
|
||||||
|
|
||||||
|
each do |r|
|
||||||
|
if r.related? fd
|
||||||
|
Baguette::Log.debug "Closing this relation: #{r}"
|
||||||
|
|
||||||
|
# Removing relations and file descriptors from C structures.
|
||||||
|
pointer_ctx = Context.service.not_nil!.pointer
|
||||||
|
LibIPC.ipc_ctx_switching_del pointer_ctx, r.fd_client
|
||||||
|
LibIPC.ipc_del_fd pointer_ctx, r.fd_client
|
||||||
|
LibIPC.ipc_del_fd pointer_ctx, r.fd_service
|
||||||
|
|
||||||
|
# Close these sockets.
|
||||||
|
# Baguette::Log.debug "Closing both #{r.fd_client} and #{r.fd_service} (ws)"
|
||||||
|
begin
|
||||||
|
s = Socket.new r.fd_client, Socket::Family::UNIX, Socket::Type::RAW
|
||||||
|
s.close
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "(ignoring) closing the client socket: #{e}"
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
r.ws.close
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "(ignoring) closing the ws: #{e}"
|
||||||
|
end
|
||||||
|
# Trying not to close this socket (ws already do that).
|
||||||
|
#begin
|
||||||
|
# s = Socket.new r.fd_service, Socket::Family::INET, Socket::Type::STREAM
|
||||||
|
# s.close
|
||||||
|
#rescue e
|
||||||
|
# Baguette::Log.error "(ignoring) closing the service socket: #{e}"
|
||||||
|
#end
|
||||||
|
|
||||||
|
all_fd.select! {|v| v != r.fd_client && v != r.fd_service }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
select! {|r| ! r.related? fd }
|
||||||
|
|
||||||
|
# Baguette::Log.warning "context after removing #{fd}"
|
||||||
|
# Context.service.not_nil!.print_self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
require "./network.cr"
|
||||||
|
|
||||||
|
def ws_cb_in(fd : Int32, pm : LibIPC::Message*, more_to_read : Int16*)
|
||||||
|
Context.service.not_nil!.relations.search?(fd).try do |relation|
|
||||||
|
# Baguette::Log.info "IN fd is #{fd} in relation #{relation}"
|
||||||
|
# Context.service.not_nil!.print_self
|
||||||
|
message = nil
|
||||||
|
begin
|
||||||
|
message = relation.ws.run_once
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "run_once FAILED: #{e}"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
end
|
||||||
|
|
||||||
|
if relation.ws.ws.io.empty?
|
||||||
|
more_to_read[0] = 0
|
||||||
|
else
|
||||||
|
more_to_read[0] = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if relation.ws.closed?
|
||||||
|
Baguette::Log.info "client closed the connection"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
end
|
||||||
|
|
||||||
|
if message.nil?
|
||||||
|
Baguette::Log.error "Reveiced a nil message"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
end
|
||||||
|
|
||||||
|
case message
|
||||||
|
when String
|
||||||
|
if relation.is_json
|
||||||
|
# Reassemble the message.
|
||||||
|
m = relation.buffer_client + message
|
||||||
|
# Clean the buffer.
|
||||||
|
relation.buffer_client = String.new
|
||||||
|
|
||||||
|
begin
|
||||||
|
ipc_message = IPC::Message.from_json m
|
||||||
|
ipc_message.copy_to_message_pointer pm
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "cannot send message coming from #{fd}, message: #{m}"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
Baguette::Log.error "error: #{e}"
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
end
|
||||||
|
|
||||||
|
Baguette::Log.debug "no error reassembling the message"
|
||||||
|
return LibIPC::IPCCB::NoError
|
||||||
|
end
|
||||||
|
Baguette::Log.error "cannot handle non-json messages!"
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
|
||||||
|
when WebSocket::Ping
|
||||||
|
Baguette::Log.debug "TODO: Received a ping message"
|
||||||
|
return LibIPC::IPCCB::Ignore
|
||||||
|
|
||||||
|
when WebSocket::NotFinal
|
||||||
|
Baguette::Log.debug "Received only part of a message"
|
||||||
|
relation.buffer_client += message.message
|
||||||
|
return LibIPC::IPCCB::Ignore
|
||||||
|
|
||||||
|
when Bytes
|
||||||
|
# TODO: we should test the format and maybe its content
|
||||||
|
Baguette::Log.debug "Received a binary message"
|
||||||
|
unless relation.is_json
|
||||||
|
# Reassemble the message.
|
||||||
|
# m = relation.buffer_client.to_slice + message
|
||||||
|
# # Clean the buffer.
|
||||||
|
# relation.buffer_client = String.new
|
||||||
|
# TODO: FIXME: cannot have a buffer for large messages, yet
|
||||||
|
m = message
|
||||||
|
|
||||||
|
begin
|
||||||
|
ipc_message = IPC::Message.from_cbor m
|
||||||
|
ipc_message.copy_to_message_pointer pm
|
||||||
|
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "cannot send message coming from #{fd}, message: #{m}"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
Baguette::Log.error "error: #{e}"
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
end
|
||||||
|
|
||||||
|
Baguette::Log.debug "no error reassembling the message"
|
||||||
|
return LibIPC::IPCCB::NoError
|
||||||
|
end
|
||||||
|
Baguette::Log.error "message received in bytes, yet it should be JSON"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
|
||||||
|
else
|
||||||
|
# every other option should be dropped
|
||||||
|
case message
|
||||||
|
when WebSocket::Error
|
||||||
|
Baguette::Log.error "An error occured"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
when WebSocket::Pong
|
||||||
|
Baguette::Log.error "Received a pong message"
|
||||||
|
return LibIPC::IPCCB::Ignore
|
||||||
|
when WebSocket::Close
|
||||||
|
Baguette::Log.debug "Received a close message"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
else
|
||||||
|
Baguette::Log.error "Received a websocket message with unknown type"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
rescue e
|
rescue e
|
||||||
puts "Exception: #{e}"
|
Baguette::Log.error "Exception (receiving a message) #{e}"
|
||||||
|
# tcp = WrappedTCPFileDescriptor.new(fd: fd, family: Socket::Family::INET)
|
||||||
|
# tcp.close
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ws_cb_out(fd : Int32, pm : Pointer(LibIPC::Message))
|
||||||
|
Context.service.not_nil!.relations.search?(fd).try do |relation|
|
||||||
|
# Baguette::Log.info "OUT fd is #{fd} in relation #{relation}"
|
||||||
|
# Context.service.not_nil!.print_self
|
||||||
|
message = IPC::Message.new pm
|
||||||
|
# Baguette::Log.info "message to send: #{message}"
|
||||||
|
|
||||||
|
if relation.is_json
|
||||||
|
buf = message.to_json
|
||||||
|
else
|
||||||
|
buf = message.to_cbor
|
||||||
|
end
|
||||||
|
|
||||||
|
relation.ws.send buf
|
||||||
|
return LibIPC::IPCCB::NoError
|
||||||
|
end
|
||||||
|
|
||||||
|
Baguette::Log.error "Wait, not supposed to get here. No relation?"
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Exception during message transfer: #{e}"
|
||||||
|
Context.service.not_nil!.relations.remove fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
class Websocketc::Service < IPC::Server
|
||||||
|
property relations : Relations = Relations.new
|
||||||
|
property config : Baguette::Configuration::Websocketc
|
||||||
|
|
||||||
|
def initialize(@config : Baguette::Configuration::Websocketc)
|
||||||
|
super "ws"
|
||||||
|
Context.service = self
|
||||||
|
end
|
||||||
|
|
||||||
|
def print_self
|
||||||
|
Baguette::Log.warning "From C perspective"
|
||||||
|
LibIPC.ipc_ctx_print self.pointer
|
||||||
|
|
||||||
|
Baguette::Log.warning "From Crystal perspective"
|
||||||
|
Baguette::Log.warning "all fd: #{@relations.all_fd.join ", "}"
|
||||||
|
@relations.each do |r| pp! r end
|
||||||
|
Baguette::Log.warning "==="
|
||||||
|
end
|
||||||
|
|
||||||
|
def first_connection(event : IPC::Event::MessageReceived)
|
||||||
|
# First message format: "URI"
|
||||||
|
payload = String.new event.message.payload
|
||||||
|
# Baguette::Log.info "First message received: #{payload}"
|
||||||
|
|
||||||
|
# TODO: handle exceptions and errors
|
||||||
|
|
||||||
|
begin
|
||||||
|
uri = URI.parse payload
|
||||||
|
ws = WebSocket.new uri
|
||||||
|
|
||||||
|
is_json = uri.path.ends_with? ".JSON"
|
||||||
|
|
||||||
|
service_fd = ws.ws.io.as(TCPSocket).fd
|
||||||
|
|
||||||
|
relation = Relation.new event.fd, service_fd, ws, is_json
|
||||||
|
@relations << relation
|
||||||
|
|
||||||
|
# Change client fd status.
|
||||||
|
LibIPC.ipc_ctx_fd_type self.pointer, event.fd, LibIPC::ConnectionType::Switched
|
||||||
|
|
||||||
|
# Add service fd as switched.
|
||||||
|
LibIPC.ipc_add_fd_switched self.pointer, service_fd
|
||||||
|
|
||||||
|
# Link both file descriptors
|
||||||
|
LibIPC.ipc_ctx_switching_add self.pointer, event.fd, service_fd
|
||||||
|
|
||||||
|
# Change service fd callbacks: only the service callbacks are changed,
|
||||||
|
# since the associated client is a simple libipc client
|
||||||
|
|
||||||
|
proc_cb_in = ->ws_cb_in(Int32, Pointer(LibIPC::Message), Int16*)
|
||||||
|
proc_cb_out = ->ws_cb_out(Int32, Pointer(LibIPC::Message))
|
||||||
|
|
||||||
|
LibIPC.ipc_switching_callbacks self.pointer, service_fd, proc_cb_in, proc_cb_out
|
||||||
|
|
||||||
|
# Baguette::Log.debug "new client: #{event.fd}"
|
||||||
|
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "cannot connect to #{payload}: #{e}"
|
||||||
|
end
|
||||||
|
|
||||||
|
send_now event.fd, 1.to_u8, "OK"
|
||||||
|
end
|
||||||
|
|
||||||
|
def run
|
||||||
|
Baguette::Log.title "Starting websocketc"
|
||||||
|
|
||||||
|
@timer = @config.ipc_timer
|
||||||
|
@base_timer = @config.ipc_timer
|
||||||
|
|
||||||
|
self.loop do |event|
|
||||||
|
begin
|
||||||
|
case event
|
||||||
|
when IPC::Event::Timer
|
||||||
|
Baguette::Log.debug "Timer" if @config.print_ipc_timer
|
||||||
|
# print_self
|
||||||
|
|
||||||
|
when IPC::Event::Connection
|
||||||
|
Baguette::Log.info "connection from #{event.fd}" if @config.print_ipc_connection
|
||||||
|
|
||||||
|
when IPC::Event::Disconnection
|
||||||
|
Baguette::Log.info "disconnection from #{event.fd}" if @config.print_ipc_disconnection
|
||||||
|
@relations.remove event.fd
|
||||||
|
|
||||||
|
when IPC::Event::MessageSent
|
||||||
|
Baguette::Log.info "message sent to #{event.fd}" if @config.print_ipc_message_sent
|
||||||
|
|
||||||
|
when IPC::Event::MessageReceived
|
||||||
|
Baguette::Log.info "message received from #{event.fd}" if @config.print_ipc_message_received
|
||||||
|
if r = @relations.search? event.fd
|
||||||
|
Baguette::Log.error "MessageReceived but from an already existent relation"
|
||||||
|
Baguette::Log.error "relation: #{r}"
|
||||||
|
exit 1
|
||||||
|
else
|
||||||
|
first_connection event
|
||||||
|
end
|
||||||
|
|
||||||
|
when IPC::Event::Switch
|
||||||
|
Baguette::Log.debug "switched message from #{event.fd}" if @config.print_ipc_switch
|
||||||
|
|
||||||
|
when IPC::Event::EventNotSet
|
||||||
|
Baguette::Log.error "Event not set: #{event.fd}"
|
||||||
|
@relations.remove event.fd
|
||||||
|
begin
|
||||||
|
s = Socket.new event.fd, Socket::Family::UNIX, Socket::Type::RAW
|
||||||
|
s.close
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.warning "cannot close the socket #{event.fd}: #{e} (ignoring)"
|
||||||
|
end
|
||||||
|
|
||||||
|
when IPC::Event::Error
|
||||||
|
Baguette::Log.error "Event Error on fd #{event.fd} (removing it)"
|
||||||
|
@relations.remove event.fd
|
||||||
|
|
||||||
|
when IPC::Exception
|
||||||
|
Baguette::Log.error "IPC::Exception: #{event.message}"
|
||||||
|
|
||||||
|
if event.message == "closed recipient"
|
||||||
|
Baguette::Log.error "Bloody closed recipient!"
|
||||||
|
# @relations.remove #
|
||||||
|
else
|
||||||
|
Baguette::Log.error "CLOSING WS"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
Baguette::Log.warning "unhandled IPC event: #{event.class}"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Exception during event handling: #{typeof(e)} - #{e.message}"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.from_cli
|
||||||
|
|
||||||
|
# First option parsing.
|
||||||
|
simulation, no_configuration, configuration_file = Baguette::Configuration.option_parser
|
||||||
|
|
||||||
|
# Websocketc configuration.
|
||||||
|
configuration = if no_configuration
|
||||||
|
Baguette::Log.info "do not load a configuration file."
|
||||||
|
Baguette::Configuration::Websocketc.new
|
||||||
|
else
|
||||||
|
# In case there is a configuration file helping with the parameters.
|
||||||
|
Baguette::Configuration::Websocketc.get(configuration_file) ||
|
||||||
|
Baguette::Configuration::Websocketc.new
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
puts parser
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Baguette::Context.verbosity = configuration.verbosity
|
||||||
|
|
||||||
|
if simulation
|
||||||
|
pp! configuration
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
|
||||||
|
::Websocketc::Service.new configuration
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Websocketc::Service.from_cli.run
|
||||||
|
|
|
@ -0,0 +1,343 @@
|
||||||
|
require "option_parser"
|
||||||
|
require "ipc"
|
||||||
|
require "socket"
|
||||||
|
require "./colors"
|
||||||
|
|
||||||
|
require "colorize"
|
||||||
|
|
||||||
|
require "json"
|
||||||
|
|
||||||
|
require "socket"
|
||||||
|
require "http/server"
|
||||||
|
require "base64"
|
||||||
|
require "digest"
|
||||||
|
require "./utils"
|
||||||
|
|
||||||
|
require "baguette-crystal-base"
|
||||||
|
|
||||||
|
require "./instance_storage.cr"
|
||||||
|
|
||||||
|
# All modifications to standard libraries go there.
|
||||||
|
require "./lib_modifications.cr"
|
||||||
|
|
||||||
|
class Context
|
||||||
|
# service instance parameters
|
||||||
|
# they can be changed via the cli
|
||||||
|
class_property service_name : String = "websocket"
|
||||||
|
class_property host : String = "0.0.0.0"
|
||||||
|
class_property port_to_listen : UInt16 = 1234
|
||||||
|
class_property timer_delay : Int32 = 30_000 # 30 seconds
|
||||||
|
end
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
||||||
|
Context.host = h
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-p port", "--port port", "Port to listen on." do |port|
|
||||||
|
Context.port_to_listen = port.to_u16
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
||||||
|
Context.service_name = name
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
||||||
|
Context.timer_delay = t.to_i32 * 1000 # stored in ms
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
|
||||||
|
Baguette::Context.verbosity = opt.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
puts parser
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sending_ping_messages
|
||||||
|
Context.context.fd_to_websocket.each do |fd, ws|
|
||||||
|
begin
|
||||||
|
ws.ping "hello from #{fd}"
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Exception: #{e}, already closed client #{fd}"
|
||||||
|
begin
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Cannot remove #{fd} from clients: #{e}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def ws_http_upgrade(client)
|
||||||
|
request = HTTP::Request.from_io client
|
||||||
|
|
||||||
|
if request.nil?
|
||||||
|
raise "#REQUEST IS NIL"
|
||||||
|
end
|
||||||
|
|
||||||
|
if request.is_a? HTTP::Status && request == HTTP::Status::BAD_REQUEST
|
||||||
|
raise "BAD REQUEST DAZE~"
|
||||||
|
end
|
||||||
|
|
||||||
|
if request.is_a? HTTP::Status
|
||||||
|
raise "Not bad request but still pretty bad: #{request.to_s}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
# Baguette::Log.debug response_key
|
||||||
|
|
||||||
|
# HTTP inside bru
|
||||||
|
headers_header = "HTTP/1.1 101 Switching Protocols"
|
||||||
|
headers = HTTP::Headers {
|
||||||
|
"Upgrade" => "websocket",
|
||||||
|
"Connection" => "Upgrade",
|
||||||
|
"Sec-WebSocket-Accept" => response_key
|
||||||
|
}
|
||||||
|
|
||||||
|
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
|
||||||
|
|
||||||
|
# requested service, fd
|
||||||
|
req_service = request.path.lchop.sub "ws/", ""
|
||||||
|
|
||||||
|
if req_service.ends_with? ".JSON"
|
||||||
|
Context.context.is_json[client.fd] = true
|
||||||
|
req_service = req_service.gsub /.JSON$/, ""
|
||||||
|
else
|
||||||
|
Context.context.is_json[client.fd] = false
|
||||||
|
end
|
||||||
|
|
||||||
|
# puts "#{headers_header}\n#{headers.to_s}\r\n"
|
||||||
|
client.send "#{headers_header}\n#{headers.to_s}\r\n"
|
||||||
|
|
||||||
|
wsclient = WebSocket.new client
|
||||||
|
wsclient.on_pong do |m|
|
||||||
|
Baguette::Log.debug "pong #{m}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# registering the client into storing structures to avoid being garbage collected
|
||||||
|
Context.context.fd_to_socket[client.fd] = client
|
||||||
|
Context.context.fd_to_websocket[client.fd] = wsclient
|
||||||
|
|
||||||
|
return req_service, request
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Exception in ws_http_upgrade: #{e}"
|
||||||
|
raise "DROP IT"
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_new_clients(service, server)
|
||||||
|
# TODO: this is a lot of C-like notation, should be changed.
|
||||||
|
# In the client accept function
|
||||||
|
# 1. accept TCP/WS connection
|
||||||
|
client = server.accept
|
||||||
|
|
||||||
|
# 2. upgrade HTTP connections and get the service name in the URI path.
|
||||||
|
req_service, request = ws_http_upgrade client
|
||||||
|
|
||||||
|
# 3. connect to the service via ipc_connection_switched
|
||||||
|
serverfd : Int32 = 0
|
||||||
|
# LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, Pointer(Libc::Int).null
|
||||||
|
spointer = pointerof(serverfd)
|
||||||
|
if req_service == "tracker"
|
||||||
|
req_service = "tracking"
|
||||||
|
end
|
||||||
|
LibIPC.ipc_connection_switched service.pointer, req_service, client.fd, spointer
|
||||||
|
if serverfd == 0
|
||||||
|
raise "Could not connect to #{req_service}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# 4. change client callbacks via ipc_switching_callbacks
|
||||||
|
# only the client callbacks are changed, since the associated server is a simple libipc service
|
||||||
|
proc_cb_in = ->ws_cb_in(Int32, Pointer(LibIPC::Message), Int16*)
|
||||||
|
proc_cb_out = ->ws_cb_out(Int32, Pointer(LibIPC::Message))
|
||||||
|
|
||||||
|
LibIPC.ipc_switching_callbacks service.pointer, client.fd, proc_cb_in, proc_cb_out
|
||||||
|
|
||||||
|
# 5. In case the requested service is trackingd,
|
||||||
|
# the websocketd has to provide the IP address as the first message.
|
||||||
|
if req_service == "tracking"
|
||||||
|
# The proxy is configured to provide the X-Real-IP in the headers.
|
||||||
|
# If not found, we use the client address from the socket.
|
||||||
|
real_ip_address = request.headers["X-Real-IP"] || client.remote_address.address
|
||||||
|
|
||||||
|
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
|
||||||
|
# message = IPC::Message.new mtype, utype, payload
|
||||||
|
# remote_address = client.remote_address.address
|
||||||
|
message = IPC::Message.new serverfd, 1, 1.to_u8, "{\"ipaddress\": \"#{real_ip_address}\"}"
|
||||||
|
service.send message
|
||||||
|
# serv = WrappedTCPFileDescriptor.new(fd: serverfd, family: Socket::Family::INET)
|
||||||
|
# serv.send message.to_packet
|
||||||
|
end
|
||||||
|
|
||||||
|
Baguette::Log.debug "new client: #{client.fd}"
|
||||||
|
rescue 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))
|
||||||
|
# Baguette::Log.info "OUT fd is #{fd}"
|
||||||
|
wsclient = Context.context.fd_to_websocket[fd]
|
||||||
|
|
||||||
|
message = IPC::Message.new pm
|
||||||
|
# pp! message
|
||||||
|
|
||||||
|
if Context.context.is_json[fd]
|
||||||
|
buf = message.to_json
|
||||||
|
else
|
||||||
|
buf = message.to_packet
|
||||||
|
end
|
||||||
|
|
||||||
|
wsclient.send buf
|
||||||
|
|
||||||
|
return LibIPC::IPCCB::NoError
|
||||||
|
rescue 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*)
|
||||||
|
# Baguette::Log.debug "IN fd is #{fd}"
|
||||||
|
|
||||||
|
wsclient = Context.context.fd_to_websocket[fd]
|
||||||
|
|
||||||
|
message = nil
|
||||||
|
begin
|
||||||
|
message = wsclient.run_once
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "run_once FAILED: #{e}"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
end
|
||||||
|
|
||||||
|
if wsclient.ws.io.empty?
|
||||||
|
more_to_read[0] = 0
|
||||||
|
else
|
||||||
|
more_to_read[0] = 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if wsclient.closed?
|
||||||
|
Baguette::Log.info "client closed the connection"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
end
|
||||||
|
|
||||||
|
if message.nil?
|
||||||
|
Baguette::Log.error "Reveiced a nil message"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
end
|
||||||
|
|
||||||
|
case message
|
||||||
|
when String
|
||||||
|
if Context.context.is_json[fd]
|
||||||
|
ipc_message = IPC::Message.from_json(message)
|
||||||
|
ipc_message.copy_to_message_pointer pm
|
||||||
|
return LibIPC::IPCCB::NoError
|
||||||
|
end
|
||||||
|
else
|
||||||
|
# every other option should be dropped
|
||||||
|
case message
|
||||||
|
when WebSocket::Error
|
||||||
|
Baguette::Log.error "An error occured"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
when WebSocket::Ping
|
||||||
|
Baguette::Log.debug "Received a ping message"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Ignore
|
||||||
|
when WebSocket::Pong
|
||||||
|
Baguette::Log.debug "Received a pong message"
|
||||||
|
return LibIPC::IPCCB::Ignore
|
||||||
|
when WebSocket::Close
|
||||||
|
Baguette::Log.debug "Received a close message"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Closing
|
||||||
|
when WebSocket::NotFinal
|
||||||
|
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
|
||||||
|
Baguette::Log.error "Received a binary message: NOT IMPLEMENTED, YET"
|
||||||
|
Context.context.remove_fd fd
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
else
|
||||||
|
Baguette::Log.error "Received a websocket message with unknown type"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
|
||||||
|
rescue 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
|
||||||
|
return LibIPC::IPCCB::Error
|
||||||
|
end
|
||||||
|
|
||||||
|
class Context
|
||||||
|
class_property context = InstanceStorage.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def main
|
||||||
|
# by default, listen on any IP address
|
||||||
|
server = TCPServer.new(Context.host, Context.port_to_listen)
|
||||||
|
service = IPC::Server.new Context.service_name
|
||||||
|
service << server.fd
|
||||||
|
|
||||||
|
# Every few seconds, the service should trigger the timer
|
||||||
|
# Allowing the sending of Ping messages to clients
|
||||||
|
service.base_timer = Context.timer_delay
|
||||||
|
service.timer = Context.timer_delay
|
||||||
|
|
||||||
|
service.loop do |event|
|
||||||
|
# Baguette::Log.info "current state of the context:"
|
||||||
|
# service.pp
|
||||||
|
case event
|
||||||
|
when IPC::Event::Timer
|
||||||
|
Baguette::Log.debug "IPC::Event::Timer"
|
||||||
|
sending_ping_messages
|
||||||
|
|
||||||
|
when IPC::Event::Connection
|
||||||
|
Baguette::Log.debug "IPC::Event::Connection: fd #{event.fd}"
|
||||||
|
|
||||||
|
when IPC::Event::Disconnection
|
||||||
|
Baguette::Log.debug "IPC::Event::Disconnection: fd #{event.fd}"
|
||||||
|
Context.context.remove_fd event.fd
|
||||||
|
|
||||||
|
when IPC::Event::ExtraSocket
|
||||||
|
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
|
||||||
|
Baguette::Log.debug "IPC::Event::Switch: fd #{event.fd}"
|
||||||
|
# raise "Not implemented."
|
||||||
|
|
||||||
|
when IPC::Event::MessageSent
|
||||||
|
Baguette::Log.debug "IPC::Event::MessageSent: fd #{event.fd}"
|
||||||
|
|
||||||
|
when IPC::Event::MessageReceived
|
||||||
|
Baguette::Log.debug "IPC::Event::Message: fd #{event.fd}"
|
||||||
|
|
||||||
|
raise "Not implemented."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
main
|
|
@ -1,5 +1,6 @@
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
require "ipc"
|
require "ipc"
|
||||||
|
require "ipc/json"
|
||||||
require "socket"
|
require "socket"
|
||||||
require "./colors"
|
require "./colors"
|
||||||
|
|
||||||
|
@ -11,37 +12,71 @@ require "base64"
|
||||||
require "digest"
|
require "digest"
|
||||||
require "./utils"
|
require "./utils"
|
||||||
|
|
||||||
|
require "baguette-crystal-base"
|
||||||
|
|
||||||
|
require "colorize"
|
||||||
|
|
||||||
# All modifications to standard libraries go there.
|
# All modifications to standard libraries go there.
|
||||||
require "./lib_modifications.cr"
|
require "./lib_modifications.cr"
|
||||||
|
|
||||||
# service instance parameters
|
class Tracking::Request
|
||||||
# they can be changed via the cli
|
IPC::JSON.message IpAddress, 1 do
|
||||||
service_name = "websocket"
|
property ipaddress : String?
|
||||||
host = "0.0.0.0"
|
|
||||||
port_to_listen = 1234
|
def initialize(@ipaddress = nil)
|
||||||
timer_delay = 30_000.to_f64 # 30 seconds
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Baguette::Configuration
|
||||||
|
class Websocket < IPC
|
||||||
|
# service instance parameters
|
||||||
|
# they can be changed via the cli
|
||||||
|
property service_name : String = "websocket"
|
||||||
|
property host : String = "0.0.0.0"
|
||||||
|
property port : UInt16 = 1234
|
||||||
|
|
||||||
|
property print_messages : Bool = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
simulation, no_configuration, configuration_file = Baguette::Configuration.option_parser
|
||||||
|
|
||||||
|
configuration = if no_configuration
|
||||||
|
Baguette::Log.info "do not load a configuration file."
|
||||||
|
Baguette::Configuration::Websocket.new
|
||||||
|
else
|
||||||
|
Baguette::Configuration::Websocket.get(configuration_file) ||
|
||||||
|
Baguette::Configuration::Websocket.new
|
||||||
|
end
|
||||||
|
|
||||||
|
Baguette::Context.verbosity = configuration.verbosity
|
||||||
|
|
||||||
verbosity = 1
|
|
||||||
|
|
||||||
OptionParser.parse do |parser|
|
OptionParser.parse do |parser|
|
||||||
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
||||||
host = h
|
configuration.host = h
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-p port", "--port port", "Port to listen on." do |port|
|
parser.on "-p port", "--port port", "Port to listen on." do |port|
|
||||||
port_to_listen = port.to_u16
|
configuration.port = port.to_u16
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
||||||
service_name = name
|
configuration.service_name = name
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
||||||
timer_delay = t.to_f64 * 1000 # stored in ms
|
configuration.ipc_timer = t.to_i32 * 1000
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
|
parser.on "-T", "--print-timer", "Print timer." do
|
||||||
verbosity = opt.to_i
|
configuration.print_ipc_timer = true
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-M", "--print-messages", "Print messages received and sent." do
|
||||||
|
configuration.print_messages = true
|
||||||
|
Baguette::Log.info "print messages"
|
||||||
end
|
end
|
||||||
|
|
||||||
parser.on "-h", "--help", "Show this help" do
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
@ -51,7 +86,7 @@ OptionParser.parse do |parser|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# Link between fd and TCPSocket, WebSocket and IPC::Connection instances.
|
# Link between fd and TCPSocket, WebSocket and IPC::Client instances.
|
||||||
class InstanceStorage
|
class InstanceStorage
|
||||||
property service : IPC::SwitchingService
|
property service : IPC::SwitchingService
|
||||||
property switchtable : Hash(Int32, Int32)
|
property switchtable : Hash(Int32, Int32)
|
||||||
|
@ -59,7 +94,8 @@ class InstanceStorage
|
||||||
property is_json : Hash(Int32, Bool)
|
property is_json : Hash(Int32, Bool)
|
||||||
property fd_to_tcpsocket : Hash(Int32, TCPSocket)
|
property fd_to_tcpsocket : Hash(Int32, TCPSocket)
|
||||||
property fd_to_websocket : Hash(Int32, WebSocket)
|
property fd_to_websocket : Hash(Int32, WebSocket)
|
||||||
property fd_to_ipcconnection : Hash(Int32, IPC::Connection)
|
property fd_to_ipcclient : Hash(Int32, IPC::Client)
|
||||||
|
property fd_to_buffer : Hash(Int32, String)
|
||||||
|
|
||||||
def initialize (@service : IPC::SwitchingService)
|
def initialize (@service : IPC::SwitchingService)
|
||||||
# fdlist_client = [] of TCPSocket
|
# fdlist_client = [] of TCPSocket
|
||||||
|
@ -68,26 +104,47 @@ class InstanceStorage
|
||||||
@is_json = Hash(Int32,Bool).new
|
@is_json = Hash(Int32,Bool).new
|
||||||
@fd_to_tcpsocket = Hash(Int32, TCPSocket).new
|
@fd_to_tcpsocket = Hash(Int32, TCPSocket).new
|
||||||
@fd_to_websocket = Hash(Int32, WebSocket).new
|
@fd_to_websocket = Hash(Int32, WebSocket).new
|
||||||
@fd_to_ipcconnection = Hash(Int32, IPC::Connection).new
|
@fd_to_ipcclient = Hash(Int32, IPC::Client).new
|
||||||
|
@fd_to_buffer = Hash(Int32, String).new
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_fd (fdclient : Int32)
|
def remove_fd (fdclient : Int32)
|
||||||
puts "#{CBLUE}closing the client:#{CRESET} #{fdclient}"
|
Baguette::Log.info "closing the client:#{CRESET} #{fdclient}"
|
||||||
# 1. closing both the client and the service
|
if fdclient == -1
|
||||||
fdservice = @switchtable[fdclient]?
|
raise "fdclient is -1, nothing to do with it"
|
||||||
tcpfdc = @fd_to_tcpsocket[fdclient]
|
end
|
||||||
|
|
||||||
# 2. closing the TCP connections
|
begin
|
||||||
tcpfdc.close unless tcpfdc.closed?
|
# 1. closing both the client and the service
|
||||||
|
tcpfdc = @fd_to_tcpsocket[fdclient]
|
||||||
|
|
||||||
|
# 2. closing the TCP connections
|
||||||
|
tcpfdc.close unless tcpfdc.closed?
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "remove_fd: 1 #{e}"
|
||||||
|
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
|
||||||
@service.remove_fd (fdclient)
|
begin
|
||||||
|
@service.remove_fd (fdclient)
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "remove_fd: 2 #{e}"
|
||||||
|
end
|
||||||
|
|
||||||
# 5. removing both the client and the service from the switchtable
|
# 5. removing both the client and the service from the switchtable
|
||||||
|
fdservice = @switchtable[fdclient]?
|
||||||
@switchtable = @switchtable.select do |fdc, fds|
|
@switchtable = @switchtable.select do |fdc, fds|
|
||||||
fdc != fdclient && fds != fdclient
|
fdc != fdclient && fds != fdclient
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@fd_to_buffer.select! do |fd, buffer|
|
||||||
|
if fdservice.nil?
|
||||||
|
fd != fdclient
|
||||||
|
else
|
||||||
|
fd != fdclient && fd != fdservice
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# 6. removing the client and the service from is_client
|
# 6. removing the client and the service from is_client
|
||||||
@is_client = @is_client.select do |fd,v| fd != fdclient end
|
@is_client = @is_client.select do |fd,v| fd != fdclient end
|
||||||
@is_json = @is_json.select do |fd,v| fd != fdclient end
|
@is_json = @is_json.select do |fd,v| fd != fdclient end
|
||||||
|
@ -96,30 +153,63 @@ class InstanceStorage
|
||||||
fd != fdclient
|
fd != fdclient
|
||||||
end
|
end
|
||||||
|
|
||||||
unless fdservice.nil?
|
begin
|
||||||
service = @fd_to_ipcconnection[fdservice]
|
if fdservice.nil?
|
||||||
service.close
|
Baguette::Log.debug "client #{fdclient} aleady has its service removed"
|
||||||
|
else
|
||||||
@service.remove_fd (fdservice)
|
@service.remove_fd (fdservice)
|
||||||
@fd_to_ipcconnection = @fd_to_ipcconnection.select do |k, v|
|
|
||||||
|
service = @fd_to_ipcclient[fdservice]
|
||||||
|
@fd_to_ipcclient = @fd_to_ipcclient.select do |k, v|
|
||||||
k != fdservice
|
k != fdservice
|
||||||
end
|
end
|
||||||
|
|
||||||
@is_client = @is_client.select do |fd,v| fd != fdservice end
|
@is_client = @is_client.select do |fd,v| fd != fdservice end
|
||||||
|
|
||||||
|
# perform the close at the end
|
||||||
|
# on crash, this service still is removed from the list of listened fd
|
||||||
|
service.close
|
||||||
end
|
end
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "remove_fd: 3 #{e}"
|
||||||
|
end
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "in InstanceStorage#remove_fd: #{e}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Context
|
||||||
|
class_property service_name = "websocketd"
|
||||||
|
end
|
||||||
|
|
||||||
|
Context.service_name = configuration.service_name
|
||||||
|
|
||||||
|
class Context
|
||||||
|
class_property service = IPC::SwitchingService.new service_name
|
||||||
|
class_property context = InstanceStorage.new service
|
||||||
|
class_property print_messages = false
|
||||||
|
class_property print_timer = false
|
||||||
|
end
|
||||||
|
|
||||||
|
Context.print_messages = configuration.print_messages
|
||||||
|
Context.print_timer = configuration.print_ipc_timer
|
||||||
|
|
||||||
|
|
||||||
|
if simulation
|
||||||
|
pp! configuration
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# by default, listen on any IP address
|
# by default, listen on any IP address
|
||||||
server = TCPServer.new(host, port_to_listen)
|
server = TCPServer.new configuration.host, configuration.port
|
||||||
service = IPC::SwitchingService.new service_name
|
Context.service << server.fd
|
||||||
service << server.fd
|
|
||||||
context = InstanceStorage.new service
|
|
||||||
|
|
||||||
|
def websocket_client_connection(client)
|
||||||
|
|
||||||
def websocket_client_connection(client, context : InstanceStorage)
|
|
||||||
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"
|
||||||
|
@ -150,7 +240,7 @@ def websocket_client_connection(client, context : InstanceStorage)
|
||||||
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
|
headers = headers.map { |key, value| "#{key}: #{value[0]}\r\n" }.join
|
||||||
|
|
||||||
# requested service, fd
|
# requested service, fd
|
||||||
req_service = request.path.lchop
|
req_service = request.path.lchop.sub "ws/", ""
|
||||||
if req_service.empty?
|
if req_service.empty?
|
||||||
client.close
|
client.close
|
||||||
return
|
return
|
||||||
|
@ -160,49 +250,55 @@ def websocket_client_connection(client, context : InstanceStorage)
|
||||||
# To that end, the client may send the name of the service it wants to reach with the prefix ".JSON".
|
# To that end, the client may send the name of the service it wants to reach with the prefix ".JSON".
|
||||||
|
|
||||||
if req_service.ends_with? ".JSON"
|
if req_service.ends_with? ".JSON"
|
||||||
context.is_json[client.fd] = true
|
Context.context.is_json[client.fd] = true
|
||||||
req_service = req_service.gsub /.JSON$/, ""
|
req_service = req_service.gsub /.JSON$/, ""
|
||||||
else
|
else
|
||||||
context.is_json[client.fd] = false
|
Context.context.is_json[client.fd] = false
|
||||||
end
|
end
|
||||||
|
|
||||||
websocket_connection_procedure req_service, client.fd, context
|
# Hack to preserve the daemon naming convention.
|
||||||
|
|
||||||
# TODO: if trackerd, send the IP address of the client
|
|
||||||
if req_service == "tracker"
|
if req_service == "tracker"
|
||||||
puts "tracker - sending the IP address"
|
req_service = "tracking"
|
||||||
puts "connection from #{client.remote_address}"
|
|
||||||
sfd = context.switchtable[client.fd]
|
|
||||||
# message = IPC::Message.from_json(JSON).to_packet
|
|
||||||
# => JSON has to include these attributes: mtype, utype, payload
|
|
||||||
# message = IPC::Message.new mtype, utype, payload
|
|
||||||
remote_address = client.remote_address.address
|
|
||||||
message = IPC::Message.new 1, 1.to_u8, "{\"ipaddress\": \"#{remote_address}\"}"
|
|
||||||
serv = WrappedTCPFileDescriptor.new(fd: sfd, family: Socket::Family::INET)
|
|
||||||
serv.send message.to_packet
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# puts "#{headers_header}\n#{headers.to_s}\r\n"
|
websocket_connection_procedure req_service, client.fd
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
sfd = Context.context.switchtable[client.fd]
|
||||||
|
Baguette::Log.info "trackingd - sending the IP address #{real_ip_address} to fd #{sfd}"
|
||||||
|
message = Tracking::Request::IpAddress.new real_ip_address
|
||||||
|
|
||||||
|
if Context.print_messages
|
||||||
|
Baguette::Log.info "to trackingd: #{message.to_s}"
|
||||||
|
end
|
||||||
|
Context.service.send_now sfd, message
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
wsclient.on_pong do |m|
|
wsclient.on_pong do |m|
|
||||||
puts "pong #{m}"
|
Baguette::Log.debug "pong #{m}"
|
||||||
end
|
end
|
||||||
context.is_client[client.fd] = true
|
Context.context.is_client[client.fd] = true
|
||||||
|
|
||||||
# listen to the client's file descriptor
|
# listen to the client's file descriptor
|
||||||
context.service << client.fd
|
Context.context.service << client.fd
|
||||||
# puts "#{CBLUE}new client: #{client.fd}#{CRESET}"
|
|
||||||
|
|
||||||
# registering the client into storing structures to avoid being garbage collected
|
# registering the client into storing structures to avoid being garbage collected
|
||||||
context.fd_to_tcpsocket[client.fd] = client
|
Context.context.fd_to_tcpsocket[client.fd] = client
|
||||||
context.fd_to_websocket[client.fd] = wsclient
|
Context.context.fd_to_websocket[client.fd] = wsclient
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def closing_client (fdclient : Int)
|
||||||
def closing_client (fdclient : Int, context : InstanceStorage)
|
Baguette::Log.warning "Closing client #{fdclient}"
|
||||||
context.remove_fd fdclient
|
Context.context.remove_fd fdclient
|
||||||
end
|
end
|
||||||
|
|
||||||
# first message from the client: requested service name
|
# first message from the client: requested service name
|
||||||
|
@ -210,143 +306,235 @@ end
|
||||||
# 2. listening on the service fd
|
# 2. listening on the service fd
|
||||||
# 3. bounding both file descriptors (through switchtable hash)
|
# 3. bounding both file descriptors (through switchtable hash)
|
||||||
# 4. indicating that the client is connected (in is_client)
|
# 4. indicating that the client is connected (in is_client)
|
||||||
def websocket_connection_procedure (requested_service : String, clientfd : Int32, context : InstanceStorage)
|
def websocket_connection_procedure (requested_service : String, clientfd : Int32)
|
||||||
begin
|
begin
|
||||||
# 2. establishing a connection to the service
|
# 1. establishing a connection to the service
|
||||||
newservice = IPC::Connection.new requested_service
|
newservice = IPC::Client.new requested_service
|
||||||
context.fd_to_ipcconnection[newservice.fd] = newservice
|
new_service_fd = newservice.fd.not_nil!
|
||||||
|
Context.context.fd_to_ipcclient[new_service_fd] = newservice
|
||||||
|
|
||||||
# 3. listening on the client fd and the service fd
|
# 2. listening on the client fd and the service fd
|
||||||
context.service << newservice.fd
|
Context.context.service << new_service_fd
|
||||||
|
|
||||||
# cannot perform automatic switching due to websockets headers
|
# cannot perform automatic switching due to websockets headers
|
||||||
# future version of the libipc lib should include some workaround, probably
|
# future version of the libipc lib should include some workaround, probably
|
||||||
# service.switch.add fdclient, newservice.fd
|
# service.switch.add fdclient, newservice.fd
|
||||||
|
|
||||||
# 4. bounding the client and the service fd
|
# 3. bounding the client and the service fd
|
||||||
context.switchtable[clientfd] = newservice.fd
|
Context.context.switchtable[clientfd] = new_service_fd
|
||||||
context.switchtable[newservice.fd] = clientfd
|
Context.context.switchtable[new_service_fd] = clientfd
|
||||||
|
|
||||||
# 5. the client is then connected, send it a message
|
# 4. the client is then connected, send it a message
|
||||||
context.is_client[clientfd] = true
|
Context.context.is_client[clientfd] = true
|
||||||
context.is_client[newservice.fd] = false
|
Context.context.is_client[new_service_fd] = false
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}"
|
Baguette::Log.error "Exception during connection to the service: #{e}"
|
||||||
context.remove_fd clientfd
|
Context.context.remove_fd clientfd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
|
def print_message(message : Bytes, origin : Int32)
|
||||||
|
return unless Context.print_messages
|
||||||
|
|
||||||
|
client = if Context.context.is_client[origin]
|
||||||
|
"client"
|
||||||
|
else
|
||||||
|
"server"
|
||||||
|
end
|
||||||
|
Baguette::Log.info "received message from #{origin} (#{client}):"
|
||||||
|
Baguette::Log.info "#{message.hexstring}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def print_message(message : String, origin : Int32)
|
||||||
|
return unless Context.print_messages
|
||||||
|
|
||||||
|
client = if Context.context.is_client[origin]
|
||||||
|
"client"
|
||||||
|
else
|
||||||
|
"server"
|
||||||
|
end
|
||||||
|
Baguette::Log.info "received message from #{origin} (#{client}):"
|
||||||
|
pp! JSON.parse message
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.warning "cannot see the message from #{origin} (#{client}): #{e}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# WARNING: currently this is only covering String messages.
|
||||||
|
# Byte messages aren't addressed, yet.
|
||||||
|
def websocket_switching_procedure (activefd : Int)
|
||||||
begin
|
begin
|
||||||
if context.is_client[activefd]
|
# Baguette::Log.title "activefd is #{activefd}"
|
||||||
|
if Context.context.is_client[activefd]
|
||||||
|
# Baguette::Log.title "activefd #{activefd} is a client"
|
||||||
# The client is a WebSocket on top of a TCP connection
|
# The client is a WebSocket on top of a TCP connection
|
||||||
client = context.fd_to_tcpsocket[activefd]
|
client = Context.context.fd_to_tcpsocket[activefd]
|
||||||
wsclient = context.fd_to_websocket[activefd]
|
wsclient = Context.context.fd_to_websocket[activefd]
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
begin
|
begin
|
||||||
message = wsclient.run_once
|
message = wsclient.run_once
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
Baguette::Log.error "Exception (receiving a message) #{e}"
|
||||||
context.remove_fd activefd
|
Context.context.remove_fd activefd
|
||||||
return
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checking the internals of WebSocket, then the contained IO within, to know if there is still something to read in the socket.
|
# Checking the internals of WebSocket, then the contained IO within, to know if there is still something to read in the socket.
|
||||||
still_something_to_read = ! wsclient.ws.io.empty?
|
still_something_to_read = ! wsclient.ws.io.empty?
|
||||||
|
|
||||||
if wsclient.closed?
|
if wsclient.closed?
|
||||||
# puts "#{CBLUE}client is closed#{CRESET}"
|
Baguette::Log.info "client #{activefd} is closing"
|
||||||
context.remove_fd client.fd
|
Context.context.remove_fd activefd
|
||||||
return
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
if message.nil?
|
if message.nil?
|
||||||
puts "#{CRED}message is nil#{CRESET}"
|
Baguette::Log.error "message received from #{activefd} is nil"
|
||||||
context.remove_fd client.fd
|
Context.context.remove_fd activefd
|
||||||
return
|
if still_something_to_read
|
||||||
|
Baguette::Log.info "Still something to read, but #{activefd} was removed"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
case message
|
case message
|
||||||
when WebSocket::Error
|
when WebSocket::Error
|
||||||
puts "#{CRED}An error occured#{CRESET}"
|
Baguette::Log.error "An error occured with client #{activefd}"
|
||||||
context.remove_fd client.fd
|
Context.context.remove_fd activefd
|
||||||
|
if still_something_to_read
|
||||||
|
Baguette::Log.debug "Still something to read, but #{activefd} was removed"
|
||||||
|
next
|
||||||
|
end
|
||||||
return
|
return
|
||||||
|
|
||||||
when WebSocket::Ping
|
when WebSocket::Ping
|
||||||
# puts "#{CBLUE}Received a ping message#{CRESET}"
|
Baguette::Log.debug "Received a ping message"
|
||||||
next if still_something_to_read
|
if still_something_to_read
|
||||||
|
Baguette::Log.debug "Still something to read"
|
||||||
|
next
|
||||||
|
end
|
||||||
break
|
break
|
||||||
|
|
||||||
when WebSocket::Pong
|
when WebSocket::Pong
|
||||||
# puts "#{CBLUE}Received a pong message#{CRESET}"
|
Baguette::Log.debug "Received a pong message"
|
||||||
next if still_something_to_read
|
if still_something_to_read
|
||||||
|
Baguette::Log.debug "Still something to read"
|
||||||
|
next
|
||||||
|
end
|
||||||
break
|
break
|
||||||
|
|
||||||
when WebSocket::Close
|
when WebSocket::Close
|
||||||
# puts "#{CBLUE}Received a close message#{CRESET}"
|
Baguette::Log.debug "Received a close message"
|
||||||
context.remove_fd client.fd
|
Context.context.remove_fd activefd
|
||||||
return
|
if still_something_to_read
|
||||||
when WebSocket::NotFinal
|
Baguette::Log.debug "Still something to read"
|
||||||
# puts "#{CBLUE}Received only part of a message#{CRESET}"
|
next
|
||||||
next if still_something_to_read
|
end
|
||||||
break
|
break
|
||||||
|
|
||||||
|
when WebSocket::NotFinal
|
||||||
|
Baguette::Log.debug "Received only part of a message"
|
||||||
|
# There is a per-user buffer in case we receive "not final" websocket messages.
|
||||||
|
# In case the buffer isn't set yet.
|
||||||
|
unless Context.context.fd_to_buffer[activefd]?
|
||||||
|
Context.context.fd_to_buffer[activefd] = String.new
|
||||||
|
end
|
||||||
|
# Add the received message to the buffer.
|
||||||
|
Context.context.fd_to_buffer[activefd] += message.message
|
||||||
|
if still_something_to_read
|
||||||
|
Baguette::Log.debug "and there is still something to read"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
break
|
||||||
|
|
||||||
when Bytes
|
when Bytes
|
||||||
# TODO: when receiving a binary message
|
# We should test the format and maybe its content when receiving a binary message.
|
||||||
# we should test the format and maybe its content
|
Baguette::Log.warning "Received a binary message"
|
||||||
# puts "#{CBLUE}Received a binary message#{CRESET}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if context.is_json[activefd] && message.is_a?(String)
|
# Is there a (non empty) buffer for the active fd?
|
||||||
|
buffer_size = if Context.context.fd_to_buffer[activefd]?
|
||||||
|
Context.context.fd_to_buffer[activefd].size
|
||||||
|
else
|
||||||
|
0
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO: make this buffer work with bytes messages.
|
||||||
|
# In case there was a previous message within a fragment.
|
||||||
|
if message.is_a?(String) && buffer_size > 0
|
||||||
|
message = Context.context.fd_to_buffer[activefd] + message
|
||||||
|
Context.context.fd_to_buffer[activefd] = String.new
|
||||||
|
end
|
||||||
|
|
||||||
|
if Context.context.is_json[activefd] && message.is_a?(String)
|
||||||
|
print_message message, activefd
|
||||||
message = IPC::Message.from_json(message).to_packet
|
message = IPC::Message.from_json(message).to_packet
|
||||||
|
elsif ! Context.context.is_json[activefd] && message.is_a?(Bytes)
|
||||||
|
# not json and bytes
|
||||||
|
print_message message, activefd
|
||||||
|
message = IPC::Message.from_cbor(message).to_packet
|
||||||
|
else
|
||||||
|
Baguette::Log.warning "message is neither JSON and string, or not JSON and bytes."
|
||||||
|
Baguette::Log.warning "message is #{message.class.to_s}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# client => service
|
# client => service
|
||||||
fdservice = context.switchtable[activefd]
|
fdservice = Context.context.switchtable[activefd]
|
||||||
|
|
||||||
# XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit
|
# XXX: this is not a TCP fd, but since behind the scene this is compatible, I'm hacking a bit
|
||||||
# Also, I changed the "finalize" method of the TCPFileDescriptor class not to close the socket
|
# Also, I changed the "finalize" method of the TCPFileDescriptor class not to close the socket
|
||||||
# when the object is GC.
|
# when the object is GC.
|
||||||
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
|
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
|
||||||
#serv = context.fd_to_ipcconnection[fdservice]
|
#serv = Context.context.fd_to_ipcclient[fdservice]
|
||||||
serv.send message
|
begin
|
||||||
|
serv.send message
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Sending a message failed: #{e}"
|
||||||
|
end
|
||||||
|
|
||||||
break unless still_something_to_read
|
break unless still_something_to_read
|
||||||
|
|
||||||
end # loop over the remaining messages to read on the websocket
|
end # loop over the remaining messages to read on the websocket
|
||||||
else
|
else
|
||||||
# service => client
|
# service => client
|
||||||
fdclient = context.switchtable[activefd]
|
fdclient = Context.context.switchtable[activefd]
|
||||||
wsclient = context.fd_to_websocket[fdclient]
|
wsclient = Context.context.fd_to_websocket[fdclient]
|
||||||
|
|
||||||
serv = context.fd_to_ipcconnection[activefd]
|
serv = Context.context.fd_to_ipcclient[activefd]
|
||||||
message = serv.read
|
message = serv.read
|
||||||
|
|
||||||
if context.is_json[fdclient]
|
buf = if Context.context.is_json[fdclient]
|
||||||
buf = message.to_json
|
message.to_json
|
||||||
else
|
else # We assume that non-JSON clients are CBOR clients.
|
||||||
buf = message.to_packet
|
message.to_cbor
|
||||||
end
|
end
|
||||||
|
|
||||||
|
print_message buf, activefd
|
||||||
|
|
||||||
wsclient.send buf
|
wsclient.send buf
|
||||||
end
|
end
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}"
|
Baguette::Log.error "Exception during message transfer: #{e}"
|
||||||
if context.is_client[activefd]
|
if Context.context.is_client[activefd]
|
||||||
closing_client activefd, context
|
closing_client activefd
|
||||||
else
|
else
|
||||||
clientfd = context.switchtable[activefd]
|
clientfd = Context.context.switchtable[activefd]
|
||||||
closing_client clientfd, context
|
closing_client clientfd
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sending_ping_messages(context : InstanceStorage)
|
def sending_ping_messages
|
||||||
context.fd_to_websocket.each do |fd, ws|
|
Context.context.fd_to_websocket.each do |fd, ws|
|
||||||
begin
|
begin
|
||||||
ws.ping "hello from #{fd}"
|
ws.ping "hello from #{fd}"
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Exception: #{e}#{CRESET}, already closed client #{fd}"
|
Baguette::Log.error "#{CRED}Exception: #{e}#{CRESET}, already closed client #{fd}"
|
||||||
begin
|
begin
|
||||||
context.remove_fd fd
|
Context.context.remove_fd fd
|
||||||
rescue e
|
rescue e
|
||||||
puts "#{CRED}Cannot remove #{fd} from clients: #{e}#{CRESET}"
|
Baguette::Log.error "#{CRED}Cannot remove #{fd} from clients: #{e}#{CRESET}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -354,60 +542,57 @@ end
|
||||||
|
|
||||||
# Every few seconds, the service should trigger the timer
|
# Every few seconds, the service should trigger the timer
|
||||||
# Allowing the sending of Ping messages to clients
|
# Allowing the sending of Ping messages to clients
|
||||||
service.base_timer = timer_delay
|
Context.service.base_timer = configuration.ipc_timer
|
||||||
|
|
||||||
service.loop do |event|
|
Context.service.loop do |event|
|
||||||
case event
|
begin
|
||||||
when IPC::Event::Timer
|
case event
|
||||||
if verbosity > 0
|
when IPC::Event::Timer
|
||||||
puts "#{CORANGE}IPC::Event::Timer#{CRESET}"
|
Baguette::Log.info "IPC::Event::Timer" if Context.print_timer
|
||||||
end
|
sending_ping_messages
|
||||||
sending_ping_messages context
|
when IPC::Event::Connection
|
||||||
when IPC::Event::Connection
|
Baguette::Log.debug "IPC::Event::Connection: #{event.fd}"
|
||||||
if verbosity > 0
|
|
||||||
puts "#{CBLUE}IPC::Event::Connection#{CRESET}: #{event.fd}"
|
|
||||||
end
|
|
||||||
when IPC::Event::Disconnection
|
|
||||||
if verbosity > 0
|
|
||||||
puts "#{CBLUE}IPC::Event::Disconnection#{CRESET}: #{event.fd}"
|
|
||||||
end
|
|
||||||
when IPC::Event::ExtraSocket
|
|
||||||
if verbosity > 0
|
|
||||||
puts "#{CBLUE}IPC::Event::ExtraSocket#{CRESET}: #{event.fd}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# 1. accept new websocket clients
|
when IPC::Event::Disconnection
|
||||||
if server.fd == event.fd
|
Baguette::Log.debug "IPC::Event::Disconnection: #{event.fd}"
|
||||||
client = server.accept
|
|
||||||
begin
|
when IPC::Event::ExtraSocket
|
||||||
websocket_client_connection client, context
|
Baguette::Log.debug "IPC::Event::ExtraSocket: #{event.fd}"
|
||||||
puts "#{CBLUE}new client:#{CRESET} #{client.fd}" unless verbosity == 0
|
|
||||||
rescue e
|
# 1. accept new websocket clients
|
||||||
puts "Exception: #{CRED}#{e}#{CRESET}"
|
if server.fd == event.fd
|
||||||
client.close
|
client = server.accept
|
||||||
|
begin
|
||||||
|
websocket_client_connection client
|
||||||
|
Baguette::Log.info "new client: #{client.fd}"
|
||||||
|
rescue e
|
||||||
|
Baguette::Log.error "Exception: #{e}"
|
||||||
|
client.close
|
||||||
|
end
|
||||||
|
next
|
||||||
end
|
end
|
||||||
next
|
|
||||||
|
# 2. active fd != server fd
|
||||||
|
activefd = event.fd
|
||||||
|
if activefd <= 0
|
||||||
|
Baguette::Log.error "faulty activefd: #{activefd}"
|
||||||
|
end
|
||||||
|
websocket_switching_procedure activefd
|
||||||
|
|
||||||
|
when IPC::Event::Switch
|
||||||
|
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
|
||||||
|
Baguette::Log.error "IPC::Event::MessageSent: #{event.fd}"
|
||||||
|
|
||||||
|
when IPC::Event::MessageReceived
|
||||||
|
Baguette::Log.debug "IPC::Event::MessageReceived: #{event.fd}"
|
||||||
|
raise "Not implemented."
|
||||||
end
|
end
|
||||||
|
rescue e
|
||||||
# 2. active fd != server fd
|
Baguette::Log.error "IPC loop final catch: #{e}"
|
||||||
activefd = event.fd
|
|
||||||
websocket_switching_procedure activefd, context
|
|
||||||
|
|
||||||
when IPC::Event::Switch
|
|
||||||
if verbosity > 0
|
|
||||||
puts "\033[36mIPC::Event::Switch#{CRESET}: from fd #{event.fd}"
|
|
||||||
end
|
|
||||||
|
|
||||||
raise "Not implemented."
|
|
||||||
|
|
||||||
when IPC::Event::MessageSent
|
|
||||||
puts "Message sent, for #{event.origin}"
|
|
||||||
|
|
||||||
when IPC::Event::MessageReceived
|
|
||||||
if verbosity > 0
|
|
||||||
puts "#{CBLUE}IPC::Event::Message#{CRESET}: #{event.fd}"
|
|
||||||
end
|
|
||||||
|
|
||||||
raise "Not implemented."
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,19 +5,13 @@ require "../test-ws"
|
||||||
ws_uri = "ws://localhost:1234/pong.JSON"
|
ws_uri = "ws://localhost:1234/pong.JSON"
|
||||||
|
|
||||||
message = IPC::Message.from_json(%({ "mtype" : 3, "utype" : 30, "payload" : "coucou" }))
|
message = IPC::Message.from_json(%({ "mtype" : 3, "utype" : 30, "payload" : "coucou" }))
|
||||||
pong = IPC::Connection.new "pong"
|
|
||||||
|
|
||||||
Benchmark.ips do |bm|
|
Benchmark.ips do |bm|
|
||||||
bm.report("connection") do
|
bm.report("connection") do
|
||||||
c = IPC::Connection.new "pong"
|
c = IPC::Client.new "pong"
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("connection through websocket") do
|
||||||
|
tws = TestWS.new ws_uri
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#puts "sleeping 5 seconds"
|
|
||||||
#sleep 5
|
|
||||||
#
|
|
||||||
#Benchmark.ips do |bm|
|
|
||||||
# bm.report("connection through websocket") do
|
|
||||||
# tws = TestWS.new ws_uri
|
|
||||||
# end
|
|
||||||
#end
|
|
||||||
|
|
|
@ -2,25 +2,44 @@ require "benchmark"
|
||||||
require "ipc"
|
require "ipc"
|
||||||
require "../test-ws"
|
require "../test-ws"
|
||||||
|
|
||||||
ws_uri = "ws://localhost:1234/pong"
|
ws_uri = "ws://localhost:1234/pong"
|
||||||
ws_uri_json = "ws://localhost:1234/pong.JSON"
|
ws_uri_json = "ws://localhost:1234/pong.JSON"
|
||||||
|
|
||||||
ws_pong = TestWS.new ws_uri
|
ws_pong = TestWS.new ws_uri
|
||||||
ws_pong_json = TestWS.new ws_uri_json
|
ws_pong_json = TestWS.new ws_uri_json
|
||||||
|
|
||||||
pong = IPC::Connection.new "pong"
|
pong = IPC::Client.new "pong"
|
||||||
|
pong.base_timer = 100_000
|
||||||
|
pong.timer = 100_000
|
||||||
|
|
||||||
Benchmark.ips do |bm|
|
Benchmark.ips do |bm|
|
||||||
bm.report("direct: round trip time") do
|
bm.report("direct: round trip time") do
|
||||||
pong.send 42, "coucou"
|
# puts "direct: round trip time"
|
||||||
m = pong.read
|
pong.send pong.server_fd.not_nil!, 42, "coucou"
|
||||||
|
::loop do
|
||||||
|
event = pong.wait_event do |event|
|
||||||
|
end
|
||||||
|
|
||||||
|
case event
|
||||||
|
when IPC::Event::MessageSent
|
||||||
|
# OK
|
||||||
|
when IPC::Event::MessageReceived
|
||||||
|
# puts "message received"
|
||||||
|
break
|
||||||
|
else
|
||||||
|
puts "event: #{event}"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
bm.report("web sockets: round trip time") do
|
#bm.report("web sockets: round trip time") do
|
||||||
ws_pong.send 42, "coucou"
|
# # puts "web sockets: round trip time"
|
||||||
m = ws_pong.read
|
# ws_pong.send 42, "coucou"
|
||||||
end
|
# m = ws_pong.read
|
||||||
|
#end
|
||||||
|
|
||||||
bm.report("web sockets + json: round trip time") do
|
bm.report("web sockets + json: round trip time") do
|
||||||
|
# puts "web sockets + json: round trip time"
|
||||||
ws_pong_json.send 42, "coucou"
|
ws_pong_json.send 42, "coucou"
|
||||||
m = ws_pong_json.read
|
m = ws_pong_json.read
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,12 +10,12 @@ require "../src/lib_modifications.cr"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
class TestIPC
|
class TestIPC
|
||||||
property ipcc : IPC::Connection
|
property ipcc : IPC::Client
|
||||||
property is_json : Bool
|
property is_json : Bool
|
||||||
|
|
||||||
def initialize(service_name : String)
|
def initialize(service_name : String)
|
||||||
@is_json = uri.ends_with? ".JSON"
|
@is_json = uri.ends_with? ".JSON"
|
||||||
@ipcc = IPC::Connection.new service_name
|
@ipcc = IPC::Client.new service_name
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
|
@ -60,9 +60,9 @@ class TestWS
|
||||||
m : String | Bytes
|
m : String | Bytes
|
||||||
|
|
||||||
if @is_json
|
if @is_json
|
||||||
m = IPC::Message.new(1.to_u8, type.to_u8, data).to_json
|
m = IPC::Message.new(0, 1.to_u8, type.to_u8, data).to_json
|
||||||
else
|
else
|
||||||
m = IPC::Message.new(1.to_u8, type.to_u8, data).to_packet
|
m = IPC::Message.new(0, 1.to_u8, type.to_u8, data).to_packet
|
||||||
end
|
end
|
||||||
|
|
||||||
@ws.send m
|
@ws.send m
|
||||||
|
|
Loading…
Reference in New Issue