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 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
}
|