-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipes.go
72 lines (61 loc) · 1.33 KB
/
pipes.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"io"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"github.com/nymsio/nyms-agent/protocol"
gl "github.com/op/go-logging"
)
var logger = gl.MustGetLogger("nymsd")
type pipePair struct {
input io.Reader
output io.Writer
}
func (pp pipePair) Read(p []byte) (int, error) {
return pp.input.Read(p)
}
func (pp pipePair) Write(p []byte) (int, error) {
return pp.output.Write(p)
}
func (pp pipePair) Close() (e error) {
if err := tryClose(pp.input); err != nil {
e = err
}
if err := tryClose(pp.output); err != nil && e == nil {
e = err
}
return e
}
func tryClose(x interface{}) error {
if c, ok := x.(io.Closer); ok {
return c.Close()
}
return nil
}
func runPipeServer(protoDebug bool) {
protocol := new(protocol.Protocol)
rpc.Register(protocol)
pp, err := createPipePair(os.Stdin, os.Stdout, protoDebug)
if err != nil {
logger.Warning(fmt.Sprintf("Failed to create pipe pair: %s", err))
return
}
codec := jsonrpc.NewServerCodec(pp)
logger.Info("Starting...")
rpc.ServeCodec(codec)
}
func createPipePair(r io.Reader, w io.Writer, protoDebug bool) (*pipePair, error) {
/*
if protoDebug {
logger.Info("Creating debug pipes")
reader, writer, err := logger.OpenProtocolLog(r, w)
if err != nil {
return nil, err
}
return &pipePair{reader, writer}, nil
}
*/
return &pipePair{r, w}, nil
}