2016-08-02 02:58:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2016-08-02 23:33:05 +00:00
|
|
|
ricochet "github.com/special/notricochet/core"
|
|
|
|
rpc "github.com/special/notricochet/rpc"
|
2016-08-02 02:58:10 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2016-08-17 00:43:39 +00:00
|
|
|
var NotImplementedError error = errors.New("Not implemented")
|
|
|
|
|
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 {
|
2016-08-02 23:28:26 +00:00
|
|
|
events := core.Network.EventMonitor().Subscribe(20)
|
|
|
|
defer core.Network.EventMonitor().Unsubscribe(events)
|
|
|
|
|
2016-08-03 05:39:13 +00:00
|
|
|
// Send initial status event
|
|
|
|
{
|
|
|
|
event := core.Network.GetStatus()
|
|
|
|
if err := stream.Send(&event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-02 23:28:26 +00:00
|
|
|
for {
|
2016-08-03 05:39:13 +00:00
|
|
|
event, ok := (<-events).(rpc.NetworkStatus)
|
2016-08-02 23:28:26 +00:00
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("RPC monitor event: %v", event)
|
2016-08-03 05:39:13 +00:00
|
|
|
if err := stream.Send(&event); err != nil {
|
2016-08-02 02:58:10 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-03 05:39:13 +00:00
|
|
|
status := core.Network.GetStatus()
|
|
|
|
return &status, 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()
|
2016-08-03 05:39:13 +00:00
|
|
|
status := core.Network.GetStatus()
|
|
|
|
return &status, nil
|
2016-08-02 02:58:10 +00:00
|
|
|
}
|
2016-08-17 00:43:39 +00:00
|
|
|
|
|
|
|
func (core *RicochetCore) GetIdentity(ctx context.Context, req *rpc.IdentityRequest) (*rpc.Identity, error) {
|
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) MonitorContacts(req *rpc.MonitorContactsRequest, stream rpc.RicochetCore_MonitorContactsServer) error {
|
|
|
|
return NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) AddContactRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.Contact, error) {
|
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) UpdateContact(ctx context.Context, req *rpc.Contact) (*rpc.Contact, error) {
|
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) DeleteContact(ctx context.Context, req *rpc.DeleteContactRequest) (*rpc.DeleteContactReply, error) {
|
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) AcceptInboundRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.Contact, error) {
|
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) RejectInboundRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.RejectInboundRequestReply, error) {
|
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (core *RicochetCore) StreamConversations(stream rpc.RicochetCore_StreamConversationsServer) error {
|
|
|
|
return NotImplementedError
|
|
|
|
}
|