Crystal bindings: IPC::JSON comments.
parent
d2ab260255
commit
32fa98bc76
|
@ -1,5 +1,14 @@
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
|
# IPC::JSON is the mother class for all exchanged messages (using JSON).
|
||||||
|
# IPC::JSON inherited classes have a common 'type' class attribute,
|
||||||
|
# which enables to find the right IPC::JSON+ class given a TypedMessage's type.
|
||||||
|
|
||||||
|
# All transfered messages are typed messages.
|
||||||
|
# TypedMessage = u8 type (= IPC::JSON+ class type) + JSON content.
|
||||||
|
# 'JSON content' being a serialized IPC::JSON+ class.
|
||||||
|
|
||||||
|
# Conventionally, IPC::JSON+ classes have a 'handle' method to process incoming messages.
|
||||||
class IPC::JSON
|
class IPC::JSON
|
||||||
include ::JSON::Serializable
|
include ::JSON::Serializable
|
||||||
|
|
||||||
|
@ -22,12 +31,18 @@ class IPC::JSON
|
||||||
end
|
end
|
||||||
|
|
||||||
class IPC
|
class IPC
|
||||||
|
# Schedule messages contained into IPC::JSON+.
|
||||||
def schedule(fd : Int32, message : IPC::JSON)
|
def schedule(fd : Int32, message : IPC::JSON)
|
||||||
schedule fd, message.type.to_u8, message.to_json
|
typed_msg = IPCMessage::TypedMessage.new message.type.to_u8, message.to_json
|
||||||
|
schedule fd, typed_msg
|
||||||
|
end
|
||||||
|
def write(fd : Int32, message : IPC::JSON)
|
||||||
|
typed_msg = IPCMessage::TypedMessage.new message.type.to_u8, message.to_json
|
||||||
|
write fd, typed_msg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# CAUTION: Only use this method on an Array(IPC::JSON.class)
|
# CAUTION: only use this method on an Array(IPC::JSON.class).
|
||||||
class Array(T)
|
class Array(T)
|
||||||
def parse_ipc_json(message : IPCMessage::TypedMessage) : IPC::JSON?
|
def parse_ipc_json(message : IPCMessage::TypedMessage) : IPC::JSON?
|
||||||
message_type = find &.type.==(message.type)
|
message_type = find &.type.==(message.type)
|
||||||
|
|
Reference in New Issue