IPC::Message#from_json and #to_json
parent
07f2099553
commit
25eb62a8f9
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in New Issue