core: Handle connection close for contacts

This commit is contained in:
John Brooks 2016-09-19 21:32:16 -06:00
parent 289d0f4dc2
commit 114720bd6d
2 changed files with 37 additions and 0 deletions

View File

@ -191,3 +191,30 @@ func (c *Contact) shouldReplaceConnection(conn *protocol.OpenConnection) bool {
}
return false
}
func (c *Contact) OnConnectionClosed(conn *protocol.OpenConnection) {
c.mutex.Lock()
if c.connection != conn {
c.mutex.Unlock()
return
}
c.connection = nil
c.status = ricochet.Contact_OFFLINE
config := c.core.Config.OpenWrite()
c.data.LastConnected = time.Now().Format(time.RFC3339)
config.Contacts[strconv.Itoa(c.id)] = c.data
config.Save()
c.mutex.Unlock()
event := ricochet.ContactEvent{
Type: ricochet.ContactEvent_UPDATE,
Subject: &ricochet.ContactEvent_Contact{
Contact: c.Data(),
},
}
c.events.Publish(event)
}

View File

@ -61,6 +61,16 @@ func (handler *protocolHandler) OnConnect(oc *protocol.OpenConnection) {
}
}
func (handler *protocolHandler) OnDisconnect(oc *protocol.OpenConnection) {
log.Printf("protocol: OnDisconnect: %v", oc)
if oc.OtherHostname != "" {
contact := handler.p.core.Identity.ContactList().ContactByAddress("ricochet:" + oc.OtherHostname)
if contact != nil {
contact.OnConnectionClosed(oc)
}
}
}
// Authentication Management
func (handler *protocolHandler) OnAuthenticationRequest(oc *protocol.OpenConnection, channelID int32, clientCookie [16]byte) {
log.Printf("protocol: OnAuthenticationRequest")