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