Obsolete
/
libipc-old
Archived
3
0
Fork 0

Crystal bindings: IPC::JSON comments.

master
Philippe Pittoli 2023-02-01 11:09:06 +01:00
parent d2ab260255
commit 32fa98bc76
1 changed files with 17 additions and 2 deletions

View File

@ -1,5 +1,14 @@
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
include ::JSON::Serializable
@ -22,12 +31,18 @@ class IPC::JSON
end
class IPC
# Schedule messages contained into 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
# 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)
def parse_ipc_json(message : IPCMessage::TypedMessage) : IPC::JSON?
message_type = find &.type.==(message.type)