work in progress: set up agent application; provide simple commandline controllers

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2495 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2008-04-03 14:13:32 +00:00
parent 7da8c3b9d1
commit 8f99753b1c
6 changed files with 149 additions and 5 deletions

View file

@ -6,4 +6,5 @@ $Id$
from cybertools.agent.base import agent, control, job, log, schedule from cybertools.agent.base import agent, control, job, log, schedule
from cybertools.agent.core import agent, control, schedule from cybertools.agent.core import agent, control, schedule
from cybertools.agent.control import cmdline
from cybertools.agent.crawl import base from cybertools.agent.crawl import base

43
agent/app.py Executable file
View file

@ -0,0 +1,43 @@
#! /usr/bin/env python2.4
#
# Copyright (c) 2008 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Agent application.
$Id$
"""
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)
master.setup()
print 'Starting agent application...'
print 'Using controller %s.' % master.config.controller.name
reactor.run()
print 'Agent application has been stopped.'

View file

@ -74,7 +74,7 @@ class Master(Agent):
def setup(self): def setup(self):
for cont in self.controllers: for cont in self.controllers:
cont.setupAgent() cont.setup()
def setupAgents(self, controller, agentSpecs): def setupAgents(self, controller, agentSpecs):
for spec in agentSpecs: for spec in agentSpecs:

View file

@ -36,7 +36,7 @@ class Controller(object):
def __init__(self, agent): def __init__(self, agent):
self.agent = agent self.agent = agent
def setupAgent(self): def setup(self):
self.agent.setupAgents(self, self._getAgents()) self.agent.setupAgents(self, self._getAgents())
self.agent.setupJobs(self, self._getCurrentJobs()) self.agent.setupJobs(self, self._getCurrentJobs())

100
agent/control/cmdline.py Normal file
View file

@ -0,0 +1,100 @@
#
# Copyright (c) 2008 Helmut Merz helmutm@cy55.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Base/sample controller implementation.
$Id$
"""
from twisted.internet import protocol, reactor, stdio
from twisted.protocols import basic
from zope.interface import implements
from cybertools.agent.base.agent import Master
from cybertools.agent.core.control import SampleController
from cybertools.agent.components import controllers
class CmdlineController(SampleController):
def setup(self):
super(CmdlineController, self).setup()
stdio.StandardIO(CmdlineProtocol())
controllers.register(CmdlineController, Master, name='cmdline')
class TelnetController(CmdlineController):
delimiter = '\r\n'
def setup(self):
super(CmdlineController, self).setup()
reactor.listenTCP(5001, TelnetServerFactory())
controllers.register(TelnetController, Master, name='telnet')
class CmdlineProtocol(basic.LineReceiver):
delimiter = '\n'
def connectionMade(self):
self.sendLine("Agent console. Type 'help' for help.")
def lineReceived(self, line):
if not line:
return
commandParts = line.split()
command = commandParts[0].lower()
args = commandParts[1:]
try:
method = getattr(self, 'do_' + command)
except AttributeError, e:
self.sendLine('Error: no such command.')
else:
try:
method(*args)
except Exception, e:
self.sendLine('Error: ' + str(e))
def do_help(self, command=None):
if command:
self.sendLine(getattr(self, 'do_' + command).__doc__)
else:
commands = [cmd[3:] for cmd in dir(self) if cmd.startswith('do_')]
self.sendLine("Valid commands: " +" ".join(commands))
def do_shutdown(self):
self.sendLine('Shutting down.')
reactor.stop()
class TelnetProtocol(CmdlineProtocol):
delimiter = '\r\n'
def do_quit(self):
self.sendLine('Goodbye.')
self.transport.loseConnection()
class TelnetServerFactory(protocol.ServerFactory):
protocol = TelnetProtocol

View file

@ -142,9 +142,9 @@ class IController(Interface):
information. information.
""" """
def setupAgent(): def setup():
""" Set up the controllers's agent by calling the agent's """ Set up the controller; e.g. create agents and jobs by calling
callback methods. the agent's callback methods.
""" """