Mercurial > cosocket
changeset 3:9e819377ce9f
Changed the way coroutine factories work.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 17 Apr 2009 16:17:37 -0700 |
parents | f6f9cc0385be |
children | 4e6ce85226f4 |
files | taw.py |
diffstat | 1 files changed, 34 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/taw.py Fri Apr 17 16:04:21 2009 -0700 +++ b/taw.py Fri Apr 17 16:17:37 2009 -0700 @@ -3,24 +3,6 @@ import asyncore import asynchat -class ChattyCoroutineServer(asyncore.dispatcher): - def __init__(self, addr, coroutineFactory): - asyncore.dispatcher.__init__(self) - self.__coroutineFactory = coroutineFactory - self.create_socket(socket.AF_INET, socket.SOCK_STREAM) - self.set_reuse_addr() - self.bind(addr) - self.listen(1) - - def run(self): - while 1: - asyncore.loop(timeout = 0.5) - - def handle_accept(self): - conn, addr = self.accept() - coroutine = self.__coroutineFactory(addr) - AsyncChatCoroutineBridge(coroutine, conn) - class AsyncChatCoroutineBridge(asynchat.async_chat): def __init__(self, coroutine, conn = None): asynchat.async_chat.__init__(self, conn) @@ -67,13 +49,32 @@ data = ''.join(self.__data) self.__process_next_instruction(data) +class ChattyCoroutineServer(asyncore.dispatcher): + def __init__(self, addr, coroutineFactory): + asyncore.dispatcher.__init__(self) + self.__coroutineFactory = coroutineFactory + self.create_socket(socket.AF_INET, socket.SOCK_STREAM) + self.set_reuse_addr() + self.bind(addr) + self.listen(1) + + def run(self): + while 1: + asyncore.loop(timeout = 0.5) + + def handle_accept(self): + conn, addr = self.accept() + coroutine = self.__coroutineFactory(ChattyInstructions(), addr) + AsyncChatCoroutineBridge(coroutine, conn) + class ChattyCoroutineClient(AsyncChatCoroutineBridge): - def __init__(self, addr, coroutine): + def __init__(self, addr, coroutineFactory): + coroutine = coroutineFactory(ChattyInstructions(), addr) AsyncChatCoroutineBridge.__init__(self, coroutine) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect(addr) -class ChattyCoroutine(object): +class ChattyInstructions(object): def receive(self, terminator = None, length = None): return {'op': 'receive', 'terminator': terminator, @@ -83,25 +84,22 @@ return {'op': 'send', 'content': content} -class LameHttpClientCoroutineFactory(ChattyCoroutine): - def __call__(self): - yield self.send('GET / HTTP/1.1\r\n\r\n') - response = yield self.receive(terminator = '\r\n\r\n') - print response +def lame_http_server_coroutine(chat, addr): + req = yield chat.receive(terminator = '\r\n\r\n') + msg = 'hello %s.' % addr[0] + yield chat.send('HTTP/1.1 200 OK\r\n' + + 'Content-Length: %d\r\n' % len(msg) + + 'Content-Type: text/plain\r\n\r\n' + + msg) -class LameHttpServerCoroutineFactory(ChattyCoroutine): - def __call__(self, addr): - req = yield self.receive(terminator = '\r\n\r\n') - msg = 'hello %s.' % addr[0] - yield self.send('HTTP/1.1 200 OK\r\n' + - 'Content-Length: %d\r\n' % len(msg) + - 'Content-Type: text/plain\r\n\r\n' + - msg) +def lame_http_client_coroutine(chat, addr): + yield chat.send('GET / HTTP/1.1\r\n\r\n') + response = yield chat.receive(terminator = '\r\n\r\n') + print response if __name__ == '__main__': server = ChattyCoroutineServer(('127.0.0.1', 8071), - LameHttpServerCoroutineFactory()) - client_factory = LameHttpClientCoroutineFactory() + lame_http_server_coroutine) client = ChattyCoroutineClient(('www.google.com', 80), - client_factory()) + lame_http_client_coroutine) server.run()