aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ipc/message.go
diff options
context:
space:
mode:
authorMoritz Poldrack <git@moritz.sh>2023-03-04 10:56:45 +0100
committerRobin Jarry <robin@jarry.cc>2023-03-07 00:12:47 +0100
commit4838efdb1d5a746432a30ef0b86b090aab52fa7a (patch)
tree10c1c6954e47395ae9b310ce268a77cf9924a4d8 /lib/ipc/message.go
parent3dbf33bb4c8988851eeed0292fcdc170eb0ee6c7 (diff)
downloadaerc-4838efdb1d5a746432a30ef0b86b090aab52fa7a.tar.gz
ipc: change protocol to JSON
In overhauling the IPC, it has become necessary to switch to a more extendable message format, to ensure more complex commands can be sent. Messages have the following basic structure and must not contain linebreaks, as these are used to delimit separate messages from one another. {"arguments": ["mailto:moritz@poldrack.dev"]} The responses have the following structure: {"error": "epic fail"} If the IPC request was successful, "error" will be empty. {"error": ""} Signed-off-by: Moritz Poldrack <git@moritz.sh> Signed-off-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/ipc/message.go')
-rw-r--r--lib/ipc/message.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/ipc/message.go b/lib/ipc/message.go
new file mode 100644
index 00000000..3bd1e85c
--- /dev/null
+++ b/lib/ipc/message.go
@@ -0,0 +1,52 @@
+package ipc
+
+import "encoding/json"
+
+// Request constains all parameters needed for the main instance to respond to
+// a request.
+type Request struct {
+ // Arguments contains the commandline arguments. The detection of what
+ // action to take is left to the receiver.
+ Arguments []string `json:"arguments"`
+}
+
+// Response is used to report the results of a command.
+type Response struct {
+ // Error contains the success-state of the command. Error is an empty
+ // string if everything ran successfully.
+ Error string `json:"error"`
+}
+
+// Encode transforms the message in an easier to transfer format
+func (msg *Request) Encode() ([]byte, error) {
+ return json.Marshal(msg)
+}
+
+// DecodeMessage consumes a raw message and returns the message contained
+// within.
+func DecodeMessage(data []byte) (*Request, error) {
+ msg := new(Request)
+ err := json.Unmarshal(data, msg)
+ return msg, err
+}
+
+// Encode transforms the message in an easier to transfer format
+func (msg *Response) Encode() ([]byte, error) {
+ return json.Marshal(msg)
+}
+
+// DecodeRequest consumes a raw message and returns the message contained
+// within.
+func DecodeRequest(data []byte) (*Request, error) {
+ msg := new(Request)
+ err := json.Unmarshal(data, msg)
+ return msg, err
+}
+
+// DecodeResponse consumes a raw message and returns the message contained
+// within.
+func DecodeResponse(data []byte) (*Response, error) {
+ msg := new(Response)
+ err := json.Unmarshal(data, msg)
+ return msg, err
+}