11
|
1 module dreactor.core.Task;
|
|
2
|
|
3 import tango.core.Thread;
|
|
4 import dreactor.core.Vat;
|
|
5 import dreactor.protocol.Protocol;
|
|
6 import dreactor.protocol.Dispatcher;
|
|
7
|
|
8 alias CircularSeq!(Message) Mailbox;
|
|
9
|
|
10 class Task
|
|
11 {
|
|
12 private
|
|
13 Fiber fiber;
|
|
14 Mailbox mailbox;
|
|
15 int id;
|
|
16 Vat vat;
|
|
17 dispatcher[Conduit] dispatchers;
|
|
18
|
|
19 public
|
|
20 this()
|
|
21 {
|
|
22 fiber = new Fiber(&run);
|
|
23 mailbox = new Mailbox;
|
|
24 }
|
|
25
|
|
26 void setId(int i)
|
|
27 {
|
|
28 id = i;
|
|
29 }
|
|
30
|
|
31 Mailbox getMailbox()
|
|
32 {
|
|
33 return mailbox;
|
|
34 }
|
|
35
|
|
36 void setVat(Vat v)
|
|
37 {
|
|
38 vat = v;
|
|
39 }
|
|
40
|
|
41 abstract void run();
|
|
42
|
|
43 protected
|
|
44
|
|
45 /***************************************************************************
|
|
46 receive
|
|
47 User-called function to get the next pending message in the mailbox.
|
|
48 If there are no pending messages, this will yield control back to
|
|
49 the scheduler/vat.
|
|
50 ***************************************************************************/
|
|
51
|
|
52 Message receive()
|
|
53 {
|
|
54 Message m = mailbox.head();
|
|
55 mailbox.removeHead();
|
|
56 return m;
|
|
57 }
|
|
58
|
|
59 int getId() { return id;}
|
|
60
|
|
61 /**************************************************************************
|
|
62
|
|
63 send
|
|
64 User-called function to send data to the counterpart at the other
|
|
65 end of the connection. This sets up a dispatcher to send
|
|
66 data as the conduit becomes free.
|
|
67
|
|
68 **************************************************************************/
|
|
69 int send(char[] outbuf, Conduit c)
|
|
70 {
|
|
71 Dispatcher dis;
|
|
72 if ( ! (dis = (c in dispatchers)))
|
|
73 dis = new Dispatcher(c);
|
|
74
|
|
75 if (dis.appendOutBuffer(outbuf))
|
|
76 {
|
|
77 if (!vat.addConnection(dis))
|
|
78 {
|
|
79 log.error("unable to register mgr");
|
|
80 }
|
|
81 }
|
|
82 return 0;
|
|
83 }
|
|
84 }
|