import gevent
from gevent import socket
from gevent import queue

import tcptunnel as tcp

#tcp.conversation = tcp.Conversation('server')
#tcp.tunnel = tcp.Tunnel()

class dummy_echo_socket(object):
    def __init__(self, filename):
        self._fd = open(filename, 'wb')
        self._seq = 0
        self._echoq = queue.Queue()
        
    def send(self, data):
        self.sendall(data)
        
    def recv(self, size):
        data = self._echoq.get()
        print 'send %d bytes to client' % len(data)
        return data
        
    def sendall(self, data):
        #print 'send', len(data), data, [ord(c) for c in data]
        self._fd.write(data)
        self._fd.flush()
        print 'write', len(data), 'bytes'
        print 'about to put queue'
        self._echoq.put(data)
        print 'queue size:', len(self._echoq)
        #gevent.sleep(0.1)

def server_con():
    return dummy_echo_socket('server.out')

PORT = 500

sever_tunnel = tcp.Tunnel('server', server_con)
gevent.spawn(sever_tunnel.tcp_listen, ('0.0.0.0', PORT))
# ? join ?

client_tunnel = tcp.Tunnel('client')

class dummy_socket(object):
    def __init__(self, filename):
        self._fd = open(filename, 'wb')
        self._rfd = open('tcptunnel.pyc','rb')
        self._seq = 0
        
    def send(self, data):
        self.sendall(data)
        
    def recv(self, size):
        gevent.sleep(1.0)
        #self._seq += 1
        #return '%010d\n' % self._seq
        return self._rfd.read(1024)
        
    def sendall(self, data):
        #print 'send', len(data), data, [ord(c) for c in data]
        self._fd.write(data)
        self._fd.flush()
        print 'write', len(data), 'bytes'
        #gevent.sleep(0.1)

sock = dummy_socket('client.out')
w = tcp.Window(sock, client_tunnel.get_conversation())

client_tunnel.tcp_connect(('127.0.0.1',PORT))

ws = gevent.spawn(w.start)
ws.join()

@@ -715,32 +715,39 @@
 %0A        print '
+server 
 write', len(data
@@ -770,100 +770,8 @@
     
-print 'about to put queue'%0D%0A        self._echoq.put(data)%0D%0A        print 'queue size:', len(
 self
@@ -777,16 +777,25 @@
 f._echoq
+.put(data
 )%0D%0A     
@@ -1345,16 +1345,17 @@
         
+#
 gevent.s
@@ -1363,11 +1363,11 @@
 eep(
-1.0
+0.1
 )%0D%0A 
@@ -1437,22 +1437,22 @@
         
-return
+data =
  self._r
@@ -1463,13 +1463,185 @@
 ead(
-1024)
+size)%0D%0A        if len(data) == 0:%0D%0A            print 'finished reanding'%0D%0A            while True:%0D%0A                gevent.sleep(1.0)%0D%0A                quit()%0D%0A        return data
 %0D%0A  
@@ -1790,32 +1790,32 @@
 lf._fd.flush()%0D%0A
-
         print 'w
@@ -1813,16 +1813,23 @@
  print '
+client 
 write', 

make a better test file