Adding code from `ìpcd`.
commit
f62b41eadc
|
@ -0,0 +1,6 @@
|
||||||
|
shard.lock
|
||||||
|
bin
|
||||||
|
lib
|
||||||
|
|
||||||
|
websocketd
|
||||||
|
websocketc
|
|
@ -0,0 +1,140 @@
|
||||||
|
PACKAGE = 'websocketd'
|
||||||
|
VERSION = '0.6.0'
|
||||||
|
|
||||||
|
PREFIX := /usr/local
|
||||||
|
BINDIR := $(PREFIX)/bin
|
||||||
|
LIBDIR := $(PREFIX)/lib
|
||||||
|
SHAREDIR := $(PREFIX)/share
|
||||||
|
INCLUDEDIR := $(PREFIX)/include
|
||||||
|
MANDIR := $(SHAREDIR)/man
|
||||||
|
CRFLAGS := --release
|
||||||
|
CRFLAGS := --release
|
||||||
|
|
||||||
|
Q := @
|
||||||
|
|
||||||
|
all: websocketc websocketd
|
||||||
|
@:
|
||||||
|
|
||||||
|
websocketc: src/websocketc.cr
|
||||||
|
@echo '[01;35m CR > [01;37mwebsocketc[00m'
|
||||||
|
$(Q)crystal build $(CRFLAGS) src/websocketc.cr -o 'websocketc'
|
||||||
|
|
||||||
|
|
||||||
|
websocketc.install: websocketc
|
||||||
|
@echo '[01;31m IN > [01;37m$(BINDIR)/websocketc[00m'
|
||||||
|
$(Q)mkdir -p '$(DESTDIR)$(BINDIR)'
|
||||||
|
$(Q)install -m0755 websocketc $(DESTDIR)$(BINDIR)/websocketc
|
||||||
|
|
||||||
|
websocketc.clean:
|
||||||
|
@echo '[01;37m RM > [01;37mwebsocketc[00m'
|
||||||
|
$(Q)rm -f websocketc
|
||||||
|
|
||||||
|
websocketc.uninstall:
|
||||||
|
@echo '[01;37m RM > [01;37m$(BINDIR)/websocketc[00m'
|
||||||
|
$(Q)rm -f '$(DESTDIR)$(BINDIR)/websocketc'
|
||||||
|
|
||||||
|
websocketd: src/websocketd.cr
|
||||||
|
@echo '[01;35m CR > [01;37mwebsocketd[00m'
|
||||||
|
$(Q)crystal build $(CRFLAGS) src/websocketd.cr -o 'websocketd'
|
||||||
|
|
||||||
|
|
||||||
|
websocketd.install: websocketd
|
||||||
|
@echo '[01;31m IN > [01;37m$(BINDIR)/websocketd[00m'
|
||||||
|
$(Q)mkdir -p '$(DESTDIR)$(BINDIR)'
|
||||||
|
$(Q)install -m0755 websocketd $(DESTDIR)$(BINDIR)/websocketd
|
||||||
|
|
||||||
|
websocketd.clean:
|
||||||
|
@echo '[01;37m RM > [01;37mwebsocketd[00m'
|
||||||
|
$(Q)rm -f websocketd
|
||||||
|
|
||||||
|
websocketd.uninstall:
|
||||||
|
@echo '[01;37m RM > [01;37m$(BINDIR)/websocketd[00m'
|
||||||
|
$(Q)rm -f '$(DESTDIR)$(BINDIR)/websocketd'
|
||||||
|
|
||||||
|
$(DESTDIR)$(PREFIX):
|
||||||
|
@echo '[01;35m DIR > [01;37m$(PREFIX)[00m'
|
||||||
|
$(Q)mkdir -p $(DESTDIR)$(PREFIX)
|
||||||
|
$(DESTDIR)$(BINDIR):
|
||||||
|
@echo '[01;35m DIR > [01;37m$(BINDIR)[00m'
|
||||||
|
$(Q)mkdir -p $(DESTDIR)$(BINDIR)
|
||||||
|
$(DESTDIR)$(LIBDIR):
|
||||||
|
@echo '[01;35m DIR > [01;37m$(LIBDIR)[00m'
|
||||||
|
$(Q)mkdir -p $(DESTDIR)$(LIBDIR)
|
||||||
|
$(DESTDIR)$(SHAREDIR):
|
||||||
|
@echo '[01;35m DIR > [01;37m$(SHAREDIR)[00m'
|
||||||
|
$(Q)mkdir -p $(DESTDIR)$(SHAREDIR)
|
||||||
|
$(DESTDIR)$(INCLUDEDIR):
|
||||||
|
@echo '[01;35m DIR > [01;37m$(INCLUDEDIR)[00m'
|
||||||
|
$(Q)mkdir -p $(DESTDIR)$(INCLUDEDIR)
|
||||||
|
$(DESTDIR)$(MANDIR):
|
||||||
|
@echo '[01;35m DIR > [01;37m$(MANDIR)[00m'
|
||||||
|
$(Q)mkdir -p $(DESTDIR)$(MANDIR)
|
||||||
|
install: websocketc.install websocketd.install
|
||||||
|
@:
|
||||||
|
|
||||||
|
uninstall: websocketc.uninstall websocketd.uninstall
|
||||||
|
@:
|
||||||
|
|
||||||
|
clean: websocketc.clean websocketd.clean
|
||||||
|
distclean: clean
|
||||||
|
dist: dist-gz dist-xz dist-bz2
|
||||||
|
$(Q)rm -- $(PACKAGE)-$(VERSION)
|
||||||
|
|
||||||
|
distdir:
|
||||||
|
$(Q)rm -rf -- $(PACKAGE)-$(VERSION)
|
||||||
|
$(Q)ln -s -- . $(PACKAGE)-$(VERSION)
|
||||||
|
|
||||||
|
dist-gz: $(PACKAGE)-$(VERSION).tar.gz
|
||||||
|
$(PACKAGE)-$(VERSION).tar.gz: distdir
|
||||||
|
@echo '[01;33m TAR > [01;37m$(PACKAGE)-$(VERSION).tar.gz[00m'
|
||||||
|
$(Q)tar czf $(PACKAGE)-$(VERSION).tar.gz \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/websocketc.cr \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/websocketd.cr
|
||||||
|
|
||||||
|
dist-xz: $(PACKAGE)-$(VERSION).tar.xz
|
||||||
|
$(PACKAGE)-$(VERSION).tar.xz: distdir
|
||||||
|
@echo '[01;33m TAR > [01;37m$(PACKAGE)-$(VERSION).tar.xz[00m'
|
||||||
|
$(Q)tar cJf $(PACKAGE)-$(VERSION).tar.xz \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/websocketc.cr \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/websocketd.cr
|
||||||
|
|
||||||
|
dist-bz2: $(PACKAGE)-$(VERSION).tar.bz2
|
||||||
|
$(PACKAGE)-$(VERSION).tar.bz2: distdir
|
||||||
|
@echo '[01;33m TAR > [01;37m$(PACKAGE)-$(VERSION).tar.bz2[00m'
|
||||||
|
$(Q)tar cjf $(PACKAGE)-$(VERSION).tar.bz2 \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/websocketc.cr \
|
||||||
|
$(PACKAGE)-$(VERSION)/src/websocketd.cr
|
||||||
|
|
||||||
|
help:
|
||||||
|
@echo '[01;37m :: websocketd-0.6.0[00m'
|
||||||
|
@echo ''
|
||||||
|
@echo '[01;37mGeneric targets:[00m'
|
||||||
|
@echo '[00m - [01;32mhelp [37m Prints this help message.[00m'
|
||||||
|
@echo '[00m - [01;32mall [37m Builds all targets.[00m'
|
||||||
|
@echo '[00m - [01;32mdist [37m Creates tarballs of the files of the project.[00m'
|
||||||
|
@echo '[00m - [01;32minstall [37m Installs the project.[00m'
|
||||||
|
@echo '[00m - [01;32mclean [37m Removes compiled files.[00m'
|
||||||
|
@echo '[00m - [01;32muninstall [37m Deinstalls the project.[00m'
|
||||||
|
@echo ''
|
||||||
|
@echo '[01;37mCLI-modifiable variables:[00m'
|
||||||
|
@echo ' - [01;34mCRFLAGS [37m ${CRFLAGS}[00m'
|
||||||
|
@echo ' - [01;34mCRFLAGS [37m ${CRFLAGS}[00m'
|
||||||
|
@echo ' - [01;34mPREFIX [37m ${PREFIX}[00m'
|
||||||
|
@echo ' - [01;34mBINDIR [37m ${BINDIR}[00m'
|
||||||
|
@echo ' - [01;34mLIBDIR [37m ${LIBDIR}[00m'
|
||||||
|
@echo ' - [01;34mSHAREDIR [37m ${SHAREDIR}[00m'
|
||||||
|
@echo ' - [01;34mINCLUDEDIR [37m ${INCLUDEDIR}[00m'
|
||||||
|
@echo ' - [01;34mMANDIR [37m ${MANDIR}[00m'
|
||||||
|
@echo ''
|
||||||
|
@echo '[01;37mProject targets: [00m'
|
||||||
|
@echo ' - [01;33mwebsocketc [37m crystal[00m'
|
||||||
|
@echo ' - [01;33mwebsocketd [37m crystal[00m'
|
||||||
|
@echo ''
|
||||||
|
@echo '[01;37mMakefile options:[00m'
|
||||||
|
@echo ' - gnu: false'
|
||||||
|
@echo ' - colors: true'
|
||||||
|
@echo ''
|
||||||
|
@echo '[01;37mRebuild the Makefile with:[00m'
|
||||||
|
@echo ' zsh ./build.zsh -c'
|
||||||
|
.PHONY: all clean distclean dist install uninstall help
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
package=websocketd
|
||||||
|
version=0.6.0 # This version should follow the libipc version.
|
||||||
|
|
||||||
|
targets=(websocketc websocketd)
|
||||||
|
|
||||||
|
for target in websocketc websocketd; do
|
||||||
|
type[$target]=crystal
|
||||||
|
sources[$target]=src/${target}.cr
|
||||||
|
done
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
name: ipcd
|
||||||
|
version: 0.6.0 # should follow libipc and ipc.cr bindings versions.
|
||||||
|
|
||||||
|
authors:
|
||||||
|
- karchnu <karchnu@karchnu.fr>
|
||||||
|
|
||||||
|
description: |
|
||||||
|
Websocketd allows the connection to IPC services from websockets.
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
ipc:
|
||||||
|
git: https://git.baguette.netlib.re/Baguette/ipc.cr
|
||||||
|
branch: poll
|
||||||
|
|
||||||
|
targets:
|
||||||
|
websocketd:
|
||||||
|
main: src/websocketd.cr
|
||||||
|
websocketc:
|
||||||
|
main: src/websocketc.cr
|
||||||
|
|
||||||
|
# tests
|
||||||
|
test-ws-pong:
|
||||||
|
main: tests/pong.cr
|
||||||
|
test-closing:
|
||||||
|
main: tests/closing.cr
|
||||||
|
|
||||||
|
# performance tests
|
||||||
|
test-perf-messages:
|
||||||
|
main: tests/performances/ipc_message.cr
|
||||||
|
|
||||||
|
test-perf-connection:
|
||||||
|
main: tests/performances/ipc_connection.cr
|
||||||
|
|
||||||
|
test-perf-message-exchange:
|
||||||
|
main: tests/performances/ipc_message_exchange.cr
|
||||||
|
|
||||||
|
test-perf-puts:
|
||||||
|
main: tests/performances/puts.cr
|
||||||
|
|
||||||
|
|
||||||
|
license: ISC
|
|
@ -0,0 +1,5 @@
|
||||||
|
CRED = "\033[31m"
|
||||||
|
CBLUE = "\033[36m"
|
||||||
|
CGREEN = "\033[32m"
|
||||||
|
CRESET = "\033[00m"
|
||||||
|
CORANGE = "\033[33m"
|
|
@ -0,0 +1,127 @@
|
||||||
|
# Libraries modifications
|
||||||
|
|
||||||
|
require "http"
|
||||||
|
require "http/web_socket/protocol"
|
||||||
|
|
||||||
|
# The Socket library uses the IO::Buffered module.
|
||||||
|
# IO::Buffered needs a new function to check if there is still something to read
|
||||||
|
# in its buffer. Since WebSocket is compatible with all IO instances, then IO
|
||||||
|
# needs this function as well, even if it doesn't really makes sense here.
|
||||||
|
class IO
|
||||||
|
def empty? : Bool
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module IO::Buffered
|
||||||
|
# :nodoc:
|
||||||
|
def empty? : Bool
|
||||||
|
@in_buffer_rem.size == 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class HTTP::WebSocket
|
||||||
|
# Infinite loop over `run_once`.
|
||||||
|
# Stops when the socket closes or an error occurs.
|
||||||
|
def run
|
||||||
|
loop do
|
||||||
|
ret = run_once
|
||||||
|
case ret
|
||||||
|
when Error | Close
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# `run_once` returns the type of the message which could be a Ping or Pong message, a closing socket, an error (when an exception occurs) or a message that is not yet entirely received.
|
||||||
|
record Ping
|
||||||
|
record Pong
|
||||||
|
record Close
|
||||||
|
record Error
|
||||||
|
record NotFinal
|
||||||
|
|
||||||
|
# :nodoc:
|
||||||
|
def run_once : Bytes | String | Close | Ping | Pong | NotFinal | Error
|
||||||
|
begin
|
||||||
|
info = @ws.receive(@buffer)
|
||||||
|
rescue IO::EOFError
|
||||||
|
close
|
||||||
|
return Error.new
|
||||||
|
end
|
||||||
|
|
||||||
|
case info.opcode
|
||||||
|
when Protocol::Opcode::PING
|
||||||
|
@current_message.write @buffer[0, info.size]
|
||||||
|
if info.final
|
||||||
|
message = @current_message.to_s
|
||||||
|
@on_ping.try &.call(message)
|
||||||
|
pong(message) unless closed?
|
||||||
|
@current_message.clear
|
||||||
|
return Ping.new
|
||||||
|
end
|
||||||
|
when Protocol::Opcode::PONG
|
||||||
|
@current_message.write @buffer[0, info.size]
|
||||||
|
if info.final
|
||||||
|
@on_pong.try &.call(@current_message.to_s)
|
||||||
|
@current_message.clear
|
||||||
|
return Pong.new
|
||||||
|
end
|
||||||
|
when Protocol::Opcode::TEXT
|
||||||
|
message = @buffer[0, info.size]
|
||||||
|
@current_message.write message
|
||||||
|
if info.final
|
||||||
|
@on_message.try &.call(@current_message.to_s)
|
||||||
|
@current_message.clear
|
||||||
|
return String.new message
|
||||||
|
end
|
||||||
|
when Protocol::Opcode::BINARY
|
||||||
|
message = @buffer[0, info.size]
|
||||||
|
@current_message.write message
|
||||||
|
if info.final
|
||||||
|
@on_binary.try &.call(@current_message.to_slice)
|
||||||
|
@current_message.clear
|
||||||
|
return message
|
||||||
|
end
|
||||||
|
when Protocol::Opcode::CLOSE
|
||||||
|
@current_message.write @buffer[0, info.size]
|
||||||
|
if info.final
|
||||||
|
message = @current_message.to_s
|
||||||
|
@on_close.try &.call(HTTP::WebSocket::CloseCode::NormalClosure, message)
|
||||||
|
close(HTTP::WebSocket::CloseCode::NormalClosure, message) unless closed?
|
||||||
|
@current_message.clear
|
||||||
|
return Close.new
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return NotFinal.new
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the websocket instance.
|
||||||
|
def ws
|
||||||
|
@ws
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class HTTP::WebSocket::Protocol
|
||||||
|
def io
|
||||||
|
@io
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
class WebSocket < HTTP::WebSocket
|
||||||
|
getter? closed = false
|
||||||
|
|
||||||
|
def finalize
|
||||||
|
# puts "WrappedTCPFileDescriptor garbage collection!!"
|
||||||
|
# super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class WrappedTCPFileDescriptor < TCPSocket
|
||||||
|
# do not close the connection when garbage collected!!
|
||||||
|
def finalize
|
||||||
|
# puts "WrappedTCPFileDescriptor garbage collection!!"
|
||||||
|
# super
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,14 @@
|
||||||
|
require "io/hexdump"
|
||||||
|
|
||||||
|
def print_hexa(message : String, aroundmsg : String)
|
||||||
|
puts "#{aroundmsg} [["
|
||||||
|
m = IO::Memory.new(message)
|
||||||
|
io = IO::Hexdump.new(m, output: STDERR, read: true)
|
||||||
|
buffer = Bytes.new 4000
|
||||||
|
io.read (buffer) # reading = should print
|
||||||
|
puts "]] #{aroundmsg}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# str = to_message(42, "coucou")
|
||||||
|
# print_hexa str, "message sent"
|
||||||
|
# ws.send str
|
|
@ -0,0 +1,76 @@
|
||||||
|
require "http/web_socket"
|
||||||
|
require "option_parser"
|
||||||
|
|
||||||
|
require "./colors"
|
||||||
|
require "./utils"
|
||||||
|
|
||||||
|
uri = "ws://localhost:1234/pong"
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-u uri", "--uri uri", "URI" do |opturi|
|
||||||
|
uri = opturi
|
||||||
|
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)
|
||||||
|
m = ws.read
|
||||||
|
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(uri))
|
||||||
|
|
||||||
|
puts "connection done: sending pong"
|
||||||
|
|
||||||
|
ws.on_close do |socket|
|
||||||
|
puts "socket is closing"
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if uri.ends_with? ".JSON"
|
||||||
|
json_message = STDIN.gets_to_end
|
||||||
|
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
|
||||||
|
|
||||||
|
ws.close
|
||||||
|
ws.read
|
||||||
|
rescue e
|
||||||
|
puts "Exception: #{e}"
|
||||||
|
end
|
|
@ -0,0 +1,413 @@
|
||||||
|
require "option_parser"
|
||||||
|
require "ipc"
|
||||||
|
require "socket"
|
||||||
|
require "./colors"
|
||||||
|
|
||||||
|
require "json"
|
||||||
|
|
||||||
|
require "socket"
|
||||||
|
require "http/server"
|
||||||
|
require "base64"
|
||||||
|
require "digest"
|
||||||
|
require "./utils"
|
||||||
|
|
||||||
|
# All modifications to standard libraries go there.
|
||||||
|
require "./lib_modifications.cr"
|
||||||
|
|
||||||
|
# service instance parameters
|
||||||
|
# they can be changed via the cli
|
||||||
|
service_name = "websocket"
|
||||||
|
host = "0.0.0.0"
|
||||||
|
port_to_listen = 1234
|
||||||
|
timer_delay = 30_000.to_f64 # 30 seconds
|
||||||
|
|
||||||
|
verbosity = 1
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-l host", "--l host", "IP address to listen on." do |h|
|
||||||
|
host = h
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-p port", "--port port", "Port to listen on." do |port|
|
||||||
|
port_to_listen = port.to_u16
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-s service-name", "--service-name service-name", "Service name." do |name|
|
||||||
|
service_name = name
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-t timer-delay", "--timer-delay timer-delay", "Timer delay (in seconds)" do |t|
|
||||||
|
timer_delay = t.to_f64 * 1000 # stored in ms
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-v verbosity-level", "--verbosity level", "Verbosity." do |opt|
|
||||||
|
verbosity = opt.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
puts parser
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# Link between fd and TCPSocket, WebSocket and IPC::Connection instances.
|
||||||
|
class InstanceStorage
|
||||||
|
property service : IPC::SwitchingService
|
||||||
|
property switchtable : Hash(Int32, Int32)
|
||||||
|
property is_client : Hash(Int32,Bool)
|
||||||
|
property is_json : Hash(Int32, Bool)
|
||||||
|
property fd_to_tcpsocket : Hash(Int32, TCPSocket)
|
||||||
|
property fd_to_websocket : Hash(Int32, WebSocket)
|
||||||
|
property fd_to_ipcconnection : Hash(Int32, IPC::Connection)
|
||||||
|
|
||||||
|
def initialize (@service : IPC::SwitchingService)
|
||||||
|
# fdlist_client = [] of TCPSocket
|
||||||
|
@switchtable = Hash(Int32, Int32).new
|
||||||
|
@is_client = Hash(Int32,Bool).new
|
||||||
|
@is_json = Hash(Int32,Bool).new
|
||||||
|
@fd_to_tcpsocket = Hash(Int32, TCPSocket).new
|
||||||
|
@fd_to_websocket = Hash(Int32, WebSocket).new
|
||||||
|
@fd_to_ipcconnection = Hash(Int32, IPC::Connection).new
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_fd (fdclient : Int32)
|
||||||
|
puts "#{CBLUE}closing the client:#{CRESET} #{fdclient}"
|
||||||
|
# 1. closing both the client and the service
|
||||||
|
fdservice = @switchtable[fdclient]?
|
||||||
|
tcpfdc = @fd_to_tcpsocket[fdclient]
|
||||||
|
|
||||||
|
# 2. closing the TCP connections
|
||||||
|
tcpfdc.close unless tcpfdc.closed?
|
||||||
|
|
||||||
|
# 3. removing the client and the service fds from the loop check
|
||||||
|
@service.remove_fd (fdclient)
|
||||||
|
|
||||||
|
# 5. removing both the client and the service from the switchtable
|
||||||
|
@switchtable = @switchtable.select do |fdc, fds|
|
||||||
|
fdc != fdclient && fds != fdclient
|
||||||
|
end
|
||||||
|
|
||||||
|
# 6. removing the client and the service from is_client
|
||||||
|
@is_client = @is_client.select do |fd,v| fd != fdclient end
|
||||||
|
@is_json = @is_json.select do |fd,v| fd != fdclient end
|
||||||
|
|
||||||
|
@fd_to_websocket.select! do |fd, ws|
|
||||||
|
fd != fdclient
|
||||||
|
end
|
||||||
|
|
||||||
|
unless fdservice.nil?
|
||||||
|
service = @fd_to_ipcconnection[fdservice]
|
||||||
|
service.close
|
||||||
|
@service.remove_fd (fdservice)
|
||||||
|
@fd_to_ipcconnection = @fd_to_ipcconnection.select do |k, v|
|
||||||
|
k != fdservice
|
||||||
|
end
|
||||||
|
|
||||||
|
@is_client = @is_client.select do |fd,v| fd != fdservice end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# by default, listen on any IP address
|
||||||
|
server = TCPServer.new(host, port_to_listen)
|
||||||
|
service = IPC::SwitchingService.new service_name
|
||||||
|
service << server.fd
|
||||||
|
context = InstanceStorage.new service
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def websocket_client_connection(client, context : InstanceStorage)
|
||||||
|
request = HTTP::Request.from_io client
|
||||||
|
# pp! request
|
||||||
|
|
||||||
|
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"
|
||||||
|
# puts 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
|
||||||
|
if req_service.empty?
|
||||||
|
client.close
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
# The client may ask to transcript JSON-based messages into IPC messages.
|
||||||
|
# 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"
|
||||||
|
context.is_json[client.fd] = true
|
||||||
|
req_service = req_service.gsub /.JSON$/, ""
|
||||||
|
else
|
||||||
|
context.is_json[client.fd] = false
|
||||||
|
end
|
||||||
|
|
||||||
|
websocket_connection_procedure req_service, client.fd, context
|
||||||
|
|
||||||
|
# TODO: if trackerd, send the IP address of the client
|
||||||
|
if req_service == "tracker"
|
||||||
|
puts "tracker - sending the IP address"
|
||||||
|
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
|
||||||
|
|
||||||
|
# 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|
|
||||||
|
puts "pong #{m}"
|
||||||
|
end
|
||||||
|
context.is_client[client.fd] = true
|
||||||
|
|
||||||
|
# listen to the client's file descriptor
|
||||||
|
context.service << client.fd
|
||||||
|
# puts "#{CBLUE}new client: #{client.fd}#{CRESET}"
|
||||||
|
|
||||||
|
# registering the client into storing structures to avoid being garbage collected
|
||||||
|
context.fd_to_tcpsocket[client.fd] = client
|
||||||
|
context.fd_to_websocket[client.fd] = wsclient
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def closing_client (fdclient : Int, context : InstanceStorage)
|
||||||
|
context.remove_fd fdclient
|
||||||
|
end
|
||||||
|
|
||||||
|
# first message from the client: requested service name
|
||||||
|
# 1. connection to the service
|
||||||
|
# 2. listening on the service fd
|
||||||
|
# 3. bounding both file descriptors (through switchtable hash)
|
||||||
|
# 4. indicating that the client is connected (in is_client)
|
||||||
|
def websocket_connection_procedure (requested_service : String, clientfd : Int32, context : InstanceStorage)
|
||||||
|
begin
|
||||||
|
# 2. establishing a connection to the service
|
||||||
|
newservice = IPC::Connection.new requested_service
|
||||||
|
context.fd_to_ipcconnection[newservice.fd] = newservice
|
||||||
|
|
||||||
|
# 3. listening on the client fd and the service fd
|
||||||
|
context.service << newservice.fd
|
||||||
|
|
||||||
|
# cannot perform automatic switching due to websockets headers
|
||||||
|
# future version of the libipc lib should include some workaround, probably
|
||||||
|
# service.switch.add fdclient, newservice.fd
|
||||||
|
|
||||||
|
# 4. bounding the client and the service fd
|
||||||
|
context.switchtable[clientfd] = newservice.fd
|
||||||
|
context.switchtable[newservice.fd] = clientfd
|
||||||
|
|
||||||
|
# 5. the client is then connected, send it a message
|
||||||
|
context.is_client[clientfd] = true
|
||||||
|
context.is_client[newservice.fd] = false
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Exception during connection to the service:#{CRESET} #{e}"
|
||||||
|
context.remove_fd clientfd
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def websocket_switching_procedure (activefd : Int, context : InstanceStorage)
|
||||||
|
begin
|
||||||
|
if context.is_client[activefd]
|
||||||
|
# The client is a WebSocket on top of a TCP connection
|
||||||
|
client = context.fd_to_tcpsocket[activefd]
|
||||||
|
wsclient = context.fd_to_websocket[activefd]
|
||||||
|
loop do
|
||||||
|
begin
|
||||||
|
message = wsclient.run_once
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Exception (receiving a message)#{CRESET} #{e}"
|
||||||
|
context.remove_fd activefd
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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?
|
||||||
|
|
||||||
|
if wsclient.closed?
|
||||||
|
# puts "#{CBLUE}client is closed#{CRESET}"
|
||||||
|
context.remove_fd client.fd
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if message.nil?
|
||||||
|
puts "#{CRED}message is nil#{CRESET}"
|
||||||
|
context.remove_fd client.fd
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
case message
|
||||||
|
when WebSocket::Error
|
||||||
|
puts "#{CRED}An error occured#{CRESET}"
|
||||||
|
context.remove_fd client.fd
|
||||||
|
return
|
||||||
|
when WebSocket::Ping
|
||||||
|
# puts "#{CBLUE}Received a ping message#{CRESET}"
|
||||||
|
next if still_something_to_read
|
||||||
|
break
|
||||||
|
when WebSocket::Pong
|
||||||
|
# puts "#{CBLUE}Received a pong message#{CRESET}"
|
||||||
|
next if still_something_to_read
|
||||||
|
break
|
||||||
|
when WebSocket::Close
|
||||||
|
# puts "#{CBLUE}Received a close message#{CRESET}"
|
||||||
|
context.remove_fd client.fd
|
||||||
|
return
|
||||||
|
when WebSocket::NotFinal
|
||||||
|
# puts "#{CBLUE}Received only part of a message#{CRESET}"
|
||||||
|
next if still_something_to_read
|
||||||
|
break
|
||||||
|
when Bytes
|
||||||
|
# TODO: when receiving a binary message
|
||||||
|
# we should test the format and maybe its content
|
||||||
|
# puts "#{CBLUE}Received a binary message#{CRESET}"
|
||||||
|
end
|
||||||
|
|
||||||
|
if context.is_json[activefd] && message.is_a?(String)
|
||||||
|
message = IPC::Message.from_json(message).to_packet
|
||||||
|
end
|
||||||
|
|
||||||
|
# client => service
|
||||||
|
fdservice = context.switchtable[activefd]
|
||||||
|
|
||||||
|
# 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
|
||||||
|
# when the object is GC.
|
||||||
|
serv = WrappedTCPFileDescriptor.new(fd: fdservice, family: Socket::Family::INET)
|
||||||
|
#serv = context.fd_to_ipcconnection[fdservice]
|
||||||
|
serv.send message
|
||||||
|
|
||||||
|
break unless still_something_to_read
|
||||||
|
|
||||||
|
end # loop over the remaining messages to read on the websocket
|
||||||
|
else
|
||||||
|
# service => client
|
||||||
|
fdclient = context.switchtable[activefd]
|
||||||
|
wsclient = context.fd_to_websocket[fdclient]
|
||||||
|
|
||||||
|
serv = context.fd_to_ipcconnection[activefd]
|
||||||
|
message = serv.read
|
||||||
|
|
||||||
|
if context.is_json[fdclient]
|
||||||
|
buf = message.to_json
|
||||||
|
else
|
||||||
|
buf = message.to_packet
|
||||||
|
end
|
||||||
|
|
||||||
|
wsclient.send buf
|
||||||
|
end
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Exception during message transfer:#{CRESET} #{e}"
|
||||||
|
if context.is_client[activefd]
|
||||||
|
closing_client activefd, context
|
||||||
|
else
|
||||||
|
clientfd = context.switchtable[activefd]
|
||||||
|
closing_client clientfd, context
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sending_ping_messages(context : InstanceStorage)
|
||||||
|
context.fd_to_websocket.each do |fd, ws|
|
||||||
|
begin
|
||||||
|
ws.ping "hello from #{fd}"
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Exception: #{e}#{CRESET}, already closed client #{fd}"
|
||||||
|
begin
|
||||||
|
context.remove_fd fd
|
||||||
|
rescue e
|
||||||
|
puts "#{CRED}Cannot remove #{fd} from clients: #{e}#{CRESET}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Every few seconds, the service should trigger the timer
|
||||||
|
# Allowing the sending of Ping messages to clients
|
||||||
|
service.base_timer = timer_delay
|
||||||
|
|
||||||
|
service.loop do |event|
|
||||||
|
case event
|
||||||
|
when IPC::Event::Timer
|
||||||
|
if verbosity > 0
|
||||||
|
puts "#{CORANGE}IPC::Event::Timer#{CRESET}"
|
||||||
|
end
|
||||||
|
sending_ping_messages context
|
||||||
|
when IPC::Event::Connection
|
||||||
|
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
|
||||||
|
if server.fd == event.fd
|
||||||
|
client = server.accept
|
||||||
|
begin
|
||||||
|
websocket_client_connection client, context
|
||||||
|
puts "#{CBLUE}new client:#{CRESET} #{client.fd}" unless verbosity == 0
|
||||||
|
rescue e
|
||||||
|
puts "Exception: #{CRED}#{e}#{CRESET}"
|
||||||
|
client.close
|
||||||
|
end
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
# 2. active fd != server fd
|
||||||
|
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
|
|
@ -0,0 +1,24 @@
|
||||||
|
require "option_parser"
|
||||||
|
require "./test-ws"
|
||||||
|
|
||||||
|
uri = "ws://localhost:1234/pong.JSON"
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-u uri", "--uri uri", "URI" do |opturi|
|
||||||
|
uri = opturi
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
puts parser
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
tws = TestWS.new uri
|
||||||
|
puts "connection done: closing it"
|
||||||
|
tws.close
|
||||||
|
tws.read
|
||||||
|
rescue e
|
||||||
|
puts "Exception: #{e}"
|
||||||
|
end
|
|
@ -0,0 +1,23 @@
|
||||||
|
require "benchmark"
|
||||||
|
require "ipc"
|
||||||
|
require "../test-ws"
|
||||||
|
|
||||||
|
ws_uri = "ws://localhost:1234/pong.JSON"
|
||||||
|
|
||||||
|
message = IPC::Message.from_json(%({ "mtype" : 3, "utype" : 30, "payload" : "coucou" }))
|
||||||
|
pong = IPC::Connection.new "pong"
|
||||||
|
|
||||||
|
Benchmark.ips do |bm|
|
||||||
|
bm.report("connection") do
|
||||||
|
c = IPC::Connection.new "pong"
|
||||||
|
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
|
|
@ -0,0 +1,20 @@
|
||||||
|
require "benchmark"
|
||||||
|
require "ipc"
|
||||||
|
|
||||||
|
message = IPC::Message.from_json(%({ "mtype" : 3, "utype" : 30, "payload" : "coucou" }))
|
||||||
|
|
||||||
|
str_message = %({ "mtype" : 3, "utype" : 30, "payload" : "coucou" })
|
||||||
|
|
||||||
|
Benchmark.ips do |bm|
|
||||||
|
bm.report("from json") do
|
||||||
|
m = IPC::Message.from_json str_message
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("to json") do
|
||||||
|
message.to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("to_packet") do
|
||||||
|
message.to_packet
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,27 @@
|
||||||
|
require "benchmark"
|
||||||
|
require "ipc"
|
||||||
|
require "../test-ws"
|
||||||
|
|
||||||
|
ws_uri = "ws://localhost:1234/pong"
|
||||||
|
ws_uri_json = "ws://localhost:1234/pong.JSON"
|
||||||
|
ws_pong = TestWS.new ws_uri
|
||||||
|
ws_pong_json = TestWS.new ws_uri_json
|
||||||
|
|
||||||
|
pong = IPC::Connection.new "pong"
|
||||||
|
|
||||||
|
Benchmark.ips do |bm|
|
||||||
|
bm.report("direct: round trip time") do
|
||||||
|
pong.send 42, "coucou"
|
||||||
|
m = pong.read
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("web sockets: round trip time") do
|
||||||
|
ws_pong.send 42, "coucou"
|
||||||
|
m = ws_pong.read
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("web sockets + json: round trip time") do
|
||||||
|
ws_pong_json.send 42, "coucou"
|
||||||
|
m = ws_pong_json.read
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,18 @@
|
||||||
|
require "benchmark"
|
||||||
|
require "ipc"
|
||||||
|
|
||||||
|
message = IPC::Message.from_json(%({ "mtype" : 3, "utype" : 30, "payload" : "coucou" }))
|
||||||
|
|
||||||
|
Benchmark.ips do |bm|
|
||||||
|
bm.report("puts") do
|
||||||
|
puts "coucou"
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("puts vide") do
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("pp! message") do
|
||||||
|
pp! message
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,28 @@
|
||||||
|
require "benchmark"
|
||||||
|
|
||||||
|
hash_i = Hash(Int32, Int32).new
|
||||||
|
hash_str = Hash(String, String).new
|
||||||
|
|
||||||
|
full_hash = Hash(Int32, String).new
|
||||||
|
|
||||||
|
1000.times do |i|
|
||||||
|
full_hash[i] = "blah #{i}"
|
||||||
|
end
|
||||||
|
|
||||||
|
Benchmark.ips do |bm|
|
||||||
|
bm.report("hash[i32] = i32") do
|
||||||
|
hash_i[1] = 3
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("hash[String] = String") do
|
||||||
|
hash_str["coucou"] = "truc"
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("hash[500]") do
|
||||||
|
blah = full_hash[500]
|
||||||
|
end
|
||||||
|
|
||||||
|
bm.report("hash[800]") do
|
||||||
|
blah = full_hash[800]
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,72 @@
|
||||||
|
require "option_parser"
|
||||||
|
require "./test-ws"
|
||||||
|
|
||||||
|
uri = "ws://localhost:1234/pong.JSON"
|
||||||
|
rounds = 5
|
||||||
|
|
||||||
|
OptionParser.parse do |parser|
|
||||||
|
parser.on "-r rounds", "--rounds rounds", "Rounds" do |r|
|
||||||
|
rounds = r.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-u uri", "--uri uri", "URI" do |opturi|
|
||||||
|
uri = opturi
|
||||||
|
end
|
||||||
|
|
||||||
|
parser.on "-h", "--help", "Show this help" do
|
||||||
|
puts parser
|
||||||
|
exit 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def multiple_messages_and_responses(tws : TestWS, data)
|
||||||
|
tws.send 1, data
|
||||||
|
tws.send 2, data
|
||||||
|
tws.send 3, data
|
||||||
|
tws.send 4, data
|
||||||
|
tws.send 5, data
|
||||||
|
|
||||||
|
response = tws.read
|
||||||
|
puts "1. received a message: #{response}"
|
||||||
|
pp! response
|
||||||
|
|
||||||
|
response = tws.read
|
||||||
|
puts "2. received a message: #{response}"
|
||||||
|
pp! response
|
||||||
|
|
||||||
|
response = tws.read
|
||||||
|
puts "3. received a message: #{response}"
|
||||||
|
pp! response
|
||||||
|
|
||||||
|
response = tws.read
|
||||||
|
puts "4. received a message: #{response}"
|
||||||
|
pp! response
|
||||||
|
|
||||||
|
response = tws.read
|
||||||
|
puts "5. received a message: #{response}"
|
||||||
|
pp! response
|
||||||
|
end
|
||||||
|
|
||||||
|
def single_message_and_response(tws : TestWS, data)
|
||||||
|
tws.send 1, data
|
||||||
|
response = tws.read
|
||||||
|
pp! response
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
tws = TestWS.new uri
|
||||||
|
# puts "connection done: sending pong"
|
||||||
|
|
||||||
|
data = STDIN.gets_to_end
|
||||||
|
data = data.chomp
|
||||||
|
|
||||||
|
rounds.times do |i|
|
||||||
|
single_message_and_response tws, "#{data} #{i}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# puts "closing the connection"
|
||||||
|
tws.close
|
||||||
|
tws.read
|
||||||
|
rescue e
|
||||||
|
puts "Exception: #{e}"
|
||||||
|
end
|
|
@ -0,0 +1,74 @@
|
||||||
|
require "http/web_socket"
|
||||||
|
|
||||||
|
require "../src/colors"
|
||||||
|
require "../src/utils"
|
||||||
|
|
||||||
|
require "ipc"
|
||||||
|
|
||||||
|
require "../src/lib_modifications.cr"
|
||||||
|
|
||||||
|
require "json"
|
||||||
|
|
||||||
|
class TestIPC
|
||||||
|
property ipcc : IPC::Connection
|
||||||
|
property is_json : Bool
|
||||||
|
|
||||||
|
def initialize(service_name : String)
|
||||||
|
@is_json = uri.ends_with? ".JSON"
|
||||||
|
@ipcc = IPC::Connection.new service_name
|
||||||
|
end
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
#def run
|
||||||
|
# yield @ipcc
|
||||||
|
# ipcc.close
|
||||||
|
#end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestWS
|
||||||
|
property ws : WebSocket
|
||||||
|
property is_json : Bool
|
||||||
|
|
||||||
|
def initialize(uri : String)
|
||||||
|
@ws = WebSocket.new(URI.parse(uri))
|
||||||
|
@is_json = uri.ends_with? ".JSON"
|
||||||
|
@ws.on_close do |socket|
|
||||||
|
raise "socket is closing"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def read
|
||||||
|
m = nil
|
||||||
|
loop do
|
||||||
|
m = @ws.run_once
|
||||||
|
if m.nil?
|
||||||
|
raise "empty message"
|
||||||
|
end
|
||||||
|
|
||||||
|
# remove ping messages, they are not application-relevent
|
||||||
|
unless m.is_a?(HTTP::WebSocket::Ping)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
puts "received a ping message, skipping"
|
||||||
|
end
|
||||||
|
|
||||||
|
m
|
||||||
|
end
|
||||||
|
|
||||||
|
def send(type : Int32, data : String)
|
||||||
|
|
||||||
|
m : String | Bytes
|
||||||
|
|
||||||
|
if @is_json
|
||||||
|
m = IPC::Message.new(1.to_u8, type.to_u8, data).to_json
|
||||||
|
else
|
||||||
|
m = IPC::Message.new(1.to_u8, type.to_u8, data).to_packet
|
||||||
|
end
|
||||||
|
|
||||||
|
@ws.send m
|
||||||
|
end
|
||||||
|
|
||||||
|
def close
|
||||||
|
@ws.close
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue