diff --git a/agent/__init__.py b/agent/__init__.py index f0cb314..cc89119 100644 --- a/agent/__init__.py +++ b/agent/__init__.py @@ -6,4 +6,5 @@ $Id$ from cybertools.agent.base import agent, control, job, log, schedule from cybertools.agent.core import agent, control, schedule +from cybertools.agent.control import cmdline from cybertools.agent.crawl import base diff --git a/agent/app.py b/agent/app.py new file mode 100755 index 0000000..be4c253 --- /dev/null +++ b/agent/app.py @@ -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.' diff --git a/agent/base/agent.py b/agent/base/agent.py index 3de42ea..8b49d36 100644 --- a/agent/base/agent.py +++ b/agent/base/agent.py @@ -74,7 +74,7 @@ class Master(Agent): def setup(self): for cont in self.controllers: - cont.setupAgent() + cont.setup() def setupAgents(self, controller, agentSpecs): for spec in agentSpecs: diff --git a/agent/base/control.py b/agent/base/control.py index 298196d..a4204df 100644 --- a/agent/base/control.py +++ b/agent/base/control.py @@ -36,7 +36,7 @@ class Controller(object): def __init__(self, agent): self.agent = agent - def setupAgent(self): + def setup(self): self.agent.setupAgents(self, self._getAgents()) self.agent.setupJobs(self, self._getCurrentJobs()) diff --git a/agent/control/cmdline.py b/agent/control/cmdline.py new file mode 100644 index 0000000..3e0c520 --- /dev/null +++ b/agent/control/cmdline.py @@ -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 diff --git a/agent/interfaces.py b/agent/interfaces.py index 2be649b..e524de9 100644 --- a/agent/interfaces.py +++ b/agent/interfaces.py @@ -142,9 +142,9 @@ class IController(Interface): information. """ - def setupAgent(): - """ Set up the controllers's agent by calling the agent's - callback methods. + def setup(): + """ Set up the controller; e.g. create agents and jobs by calling + the agent's callback methods. """