changeset 11:8574ff006a28

instructions are now objects. renamed async chat coroutine bridge to coroutine dispatcher.
author Atul Varma <varmaa@toolness.com>
date Sat, 18 Apr 2009 10:12:18 -0700
parents f51acb369f03
children 1ffa6554ff3a
files cosocket.py
diffstat 1 files changed, 25 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/cosocket.py	Fri Apr 17 17:31:07 2009 -0700
+++ b/cosocket.py	Sat Apr 18 10:12:18 2009 -0700
@@ -2,7 +2,7 @@
 import asyncore
 import asynchat
 
-class _AsyncChatCoroutineBridge(asynchat.async_chat):
+class _AsyncChatCoroutineDispatcher(asynchat.async_chat):
     def __init__(self, coroutine, conn = None):
         asynchat.async_chat.__init__(self, conn)
         self.set_terminator(None)
@@ -15,19 +15,9 @@
         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()
+        else:
+            instruction.execute(self)
 
     def handle_connect(self):
         self.__process_next_instruction()
@@ -65,22 +55,34 @@
     def handle_accept(self):
         conn, addr = self.accept()
         coroutine = self.__coroutineFactory(addr)
-        _AsyncChatCoroutineBridge(coroutine, conn)
+        _AsyncChatCoroutineDispatcher(coroutine, conn)
 
-class CoroutineSocketClient(_AsyncChatCoroutineBridge):
+class CoroutineSocketClient(_AsyncChatCoroutineDispatcher):
     def __init__(self, addr, coroutineFactory):
         coroutine = coroutineFactory(addr)
-        _AsyncChatCoroutineBridge.__init__(self, coroutine)
+        _AsyncChatCoroutineDispatcher.__init__(self, coroutine)
         self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
         self.connect(addr)
 
 # Instructions that coroutines yield.
 
-def until_received(terminator = None, bytes = None):
-    return {'op': 'receive',
-            'terminator': terminator,
-            'bytes': bytes}
+class until_received(object):
+    def __init__(self, terminator = None, bytes = None):
+        if terminator:
+            self._terminator = terminator
+        elif bytes:
+            self._terminator = bytes
+        else:
+            raise ValueError()
 
-def until_sent(content):
-    return {'op': 'send',
-            'content': content}
+    def execute(self, dispatcher):
+        dispatcher.set_terminator(self._terminator)
+
+class until_sent(object):
+    def __init__(self, content):
+        if not content:
+            raise ValueError(content)
+        self.content = content
+
+    def execute(self, dispatcher):
+        dispatcher.push(self.content)