annotate src/alice/datasegment/DataSegmentKey.java @ 14:e3f1b21718b0

implements RemoteDataSegment
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Sun, 15 Jan 2012 00:56:25 +0900
parents 30f97d776a3e
children bb075e103cd3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
1 package alice.datasegment;
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
2
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
3 import java.util.ArrayList;
10
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
4 import java.util.LinkedList;
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
5 import java.util.concurrent.LinkedBlockingQueue;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
6 import java.util.concurrent.atomic.AtomicInteger;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
7
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
8 import alice.datasegment.Command;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
9
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
10 public class DataSegmentKey {
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
11
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
12 private LinkedBlockingQueue<Command> cmdQueue = new LinkedBlockingQueue<Command>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
13 private ArrayList<DataSegmentValue> dataList = new ArrayList<DataSegmentValue>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
14 private ArrayList<Command> waitList = new ArrayList<Command>();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
15 private AtomicInteger tailIndex = new AtomicInteger(1);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
16 private Runnable keyThread;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
17
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
18 public DataSegmentKey() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
19
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
20 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
21
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
22 public void addCommand(Command cmd) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
23 cmdQueue.add(cmd);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
24 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
25
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
26 public void runKeyThread() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
27 keyThread = new Runnable() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
28 @Override
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
29 public void run() {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
30 while (true) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
31 try {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
32 Command cmd = cmdQueue.take();
13
30f97d776a3e implements Alice daemon
one
parents: 12
diff changeset
33 switch (cmd.type) {
5
80375ae09a1f add update api
one
parents: 3
diff changeset
34 case UPDATE:
80375ae09a1f add update api
one
parents: 3
diff changeset
35 if (dataList.size() != 0) {
80375ae09a1f add update api
one
parents: 3
diff changeset
36 dataList.remove(0);
80375ae09a1f add update api
one
parents: 3
diff changeset
37 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
38 case PUT:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
39 int index = tailIndex.getAndIncrement();
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
40 DataSegmentValue dsv = new DataSegmentValue(index, cmd.val);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
41 dataList.add(dsv);
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
42 // run waiting peek and take
10
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
43 LinkedList<Command> removeList = new LinkedList<Command>();
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
44 for (Command waitCmd : waitList) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
45 if (waitCmd.index < index) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
46 waitCmd.replyQueue.put(new Command(CommandType.REPLY, null, null, cmd.val, index, waitCmd.seq, null, null));
10
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
47 removeList.add(waitCmd);
13
30f97d776a3e implements Alice daemon
one
parents: 12
diff changeset
48 if (waitCmd.type == CommandType.TAKE) { // delete data, if it run take cmd.
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
49 dataList.remove(dsv);
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
50 break;
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
51 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
52 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
53 }
10
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
54 for (Command rmCmd : removeList) {
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
55 waitList.remove(rmCmd);
5f7cce38b25c bug fix for wait command
one
parents: 7
diff changeset
56 }
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
57 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
58 case PEEK:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
59 if (cmd.index >= tailIndex.get()) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
60 waitList.add(cmd);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
61 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
62 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
63 for (DataSegmentValue data : dataList) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
64 if (data.index > cmd.index) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
65 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null));
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
66 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
67 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
68 }
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
69 waitList.add(cmd);
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
70 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
71 case TAKE:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
72 if (cmd.index >= tailIndex.get()) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
73 waitList.add(cmd);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
74 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
75 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
76 for (DataSegmentValue data : dataList) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
77 if (data.index > cmd.index) {
14
e3f1b21718b0 implements RemoteDataSegment
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents: 13
diff changeset
78 cmd.replyQueue.put(new Command(CommandType.REPLY, null, null, data.val, data.index, cmd.seq, null, null));
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
79 dataList.remove(data);
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
80 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
81 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
82 }
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
83 waitList.add(cmd);
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
84 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
85 case REMOVE:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
86 // TODO: implements later
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
87 break;
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
88 default:
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
89 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
90 } catch (InterruptedException e) {
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
91 e.printStackTrace();
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
92 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
93 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
94 }
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
95 };
6
c78a1cc2cd8f implements Reply
one
parents: 5
diff changeset
96 new Thread(keyThread).start();
3
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
97 };
91057e15065f add DataSegment API and CodeSegment
one
parents: 2
diff changeset
98
2
f71eabb1df2a create outline of DataSegment model
one
parents:
diff changeset
99 }