Mercurial > cosocket
changeset 5:4ec829dc7d1d
Renamed ChatInstructions to 'to_send()', 'to_receive()' for readability.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 17 Apr 2009 17:02:35 -0700 |
parents | 4e6ce85226f4 |
children | fdcb6f3e1a0f |
files | taw.py |
diffstat | 1 files changed, 22 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/taw.py Fri Apr 17 16:23:31 2009 -0700 +++ b/taw.py Fri Apr 17 17:02:35 2009 -0700 @@ -21,8 +21,8 @@ if instruction['op'] == 'receive': if instruction['terminator']: self.set_terminator(instruction['terminator']) - elif instruction['length']: - self.set_terminator(instruction['length']) + elif instruction['bytes']: + self.set_terminator(instruction['bytes']) else: raise ValueError(instruction) elif instruction['op'] == 'send': @@ -65,40 +65,39 @@ def handle_accept(self): conn, addr = self.accept() - coroutine = self.__coroutineFactory(ChattyInstructions(), addr) + coroutine = self.__coroutineFactory(addr) AsyncChatCoroutineBridge(coroutine, conn) class ChattyCoroutineClient(AsyncChatCoroutineBridge): def __init__(self, addr, coroutineFactory): - coroutine = coroutineFactory(ChattyInstructions(), addr) + coroutine = coroutineFactory(addr) AsyncChatCoroutineBridge.__init__(self, coroutine) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect(addr) -class ChattyInstructions(object): - def receive(self, terminator = None, length = None): - return {'op': 'receive', - 'terminator': terminator, - 'length': length} +def to_receive(terminator = None, bytes = None): + return {'op': 'receive', + 'terminator': terminator, + 'bytes': bytes} - def send(self, content): - return {'op': 'send', - 'content': content} +def to_send(content): + return {'op': 'send', + 'content': content} -def lame_http_server_coroutine(chat, addr): - req = yield chat.receive(terminator = '\r\n\r\n') +def lame_http_server_coroutine(addr): + request = yield to_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) + yield to_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_headers = yield chat.receive(terminator = '\r\n\r\n') - response = yield chat.receive(20) +def lame_http_client_coroutine(addr): + yield to_send('GET / HTTP/1.1\r\n\r\n') + response_headers = yield to_receive(terminator = '\r\n\r\n') + response = yield to_receive(bytes = 20) print 'first response: %s' % repr(response) - response = yield chat.receive(20) + response = yield to_receive(bytes = 20) print 'second response: %s' % repr(response) if __name__ == '__main__':