ricochet-go/core/rpcserver.go

266 lines
7.5 KiB
Go
Raw Normal View History

2016-11-04 04:37:00 +00:00
package core
2016-08-02 02:58:10 +00:00
import (
"errors"
2016-11-04 04:37:00 +00:00
"github.com/ricochet-im/ricochet-go/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
)
var NotImplementedError error = errors.New("Not implemented")
2016-09-11 04:57:54 +00:00
type RpcServer struct {
2016-11-04 04:37:00 +00:00
Core *Ricochet
2016-08-02 02:58:10 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) GetServerStatus(ctx context.Context, req *ricochet.ServerStatusRequest) (*ricochet.ServerStatusReply, error) {
2016-08-02 02:58:10 +00:00
if req.RpcVersion != 1 {
return nil, errors.New("Unsupported RPC protocol version")
}
2016-11-04 04:37:00 +00:00
return &ricochet.ServerStatusReply{
2016-08-02 02:58:10 +00:00
RpcVersion: 1,
ServerVersion: "0.0.0",
}, nil
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) MonitorNetwork(req *ricochet.MonitorNetworkRequest, stream ricochet.RicochetCore_MonitorNetworkServer) error {
events := s.Core.Network.EventMonitor().Subscribe(20)
defer s.Core.Network.EventMonitor().Unsubscribe(events)
2016-08-02 23:28:26 +00:00
2016-08-03 05:39:13 +00:00
// Send initial status event
{
2016-11-04 04:37:00 +00:00
event := s.Core.Network.GetStatus()
2016-08-03 05:39:13 +00:00
if err := stream.Send(&event); err != nil {
return err
}
}
2016-08-02 23:28:26 +00:00
for {
2016-11-04 04:37:00 +00:00
event, ok := (<-events).(ricochet.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
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) StartNetwork(ctx context.Context, req *ricochet.StartNetworkRequest) (*ricochet.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 := s.Core.Network.Start()
2016-08-02 23:28:26 +00:00
if !ok {
return nil, err
}
2016-11-04 04:37:00 +00:00
status := s.Core.Network.GetStatus()
2016-08-03 05:39:13 +00:00
return &status, nil
2016-08-02 02:58:10 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) StopNetwork(ctx context.Context, req *ricochet.StopNetworkRequest) (*ricochet.NetworkStatus, error) {
s.Core.Network.Stop()
status := s.Core.Network.GetStatus()
2016-08-03 05:39:13 +00:00
return &status, nil
2016-08-02 02:58:10 +00:00
}
2016-08-17 00:43:39 +00:00
2016-11-04 04:37:00 +00:00
func (s *RpcServer) GetIdentity(ctx context.Context, req *ricochet.IdentityRequest) (*ricochet.Identity, error) {
reply := ricochet.Identity{
Address: s.Core.Identity.Address(),
}
return &reply, nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) MonitorContacts(req *ricochet.MonitorContactsRequest, stream ricochet.RicochetCore_MonitorContactsServer) error {
monitor := s.Core.Identity.ContactList().EventMonitor().Subscribe(20)
defer s.Core.Identity.ContactList().EventMonitor().Unsubscribe(monitor)
2016-08-30 02:46:41 +00:00
// Populate
2016-11-04 04:37:00 +00:00
contacts := s.Core.Identity.ContactList().Contacts()
2016-08-30 02:46:41 +00:00
for _, contact := range contacts {
2016-11-04 04:37:00 +00:00
event := &ricochet.ContactEvent{
Type: ricochet.ContactEvent_POPULATE,
Subject: &ricochet.ContactEvent_Contact{
Contact: contact.Data(),
2016-08-30 02:46:41 +00:00
},
}
if err := stream.Send(event); err != nil {
return err
}
}
// Terminate populate list with a null subject
{
2016-11-04 04:37:00 +00:00
event := &ricochet.ContactEvent{
Type: ricochet.ContactEvent_POPULATE,
2016-08-30 02:46:41 +00:00
}
if err := stream.Send(event); err != nil {
return err
}
}
for {
2016-11-04 04:37:00 +00:00
event, ok := (<-monitor).(ricochet.ContactEvent)
if !ok {
break
}
log.Printf("Contact event: %v", event)
if err := stream.Send(&event); err != nil {
return err
}
}
return nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) AddContactRequest(ctx context.Context, req *ricochet.ContactRequest) (*ricochet.Contact, error) {
contactList := s.Core.Identity.ContactList()
if req.Direction != ricochet.ContactRequest_OUTBOUND {
return nil, errors.New("Request must be outbound")
}
contact, err := contactList.AddContactRequest(req.Address, req.Nickname, req.FromNickname, req.Text)
if err != nil {
return nil, err
}
return contact.Data(), nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) UpdateContact(ctx context.Context, req *ricochet.Contact) (*ricochet.Contact, error) {
2016-08-17 00:43:39 +00:00
return nil, NotImplementedError
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) DeleteContact(ctx context.Context, req *ricochet.DeleteContactRequest) (*ricochet.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")
}
if err := contactList.RemoveContact(contact); err != nil {
return nil, err
}
2016-11-04 04:37:00 +00:00
return &ricochet.DeleteContactReply{}, nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) AcceptInboundRequest(ctx context.Context, req *ricochet.ContactRequest) (*ricochet.Contact, error) {
if req.Direction != ricochet.ContactRequest_INBOUND {
return nil, errors.New("Request must be inbound")
}
contactList := s.Core.Identity.ContactList()
request := contactList.InboundRequestByAddress(req.Address)
if request == nil {
return nil, errors.New("Request does not exist")
}
contact, err := request.Accept()
if err != nil {
return nil, err
}
return contact.Data(), nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) RejectInboundRequest(ctx context.Context, req *ricochet.ContactRequest) (*ricochet.RejectInboundRequestReply, error) {
if req.Direction != ricochet.ContactRequest_INBOUND {
return nil, errors.New("Request must be inbound")
}
contactList := s.Core.Identity.ContactList()
request := contactList.InboundRequestByAddress(req.Address)
if request == nil {
return nil, errors.New("Request does not exist")
}
request.Reject()
return &ricochet.RejectInboundRequestReply{}, nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) MonitorConversations(req *ricochet.MonitorConversationsRequest, stream ricochet.RicochetCore_MonitorConversationsServer) error {
// XXX Technically there is a race between starting to monitor
// and the list and state of messages used to populate, that could
// result in duplicate messages or other weird behavior.
// Same problem exists for other places this pattern is used.
2016-11-04 04:37:00 +00:00
monitor := s.Core.Identity.ConversationStream.Subscribe(100)
defer s.Core.Identity.ConversationStream.Unsubscribe(monitor)
{
// Populate with existing conversations
2016-11-04 04:37:00 +00:00
contacts := s.Core.Identity.ContactList().Contacts()
for _, contact := range contacts {
messages := contact.Conversation().Messages()
for _, message := range messages {
2016-11-04 04:37:00 +00:00
event := ricochet.ConversationEvent{
Type: ricochet.ConversationEvent_POPULATE,
Msg: message,
}
if err := stream.Send(&event); err != nil {
return err
}
}
}
// End population with an empty populate event
2016-11-04 04:37:00 +00:00
event := ricochet.ConversationEvent{
Type: ricochet.ConversationEvent_POPULATE,
}
if err := stream.Send(&event); err != nil {
return err
}
}
for {
2016-11-04 04:37:00 +00:00
event, ok := (<-monitor).(ricochet.ConversationEvent)
if !ok {
break
}
if err := stream.Send(&event); err != nil {
return err
}
}
return nil
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) SendMessage(ctx context.Context, req *ricochet.Message) (*ricochet.Message, error) {
if req.Sender == nil || !req.Sender.IsSelf {
return nil, errors.New("Invalid message sender")
} else if req.Recipient == nil || req.Recipient.IsSelf {
return nil, errors.New("Invalid message recipient")
}
2016-11-04 04:37:00 +00:00
contact := s.Core.Identity.ContactList().ContactByAddress(req.Recipient.Address)
if contact == nil || (req.Recipient.ContactId != 0 && int32(contact.Id()) != req.Recipient.ContactId) {
return nil, errors.New("Unknown recipient")
}
// XXX timestamp
// XXX validate text
// XXX identifier
message, err := contact.Conversation().Send(req.Text)
if err != nil {
return nil, err
}
return message, nil
2016-08-17 00:43:39 +00:00
}
2016-11-04 04:37:00 +00:00
func (s *RpcServer) MarkConversationRead(ctx context.Context, req *ricochet.MarkConversationReadRequest) (*ricochet.Reply, error) {
if req.Entity == nil || req.Entity.IsSelf {
return nil, errors.New("Invalid entity")
}
2016-11-04 04:37:00 +00:00
contact := s.Core.Identity.ContactList().ContactByAddress(req.Entity.Address)
if contact == nil || (req.Entity.ContactId != 0 && int32(contact.Id()) != req.Entity.ContactId) {
return nil, errors.New("Unknown entity")
}
contact.Conversation().MarkReadBeforeMessage(req.LastRecvIdentifier)
2016-11-04 04:37:00 +00:00
return &ricochet.Reply{}, nil
}