From a95866606031eb5c56f1321ebd832cadc62d5943 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 25 Jan 2023 04:47:17 +0100 Subject: [PATCH] Crystal library: uncluttered by test code. --- .../some-crystal-app/src/some-crystal-app.cr | 64 +++++-------------- 1 file changed, 16 insertions(+), 48 deletions(-) diff --git a/zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr b/zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr index 721ad49..a563ebd 100644 --- a/zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr +++ b/zig-impl/crystal/some-crystal-app/src/some-crystal-app.cr @@ -83,6 +83,16 @@ class IPC LibIPC.deinit(pointerof(@context)) end + def service_init(name : String) : Int + fd = uninitialized Int32 + puts "service name: #{name}" + puts "service name len: #{name.size}" + if LibIPC.service_init(@context, pointerof(fd), name, name.size) != 0 + raise "oh noes, 'service_init' iz brkn" + end + fd + end + def connect(name : String) : Int fd = uninitialized Int32 if LibIPC.connect_service(@context, pointerof(fd), name, name.size) != 0 @@ -105,11 +115,15 @@ class IPC end end - def schedule(fd : Int, string : String) + def schedule(fd : Int32, string : String) self.schedule(fd, string.to_unsafe, string.size.to_u64) end - def schedule(fd : Int, buffer : UInt8*, buflen : UInt64) + def schedule(fd : Int32, buffer : Array(UInt8), buflen : Int32) + self.schedule(fd, buffer.to_unsafe, buflen.to_u64) + end + + def schedule(fd : Int32, buffer : UInt8*, buflen : UInt64) if LibIPC.schedule(@context, fd, buffer, buflen) != 0 raise "oh noes, 'schedule' iz brkn" end @@ -158,49 +172,3 @@ class IPC end end end - - -def test_high_level - ipc = IPC.new - fd = ipc.connect("pong") - ipc.write(fd, "hello this is some value") - event = ipc.wait() - - m = event.message - if m.nil? - puts "No message" - else - pp! String.new(m.to_unsafe, m.size) - end -end - -def test_loop - ipc = IPC.new - fd = ipc.connect("pong") - ipc.schedule(fd, "hello this is some value") - ipc.loop do |event| - case event.type - when LibIPC::EventType::MessageRx - m = event.message - if m.nil? - puts "No message" - else - pp! String.new(m.to_unsafe, m.size) - end - exit 0 - when LibIPC::EventType::MessageTx - puts "A message has been sent" - else - puts "Unexpected: #{event.type}" - exit 1 - end - end -end - -# TODO: Write documentation for `Some::Crystal::App` -module Some::Crystal::App - VERSION = "0.1.0" - - test_high_level - test_loop -end