Mercurial > projects > dreactor
diff 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 diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dreactor/core/Task.d Tue Aug 12 16:59:56 2008 -0400 @@ -0,0 +1,84 @@ +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; + } +}