Configurable proxy name and proxified service.
parent
0a3a72a5fc
commit
bee8e04b3c
24
src/ipcd.cr
24
src/ipcd.cr
|
@ -4,10 +4,17 @@ require "hexa"
|
|||
# TODO: Write documentation for `IPCd`
|
||||
module IPCd
|
||||
VERSION = "0.1.0"
|
||||
@@proxy_name = "ipcd"
|
||||
@@proxied_service = "pong"
|
||||
|
||||
def self.start
|
||||
@@proxied_service = ARGV[0] if ARGV.size >= 1
|
||||
@@proxy_name = ARGV[1] if ARGV.size >= 2
|
||||
|
||||
puts "proxy named #{@@proxy_name} for service #{@@proxied_service}"
|
||||
|
||||
ipcd = IPC.new()
|
||||
ipcd.service_init "hello"
|
||||
ipcd.service_init @@proxy_name
|
||||
ipcd.timer 10_000
|
||||
timer = 0
|
||||
ipcd.loop do |event|
|
||||
|
@ -19,12 +26,12 @@ module IPCd
|
|||
timer += 1
|
||||
|
||||
when LibIPC::EventType::Connection
|
||||
fd = ipcd.connect "pong"
|
||||
fd = ipcd.connect @@proxied_service
|
||||
ipcd.add_switch event.newfd, fd
|
||||
input = -> fn_input( LibC::Int, LibC::Char*, LibC::UInt64T*)
|
||||
output = -> fn_output(LibC::Int, LibC::Char*, LibC::UInt64T )
|
||||
ipcd.switch_callbacks event.newfd, input, output
|
||||
puts "New connection (#{event.newfd}) now automatically switched to 'pong' (#{fd})!"
|
||||
puts "New connection (#{event.newfd}) now automatically switched to '#{@@proxied_service}' (#{fd})!"
|
||||
|
||||
when LibIPC::EventType::Disconnection
|
||||
puts "Disconnection from #{event.fd}."
|
||||
|
@ -59,17 +66,7 @@ module IPCd
|
|||
end
|
||||
end
|
||||
|
||||
#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
|
||||
|
||||
def fn_input(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T*) : LibC::Char
|
||||
# puts "switch input: #{fd}"
|
||||
io = IO::FileDescriptor.new fd, close_on_finalize: false
|
||||
slice = Bytes.new buffer, buflen.value
|
||||
len = io.read slice
|
||||
|
@ -87,7 +84,6 @@ rescue e
|
|||
end
|
||||
|
||||
def fn_output(fd : LibC::Int, buffer : LibC::Char*, buflen : LibC::UInt64T) : LibC::Char
|
||||
#puts "switch output: #{fd}"
|
||||
hexa = Hexa.new 50_000
|
||||
bytes = Bytes.new buffer, buflen
|
||||
puts hexa.dump "message to send to #{fd}", bytes
|
||||
|
|
Loading…
Reference in New Issue