ricochet-go/backend/rpc.go

73 lines
1.8 KiB
Go
Raw Normal View History

2016-08-02 02:58:10 +00:00
package main
import (
"errors"
2016-08-02 23:28:26 +00:00
ricochet "github.com/special/ricochet-go/core"
2016-08-02 02:58:10 +00:00
rpc "github.com/special/ricochet-go/rpc"
"golang.org/x/net/context"
2016-08-02 23:28:26 +00:00
"log"
2016-08-02 02:58:10 +00:00
)
type RicochetCore struct {
2016-08-02 23:28:26 +00:00
Network *ricochet.Network
Config *ricochet.Config
2016-08-02 02:58:10 +00:00
}
func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
if req.RpcVersion != 1 {
return nil, errors.New("Unsupported RPC protocol version")
}
return &rpc.ServerStatusReply{
RpcVersion: 1,
ServerVersion: "0.0.0",
}, nil
}
func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream rpc.RicochetCore_MonitorNetworkServer) error {
status := &rpc.NetworkStatus{
Control: &rpc.TorControlStatus{
Status: rpc.TorControlStatus_ERROR,
ErrorMessage: "Not implemented",
},
}
2016-08-02 23:28:26 +00:00
events := core.Network.EventMonitor().Subscribe(20)
defer core.Network.EventMonitor().Unsubscribe(events)
for {
event, ok := (<-events).(bool)
if !ok {
break
}
log.Printf("RPC monitor event: %v", event)
2016-08-02 02:58:10 +00:00
if err := stream.Send(status); err != nil {
return err
}
}
2016-08-02 23:28:26 +00:00
2016-08-02 02:58:10 +00:00
return nil
}
func (core *RicochetCore) StartNetwork(ctx context.Context, req *rpc.StartNetworkRequest) (*rpc.NetworkStatus, error) {
2016-08-02 23:28:26 +00:00
// err represents the result of the first connection attempt, but as long
// as 'ok' is true, the network has started and this call was successful.
ok, err := core.Network.Start("tcp://127.0.0.1:9051", "")
if !ok {
return nil, err
}
// XXX real status
return &rpc.NetworkStatus{
Control: &rpc.TorControlStatus{
Status: rpc.TorControlStatus_CONNECTED,
},
}, nil
2016-08-02 02:58:10 +00:00
}
func (core *RicochetCore) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
2016-08-02 23:28:26 +00:00
core.Network.Stop()
return &rpc.NetworkStatus{}, nil
2016-08-02 02:58:10 +00:00
}