| 1 |
package ipc |
| 2 |
|
| 3 |
import "encoding/json" |
| 4 |
|
| 5 |
// Request contains all parameters needed for the main instance to respond to |
| 6 |
// a request. |
| 7 |
type Request struct { |
| 8 |
// Arguments contains the commandline arguments. The detection of what |
| 9 |
// action to take is left to the receiver. |
| 10 |
Arguments []string `json:"arguments"` |
| 11 |
} |
| 12 |
|
| 13 |
// Response is used to report the results of a command. |
| 14 |
type Response struct { |
| 15 |
// Error contains the success-state of the command. Error is an empty |
| 16 |
// string if everything ran successfully. |
| 17 |
Error string `json:"error"` |
| 18 |
} |
| 19 |
|
| 20 |
// Encode transforms the message in an easier to transfer format |
| 21 |
func (msg *Request) Encode() ([]byte, error) { |
| 22 |
return json.Marshal(msg) |
| 23 |
} |
| 24 |
|
| 25 |
// DecodeMessage consumes a raw message and returns the message contained |
| 26 |
// within. |
| 27 |
func DecodeMessage(data []byte) (*Request, error) { |
| 28 |
msg := new(Request) |
| 29 |
err := json.Unmarshal(data, msg) |
| 30 |
return msg, err |
| 31 |
} |
| 32 |
|
| 33 |
// Encode transforms the message in an easier to transfer format |
| 34 |
func (msg *Response) Encode() ([]byte, error) { |
| 35 |
return json.Marshal(msg) |
| 36 |
} |
| 37 |
|
| 38 |
// DecodeRequest consumes a raw message and returns the message contained |
| 39 |
// within. |
| 40 |
func DecodeRequest(data []byte) (*Request, error) { |
| 41 |
msg := new(Request) |
| 42 |
err := json.Unmarshal(data, msg) |
| 43 |
return msg, err |
| 44 |
} |
| 45 |
|
| 46 |
// DecodeResponse consumes a raw message and returns the message contained |
| 47 |
// within. |
| 48 |
func DecodeResponse(data []byte) (*Response, error) { |
| 49 |
msg := new(Response) |
| 50 |
err := json.Unmarshal(data, msg) |
| 51 |
return msg, err |
| 52 |
} |