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
|
|
|
)
|
|
|
|
|
2016-08-30 01:49:30 +00:00
|
|
|
var NotImplementedError error = errors.New("Not implemented")
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
type RpcServer struct {
|
|
|
|
core *ricochet.Ricochet
|
2016-08-02 02:58:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) GetServerStatus(ctx context.Context, req *rpc.ServerStatusRequest) (*rpc.ServerStatusReply, error) {
|
2016-08-02 02:58:10 +00:00
|
|
|
if req.RpcVersion != 1 {
|
|
|
|
return nil, errors.New("Unsupported RPC protocol version")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpc.ServerStatusReply{
|
|
|
|
RpcVersion: 1,
|
|
|
|
ServerVersion: "0.0.0",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
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)
|
2016-08-02 23:28:26 +00:00
|
|
|
|
2016-08-03 05:39:13 +00:00
|
|
|
// Send initial status event
|
|
|
|
{
|
2016-09-11 04:57:54 +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-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
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) 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.
|
2016-09-11 04:57:54 +00:00
|
|
|
ok, err := s.core.Network.Start("tcp://127.0.0.1:9051", "")
|
2016-08-02 23:28:26 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +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-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) StopNetwork(ctx context.Context, req *rpc.StopNetworkRequest) (*rpc.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-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) GetIdentity(ctx context.Context, req *rpc.IdentityRequest) (*rpc.Identity, error) {
|
2016-08-30 01:49:30 +00:00
|
|
|
reply := rpc.Identity{
|
2016-09-11 04:57:54 +00:00
|
|
|
Address: s.core.Identity.Address(),
|
2016-08-30 01:49:30 +00:00
|
|
|
}
|
|
|
|
return &reply, nil
|
2016-08-17 00:43:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
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)
|
2016-09-02 16:51:24 +00:00
|
|
|
|
2016-08-30 02:46:41 +00:00
|
|
|
// Populate
|
2016-09-11 04:57:54 +00:00
|
|
|
contacts := s.core.Identity.ContactList().Contacts()
|
2016-08-30 02:46:41 +00:00
|
|
|
for _, contact := range contacts {
|
|
|
|
event := &rpc.ContactEvent{
|
|
|
|
Type: rpc.ContactEvent_POPULATE,
|
|
|
|
Subject: &rpc.ContactEvent_Contact{
|
2016-09-20 01:53:21 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
event := &rpc.ContactEvent{
|
|
|
|
Type: rpc.ContactEvent_POPULATE,
|
|
|
|
}
|
|
|
|
if err := stream.Send(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 16:51:24 +00:00
|
|
|
for {
|
|
|
|
event, ok := (<-monitor).(rpc.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-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) AddContactRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.Contact, error) {
|
2016-08-17 00:43:39 +00:00
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) UpdateContact(ctx context.Context, req *rpc.Contact) (*rpc.Contact, error) {
|
2016-08-17 00:43:39 +00:00
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) DeleteContact(ctx context.Context, req *rpc.DeleteContactRequest) (*rpc.DeleteContactReply, error) {
|
|
|
|
contactList := s.core.Identity.ContactList()
|
2016-09-02 16:51:24 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rpc.DeleteContactReply{}, nil
|
2016-08-17 00:43:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) AcceptInboundRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.Contact, error) {
|
2016-08-17 00:43:39 +00:00
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
2016-09-11 04:57:54 +00:00
|
|
|
func (s *RpcServer) RejectInboundRequest(ctx context.Context, req *rpc.ContactRequest) (*rpc.RejectInboundRequestReply, error) {
|
2016-08-17 00:43:39 +00:00
|
|
|
return nil, NotImplementedError
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:38:18 +00:00
|
|
|
func (s *RpcServer) MonitorConversations(req *rpc.MonitorConversationsRequest, stream rpc.RicochetCore_MonitorConversationsServer) error {
|
2016-10-16 00:04:19 +00:00
|
|
|
// 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.
|
|
|
|
monitor := s.core.Identity.ConversationStream.Subscribe(100)
|
|
|
|
defer s.core.Identity.ConversationStream.Unsubscribe(monitor)
|
2016-10-05 21:38:18 +00:00
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
{
|
|
|
|
// Populate with existing conversations
|
|
|
|
contacts := s.core.Identity.ContactList().Contacts()
|
|
|
|
for _, contact := range contacts {
|
|
|
|
messages := contact.Conversation().Messages()
|
|
|
|
for _, message := range messages {
|
|
|
|
event := rpc.ConversationEvent{
|
|
|
|
Type: rpc.ConversationEvent_POPULATE,
|
|
|
|
Msg: message,
|
|
|
|
}
|
|
|
|
if err := stream.Send(&event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End population with an empty populate event
|
|
|
|
event := rpc.ConversationEvent{
|
|
|
|
Type: rpc.ConversationEvent_POPULATE,
|
|
|
|
}
|
|
|
|
if err := stream.Send(&event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 21:38:18 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
event, ok := (<-monitor).(rpc.ConversationEvent)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stream.Send(&event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RpcServer) SendMessage(ctx context.Context, req *rpc.Message) (*rpc.Message, error) {
|
2016-10-06 23:50:07 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10-16 00:57:08 +00:00
|
|
|
|
|
|
|
func (s *RpcServer) MarkConversationRead(ctx context.Context, req *rpc.MarkConversationReadRequest) (*rpc.Reply, error) {
|
|
|
|
if req.Entity == nil || req.Entity.IsSelf {
|
|
|
|
return nil, errors.New("Invalid entity")
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
return &rpc.Reply{}, nil
|
|
|
|
}
|