core: Change the 'Ricochet' API again

This commit is contained in:
John Brooks 2016-09-10 22:57:54 -06:00
parent 20700414fc
commit 83d4d8841d
5 changed files with 56 additions and 59 deletions

View File

@ -23,16 +23,15 @@ func main() {
log.Fatalf("config error: %v", err)
}
core := &RicochetCore{
config: config,
}
core.network = ricochet.CreateNetwork()
core.identity, err = ricochet.CreateIdentity(core)
if err != nil {
log.Fatalf("identity error: %v", err)
core := new(ricochet.Ricochet)
if err := core.Init(config); err != nil {
log.Fatalf("init error: %v", err)
}
rpcServer := grpc.NewServer()
rpc.RegisterRicochetCoreServer(rpcServer, core)
rpcServer.Serve(listener)
server := &RpcServer{
core: core,
}
grpcServer := grpc.NewServer()
rpc.RegisterRicochetCoreServer(grpcServer, server)
grpcServer.Serve(listener)
}

View File

@ -11,25 +11,11 @@ import (
var NotImplementedError error = errors.New("Not implemented")
type RicochetCore struct {
config *ricochet.Config
network *ricochet.Network
identity *ricochet.Identity
type RpcServer struct {
core *ricochet.Ricochet
}
func (core *RicochetCore) Config() *ricochet.Config {
return core.config
}
func (core *RicochetCore) Network() *ricochet.Network {
return core.network
}
func (core *RicochetCore) Identity() *ricochet.Identity {
return core.identity
}
func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
func (s *RpcServer) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
if req.RpcVersion != 1 {
return nil, errors.New("Unsupported RPC protocol version")
}
@ -40,13 +26,13 @@ func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerSt
}, nil
}
func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream rpc.RicochetCore_MonitorNetworkServer) error {
events := core.Network().EventMonitor().Subscribe(20)
defer core.Network().EventMonitor().Unsubscribe(events)
func (s *RpcServer) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream rpc.RicochetCore_MonitorNetworkServer) error {
events := s.core.Network.EventMonitor().Subscribe(20)
defer s.core.Network.EventMonitor().Unsubscribe(events)
// Send initial status event
{
event := core.Network().GetStatus()
event := s.core.Network.GetStatus()
if err := stream.Send(&event); err != nil {
return err
}
@ -67,37 +53,37 @@ func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream
return nil
}
func (core *RicochetCore) StartNetwork(ctx context.Context, req *rpc.StartNetworkRequest) (*rpc.NetworkStatus, error) {
func (s *RpcServer) StartNetwork(ctx context.Context, req *rpc.StartNetworkRequest) (*rpc.NetworkStatus, error) {
// 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", "")
ok, err := s.core.Network.Start("tcp://127.0.0.1:9051", "")
if !ok {
return nil, err
}
status := core.Network().GetStatus()
status := s.core.Network.GetStatus()
return &status, nil
}
func (core *RicochetCore) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
core.Network().Stop()
status := core.Network().GetStatus()
func (s *RpcServer) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
s.core.Network.Stop()
status := s.core.Network.GetStatus()
return &status, nil
}
func (core *RicochetCore) GetIdentity(ctx context.Context, req *rpc.IdentityRequest) (*rpc.Identity, error) {
func (s *RpcServer) GetIdentity(ctx context.Context, req *rpc.IdentityRequest) (*rpc.Identity, error) {
reply := rpc.Identity{
Address: core.Identity().Address(),
Address: s.core.Identity.Address(),
}
return &reply, nil
}
func (core *RicochetCore) MonitorContacts(req *rpc.MonitorContactsRequest, stream rpc.RicochetCore_MonitorContactsServer) error {
monitor := core.Identity().ContactList().EventMonitor().Subscribe(20)
defer core.Identity().ContactList().EventMonitor().Unsubscribe(monitor)
func (s *RpcServer) MonitorContacts(req *rpc.MonitorContactsRequest, stream rpc.RicochetCore_MonitorContactsServer) error {
monitor := s.core.Identity.ContactList().EventMonitor().Subscribe(20)
defer s.core.Identity.ContactList().EventMonitor().Unsubscribe(monitor)
// Populate
contacts := core.Identity().ContactList().Contacts()
contacts := s.core.Identity.ContactList().Contacts()
for _, contact := range contacts {
data := &rpc.Contact{
Id: int32(contact.Id()),
@ -141,16 +127,16 @@ func (core *RicochetCore) MonitorContacts(req *rpc.MonitorContactsRequest, strea
return nil
}
func (core *RicochetCore) AddContactRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.Contact, error) {
func (s *RpcServer) 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) {
func (s *RpcServer) 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) {
contactList := core.Identity().ContactList()
func (s *RpcServer) DeleteContact(ctx context.Context, req *rpc.DeleteContactRequest) (*rpc.DeleteContactReply, error) {
contactList := s.core.Identity.ContactList()
contact := contactList.ContactByAddress(req.Address)
if contact == nil || (req.Id != 0 && contact.Id() != int(req.Id)) {
return nil, errors.New("Contact not found")
@ -163,14 +149,14 @@ func (core *RicochetCore) DeleteContact(ctx context.Context, req *rpc.DeleteCont
return &rpc.DeleteContactReply{}, nil
}
func (core *RicochetCore) AcceptInboundRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.Contact, error) {
func (s *RpcServer) 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) {
func (s *RpcServer) RejectInboundRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.RejectInboundRequestReply, error) {
return nil, NotImplementedError
}
func (core *RicochetCore) StreamConversations(stream rpc.RicochetCore_StreamConversationsServer) error {
func (s *RpcServer) StreamConversations(stream rpc.RicochetCore_StreamConversationsServer) error {
return NotImplementedError
}

View File

@ -18,12 +18,12 @@ type ContactList struct {
inboundRequests map[int]*InboundContactRequest
}
func LoadContactList(core Ricochet) (*ContactList, error) {
func LoadContactList(core *Ricochet) (*ContactList, error) {
list := &ContactList{
events: utils.CreatePublisher(),
}
config := core.Config().OpenRead()
config := core.Config.OpenRead()
defer config.Close()
list.contacts = make(map[int]*Contact, len(config.Contacts))

View File

@ -9,7 +9,7 @@ import (
)
type Identity struct {
core Ricochet
core *Ricochet
address string
privateKey *rsa.PrivateKey
@ -17,7 +17,7 @@ type Identity struct {
contactList *ContactList
}
func CreateIdentity(core Ricochet) (*Identity, error) {
func CreateIdentity(core *Ricochet) (*Identity, error) {
me := &Identity{
core: core,
}
@ -39,7 +39,7 @@ func CreateIdentity(core Ricochet) (*Identity, error) {
}
func (me *Identity) loadIdentity() error {
config := me.core.Config().OpenRead()
config := me.core.Config.OpenRead()
defer config.Close()
if config.Identity.ServiceKey != "" {

View File

@ -1,7 +1,19 @@
package core
type Ricochet interface {
Config() *Config
Network() *Network
Identity() *Identity
type Ricochet struct {
Config *Config
Network *Network
Identity *Identity
}
func (core *Ricochet) Init(conf *Config) error {
var err error
core.Config = conf
core.Network = CreateNetwork()
core.Identity, err = CreateIdentity(core)
if err != nil {
return err
}
return nil
}