work in progress: agent application with commandline controllers
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2496 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
		
							parent
							
								
									8f99753b1c
								
							
						
					
					
						commit
						771cf29cc2
					
				
					 7 changed files with 60 additions and 25 deletions
				
			
		| 
						 | 
				
			
			@ -106,7 +106,7 @@ the path to the configuration file.
 | 
			
		|||
  >>> configFile.close()
 | 
			
		||||
 | 
			
		||||
  >>> master.config
 | 
			
		||||
  controller.name = 'base.sample'
 | 
			
		||||
  controller.names = ['base.sample']
 | 
			
		||||
  logger.name = 'default'
 | 
			
		||||
  logger.standard = 30
 | 
			
		||||
  scheduler.name = 'sample'
 | 
			
		||||
| 
						 | 
				
			
			@ -197,7 +197,7 @@ the core package so that now everything is running under the control of
 | 
			
		|||
the twisted reactor.
 | 
			
		||||
 | 
			
		||||
  >>> config = '''
 | 
			
		||||
  ... controller(name='core.sample')
 | 
			
		||||
  ... controller(names=['core.sample'])
 | 
			
		||||
  ... scheduler(name='core')
 | 
			
		||||
  ... logger(name='default', standard=30)
 | 
			
		||||
  ... '''
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										10
									
								
								agent/agent.cfg
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								agent/agent.cfg
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
#
 | 
			
		||||
#   Standard configuration for agent application
 | 
			
		||||
#
 | 
			
		||||
#   $Id$
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
controller(names=['cmdline', 'telnet'])
 | 
			
		||||
controller.telnet.port = 5001
 | 
			
		||||
scheduler(name='core')
 | 
			
		||||
logger(name='default', standard=30)
 | 
			
		||||
							
								
								
									
										25
									
								
								agent/app.py
									
										
									
									
									
								
							
							
						
						
									
										25
									
								
								agent/app.py
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -23,21 +23,32 @@ Agent application.
 | 
			
		|||
$Id$
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
from twisted.internet import reactor
 | 
			
		||||
 | 
			
		||||
from cybertools.agent.base.agent import Master
 | 
			
		||||
 | 
			
		||||
config = '''
 | 
			
		||||
controller(name='telnet')
 | 
			
		||||
scheduler(name='core')
 | 
			
		||||
logger(name='default', standard=30)
 | 
			
		||||
'''
 | 
			
		||||
 | 
			
		||||
master = Master(config)
 | 
			
		||||
def getConfig():
 | 
			
		||||
    agentHome = os.path.abspath(os.path.dirname(__file__))
 | 
			
		||||
    configName = 'agent.cfg'
 | 
			
		||||
    configFile = open(os.path.join(agentHome, configName))
 | 
			
		||||
    config = configFile.read()
 | 
			
		||||
    configFile.close()
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def start():
 | 
			
		||||
    master = Master(getConfig())
 | 
			
		||||
    master.setup()
 | 
			
		||||
 | 
			
		||||
    print 'Starting agent application...'
 | 
			
		||||
print 'Using controller %s.' % master.config.controller.name
 | 
			
		||||
    print 'Using controllers %s.' % ', '.join(master.config.controller.names)
 | 
			
		||||
 | 
			
		||||
    reactor.run()
 | 
			
		||||
 | 
			
		||||
    print 'Agent application has been stopped.'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    start()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -61,15 +61,18 @@ class Master(Agent):
 | 
			
		|||
    name = 'master'
 | 
			
		||||
    scheduler = None
 | 
			
		||||
 | 
			
		||||
    def __init__(self, configuration=None):
 | 
			
		||||
    def __init__(self, configuration):
 | 
			
		||||
        if isinstance(configuration, Configurator):
 | 
			
		||||
            config = configuration
 | 
			
		||||
        else:   # configuration is path to config file
 | 
			
		||||
            config = self.config = Configurator()
 | 
			
		||||
            config.load(configuration)
 | 
			
		||||
        self.master = self
 | 
			
		||||
        self.controllers = []
 | 
			
		||||
        self.children = {}
 | 
			
		||||
        if configuration is not None:
 | 
			
		||||
            config.load(configuration)
 | 
			
		||||
        self.logger = loggers(self, name=config.logger.name)
 | 
			
		||||
            self.controllers.append(controllers(self, name=config.controller.name))
 | 
			
		||||
        for n in config.controller.names:
 | 
			
		||||
            self.controllers.append(controllers(self, n))
 | 
			
		||||
        self.scheduler = schedulers(self, name=config.scheduler.name)
 | 
			
		||||
 | 
			
		||||
    def setup(self):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,6 +4,6 @@
 | 
			
		|||
#   $Id$
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
controller(name='base.sample')
 | 
			
		||||
controller(names=['base.sample'])
 | 
			
		||||
scheduler(name='sample')
 | 
			
		||||
logger(name='default', standard=30)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,7 +35,9 @@ class CmdlineController(SampleController):
 | 
			
		|||
 | 
			
		||||
    def setup(self):
 | 
			
		||||
        super(CmdlineController, self).setup()
 | 
			
		||||
        stdio.StandardIO(CmdlineProtocol())
 | 
			
		||||
        prot = CmdlineProtocol()
 | 
			
		||||
        prot.controller = self
 | 
			
		||||
        stdio.StandardIO(prot)
 | 
			
		||||
 | 
			
		||||
controllers.register(CmdlineController, Master, name='cmdline')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -46,7 +48,8 @@ class TelnetController(CmdlineController):
 | 
			
		|||
 | 
			
		||||
    def setup(self):
 | 
			
		||||
        super(CmdlineController, self).setup()
 | 
			
		||||
        reactor.listenTCP(5001, TelnetServerFactory())
 | 
			
		||||
        port = self.agent.config.controller.telnet.port
 | 
			
		||||
        reactor.listenTCP(port, TelnetServerFactory(self))
 | 
			
		||||
 | 
			
		||||
controllers.register(TelnetController, Master, name='telnet')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -54,6 +57,7 @@ controllers.register(TelnetController, Master, name='telnet')
 | 
			
		|||
class CmdlineProtocol(basic.LineReceiver):
 | 
			
		||||
 | 
			
		||||
    delimiter = '\n'
 | 
			
		||||
    controller = None
 | 
			
		||||
 | 
			
		||||
    def connectionMade(self):
 | 
			
		||||
        self.sendLine("Agent console. Type 'help' for help.")
 | 
			
		||||
| 
						 | 
				
			
			@ -97,4 +101,11 @@ class TelnetProtocol(CmdlineProtocol):
 | 
			
		|||
 | 
			
		||||
class TelnetServerFactory(protocol.ServerFactory):
 | 
			
		||||
 | 
			
		||||
    protocol = TelnetProtocol
 | 
			
		||||
    def __init__(self, controller):
 | 
			
		||||
        self.controller = controller
 | 
			
		||||
 | 
			
		||||
    def protocol(self, *args, **kw):
 | 
			
		||||
        prot = TelnetProtocol(*args, **kw)
 | 
			
		||||
        prot.controller = self.controller
 | 
			
		||||
        return prot
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ Agents for Job Execution and Communication Tasks
 | 
			
		|||
  >>> from cybertools.agent.base.agent import Master
 | 
			
		||||
 | 
			
		||||
  >>> config = '''
 | 
			
		||||
  ... controller(name='core.sample')
 | 
			
		||||
  ... controller(names=['core.sample'])
 | 
			
		||||
  ... scheduler(name='core')
 | 
			
		||||
  ... logger(name='default', standard=30)
 | 
			
		||||
  ... '''
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue