AccessGrid.org

Developer Example: ArbitraryCommandService

Services are used for integrating new applications into the VenueClient. In most cases, these services are for media tools such as vic, rat, DVTS, VLC, etc., which stream audio or video to multicast addresses allocated by the Venue. As a general demonstration of services, however, this example describes a service with one configurable parameter, the path to an executable, which is executed whenever the VenueClient enters a Venue. It is intended simply as an example, but it could be useful in cases in which one wishes to run an application on a machine in the node (yes, any machine) whenever a Venue is entered.

The entire code of the service implementation is included here, since it's so short. Note that the ArbitraryCommandService class includes only four methods:

__init__
The constructor for the class, in which the executableOption TextParameter is added to the service configuration

Start
Grabs the path to the executable from the executableOption parameter and executes it

Stop
Stops the service (i.e., terminates the executable)

SetStream
Sets the stream (which is, in this case, unused), and stops/starts the service

 

 

from AccessGrid.Descriptions import Capability
from AccessGrid.AGService import AGService
from AccessGrid.AGParameter import TextParameter

class ArbitraryCommandService( AGService ):

def __init__( self ):
AGService.__init__( self )

self.capabilities = [ Capability( Capability.CONSUMER,
Capability.VIDEO,
"h261",16000,self.id)]
self.executable = None

# Set configuration parameters
self.executableOption = TextParameter( "Executable",
"/Applications//TextEdit.app/Contents/MacOS/TextEdit" )
self.configuration.append(self.executableOption)

def Start( self ):
"""Start service"""
try:
self.executable = self.executableOption.value
if self.executable:
self.log.info("Starting ArbitraryCommandService")
self.log.info(" executable = %s" % self.executable)
self._Start( [] )

except:
self.log.exception("Exception in ArbitraryCommandService.Start")
raise

def Stop( self ):
"""Stop the service"""
self.started = 0
try:
self.log.info("Stop service")
self.processManager.TerminateAllProcesses()
except:
self.log.exception("Exception in AGService.Stop ")
raise

def SetStream( self, streamDescription ):
"""
Configure the Service according to the StreamDescription, and
stop and start rat
"""

# Configure the stream
ret = AGService.SetStream( self, streamDescription )
if ret and self.started:
# service is already running with this config; ignore
return

# If started, stop
if self.started:
self.Stop()

# If enabled, start
if self.enabled:
self.Start()

if __name__ == '__main__':

from AccessGrid.interfaces.AGService_interface import AGService as AGServiceI
from AccessGrid.AGService import RunService

service = ArbitraryCommandService()
serviceI = AGServiceI(service)
RunService(service,serviceI)

Supporting Code

 

login or register to post comments