Package qpid :: Package messaging :: Module transports
[hide private]
[frames] | no frames]

Source Code for Module qpid.messaging.transports

  1  # 
  2  # Licensed to the Apache Software Foundation (ASF) under one 
  3  # or more contributor license agreements.  See the NOTICE file 
  4  # distributed with this work for additional information 
  5  # regarding copyright ownership.  The ASF licenses this file 
  6  # to you under the Apache License, Version 2.0 (the 
  7  # "License"); you may not use this file except in compliance 
  8  # with the License.  You may obtain a copy of the License at 
  9  # 
 10  #   http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, 
 13  # software distributed under the License is distributed on an 
 14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 15  # KIND, either express or implied.  See the License for the 
 16  # specific language governing permissions and limitations 
 17  # under the License. 
 18  # 
 19   
 20  import socket 
 21  from qpid.util import connect 
 22   
 23  TRANSPORTS = {} 
 24   
25 -class SocketTransport:
26
27 - def __init__(self, conn, host, port):
28 self.socket = connect(host, port) 29 if conn.tcp_nodelay: 30 self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
31
32 - def fileno(self):
33 return self.socket.fileno()
34
35 -class tcp(SocketTransport):
36
37 - def reading(self, reading):
38 return reading
39
40 - def writing(self, writing):
41 return writing
42
43 - def send(self, bytes):
44 return self.socket.send(bytes)
45
46 - def recv(self, n):
47 return self.socket.recv(n)
48
49 - def close(self):
50 self.socket.close()
51 52 TRANSPORTS["tcp"] = tcp 53 54 try: 55 from ssl import wrap_socket, SSLError, SSL_ERROR_WANT_READ, \ 56 SSL_ERROR_WANT_WRITE 57 except ImportError: 58 pass 59 else:
60 - class tls(SocketTransport):
61
62 - def __init__(self, conn, host, port):
63 SocketTransport.__init__(self, conn, host, port) 64 self.tls = wrap_socket(self.socket, keyfile=conn.ssl_keyfile, certfile=conn.ssl_certfile, ca_certs=conn.ssl_trustfile) 65 self.socket.setblocking(0) 66 self.state = None
67
68 - def reading(self, reading):
69 if self.state is None: 70 return reading 71 else: 72 return self.state == SSL_ERROR_WANT_READ
73
74 - def writing(self, writing):
75 if self.state is None: 76 return writing 77 else: 78 return self.state == SSL_ERROR_WANT_WRITE
79
80 - def send(self, bytes):
81 self._clear_state() 82 try: 83 return self.tls.write(bytes) 84 except SSLError, e: 85 if self._update_state(e.args[0]): 86 return 0 87 else: 88 raise
89
90 - def recv(self, n):
91 self._clear_state() 92 try: 93 return self.tls.read(n) 94 except SSLError, e: 95 if self._update_state(e.args[0]): 96 return None 97 else: 98 raise
99
100 - def _clear_state(self):
101 self.state = None
102
103 - def _update_state(self, code):
104 if code in (SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE): 105 self.state = code 106 return True 107 else: 108 return False
109
110 - def close(self):
111 self.socket.setblocking(1) 112 # this closes the underlying socket 113 self.tls.close()
114 115 TRANSPORTS["ssl"] = tls 116 TRANSPORTS["tcp+tls"] = tls 117