cli: Fix threading issues around event monitors
This commit is contained in:
parent
c089cf3e34
commit
99d44e1278
|
@ -42,6 +42,8 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go c.Run()
|
||||||
|
|
||||||
input.SetPrompt("> ")
|
input.SetPrompt("> ")
|
||||||
for {
|
for {
|
||||||
line := input.Line()
|
line := input.Line()
|
||||||
|
|
269
cli/client.go
269
cli/client.go
|
@ -16,15 +16,18 @@ type Client struct {
|
||||||
ServerStatus ricochet.ServerStatusReply
|
ServerStatus ricochet.ServerStatusReply
|
||||||
Identity ricochet.Identity
|
Identity ricochet.Identity
|
||||||
|
|
||||||
// XXX threadsafety
|
|
||||||
NetworkStatus ricochet.NetworkStatus
|
NetworkStatus ricochet.NetworkStatus
|
||||||
Contacts *ContactList
|
Contacts *ContactList
|
||||||
CurrentContact *Contact
|
CurrentContact *Contact
|
||||||
|
|
||||||
|
monitorsChannel chan interface{}
|
||||||
|
populatedContacts bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX need to handle backend connection loss/reconnection..
|
// XXX need to handle backend connection loss/reconnection..
|
||||||
func (c *Client) Initialize() error {
|
func (c *Client) Initialize() error {
|
||||||
c.Contacts = NewContactList()
|
c.Contacts = NewContactList()
|
||||||
|
c.monitorsChannel = make(chan interface{}, 10)
|
||||||
|
|
||||||
// Query server status and version
|
// Query server status and version
|
||||||
status, err := c.Backend.GetServerStatus(context.Background(), &ricochet.ServerStatusRequest{
|
status, err := c.Backend.GetServerStatus(context.Background(), &ricochet.ServerStatusRequest{
|
||||||
|
@ -48,12 +51,30 @@ func (c *Client) Initialize() error {
|
||||||
// Spawn routines to query and monitor state changes
|
// Spawn routines to query and monitor state changes
|
||||||
go c.monitorNetwork()
|
go c.monitorNetwork()
|
||||||
go c.monitorContacts()
|
go c.monitorContacts()
|
||||||
go c.monitorConversations()
|
// Conversation monitor isn't started until contacts are populated
|
||||||
|
|
||||||
// XXX block until populated/initialized?
|
// XXX block until populated/initialized?
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) Run() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case v := <-c.monitorsChannel:
|
||||||
|
switch event := v.(type) {
|
||||||
|
case *ricochet.NetworkStatus:
|
||||||
|
c.onNetworkStatus(event)
|
||||||
|
case *ricochet.ContactEvent:
|
||||||
|
c.onContactEvent(event)
|
||||||
|
case *ricochet.ConversationEvent:
|
||||||
|
c.onConversationEvent(event)
|
||||||
|
default:
|
||||||
|
log.Panicf("Unknown event type on monitor channel: %v", event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) SetCurrentContact(contact *Contact) {
|
func (c *Client) SetCurrentContact(contact *Contact) {
|
||||||
c.CurrentContact = contact
|
c.CurrentContact = contact
|
||||||
if c.CurrentContact != nil {
|
if c.CurrentContact != nil {
|
||||||
|
@ -86,8 +107,7 @@ func (c *Client) monitorNetwork() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Network status changed: %v", status)
|
c.monitorsChannel <- status
|
||||||
c.NetworkStatus = *status
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,84 +119,15 @@ func (c *Client) monitorContacts() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate initial contacts list
|
|
||||||
for {
|
for {
|
||||||
event, err := stream.Recv()
|
event, err := stream.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Contact populate error: %v", err)
|
log.Printf("Contact monitor error: %v", err)
|
||||||
// XXX handle
|
// XXX handle
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if event.Type != ricochet.ContactEvent_POPULATE {
|
c.monitorsChannel <- event
|
||||||
log.Printf("Ignoring unexpected contact event during populate: %v", event)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Populate is terminated by a nil subject
|
|
||||||
if event.Subject == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if contact := event.GetContact(); contact != nil {
|
|
||||||
c.Contacts.Populate(contact)
|
|
||||||
} else if request := event.GetRequest(); request != nil {
|
|
||||||
// XXX handle requests
|
|
||||||
log.Printf("XXX contact requests not supported")
|
|
||||||
} else {
|
|
||||||
log.Printf("XXX invalid event")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Loaded %d contacts", len(c.Contacts.Contacts))
|
|
||||||
|
|
||||||
for {
|
|
||||||
event, err := stream.Recv()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Contact status monitor error: %v", err)
|
|
||||||
// XXX handle
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
data := event.GetContact()
|
|
||||||
|
|
||||||
switch event.Type {
|
|
||||||
case ricochet.ContactEvent_ADD:
|
|
||||||
if data == nil {
|
|
||||||
log.Printf("Ignoring contact add event with null data")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Contacts.Added(data)
|
|
||||||
|
|
||||||
case ricochet.ContactEvent_UPDATE:
|
|
||||||
if data == nil {
|
|
||||||
log.Printf("Ignoring contact update event with null data")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
contact := c.Contacts.ByIdAndAddress(data.Id, data.Address)
|
|
||||||
if contact == nil {
|
|
||||||
log.Printf("Ignoring contact update event for unknown contact: %v", data)
|
|
||||||
} else {
|
|
||||||
contact.Updated(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
case ricochet.ContactEvent_DELETE:
|
|
||||||
if data == nil {
|
|
||||||
log.Printf("Ignoring contact delete event with null data")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
contact, _ := c.Contacts.Deleted(data)
|
|
||||||
|
|
||||||
if c.CurrentContact == contact {
|
|
||||||
c.SetCurrentContact(nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
log.Printf("Ignoring unknown contact event: %v", event)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,8 +139,6 @@ func (c *Client) monitorConversations() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Monitoring conversations")
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
event, err := stream.Recv()
|
event, err := stream.Recv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -198,54 +147,122 @@ func (c *Client) monitorConversations() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX Should also handle POPULATE
|
c.monitorsChannel <- event
|
||||||
if event.Type != ricochet.ConversationEvent_RECEIVE &&
|
}
|
||||||
event.Type != ricochet.ConversationEvent_SEND {
|
}
|
||||||
continue
|
|
||||||
}
|
func (c *Client) onNetworkStatus(status *ricochet.NetworkStatus) {
|
||||||
|
log.Printf("Network status changed: %v", status)
|
||||||
message := event.Msg
|
c.NetworkStatus = *status
|
||||||
if message == nil || message.Recipient == nil || message.Sender == nil {
|
}
|
||||||
log.Printf("Ignoring invalid conversation event: %v", event)
|
|
||||||
continue
|
func (c *Client) onContactEvent(event *ricochet.ContactEvent) {
|
||||||
}
|
if !c.populatedContacts && event.Type != ricochet.ContactEvent_POPULATE {
|
||||||
|
log.Printf("Ignoring unexpected contact event during populate: %v", event)
|
||||||
var remoteEntity *ricochet.Entity
|
return
|
||||||
if !message.Sender.IsSelf {
|
}
|
||||||
remoteEntity = message.Sender
|
|
||||||
} else {
|
data := event.GetContact()
|
||||||
remoteEntity = message.Recipient
|
|
||||||
}
|
switch event.Type {
|
||||||
|
case ricochet.ContactEvent_POPULATE:
|
||||||
remoteContact := c.Contacts.ByIdAndAddress(remoteEntity.ContactId, remoteEntity.Address)
|
// Populate is terminated by a nil subject
|
||||||
if remoteContact == nil {
|
if event.Subject == nil {
|
||||||
log.Printf("Ignoring conversation event with unknown contact: %v", event)
|
c.populatedContacts = true
|
||||||
continue
|
log.Printf("Loaded %d contacts", len(c.Contacts.Contacts))
|
||||||
}
|
go c.monitorConversations()
|
||||||
|
} else if data != nil {
|
||||||
if remoteContact == c.CurrentContact {
|
c.Contacts.Populate(data)
|
||||||
// XXX so unsafe
|
} else {
|
||||||
if message.Sender.IsSelf {
|
log.Printf("Invalid contact populate event: %v", event)
|
||||||
fmt.Fprintf(c.Input.Stdout(), "\r%s > %s\n", remoteContact.Data.Nickname, message.Text)
|
}
|
||||||
} else {
|
|
||||||
fmt.Fprintf(c.Input.Stdout(), "\r%s < %s\n", remoteContact.Data.Nickname, message.Text)
|
case ricochet.ContactEvent_ADD:
|
||||||
}
|
if data == nil {
|
||||||
} else if !message.Sender.IsSelf {
|
log.Printf("Ignoring contact add event with null data")
|
||||||
fmt.Fprintf(c.Input.Stdout(), "\r---- %s < %s\n", remoteContact.Data.Nickname, message.Text)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !message.Sender.IsSelf {
|
c.Contacts.Added(data)
|
||||||
backend := c.Backend
|
|
||||||
message := message
|
case ricochet.ContactEvent_UPDATE:
|
||||||
go func() {
|
if data == nil {
|
||||||
_, err := backend.MarkConversationRead(context.Background(), &ricochet.MarkConversationReadRequest{
|
log.Printf("Ignoring contact update event with null data")
|
||||||
Entity: message.Sender,
|
return
|
||||||
LastRecvIdentifier: message.Identifier,
|
}
|
||||||
})
|
|
||||||
if err != nil {
|
contact := c.Contacts.ByIdAndAddress(data.Id, data.Address)
|
||||||
log.Printf("Mark conversation read failed: %v", err)
|
if contact == nil {
|
||||||
}
|
log.Printf("Ignoring contact update event for unknown contact: %v", data)
|
||||||
}()
|
} else {
|
||||||
}
|
contact.Updated(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
case ricochet.ContactEvent_DELETE:
|
||||||
|
if data == nil {
|
||||||
|
log.Printf("Ignoring contact delete event with null data")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contact, _ := c.Contacts.Deleted(data)
|
||||||
|
|
||||||
|
if c.CurrentContact == contact {
|
||||||
|
c.SetCurrentContact(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
log.Printf("Ignoring unknown contact event: %v", event)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) onConversationEvent(event *ricochet.ConversationEvent) {
|
||||||
|
if event.Type != ricochet.ConversationEvent_RECEIVE &&
|
||||||
|
event.Type != ricochet.ConversationEvent_SEND &&
|
||||||
|
event.Type != ricochet.ConversationEvent_POPULATE {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
message := event.Msg
|
||||||
|
if message == nil || message.Recipient == nil || message.Sender == nil {
|
||||||
|
log.Printf("Ignoring invalid conversation event: %v", event)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var remoteEntity *ricochet.Entity
|
||||||
|
if !message.Sender.IsSelf {
|
||||||
|
remoteEntity = message.Sender
|
||||||
|
} else {
|
||||||
|
remoteEntity = message.Recipient
|
||||||
|
}
|
||||||
|
|
||||||
|
remoteContact := c.Contacts.ByIdAndAddress(remoteEntity.ContactId, remoteEntity.Address)
|
||||||
|
if remoteContact == nil {
|
||||||
|
log.Printf("Ignoring conversation event with unknown contact: %v", event)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if remoteContact == c.CurrentContact {
|
||||||
|
// XXX so unsafe
|
||||||
|
if message.Sender.IsSelf {
|
||||||
|
fmt.Fprintf(c.Input.Stdout(), "\r%s > %s\n", remoteContact.Data.Nickname, message.Text)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(c.Input.Stdout(), "\r%s < %s\n", remoteContact.Data.Nickname, message.Text)
|
||||||
|
}
|
||||||
|
} else if !message.Sender.IsSelf {
|
||||||
|
fmt.Fprintf(c.Input.Stdout(), "\r---- %s < %s\n", remoteContact.Data.Nickname, message.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !message.Sender.IsSelf {
|
||||||
|
backend := c.Backend
|
||||||
|
message := message
|
||||||
|
go func() {
|
||||||
|
_, err := backend.MarkConversationRead(context.Background(), &ricochet.MarkConversationReadRequest{
|
||||||
|
Entity: message.Sender,
|
||||||
|
LastRecvIdentifier: message.Identifier,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Mark conversation read failed: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue