From 3baf1fabb582e5e9fec9aeebb3040f50b68ed447 Mon Sep 17 00:00:00 2001 From: helmutm Date: Sun, 19 Oct 2008 17:46:10 +0000 Subject: [PATCH] work in progress: talk package for generic data communication tasks git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@2925 fd906abe-77d9-0310-91a1-e0d9ade77398 --- agent/talk/base.py | 47 +++++++++++++++++++++++++++++ agent/talk/http.py | 64 ++++++++++++++++++++++------------------ agent/talk/interfaces.py | 8 ++--- 3 files changed, 87 insertions(+), 32 deletions(-) create mode 100644 agent/talk/base.py diff --git a/agent/talk/base.py b/agent/talk/base.py new file mode 100644 index 0000000..e3097fe --- /dev/null +++ b/agent/talk/base.py @@ -0,0 +1,47 @@ +# +# 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 +# + +""" +Handling asynchronous communication tasks - common and base classes. + +$Id$ +""" + +from zope.interface import implements + +from cybertools.agent.talk.interfaces import ISession, IInteraction + + +class Session(object): + + implements(ISession) + + def __init__(self, manager): + self.manager = manager + self.interactions = {} + + +class Interaction(object): + + implements(IInteraction) + + finished = False + + def __init__(self, session): + self.session = session + diff --git a/agent/talk/http.py b/agent/talk/http.py index 7eda581..7ee90f9 100644 --- a/agent/talk/http.py +++ b/agent/talk/http.py @@ -17,7 +17,7 @@ # """ -Handling asynchronous and possibly asymmetric communication tasks. +Handling asynchronous and possibly asymmetric communication tasks via HTTP. $Id$ """ @@ -30,8 +30,38 @@ from zope.interface import implements from cybertools.agent.base.agent import Master from cybertools.agent.components import servers, clients from cybertools.agent.system.http import listener +from cybertools.agent.talk.base import Session, Interaction from cybertools.agent.talk.interfaces import IServer, IClient -from cybertools.agent.talk.interfaces import ISession, IInteraction + + +# server implementation + +class HttpServer(object): + + implements(IServer) + + def __init__(self, agent): + self.agent = agent + self.port = agent.config.talk.server.http.port + self.subscribers = {} + self.sessions = {} + self.site = Site(RootResource()) + + def setup(self): + print 'Setting up HTTP handler for port %i.' % self.port + listener.listenTCP(self.port, self.site) + + def subscribe(self, subscriber, aspect): + pass + + def unsubscribe(self, subscriber, aspect): + pass + + def send(self, session, data, interaction=None): + # respond to open poll request or put in queue + return defer.Deferred() # Interaction + +servers.register(HttpServer, Master, name='http') class RootResource(Resource): @@ -46,33 +76,11 @@ class CommandHandler(Resource): self.command = path def render(self, request): + # return '{"message": "OK"}' -class HttpServer(object): - - implements(IServer) - - def __init__(self, agent): - self.agent = agent - self.port = agent.config.talk.server.http.port - self.subscribers = {} - - def setup(self): - print 'Setting up HTTP handler for port %i.' % self.port - listener.listenTCP(self.port, Site(RootResource())) - - def subscribe(self, subscriber, aspect): - pass - - def unsubscribe(self, subscriber, aspect): - pass - - def send(self, client, data, interaction=None): - return defer.Deferred() # Interaction - -servers.register(HttpServer, Master, name='http') - +# client implementation class HttpClient(object): @@ -81,10 +89,10 @@ class HttpClient(object): def __init__(self, agent): self.agent = agent - def logon(self, subscriber, url): + def connect(self, subscriber, url, credentials=None): return defer.Deferred() # Session - def logoff(self, session): + def disconnect(self, session): pass def send(self, session, data, interaction=None): diff --git a/agent/talk/interfaces.py b/agent/talk/interfaces.py index f0775ff..c2211d0 100644 --- a/agent/talk/interfaces.py +++ b/agent/talk/interfaces.py @@ -57,7 +57,7 @@ class IClient(Interface): sent data to or receive data from the server. """ - def logon(subscriber, url, credentials=None): + def connect(subscriber, url, credentials=None): """ Connect to a server using the URL given, optionally logging in with the credentials given. @@ -67,7 +67,7 @@ class IClient(Interface): this may then be used sending data to the server. """ - def logoff(session): + def disconnect(session): """ Close the connection for the session given. """ @@ -97,8 +97,8 @@ class ISession(Interface): a remote client connection within a server. """ - issuer = Attribute("""The issuer of the session, i.e. the server or - client object, respectively.""") + manager = Attribute("""The server or client object, respectively, that + created the session.""") state = Attribute("""A string specifying the current state of the session: 'logon': The remote client is trying to connect/log in,