VirtualBox

Ignore:
Timestamp:
Jun 29, 2010 11:25:54 AM (14 years ago)
Author:
vboxsync
Message:

python: bridge code for active listeners, sample using it in shell

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxShell/vboxshell.py

    r30455 r30492  
    432432
    433433def monitorVBox2(ctx, dur):
     434    def handleEventImpl(ev):
     435         print "got event: %s %s" %(ev, str(ev.type))
     436         if ev.type == ctx['global'].constants.VBoxEventType_OnMachineStateChange:
     437             scev = ctx['global'].queryInterface(ev, 'IMachineStateChangeEvent')
     438             if scev:
     439                 print "state event: mach=%s state=%s" %(scev.machineId, scev.state)
     440
     441    class EventListener:
     442     def __init__(self, arg):
     443         pass
     444
     445     def handleEvent(self, ev):
     446         handleEventImpl(ev)
     447
    434448    vbox = ctx['vb']
    435     listener = vbox.eventSource.createListener()
     449    active = True
     450    if active:
     451        listener = ctx['global'].createListener(EventListener)
     452    else:
     453        listener = vbox.eventSource.createListener()
    436454    registered = False
    437455    if dur == -1:
     
    439457        dur = 100000
    440458    try:
    441         vbox.eventSource.registerListener(listener, [ctx['global'].constants.VBoxEventType_Any], False)
     459        vbox.eventSource.registerListener(listener, [ctx['global'].constants.VBoxEventType_Any], active)
    442460        registered = True
    443461        end = time.time() + dur
    444462        while  time.time() < end:
    445             ev = vbox.eventSource.getEvent(listener, 500)
    446             if ev:
    447                 print "got event: %s %s" %(ev, str(ev.type))
    448                 if ev.type == ctx['global'].constants.VBoxEventType_OnMachineStateChange:
    449                     scev = ctx['global'].queryInterface(ev, 'IMachineStateChangeEvent')
    450                     if scev:
    451                         print "state event: mach=%s state=%s" %(scev.machineId, scev.state)
    452                 # otherwise waitable events will leak
    453                 vbox.eventSource.eventProcessed(listener, ev)
     463            if active:
     464                ctx['global'].waitForEvents(500)
     465            else:
     466                ev = vbox.eventSource.getEvent(listener, 500)
     467                if ev:
     468                    handleEventImpl(ev)
     469                    # otherwise waitable events will leak (active listeners ACK automatically)
     470                    vbox.eventSource.eventProcessed(listener, ev)
    454471    # We need to catch all exceptions here, otherwise listener will never be unregistered
    455     except Exception, e:
    456         printErr(ctx,e)
    457         traceback.print_exc()
     472    except:
    458473        pass
    459474    if listener and registered:
     
    10131028        try:
    10141029            while True:
    1015                 data = guest.getProcessOutput(pid, 0, 1000, 4096)
     1030                data = guest.getProcessOutput(pid, 0, 10000, 4096)
    10161031                if data and len(data) > 0:
    10171032                    sys.stdout.write(data)
     
    30583073        pass
    30593074    cmds = []
    3060    
     3075
    30613076    if g_cmd is not None:
    30623077        cmds = g_cmd.split(';')
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette