PageRenderTime 48ms CodeModel.GetById 41ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/jsonrpc/public/services/jsonrpc/proxy.py

http://pyjamas.googlecode.com/
Python | 76 lines | 43 code | 13 blank | 20 comment | 0 complexity | b3e5a0c93973e8a827acd27be2dcc728 MD5 | raw file
 1"""
 2  Copyright (c) 2006 Jan-Klaas Kollhof
 3
 4  This file is part of jsonrpc.
 5
 6  jsonrpc is free software; you can redistribute it and/or modify
 7  it under the terms of the GNU Lesser General Public License as published by
 8  the Free Software Foundation; either version 2.1 of the License, or
 9  (at your option) any later version.
10
11  This software is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU Lesser General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with this software; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19"""
20
21
22        
23class PeerObjectProxy(object):
24    """creates a peer object which will send requests to the remote service when invoked."""
25    def __init__(self, name, conn):
26        self._name = name
27        self._conn = conn
28    
29    def notify(self, *args):
30        self._conn.sendNotify(self._name, args)
31        
32    def __call__(self, *args):
33        evt  = self._conn.sendRequest(self._name, args)
34        return evt.waitForResponse()
35
36    def __getattr__(self, name):
37        return PeerObjectProxy(self._name + "." + name, self._conn)
38
39
40class PeerProxy:
41    def __init__(self, connectionHandler):
42        self._connectionHandler = connectionHandler
43    
44    def __getattr__(self, name):
45        return PeerObjectProxy(name, self._connectionHandler)
46
47
48import re
49
50
51class ServiceProxy(PeerProxy):
52    def __init__(self, url, localService=None, messageDelimiter=""):
53        m = re.match(r"^jsonrpc:\/\/(.*):(\d*)$", url)
54        if m:
55            from jsonrpc.socketserver import SocketServiceHandler
56            import socket
57            from threading import Thread
58            
59            (host, port)= m.groups()
60            port = int(port)
61            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
62            s.connect((host, port))
63            conn = SocketServiceHandler(s, localService,messageDelimiter=messageDelimiter)
64            PeerProxy.__init__(self, conn)
65            
66            t=Thread(target=conn.receiveForever)
67            t.setDaemon(True)
68            t.start()
69        else:
70            from jsonrpc.http import HTTPClientConnectionHandler
71            conn= HTTPClientConnectionHandler(url, localService,messageDelimiter=messageDelimiter)
72            PeerProxy.__init__(self, conn)
73
74    
75
76