core: Change the 'Ricochet' API again
This commit is contained in:
parent
20700414fc
commit
83d4d8841d
|
@ -23,16 +23,15 @@ func main() {
|
||||||
log.Fatalf("config error: %v", err)
|
log.Fatalf("config error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
core := &RicochetCore{
|
core := new(ricochet.Ricochet)
|
||||||
config: config,
|
if err := core.Init(config); err != nil {
|
||||||
}
|
log.Fatalf("init error: %v", err)
|
||||||
core.network = ricochet.CreateNetwork()
|
|
||||||
core.identity, err = ricochet.CreateIdentity(core)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("identity error: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rpcServer := grpc.NewServer()
|
server := &RpcServer{
|
||||||
rpc.RegisterRicochetCoreServer(rpcServer, core)
|
core: core,
|
||||||
rpcServer.Serve(listener)
|
}
|
||||||
|
grpcServer := grpc.NewServer()
|
||||||
|
rpc.RegisterRicochetCoreServer(grpcServer, server)
|
||||||
|
grpcServer.Serve(listener)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,25 +11,11 @@ import (
|
||||||
|
|
||||||
var NotImplementedError error = errors.New("Not implemented")
|
var NotImplementedError error = errors.New("Not implemented")
|
||||||
|
|
||||||
type RicochetCore struct {
|
type RpcServer struct {
|
||||||
config *ricochet.Config
|
core *ricochet.Ricochet
|
||||||
network *ricochet.Network
|
|
||||||
identity *ricochet.Identity
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (core *RicochetCore) Config() *ricochet.Config {
|
func (s *RpcServer) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
|
||||||
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) {
|
|
||||||
if req.RpcVersion != 1 {
|
if req.RpcVersion != 1 {
|
||||||
return nil, errors.New("Unsupported RPC protocol version")
|
return nil, errors.New("Unsupported RPC protocol version")
|
||||||
}
|
}
|
||||||
|
@ -40,13 +26,13 @@ func (core *RicochetCore) GetServerStatus(ctx context.Context, req *rpc.ServerSt
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream rpc.RicochetCore_MonitorNetworkServer) error {
|
func (s *RpcServer) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream rpc.RicochetCore_MonitorNetworkServer) error {
|
||||||
events := core.Network().EventMonitor().Subscribe(20)
|
events := s.core.Network.EventMonitor().Subscribe(20)
|
||||||
defer core.Network().EventMonitor().Unsubscribe(events)
|
defer s.core.Network.EventMonitor().Unsubscribe(events)
|
||||||
|
|
||||||
// Send initial status event
|
// Send initial status event
|
||||||
{
|
{
|
||||||
event := core.Network().GetStatus()
|
event := s.core.Network.GetStatus()
|
||||||
if err := stream.Send(&event); err != nil {
|
if err := stream.Send(&event); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -67,37 +53,37 @@ func (core *RicochetCore) MonitorNetwork(req *rpc.MonitorNetworkRequest, stream
|
||||||
return nil
|
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
|
// 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.
|
// 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 {
|
if !ok {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
status := core.Network().GetStatus()
|
status := s.core.Network.GetStatus()
|
||||||
return &status, nil
|
return &status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (core *RicochetCore) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
|
func (s *RpcServer) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.NetworkStatus, error) {
|
||||||
core.Network().Stop()
|
s.core.Network.Stop()
|
||||||
status := core.Network().GetStatus()
|
status := s.core.Network.GetStatus()
|
||||||
return &status, nil
|
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{
|
reply := rpc.Identity{
|
||||||
Address: core.Identity().Address(),
|
Address: s.core.Identity.Address(),
|
||||||
}
|
}
|
||||||
return &reply, nil
|
return &reply, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (core *RicochetCore) MonitorContacts(req *rpc.MonitorContactsRequest, stream rpc.RicochetCore_MonitorContactsServer) error {
|
func (s *RpcServer) MonitorContacts(req *rpc.MonitorContactsRequest, stream rpc.RicochetCore_MonitorContactsServer) error {
|
||||||
monitor := core.Identity().ContactList().EventMonitor().Subscribe(20)
|
monitor := s.core.Identity.ContactList().EventMonitor().Subscribe(20)
|
||||||
defer core.Identity().ContactList().EventMonitor().Unsubscribe(monitor)
|
defer s.core.Identity.ContactList().EventMonitor().Unsubscribe(monitor)
|
||||||
|
|
||||||
// Populate
|
// Populate
|
||||||
contacts := core.Identity().ContactList().Contacts()
|
contacts := s.core.Identity.ContactList().Contacts()
|
||||||
for _, contact := range contacts {
|
for _, contact := range contacts {
|
||||||
data := &rpc.Contact{
|
data := &rpc.Contact{
|
||||||
Id: int32(contact.Id()),
|
Id: int32(contact.Id()),
|
||||||
|
@ -141,16 +127,16 @@ func (core *RicochetCore) MonitorContacts(req *rpc.MonitorContactsRequest, strea
|
||||||
return nil
|
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
|
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
|
return nil, NotImplementedError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (core *RicochetCore) DeleteContact(ctx context.Context, req *rpc.DeleteContactRequest) (*rpc.DeleteContactReply, error) {
|
func (s *RpcServer) DeleteContact(ctx context.Context, req *rpc.DeleteContactRequest) (*rpc.DeleteContactReply, error) {
|
||||||
contactList := core.Identity().ContactList()
|
contactList := s.core.Identity.ContactList()
|
||||||
contact := contactList.ContactByAddress(req.Address)
|
contact := contactList.ContactByAddress(req.Address)
|
||||||
if contact == nil || (req.Id != 0 && contact.Id() != int(req.Id)) {
|
if contact == nil || (req.Id != 0 && contact.Id() != int(req.Id)) {
|
||||||
return nil, errors.New("Contact not found")
|
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
|
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
|
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
|
return nil, NotImplementedError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (core *RicochetCore) StreamConversations(stream rpc.RicochetCore_StreamConversationsServer) error {
|
func (s *RpcServer) StreamConversations(stream rpc.RicochetCore_StreamConversationsServer) error {
|
||||||
return NotImplementedError
|
return NotImplementedError
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,12 +18,12 @@ type ContactList struct {
|
||||||
inboundRequests map[int]*InboundContactRequest
|
inboundRequests map[int]*InboundContactRequest
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadContactList(core Ricochet) (*ContactList, error) {
|
func LoadContactList(core *Ricochet) (*ContactList, error) {
|
||||||
list := &ContactList{
|
list := &ContactList{
|
||||||
events: utils.CreatePublisher(),
|
events: utils.CreatePublisher(),
|
||||||
}
|
}
|
||||||
|
|
||||||
config := core.Config().OpenRead()
|
config := core.Config.OpenRead()
|
||||||
defer config.Close()
|
defer config.Close()
|
||||||
|
|
||||||
list.contacts = make(map[int]*Contact, len(config.Contacts))
|
list.contacts = make(map[int]*Contact, len(config.Contacts))
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Identity struct {
|
type Identity struct {
|
||||||
core Ricochet
|
core *Ricochet
|
||||||
|
|
||||||
address string
|
address string
|
||||||
privateKey *rsa.PrivateKey
|
privateKey *rsa.PrivateKey
|
||||||
|
@ -17,7 +17,7 @@ type Identity struct {
|
||||||
contactList *ContactList
|
contactList *ContactList
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateIdentity(core Ricochet) (*Identity, error) {
|
func CreateIdentity(core *Ricochet) (*Identity, error) {
|
||||||
me := &Identity{
|
me := &Identity{
|
||||||
core: core,
|
core: core,
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ func CreateIdentity(core Ricochet) (*Identity, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (me *Identity) loadIdentity() error {
|
func (me *Identity) loadIdentity() error {
|
||||||
config := me.core.Config().OpenRead()
|
config := me.core.Config.OpenRead()
|
||||||
defer config.Close()
|
defer config.Close()
|
||||||
|
|
||||||
if config.Identity.ServiceKey != "" {
|
if config.Identity.ServiceKey != "" {
|
||||||
|
|
|
@ -1,7 +1,19 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
type Ricochet interface {
|
type Ricochet struct {
|
||||||
Config() *Config
|
Config *Config
|
||||||
Network() *Network
|
Network *Network
|
||||||
Identity() *Identity
|
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue