PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/jsonrpc/public/services/jsonrpc/socketserver.py

http://pyjamas.googlecode.com/
Python | 91 lines | 69 code | 3 blank | 19 comment | 0 complexity | c038c0f2c5658c6e3197e41f1021d095 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. from jsonrpc import SimpleServiceHandler
  17. import socket
  18. from threading import Thread
  19. class SocketServiceHandler(SimpleServiceHandler):
  20. def __init__(self, socket, service, messageDelimiter=""):
  21. self.socket = socket
  22. SimpleServiceHandler.__init__(self, service, messageDelimiter=messageDelimiter)
  23. def receiveForever(self):
  24. while 1:
  25. try:
  26. data = self.socket.recv(1024)
  27. except:
  28. data = None
  29. if not data:
  30. if self.socket:
  31. self.close()
  32. return
  33. else:
  34. self.handlePartialData(data)
  35. def send(self, data):
  36. self.socket.send(data)
  37. def close(self):
  38. SimpleServiceHandler.close(self)
  39. if self.socket:
  40. try:
  41. self.socket.shutdown(socket.SHUT_RDWR)
  42. self.socket = None
  43. except:
  44. pass
  45. class TCPServiceServer:
  46. def __init__(self, service, ConnectionHandler = SocketServiceHandler, messageDelimiter=""):
  47. self.service = service
  48. self.ConnectionHandler = ConnectionHandler
  49. self.messageDelimiter=messageDelimiter
  50. def serve(self, address):
  51. self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  52. self.socket.bind(address)
  53. self.socket.listen(5)
  54. print "serving", self.socket
  55. while 1:
  56. (conn,addr) = self.socket.accept()
  57. self.acceptConnection(conn)
  58. def acceptConnection(self, conn):
  59. self.handleConnection(conn)
  60. def handleConnection(self, conn):
  61. self.ConnectionHandler(conn, self.service, messageDelimiter=self.messageDelimiter).receiveForever()
  62. class ThreadingMixin:
  63. def acceptConnection(self, conn):
  64. t = Thread(target=self.handleConnection, args=(conn,))
  65. t.setDaemon(True)
  66. t.start()
  67. class ThreadedTCPServiceServer(ThreadingMixin, TCPServiceServer):
  68. pass