#!/usr/bin/env python
"""
ProcessorChannel.py
Andrew Francis
October 13th, 2006

Doctor Blind - Emily Haines

ProcessorChannel is a wrapper for the stackless channel.
Two differences

1. Name
2. Methods to make attaching and detaching the channel from
   its tasklets easier.

"""

import stackless

class ProcessorChannel(object):
    def __init__(self, name = None):
        self.name = name
        self.channel = stackless.channel()
        self.state = None
        self.tasklets = []
        
        
    def __repr__(self):
        return self.name 
   
   
    """
    get the underlying channel's balance
    """
    def getBalance(self):
        return self.channel.balance
    
    
    """
    
    """
    def attach(self):
        x, y, (balance, flags, waste) = self.channel.__reduce__()
        self.channel.__setstate__((balance, flags, self.tasklets))
        return 
    
    
    def detach(self):
        x, y, (balance, flags, self.tasklets) = self.channel.__reduce__()
        self.channel.__setstate__((balance, flags, []))
        return 
    
    
    def receive(self):
        return self.channel.receive()
    
    
    def send(self, message):
        self.channel.send(message)
        
        
    balance = property(getBalance)
    
    