diff options
author | Wolfy-J <[email protected]> | 2018-01-23 19:51:15 -0500 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-01-23 19:51:15 -0500 |
commit | 78a42de837928cf7d10a1ae04d7e82e56d66e1e2 (patch) | |
tree | 8882b9a051bcc9c42328df583c0bb8c39a89591e /protocol.go | |
parent | fa4bd78d9f7c5f74e8445374370927c742fc4e78 (diff) |
API update
Diffstat (limited to 'protocol.go')
-rw-r--r-- | protocol.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/protocol.go b/protocol.go new file mode 100644 index 00000000..b78f2807 --- /dev/null +++ b/protocol.go @@ -0,0 +1,52 @@ +package roadrunner + +import ( + "encoding/json" + "fmt" + "github.com/spiral/goridge" + "os" +) + +type stopCommand struct { + Stop bool `json:"stop"` +} + +type pidCommand struct { + Pid int `json:"pid"` +} + +func sendHead(rl goridge.Relay, v interface{}) error { + if v == nil { + rl.Send(nil, goridge.PayloadControl) + } + + if data, ok := v.([]byte); ok { + return rl.Send(data, goridge.PayloadControl) + } + + data, err := json.Marshal(v) + if err != nil { + return fmt.Errorf("invalid payload: %s", err) + } + + return rl.Send(data, goridge.PayloadControl) +} + +func fetchPID(rl goridge.Relay) (pid int, err error) { + if err := sendHead(rl, pidCommand{Pid: os.Getpid()}); err != nil { + return 0, err + } + + body, p, err := rl.Receive() + if !p.HasFlag(goridge.PayloadControl) { + return 0, fmt.Errorf("unexpected response, header is missing") + } + + link := &pidCommand{} + //log.Println(string(body)) + if err := json.Unmarshal(body, link); err != nil { + return 0, err + } + + return link.Pid, nil +} |