composed apps use their own mutexs
the build was successful Details

This commit is contained in:
Dan Ballard 2019-12-13 11:34:59 -08:00
parent 4ecc7c0f2b
commit 2dbab8cfc4
3 changed files with 22 additions and 19 deletions

View File

@ -25,7 +25,7 @@ type applicationCore struct {
eventBuses map[string]event.Manager eventBuses map[string]event.Manager
directory string directory string
acmutex sync.Mutex coremutex sync.Mutex
} }
type application struct { type application struct {
@ -85,8 +85,8 @@ func (ac *applicationCore) CreatePeer(name string) (*model.Profile, error) {
profile := storage.NewProfile(name) profile := storage.NewProfile(name)
ac.acmutex.Lock() ac.coremutex.Lock()
defer ac.acmutex.Unlock() defer ac.coremutex.Unlock()
_, exists := ac.eventBuses[profile.Onion] _, exists := ac.eventBuses[profile.Onion]
if exists { if exists {
@ -100,8 +100,8 @@ func (ac *applicationCore) CreatePeer(name string) (*model.Profile, error) {
} }
func (ac *applicationCore) DeletePeer(onion string) { func (ac *applicationCore) DeletePeer(onion string) {
ac.acmutex.Lock() ac.coremutex.Lock()
defer ac.acmutex.Unlock() defer ac.coremutex.Unlock()
ac.eventBuses[onion].Shutdown() ac.eventBuses[onion].Shutdown()
delete(ac.eventBuses, onion) delete(ac.eventBuses, onion)
@ -144,7 +144,7 @@ func (app *application) CreatePeer(name string, password string) {
func (app *application) DeletePeer(onion string) { func (app *application) DeletePeer(onion string) {
log.Infof("DeletePeer called on %v\n", onion) log.Infof("DeletePeer called on %v\n", onion)
app.appmutex.Lock() app.appmutex.Lock()
defer app.appmutex.Lock() defer app.appmutex.Unlock()
app.appletPlugins.ShutdownPeer(onion) app.appletPlugins.ShutdownPeer(onion)
app.plugins.Delete(onion) app.plugins.Delete(onion)
@ -198,9 +198,9 @@ func (ac *applicationCore) LoadProfiles(password string, timeline bool, loadProf
continue continue
} }
ac.acmutex.Lock() ac.coremutex.Lock()
ac.eventBuses[profile.Onion] = eventBus ac.eventBuses[profile.Onion] = eventBus
ac.acmutex.Unlock() ac.coremutex.Unlock()
loadProfileFn(profile, profileStore) loadProfileFn(profile, profileStore)
} }

View File

@ -9,6 +9,7 @@ import (
"git.openprivacy.ca/openprivacy/libricochet-go/log" "git.openprivacy.ca/openprivacy/libricochet-go/log"
"path" "path"
"strconv" "strconv"
"sync"
) )
type applicationClient struct { type applicationClient struct {
@ -16,6 +17,7 @@ type applicationClient struct {
appletPeers appletPeers
appBus event.Manager appBus event.Manager
acmutex sync.Mutex
} }
// NewAppClient returns an Application that acts as a client to a AppService, connected by the IPCBridge supplied // NewAppClient returns an Application that acts as a client to a AppService, connected by the IPCBridge supplied
@ -76,8 +78,8 @@ func (ac *applicationClient) newPeer(localID, password string, reload bool) {
peer := peer.FromProfile(profile) peer := peer.FromProfile(profile)
peer.Init(eventBus) peer.Init(eventBus)
ac.mutex.Lock() ac.acmutex.Lock()
defer ac.mutex.Unlock() defer ac.acmutex.Unlock()
ac.peers[profile.Onion] = peer ac.peers[profile.Onion] = peer
ac.eventBuses[profile.Onion] = eventBus ac.eventBuses[profile.Onion] = eventBus
npEvent := event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion}) npEvent := event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion})
@ -114,11 +116,11 @@ func (ac *applicationClient) ChangePeerPassword(onion, oldpass, newpass string)
} }
func (ac *applicationClient) handleDeletedPeer(onion string) { func (ac *applicationClient) handleDeletedPeer(onion string) {
ac.mutex.Lock() ac.acmutex.Lock()
defer ac.acmutex.Unlock()
ac.peers[onion].Shutdown() ac.peers[onion].Shutdown()
delete(ac.peers, onion) delete(ac.peers, onion)
ac.eventBuses[onion].Publish(event.NewEventList(event.ShutdownPeer, event.Identity, onion)) ac.eventBuses[onion].Publish(event.NewEventList(event.ShutdownPeer, event.Identity, onion))
ac.mutex.Unlock()
ac.applicationCore.DeletePeer(onion) ac.applicationCore.DeletePeer(onion)
} }
@ -141,8 +143,8 @@ func (ac *applicationClient) QueryACNStatus() {
// ShutdownPeer shuts down a peer and removes it from the app's management // ShutdownPeer shuts down a peer and removes it from the app's management
func (ac *applicationClient) ShutdownPeer(onion string) { func (ac *applicationClient) ShutdownPeer(onion string) {
ac.mutex.Lock() ac.acmutex.Lock()
defer ac.mutex.Unlock() defer ac.acmutex.Unlock()
ac.eventBuses[onion].Shutdown() ac.eventBuses[onion].Shutdown()
delete(ac.eventBuses, onion) delete(ac.eventBuses, onion)
ac.peers[onion].Shutdown() ac.peers[onion].Shutdown()

View File

@ -11,6 +11,7 @@ import (
"git.openprivacy.ca/openprivacy/libricochet-go/log" "git.openprivacy.ca/openprivacy/libricochet-go/log"
"path" "path"
"strconv" "strconv"
"sync"
) )
type applicationService struct { type applicationService struct {
@ -20,6 +21,7 @@ type applicationService struct {
storage map[string]storage.ProfileStore storage map[string]storage.ProfileStore
engines map[string]connections.Engine engines map[string]connections.Engine
asmutex sync.Mutex
} }
// ApplicationService is the back end of an application that manages engines and writing storage and communicates to an ApplicationClient by an IPCBridge // ApplicationService is the back end of an application that manages engines and writing storage and communicates to an ApplicationClient by an IPCBridge
@ -123,10 +125,10 @@ func (as *applicationService) loadProfiles(password string) {
blockedPeers := profile.BlockedPeers() blockedPeers := profile.BlockedPeers()
identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey) identity := primitives.InitializeIdentity(profile.Name, &profile.Ed25519PrivateKey, &profile.Ed25519PublicKey)
engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], profile.GetContacts(), blockedPeers) engine := connections.NewProtocolEngine(identity, profile.Ed25519PrivateKey, as.acn, as.eventBuses[profile.Onion], profile.GetContacts(), blockedPeers)
as.mutex.Lock() as.asmutex.Lock()
as.storage[profile.Onion] = profileStore as.storage[profile.Onion] = profileStore
as.engines[profile.Onion] = engine as.engines[profile.Onion] = engine
as.mutex.Unlock() as.asmutex.Unlock()
message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.LocalID, event.Password: password})} message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.LocalID, event.Password: password})}
as.bridge.Write(&message) as.bridge.Write(&message)
count++ count++
@ -148,7 +150,8 @@ func (as *applicationService) getACNStatusHandler() func(int, string) {
} }
func (as *applicationService) deletePeer(onion string) { func (as *applicationService) deletePeer(onion string) {
as.mutex.Lock() as.asmutex.Lock()
defer as.asmutex.Unlock()
as.appletPlugins.ShutdownPeer(onion) as.appletPlugins.ShutdownPeer(onion)
as.plugins.Delete(onion) as.plugins.Delete(onion)
@ -160,8 +163,6 @@ func (as *applicationService) deletePeer(onion string) {
as.storage[onion].Delete() as.storage[onion].Delete()
delete(as.storage, onion) delete(as.storage, onion)
as.mutex.Unlock()
as.applicationCore.DeletePeer(onion) as.applicationCore.DeletePeer(onion)
} }