Mercurial > hg > Members > tatsuki > Alice
annotate src/alice/codesegment/InputDataSegment.java @ 212:b5daccf36104 working
add Receiver state pattern
author | one |
---|---|
date | Wed, 27 Mar 2013 17:30:52 +0900 |
parents | 96110f25adcc |
children | d50cddf64396 |
rev | line source |
---|---|
3 | 1 package alice.codesegment; |
2 | |
3 import java.util.concurrent.atomic.AtomicInteger; | |
4 | |
202 | 5 import alice.datasegment.Command; |
32
2bfb796b0fa1
change method to create DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
6 import alice.datasegment.CommandType; |
3 | 7 import alice.datasegment.DataSegment; |
212 | 8 import alice.datasegment.ReceiveRemoteData; |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
9 import alice.datasegment.Receiver; |
3 | 10 |
57 | 11 /** |
12 * InputDataSegment Manager | |
13 * keep tracking unbound/bound count | |
14 * @author kazz | |
15 * | |
16 */ | |
3 | 17 public class InputDataSegment { |
18 | |
119 | 19 public CodeSegment cs; |
57 | 20 private AtomicInteger count = new AtomicInteger(1); // 1 for no input data segments |
21 private AtomicInteger keyCount = new AtomicInteger(0); // number of DataSegments | |
3 | 22 |
23 public InputDataSegment(CodeSegment cs) { | |
24 this.cs = cs; | |
25 } | |
26 | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
27 public void peek(Receiver receiver, String managerKey, String key) { |
18 | 28 peek(receiver, managerKey, key, 0); |
3 | 29 } |
30 | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
31 public void peek(Receiver receiver, String managerKey, String key, int index) { |
18 | 32 DataSegment.get(managerKey).peek(receiver, key, index, cs); |
3 | 33 } |
34 | |
65 | 35 public void peek(Receiver receiver, String key) { |
36 peek(receiver, key, 0); | |
37 } | |
38 | |
39 public void peek(Receiver receiver, String key, int index) { | |
40 DataSegment.getLocal().peek(receiver, key, index, cs); | |
41 } | |
42 | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
43 public void take(Receiver receiver, String managerKey, String key) { |
18 | 44 take(receiver, managerKey, key, 0); |
3 | 45 } |
46 | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
47 public void take(Receiver receiver, String managerKey, String key, int index) { |
18 | 48 DataSegment.get(managerKey).take(receiver, key, index, cs); |
3 | 49 } |
7 | 50 |
65 | 51 public void take(Receiver receiver, String key) { |
52 take(receiver, key, 0); | |
53 } | |
54 | |
55 public void take(Receiver receiver, String key, int index) { | |
56 DataSegment.getLocal().take(receiver, key, index, cs); | |
57 } | |
58 | |
209 | 59 public void reply(Receiver receiver, Command reply) { |
60 receiver.index = reply.index; | |
61 receiver.from = reply.reverseKey; | |
212 | 62 if (!reply.reverseKey.equals("local")) { |
63 receiver.setData(new ReceiveRemoteData(reply.val)); | |
64 } else { | |
65 receiver.setData(new ReceiveLocalData(reply.obj)); | |
66 } | |
19 | 67 receive(); |
68 } | |
69 | |
70 public void regist() { | |
71 count.getAndIncrement(); | |
72 keyCount.getAndIncrement(); | |
9 | 73 } |
74 | |
19 | 75 public void setKey() { |
76 if (keyCount.decrementAndGet() == 0) { | |
77 receive(); | |
78 } | |
79 } | |
80 | |
81 public void receive() { | |
8 | 82 if (count.decrementAndGet() == 0) { |
57 | 83 CodeSegmentManager.submit(cs); |
8 | 84 } |
85 } | |
32
2bfb796b0fa1
change method to create DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
86 |
57 | 87 /** |
88 * InputDataSegment factory | |
89 * @param type PEEK or TAKE | |
90 * @return Receiver of DataSegment reply | |
91 */ | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
92 public Receiver create(CommandType type) { |
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
32
diff
changeset
|
93 return new Receiver(this, type); |
32
2bfb796b0fa1
change method to create DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
28
diff
changeset
|
94 } |
3 | 95 } |