backend/cli: More RPC feature demos

This commit is contained in:
John Brooks 2016-08-02 17:28:26 -06:00
parent 7733c6d572
commit f91f007295
3 changed files with 51 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package main
import (
ricochet "github.com/special/ricochet-go/core"
rpc "github.com/special/ricochet-go/rpc"
"google.golang.org/grpc"
"log"
@ -16,7 +17,18 @@ func main() {
if err != nil {
log.Fatalf("listen failed: %v", err)
}
config, err := ricochet.LoadConfig(".")
if err != nil {
log.Fatalf("config error: %v", err)
}
ricochet := &RicochetCore{
Network: ricochet.CreateNetwork(),
Config: config,
}
rpcServer := grpc.NewServer()
rpc.RegisterRicochetCoreServer(rpcServer, &RicochetCore{})
rpc.RegisterRicochetCoreServer(rpcServer, ricochet)
rpcServer.Serve(listener)
}

View File

@ -2,12 +2,15 @@ package main
import (
"errors"
ricochet "github.com/special/ricochet-go/core"
rpc "github.com/special/ricochet-go/rpc"
"golang.org/x/net/context"
"time"
"log"
)
type RicochetCore struct {
Network *ricochet.Network
Config *ricochet.Config
}
func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
@ -29,19 +32,41 @@ func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream
},
}
for i := 0; i < 100; i++ {
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)
if err := stream.Send(status); err != nil {
return err
}
time.Sleep(100 * time.Millisecond)
}
return nil
}
func (core *RicochetCore) StartNetwork(ctx context.Context, req *rpc.StartNetworkRequest) (*rpc.NetworkStatus, error) {
return nil, errors.New("Not implemented")
// 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
}
func (core *RicochetCore) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
return nil, errors.New("Not implemented")
core.Network.Stop()
return &rpc.NetworkStatus{}, nil
}

View File

@ -47,8 +47,15 @@ func main() {
}
log.Printf("stream says: %v", resp)
}
wait <- struct{}{}
close(wait)
}()
status, err := c.StartNetwork(context.Background(), &rpc.StartNetworkRequest{})
if err != nil {
log.Printf("start network error: %v", err)
} else {
log.Printf("network started: %v", status)
}
<-wait
}