Obsolete
/
libipc-old
Archived
3
0
Fork 0

Crystal bindings: restructuration.

master
Philippe Pittoli 2023-02-03 04:06:18 +01:00
parent dda7514a61
commit 08523fa90a
7 changed files with 1 additions and 185 deletions

View File

@ -1,25 +0,0 @@
require "./message.cr"
# TODO:
class AuthMessage < IPCMessage::TypedMessage
def to_s
content = String.new(@payload.to_unsafe, @payload.size)
"AUTH MSG: type #{@type} fd #{@fd} bytes #{@payload.size} [#{content}]"
rescue
"AUTH MSG: type #{@type} fd #{@fd} bytes #{@payload.size} [#{@payload}]"
end
end
module Request
class Auth < AuthMessage
@type = 1
end
end
module Response
class Auth < AuthMessage
@type = 2
end
end
puts Request::Auth.new(5, "hello I'm karchnu").to_s
puts Response::Auth.new(3, "hello okay you're in").to_s

View File

@ -1,38 +0,0 @@
require "./main.cr"
# In 5 messages: quit
count = 5
ipc = IPC.new
fd = ipc.service_init("pong")
ipc.loop do |event|
case event.type
when LibIPC::EventType::MessageRx
m = event.message
if m.nil?
puts "No message"
else
received = String.new(m.to_unsafe, m.size)
pp! received
ipc.schedule event.fd, m, m.size
end
when LibIPC::EventType::MessageTx
puts "A message has been sent"
count -= 1
if count == 0
exit
end
when LibIPC::EventType::Connection
puts "A client just connected #JOY"
when LibIPC::EventType::Disconnection
puts "A client just disconnected #SAD"
else
puts "Unexpected: #{event.type}"
exit 1
end
end

View File

@ -1,82 +0,0 @@
require "./main.cr"
require "./json.cr"
require "baguette-crystal-base"
# Context class, so the variables are available everywhere.
class Context
class_property requests = [] of IPC::JSON.class
class_property responses = [] of IPC::JSON.class
end
class IPC::JSON
def handle
raise "unimplemented"
end
end
IPC::JSON.message Message, 10 do
property content : String?
property some_number : Int32?
def initialize(@content = nil, @some_number = nil)
end
def handle
Baguette::Log.info "message received: #{@content}, number: #{@some_number}"
if number = @some_number
::MessageReceived.new number - 1
else
::MessageReceived.new
end
end
end
Context.requests << Message
IPC::JSON.message Error, 0 do
property reason : String
def initialize(@reason)
end
end
Context.responses << Error
IPC::JSON.message MessageReceived, 20 do
property minus_one : Int32?
def initialize(@minus_one = nil)
end
def handle
Baguette::Log.info "<< MessageReceived (#{@minus_one})"
end
end
Context.responses << MessageReceived
pp! Context.requests
pp! Context.responses
request = ::Message.new "hello this is a request", 30
pp! request
pp! request.to_json
response = request.handle
pp! response
response.handle
received_raw_message = Bytes[0x0a, 0x68, 0x65, 0x6c, 0x6c, 0x6f]
received_message = IPCMessage::TypedMessage.deserialize received_raw_message
pp! received_message
if received_message.nil?
puts "received message: nil!!!"
else
s = String.new received_message.payload.not_nil!
pp! s
jpayload = "{\"content\":\"hello this is a request\",\"some_number\":30}"
payload = Bytes.new jpayload.to_unsafe, jpayload.size
received_message.payload = payload
begin
r = Context.requests.parse_ipc_json(received_message)
pp! r
rescue e
puts "error while parsing JSON: #{e}"
end
end

View File

@ -1,39 +0,0 @@
def test_without_wait()
ctx = Pointer(Void).null
LibIPC.init (pointerof(ctx))
fd : Int32 = 0
LibIPC.connect_service(ctx, pointerof(fd), "pong", 4)
pp! fd
LibIPC.write(ctx, fd, "Hello", 5)
buflen : LibC::UInt64T = 10
buffer = uninitialized UInt8[10]
LibIPC.read(ctx, fd, buffer.to_unsafe, pointerof(buflen))
received = String.new(buffer.to_unsafe, buflen)
pp! received
LibIPC.deinit (pointerof(ctx))
end
def test_with_wait()
ctx = Pointer(Void).null
LibIPC.init (pointerof(ctx))
fd : Int32 = 0
LibIPC.connect_service(ctx, pointerof(fd), "pong", 4)
LibIPC.write(ctx, fd, "Hello", 5)
buflen : LibC::UInt64T = 10
buffer = uninitialized UInt8[10]
eventtype : UInt8 = 0
index : LibC::UInt64T = 0
LibIPC.timer(ctx, 2000) # Wait at most 2 seconds.
LibIPC.wait(ctx, pointerof(eventtype), pointerof(index), pointerof(fd), buffer.to_unsafe, pointerof(buflen))
#pp! LibIPC::EventType.new(eventtype), fd, index, buflen
received = String.new(buffer.to_unsafe, buflen)
pp! received
LibIPC.deinit (pointerof(ctx))
end

View File

@ -1,4 +1,4 @@
require "./some-crystal-app.cr"
require "../main.cr"
def test_high_level
ipc = IPC.new