From c0ce542b9cf3ca75c498c5c3d962dc52a1555514 Mon Sep 17 00:00:00 2001 From: helmutm Date: Sun, 15 Mar 2009 09:38:43 +0000 Subject: [PATCH] work in progress: 'agent.talk' HTTP communication git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3276 fd906abe-77d9-0310-91a1-e0d9ade77398 --- agent/README.txt | 2 +- agent/talk/README.txt | 44 +++++++++++++++++++++++++++++++++++++++- agent/talk/base.py | 6 ++++++ agent/talk/http.py | 11 +++++++--- agent/talk/interfaces.py | 4 ++-- agent/tests.py | 5 ++++- 6 files changed, 64 insertions(+), 8 deletions(-) diff --git a/agent/README.txt b/agent/README.txt index 804562e..b425a03 100644 --- a/agent/README.txt +++ b/agent/README.txt @@ -2,7 +2,7 @@ Agents for Job Execution and Communication Tasks ================================================ -Agents do some work specified by a jobs, the main task being to collect +Agents do some work specified by jobs, the main task being to collect information objects from the local machine or some external source and transfer them e.g. to a loops server on the same machine or another. diff --git a/agent/talk/README.txt b/agent/talk/README.txt index 6e984ef..5dd4741 100644 --- a/agent/talk/README.txt +++ b/agent/talk/README.txt @@ -11,6 +11,9 @@ Communication Handling Communication services are provided by handlers specified in the ``talk`` package. +Set up and start an agent with a server +--------------------------------------- + >>> config = ''' ... controller(names=['core.sample']) ... scheduler(name='core') @@ -25,6 +28,45 @@ package. Using controllers core.sample. Setting up HTTP handler for port 8081. - >>> master.servers [] + +We also provide a class to be used for creating subscribers, i.e. objects +that receive messages. + + >>> class Subscriber(object): + ... def __init__(self, name): + ... self.name = name + ... def onMessage(self, interaction, data): + ... print ('%s receiving: interaction=%s, data=%s' % + ... (self.name, interaction, data)) + + >>> serverSub = Subscriber('server') + + >>> master.servers[0].subscribe(serverSub, 'testing') + +Set up a client +--------------- + +In order to simplify the testing we do not set up a separate agent to +work with the client but handle the client directly. + + >>> from cybertools.agent.talk.http import HttpClient + >>> client = HttpClient(master) + + >>> clientSub = Subscriber('client') + + >>> session = client.connect(clientSub, 'http://localhost:8081/') + +Run the communication dialog +---------------------------- + + >>> from cybertools.agent.tests import tester + >>> tester.iterate(400) + Session receiving, data={"message": "OK"} + + +Fin de Partie +============= + + >>> tester.stopThreads() diff --git a/agent/talk/base.py b/agent/talk/base.py index e3097fe..c792574 100644 --- a/agent/talk/base.py +++ b/agent/talk/base.py @@ -33,8 +33,14 @@ class Session(object): def __init__(self, manager): self.manager = manager + self.state = 'logon' + self.id = None + self.queue = [] self.interactions = {} + def receive(self, data): + print ('Session receiving, data=%s' % data) + class Interaction(object): diff --git a/agent/talk/http.py b/agent/talk/http.py index b42228f..349d8c5 100644 --- a/agent/talk/http.py +++ b/agent/talk/http.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2008 Helmut Merz helmutm@cy55.de +# Copyright (c) 2009 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 @@ -23,6 +23,7 @@ $Id$ """ from twisted.internet import defer +from twisted.web.client import getPage from twisted.web.resource import Resource from twisted.web.server import Site from zope.interface import implements @@ -77,7 +78,7 @@ class CommandHandler(Resource): self.command = path def render(self, request): - # + #print request return '{"message": "OK"}' @@ -90,9 +91,13 @@ class HttpClient(object): def __init__(self, agent): self.agent = agent + self.sessions = {} def connect(self, subscriber, url, credentials=None): - return defer.Deferred() # Session + s = Session(self) + d = getPage(url) + d.addCallback(s.receive) + return s def disconnect(self, session): pass diff --git a/agent/talk/interfaces.py b/agent/talk/interfaces.py index 41cd140..6f2d8b9 100644 --- a/agent/talk/interfaces.py +++ b/agent/talk/interfaces.py @@ -61,8 +61,8 @@ class IClient(Interface): The subscriber will receive messages via its ``onMesssage`` callback. - Return a Deferred that will provide an ISession implementation; - this may then be used sending data to the server. + Return a an ISession implementation that may be used for sending + data to the server. """ def disconnect(session): diff --git a/agent/tests.py b/agent/tests.py index f73ebe6..3dbd824 100755 --- a/agent/tests.py +++ b/agent/tests.py @@ -21,6 +21,9 @@ class Tester(object): delay = delays.get(i, 0) reactor.iterate(delay) + def stopThreads(self): + reactor.threadpool.stop() + tester = Tester() @@ -42,11 +45,11 @@ def test_suite(): testSuite = unittest.TestSuite(( unittest.makeSuite(Test), DocFileSuite('README.txt', optionflags=flags), - DocFileSuite('talk/README.txt', optionflags=flags), DocFileSuite('crawl/README.txt', optionflags=flags), DocFileSuite('crawl/filesystem.txt', optionflags=flags), DocFileSuite('crawl/outlook.txt', optionflags=flags), DocFileSuite('transport/transporter.txt', optionflags=flags), + DocFileSuite('talk/README.txt', optionflags=flags), )) return testSuite