process fixes for android

This commit is contained in:
erinn 2021-06-21 17:47:43 -07:00
parent e09e9c09a7
commit 001470257a
2 changed files with 250 additions and 225 deletions

30
lib.go
View File

@ -67,9 +67,10 @@ func c_StartCwtch(dir_c *C.char, len C.int, tor_c *C.char, torLen C.int) int8 {
// message: CwtchStarted when start up is complete and app is safe to use // message: CwtchStarted when start up is complete and app is safe to use
// CwtchStartError message when start up fails (includes event.Error data field) // CwtchStartError message when start up fails (includes event.Error data field)
func StartCwtch(appDir string, torPath string) int { func StartCwtch(appDir string, torPath string) int {
eventHandler = utils.NewEventHandler()
log.SetLevel(log.LevelInfo) log.SetLevel(log.LevelInfo)
log.Infof("StartCwtch(...)")
// Quick hack check that we're being called with the correct params // Quick hack check that we're being called with the correct params
// On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker // On android a stale worker could be calling us with "last apps" directory. Best to abort fast so the app can make a new worker
if runtime.GOOS == "android" { if runtime.GOOS == "android" {
@ -86,13 +87,16 @@ func StartCwtch(appDir string, torPath string) int {
} }
func _startCwtch(appDir string, torPath string) { func _startCwtch(appDir string, torPath string) {
log.Infof("application: %v eventHandler: %v acn: %v", application, eventHandler, globalACN)
if application != nil { if application != nil {
log.Infof("_startCwtch detected existing application; resuming instead of relaunching") log.Infof("_startCwtch detected existing application; resuming instead of relaunching")
ReconnectCwtchForeground() ReconnectCwtchForeground()
return return
} }
// Exclude Tapir wire Messages (We need a TRACE level) // Exclude Tapir wire Messages
//(We need a TRACE level)
log.ExcludeFromPattern("service.go") log.ExcludeFromPattern("service.go")
// Ensure that the application directory exists...and then initialize settings.. // Ensure that the application directory exists...and then initialize settings..
@ -112,6 +116,9 @@ func _startCwtch(appDir string, torPath string) {
panic(err) panic(err)
} }
log.Infof("Creating new EventHandler()")
eventHandler = utils.NewEventHandler()
log.Infof("making directory %v", appDir) log.Infof("making directory %v", appDir)
os.MkdirAll(path.Join(appDir, "/.tor", "tor"), 0700) os.MkdirAll(path.Join(appDir, "/.tor", "tor"), 0700)
tor.NewTorrc().WithSocksPort(port).WithOnionTrafficOnly().WithControlPort(controlPort).WithHashedPassword(base64.StdEncoding.EncodeToString(key)).Build(filepath.Join(appDir, ".tor", "tor", "torrc")) tor.NewTorrc().WithSocksPort(port).WithOnionTrafficOnly().WithControlPort(controlPort).WithHashedPassword(base64.StdEncoding.EncodeToString(key)).Build(filepath.Join(appDir, ".tor", "tor", "torrc"))
@ -167,6 +174,7 @@ func c_ReconnectCwtchForeground() {
// Like StartCwtch, but StartCwtch has already been called so we don't need to restart Tor etc (probably) // Like StartCwtch, but StartCwtch has already been called so we don't need to restart Tor etc (probably)
// Do need to re-send initial state tho, eg profiles that are already loaded // Do need to re-send initial state tho, eg profiles that are already loaded
func ReconnectCwtchForeground() { func ReconnectCwtchForeground() {
log.Infof("Reconnecting cwtchforeground")
if application == nil { if application == nil {
log.Errorf("ReconnectCwtchForeground: Application is nil, presuming stale thread, EXITING Reconnect\n") log.Errorf("ReconnectCwtchForeground: Application is nil, presuming stale thread, EXITING Reconnect\n")
return return
@ -338,10 +346,18 @@ func c_GetAppBusEvent() *C.char {
// GetAppBusEvent blocks until an event // GetAppBusEvent blocks until an event
func GetAppBusEvent() string { func GetAppBusEvent() string {
log.Infof("appbusevent called")
for eventHandler == nil {
log.Infof("waiting for eventHandler != nil")
time.Sleep(time.Second)
}
var json = "" var json = ""
for json == "" { for json == "" {
log.Infof("waiting for json != ''")
json = eventHandler.GetNextEvent() json = eventHandler.GetNextEvent()
} }
log.Infof("appbusevent: %v", json)
return json return json
} }
@ -701,11 +717,15 @@ func ShutdownCwtch() {
eventHandler.Push(event.NewEvent(event.Shutdown, map[event.Field]string{})) eventHandler.Push(event.NewEvent(event.Shutdown, map[event.Field]string{}))
// Allow for the shutdown events to go through and then purge everything else... // Allow for the shutdown events to go through and then purge everything else...
log.Debugf("Shutting Down Application...") log.Infof("Shutting Down Application...")
application.Shutdown() application.Shutdown()
log.Debugf("Shutting Down ACN...") log.Infof("Shutting Down ACN...")
globalACN.Close() globalACN.Close()
log.Debugf("Library Shutdown Complete!") log.Infof("Library Shutdown Complete!")
// do not remove - important for state checks elsewhere
application = nil
globalACN = nil
eventHandler = nil
} }
} }

View File

@ -65,6 +65,7 @@ func (eh *EventHandler) GetNextEvent() string {
// handleAppBusEvent enriches AppBus events so they are usable with out further data fetches // handleAppBusEvent enriches AppBus events so they are usable with out further data fetches
func (eh *EventHandler) handleAppBusEvent(e *event.Event) string { func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
log.Debugf("New AppBus Event to Handle: %v", e) log.Debugf("New AppBus Event to Handle: %v", e)
if eh.app != nil {
switch e.EventType { switch e.EventType {
case event.ACNStatus: case event.ACNStatus:
if e.Data[event.Progress] == "100" { if e.Data[event.Progress] == "100" {
@ -209,6 +210,7 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
log.Infof("contactsJson %v", e.Data["ContactsJson"]) log.Infof("contactsJson %v", e.Data["ContactsJson"])
} }
}
json, _ := json.Marshal(e) json, _ := json.Marshal(e)
return string(json) return string(json)
@ -216,7 +218,9 @@ func (eh *EventHandler) handleAppBusEvent(e *event.Event) string {
// handleProfileEvent enriches Profile events so they are usable with out further data fetches // handleProfileEvent enriches Profile events so they are usable with out further data fetches
func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string { func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
if eh.app == nil {
log.Errorf("eh.app == nil in handleProfileEvent... this shouldnt happen?")
} else {
peer := eh.app.GetPeer(ev.Profile) peer := eh.app.GetPeer(ev.Profile)
ph := NewPeerHelper(peer) ph := NewPeerHelper(peer)
log.Debugf("New Profile Event to Handle: %v", ev) log.Debugf("New Profile Event to Handle: %v", ev)
@ -304,6 +308,7 @@ func (eh *EventHandler) handleProfileEvent(ev *EventProfileEnvelope) string {
} }
} }
} }
}
json, _ := json.Marshal(unwrap(ev)) json, _ := json.Marshal(unwrap(ev))
return string(json) return string(json)