view dreactor/core/Task.d @ 11:5836613d16ac

reorg! reorg!
author rick@minifunk
date Tue, 12 Aug 2008 16:59:56 -0400
parents
children d6a3cfe7c3de
line wrap: on
line source

module dreactor.core.Task;

import tango.core.Thread;
import dreactor.core.Vat;
import dreactor.protocol.Protocol;
import dreactor.protocol.Dispatcher;

alias CircularSeq!(Message) Mailbox;

class Task
{
private
    Fiber fiber;
    Mailbox mailbox;
    int id;
    Vat vat;
    dispatcher[Conduit] dispatchers;
    
public
    this() 
    {
        fiber = new Fiber(&run);
        mailbox = new Mailbox;
    }

    void setId(int i)
    {
        id = i;
    }

    Mailbox getMailbox() 
    { 
        return mailbox; 
    }

    void setVat(Vat v)
    {
        vat = v;
    }

    abstract void run();

protected

    /***************************************************************************
        receive
        User-called function to get the next pending message in the mailbox. 
        If there are no pending messages, this will yield control back to 
        the scheduler/vat. 
    ***************************************************************************/

    Message receive() 
    {
        Message m = mailbox.head();
        mailbox.removeHead();
        return m;
    }

    int getId() { return id;}
    
    /**************************************************************************
    
        send
        User-called function to send data to the counterpart at the other
        end of the connection. This sets up a dispatcher to send
        data as the conduit becomes free. 

    **************************************************************************/
    int send(char[] outbuf, Conduit c)
    {
        Dispatcher dis;
        if ( ! (dis = (c in dispatchers)))
            dis = new Dispatcher(c);

        if (dis.appendOutBuffer(outbuf))
        {
            if (!vat.addConnection(dis))
            {
                log.error("unable to register mgr");
            }
        }
        return 0;
    } 
}