aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ipc/message.go
blob: 3bd1e85c7e1b3b7c1e44706d33b664fbcb964a53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
}