From 25eb62a8f9f713458a2a88928c9813c4b2bb9a6f Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Mon, 3 Feb 2020 23:50:13 +0100 Subject: [PATCH] IPC::Message#from_json and #to_json --- src/ipc/connection.cr | 13 ++++--- src/ipc/message.cr | 91 +++++++++++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 38 deletions(-) diff --git a/src/ipc/connection.cr b/src/ipc/connection.cr index c1da50d..fea663f 100644 --- a/src/ipc/connection.cr +++ b/src/ipc/connection.cr @@ -41,8 +41,11 @@ class IPC::Connection @connection.fd end - def send(type : UInt8, payload : Bytes) - message = LibIPC::Message.new type: LibIPC::MessageType::Data.to_u8, user_type: type, length: payload.bytesize, payload: payload.to_unsafe + def send(utype : UInt8, payload : Bytes) + message = LibIPC::Message.new type: LibIPC::MessageType::Data.to_u8, + user_type: utype, + length: payload.bytesize, + payload: payload.to_unsafe r = LibIPC.ipc_write(self.pointer, pointerof(message)) if r.error_code != 0 @@ -51,12 +54,12 @@ class IPC::Connection end end - def send(type : UInt8, payload : String) - send(type, Bytes.new(payload.to_unsafe, payload.bytesize)) + def send(utype : UInt8, payload : String) + send(utype, Bytes.new(payload.to_unsafe, payload.bytesize)) end def send(message : IPC::Message) - send(message.type, message.payload) + send(message.utype, message.payload) end def read diff --git a/src/ipc/message.cr b/src/ipc/message.cr index 8b2fa79..0276e9f 100644 --- a/src/ipc/message.cr +++ b/src/ipc/message.cr @@ -1,7 +1,63 @@ require "./lowlevel" +require "json" + +# JSON is currently used for messages over websockets +# At some point, this will be replaced by the CBOR format class IPC::Message + property mtype : UInt8 # libipc message type + property utype : UInt8 # libipc user message type + property payload : Bytes + + struct JSONMessage + include JSON::Serializable + + property mtype : UInt8 = 1 # libipc message type + property utype : UInt8 # libipc user message type + property payload : String + + def initialize(@utype, @payload, @mtype = 1) + end + end + + def self.from_json (str : String) + jsonmessage = JSONMessage.from_json str + + IPC::Message.new jsonmessage.mtype, jsonmessage.utype, jsonmessage.payload + end + + def to_json + JSONMessage.new(@utype, String.new(@payload), @mtype).to_json + end + + def initialize(message : Pointer(LibIPC::Message)) + if message.null? + @mtype = LibIPC::MessageType::Error.to_u8 + @utype = 0 + @payload = Bytes.new "".to_unsafe, 0 + else + m = message.value + @mtype = m.type + @utype = m.user_type + @payload = Bytes.new m.payload, m.length + end + end + + def initialize(message : LibIPC::Message) + initialize pointerof(message) + end + + def initialize(mtype, utype, payload : Bytes) + @mtype = mtype.to_u8 + @utype = utype + @payload = payload + end + + def initialize(mtype, utype, payload : String) + initialize(mtype, utype, Bytes.new(payload.to_unsafe, payload.bytesize)) + end + def self.to_packet (user_type : Int, message : String) payload = Bytes.new (6 + message.to_slice.size) @@ -16,43 +72,12 @@ class IPC::Message return payload end - getter mtype : UInt8 # libipc message type - property type : UInt8 # libipc user message type - property payload : Bytes - - def initialize(message : Pointer(LibIPC::Message)) - if message.null? - @mtype = LibIPC::MessageType::Error.to_u8 - @type = 0 - @payload = Bytes.new "".to_unsafe, 0 - else - m = message.value - @mtype = m.type - @type = m.user_type - @payload = Bytes.new m.payload, m.length - end - end - - def initialize(message : LibIPC::Message) - initialize pointerof(message) - end - - def initialize(mtype, type, payload : Bytes) - @mtype = mtype.to_u8 - @type = type - @payload = payload - end - - def initialize(mtype, type, payload : String) - initialize(mtype, type, Bytes.new(payload.to_unsafe, payload.bytesize)) - end - def to_packet - IPC::Message.to_packet @type, String.new(@payload) + IPC::Message.to_packet @utype, String.new(@payload) end def to_s - "(internal) type #{@mtype}, (user) type #{@type}, payload #{String.new @payload}" + "(internal) utype #{@mtype}, (user) utype #{@utype}, payload #{String.new @payload}" end end