PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 11ms RepoModel.GetById 2ms 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
 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"""
20from jsonrpc import SimpleServiceHandler
21import socket
22
23from threading import Thread
24    
25    
26class SocketServiceHandler(SimpleServiceHandler):
27    def __init__(self, socket, service, messageDelimiter=""):
28        self.socket = socket
29        SimpleServiceHandler.__init__(self, service, messageDelimiter=messageDelimiter)
30        
31    def receiveForever(self):
32        while 1:
33            try:
34                data = self.socket.recv(1024)
35            except:
36                data = None
37
38            if not data:
39                if self.socket:
40                    self.close()
41                return
42            else:
43                self.handlePartialData(data)
44            
45    def send(self, data):
46        self.socket.send(data)
47    
48    def close(self):
49        SimpleServiceHandler.close(self)
50        if self.socket:
51            try:
52                self.socket.shutdown(socket.SHUT_RDWR)
53                self.socket = None
54            except:
55                pass
56            
57
58
59class TCPServiceServer:
60    def __init__(self, service, ConnectionHandler = SocketServiceHandler, messageDelimiter=""):
61        self.service = service
62        self.ConnectionHandler = ConnectionHandler
63        self.messageDelimiter=messageDelimiter
64        
65    def serve(self, address):
66        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
67        self.socket.bind(address)        
68        self.socket.listen(5)
69        print "serving", self.socket
70        while 1:
71            (conn,addr) = self.socket.accept()
72            self.acceptConnection(conn)
73    
74    def acceptConnection(self, conn):
75        self.handleConnection(conn)
76    
77    def handleConnection(self, conn):
78        self.ConnectionHandler(conn, self.service, messageDelimiter=self.messageDelimiter).receiveForever()
79    
80
81
82class ThreadingMixin:
83    def acceptConnection(self, conn):
84        t = Thread(target=self.handleConnection, args=(conn,))
85        t.setDaemon(True)
86        t.start()
87
88class ThreadedTCPServiceServer(ThreadingMixin, TCPServiceServer):
89    pass
90        
91