mobile-insight / mobileinsight-mobile

Mobile Network Intelligence Made Easy -- Android version of MobileInsight app
http://mobileinsight.net
Other
88 stars 54 forks source link

Plugin stop mechanism #2

Closed moonsky219 closed 7 years ago

moonsky219 commented 7 years ago

The whole procedure works like this:

main.py 1) main.py will broadcast MobileInsight.Main.StopService when user click stop xxx plugin. 2) main.py waits for MobileInsight.Plugin.StopServiceAck (or a 5 seconds timer expired) 3) main.py stops plugin forcefully

plugin 1) waiting for MobileInsight.Main.StopService intent 2) finish any closure work 3) broadcasting MobileInsight.Plugin.StopServiceAck

To utilize this mechanism, plugin need to add following extra code: 1) define a callback function for doing closure work and sending StopServiceAck back to main.py

def on_broadcast(context, intent):
    '''
    This plugin is going to be stopped, finish closure work
    '''
    IntentClass = autoclass("android.content.Intent")
    intent = IntentClass()
    action = 'MobileInsight.Plugin.StopServiceAck'
    intent.setAction(action)
    try:
        mi2app_utils.pyService.sendBroadcast(intent)
    except Exception as e:
        import traceback
        analyzer.log_error(str(traceback.format_exc()))

2) register this callback for receiving MobileInsight.Main.StopService at initial function

br = BroadcastReceiver(
        on_broadcast, actions=['MobileInsight.Main.StopService'])
br.start()
yuanjieli commented 7 years ago

I would suggest to move on_broadcast() to mobileinsight-mobile/app/service/main.py. In this way, the plugin developers do not need to manually add this function. We can also prevent any unexpected deadlocks due to imperfect plugins.

On Sun, Jul 23, 2017 at 11:10 PM, Haotian Deng notifications@github.com wrote:

The whole procedure works like this:

main.py

  1. main.py will broadcast MobileInsight.Main.StopService when user click stop xxx plugin.
  2. main.py waits for MobileInsight.Plugin.StopServiceAck (or a 5 seconds timer expired)
  3. main.py stops plugin forcefully

plugin

  1. waiting for MobileInsight.Main.StopService intent
  2. finish any closure work
  3. broadcasting MobileInsight.Plugin.StopServiceAck

To utilize this mechanism, plugin need to add following extra code:

  1. define a callback function for doing closure work and sending StopServiceAck back to main.py

def on_broadcast(context, intent): ''' This plugin is going to be stopped, finish closure work ''' IntentClass = autoclass("android.content.Intent") intent = IntentClass() action = 'MobileInsight.Plugin.StopServiceAck' intent.setAction(action) try: mi2app_utils.pyService.sendBroadcast(intent) except Exception as e: import traceback analyzer.log_error(str(traceback.format_exc()))

  1. register this callback for receiving MobileInsight.Main.StopService at initial function

br = BroadcastReceiver( on_broadcast, actions=['MobileInsight.Main.StopService']) br.start()


You can view, comment on, or merge this pull request online at:

https://github.com/mobile-insight/mobileinsight-mobile/pull/2 Commit Summary

  • Add stop mechanism for plugins
  • Merge branch 'dev' of github.com:mobile-insight/mobileinsight-mobile into dev
  • Plugins will be forced to stop if waiting time since user click stop button is longer than 5 seconds

File Changes

Patch Links:

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mobile-insight/mobileinsight-mobile/pull/2, or mute the thread https://github.com/notifications/unsubscribe-auth/AENZ91NEXdx-i2RpvdbnPMHszicU7Htzks5sRDVAgaJpZM4OgwsU .

moonsky219 commented 7 years ago

@yuanjieli But I think the plugin also need to do cleaning work itself inside the registered function (for example on_broadcast here).

yuanjieli commented 7 years ago

@moonsky219 I see. But what would happen if the plugin developer forgets to do so? My main concern is that on_broadcast is currently not mandatory. We should (1) make it mandatory programmatically since it is critical; (2) simplify the developer's efforts to overload this function.

moonsky219 commented 7 years ago

Yes, I totally agree with you. My current work around is letting main.py to wait for maximum 5 seconds before stopping the plugin compulsively. A better implementation is still needed.

moonsky219 commented 7 years ago

I remove the code about checking orphan log in main.py and support it in LoggingAnalyzer of NetLogger