Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

compatibility with tornado v1.0, v1.1 and master

  • Loading branch information...
commit 4bf2c04967fda9b70c48edb6eaa70e01727a61c4 1 parent 0e76637
@paolo-losi authored
Showing with 19 additions and 5 deletions.
  1. +19 −5 stormed/connection.py
View
24 stormed/connection.py
@@ -1,5 +1,6 @@
import time
import socket
+
from tornado.iostream import IOStream
from tornado.ioloop import IOLoop
from tornado import stack_context
@@ -10,6 +11,7 @@
from stormed.channel import Channel
from stormed.method.connection import Close
+TORNADO_1_2 = hasattr(IOStream, 'connect')
class Connection(FrameHandler):
@@ -40,10 +42,15 @@ def connect(self, callback):
self.status = status.OPENING
sock = socket.socket()
sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
- self.stream = IOStream(sock, io_loop=self.io_loop)
- self.stream.connect((self.host, self.port), self._handshake)
- self.stream.set_close_callback(self.on_closed_stream)
self.on_connect = callback
+ if TORNADO_1_2:
+ self.stream = IOStream(sock, io_loop=self.io_loop)
+ self.stream.connect((self.host, self.port), self._handshake)
+ else:
+ sock.connect((self.host, self.port))
+ self.stream = IOStream(sock, io_loop=self.io_loop)
+ self._handshake()
+ self.stream.set_close_callback(self.on_closed_stream)
def close(self, callback=None):
if self.status != status.CLOSING:
@@ -83,16 +90,23 @@ def _frame_loop(self, frame):
if self.stream:
# Every 5 frames ioloop gets the control back in order
# to avoid hitting the recursion limit
- # reading one frame cost 13 level of stack recursion
+ # reading one frame cost 13 levels of stack recursion
# TODO check if always using _callbacks is faster that frame
# counting
if self._frame_count == 5:
self._frame_count = 0
cb = lambda: FrameReader(self.stream, self._frame_loop)
- self.io_loop._callbacks.append(cb)
+ self._add_ioloop_callback(cb)
else:
FrameReader(self.stream, self._frame_loop)
+ if TORNADO_1_2:
+ def _add_ioloop_callback(self, callback):
+ self.io_loop._callbacks.append(callback)
+ else:
+ def _add_ioloop_callback(self, callback):
+ self.io_loop._callbacks.add(callback)
+
def close_stream(self):
self.status = status.CLOSED
self.stream.close()
Please sign in to comment.
Something went wrong with that request. Please try again.