changeset 8:2571a5297800

Renamed taw.py to cosocket.py
author Atul Varma <varmaa@toolness.com>
date Fri, 17 Apr 2009 17:09:00 -0700
parents d3ae3fc76711
children 9053424eb47d
files cosocket.py example.py taw.py
diffstat 3 files changed, 87 insertions(+), 87 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cosocket.py	Fri Apr 17 17:09:00 2009 -0700
@@ -0,0 +1,86 @@
+import socket
+import asyncore
+import asynchat
+
+class _AsyncChatCoroutineBridge(asynchat.async_chat):
+    def __init__(self, coroutine, conn = None):
+        asynchat.async_chat.__init__(self, conn)
+        self.set_terminator(None)
+        self.__coroutine = coroutine
+        self.__data = []
+        if conn:
+            self.__process_next_instruction()
+
+    def __process_next_instruction(self, feedback = None):
+        try:
+            instruction = self.__coroutine.send(feedback)
+        except StopIteration:
+            instruction = {'op': 'close'}
+
+        if instruction['op'] == 'receive':
+            if instruction['terminator']:
+                self.set_terminator(instruction['terminator'])
+            elif instruction['bytes']:
+                self.set_terminator(instruction['bytes'])
+            else:
+                raise ValueError(instruction)
+        elif instruction['op'] == 'send':
+            self.push(instruction['content'])
+        elif instruction['op'] == 'close':
+            self.close()
+
+    def handle_connect(self):
+        self.__process_next_instruction()
+
+    def initiate_send(self):
+        asynchat.async_chat.initiate_send(self)
+        if ((not self.ac_out_buffer) and
+            (len(self.producer_fifo) == 0) and
+            self.connected):
+            self.__process_next_instruction()
+
+    def collect_incoming_data(self, data):
+        # TODO: Enforce some maximum data length.
+        self.__data.append(data)
+
+    def found_terminator(self):
+        self.set_terminator(None)
+        data = ''.join(self.__data)
+        self.__data = []
+        self.__process_next_instruction(data)
+
+class CoroutineSocketServer(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 CoroutineSocketClient(_AsyncChatCoroutineBridge):
+    def __init__(self, addr, coroutineFactory):
+        coroutine = coroutineFactory(addr)
+        _AsyncChatCoroutineBridge.__init__(self, coroutine)
+        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.connect(addr)
+
+# Instructions that coroutines yield.
+
+def to_receive(terminator = None, bytes = None):
+    return {'op': 'receive',
+            'terminator': terminator,
+            'bytes': bytes}
+
+def to_send(content):
+    return {'op': 'send',
+            'content': content}
--- a/example.py	Fri Apr 17 17:07:27 2009 -0700
+++ b/example.py	Fri Apr 17 17:09:00 2009 -0700
@@ -1,4 +1,4 @@
-from taw import *
+from cosocket import *
 
 def lame_http_server_coroutine(addr):
     request = yield to_receive(terminator = '\r\n\r\n')
--- a/taw.py	Fri Apr 17 17:07:27 2009 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,86 +0,0 @@
-import socket
-import asyncore
-import asynchat
-
-class _AsyncChatCoroutineBridge(asynchat.async_chat):
-    def __init__(self, coroutine, conn = None):
-        asynchat.async_chat.__init__(self, conn)
-        self.set_terminator(None)
-        self.__coroutine = coroutine
-        self.__data = []
-        if conn:
-            self.__process_next_instruction()
-
-    def __process_next_instruction(self, feedback = None):
-        try:
-            instruction = self.__coroutine.send(feedback)
-        except StopIteration:
-            instruction = {'op': 'close'}
-
-        if instruction['op'] == 'receive':
-            if instruction['terminator']:
-                self.set_terminator(instruction['terminator'])
-            elif instruction['bytes']:
-                self.set_terminator(instruction['bytes'])
-            else:
-                raise ValueError(instruction)
-        elif instruction['op'] == 'send':
-            self.push(instruction['content'])
-        elif instruction['op'] == 'close':
-            self.close()
-
-    def handle_connect(self):
-        self.__process_next_instruction()
-
-    def initiate_send(self):
-        asynchat.async_chat.initiate_send(self)
-        if ((not self.ac_out_buffer) and
-            (len(self.producer_fifo) == 0) and
-            self.connected):
-            self.__process_next_instruction()
-
-    def collect_incoming_data(self, data):
-        # TODO: Enforce some maximum data length.
-        self.__data.append(data)
-
-    def found_terminator(self):
-        self.set_terminator(None)
-        data = ''.join(self.__data)
-        self.__data = []
-        self.__process_next_instruction(data)
-
-class CoroutineSocketServer(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 CoroutineSocketClient(_AsyncChatCoroutineBridge):
-    def __init__(self, addr, coroutineFactory):
-        coroutine = coroutineFactory(addr)
-        _AsyncChatCoroutineBridge.__init__(self, coroutine)
-        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
-        self.connect(addr)
-
-# Instructions that coroutines yield.
-
-def to_receive(terminator = None, bytes = None):
-    return {'op': 'receive',
-            'terminator': terminator,
-            'bytes': bytes}
-
-def to_send(content):
-    return {'op': 'send',
-            'content': content}