0
|
1 package suikwasha.distributedalgorithm.link;
|
|
2
|
|
3 import java.util.concurrent.LinkedBlockingQueue;
|
|
4
|
|
5 import suikwasha.distributedalgorithm.framework.Link;
|
|
6 import suikwasha.distributedalgorithm.framework.LinkBuilder;
|
|
7 import suikwasha.distributedalgorithm.framework.Message;
|
|
8 import suikwasha.distributedalgorithm.framework.Port;
|
|
9
|
|
10 public class ReliableLinkBuilder implements LinkBuilder
|
|
11 {
|
|
12 public Link build()
|
|
13 {
|
|
14 return new LinkImpl();
|
|
15 }
|
|
16
|
|
17 private static class LinkImpl implements Link
|
|
18 {
|
|
19 private final Port port1,port2;
|
|
20
|
|
21 public LinkImpl()
|
|
22 {
|
|
23 LinkedBlockingQueue<Message> queue1 = new LinkedBlockingQueue<Message>();
|
|
24 LinkedBlockingQueue<Message> queue2 = new LinkedBlockingQueue<Message>();
|
|
25
|
|
26 port1 = new PortImpl(queue1,queue2);
|
|
27 port2 = new PortImpl(queue2,queue1);
|
|
28 }
|
|
29
|
|
30 public Port getPort1()
|
|
31 {
|
|
32 return port1;
|
|
33 }
|
|
34
|
|
35 public Port getPort2()
|
|
36 {
|
|
37 return port2;
|
|
38 }
|
|
39 }
|
|
40
|
|
41 private static class PortImpl implements Port
|
|
42 {
|
|
43 private final LinkedBlockingQueue<Message> in;
|
|
44 private final LinkedBlockingQueue<Message> out;
|
|
45
|
|
46 public PortImpl(LinkedBlockingQueue<Message> _in,LinkedBlockingQueue<Message> _out)
|
|
47 {
|
|
48 in = _in;
|
|
49 out = _out;
|
|
50 }
|
|
51
|
|
52 public void send(Message _mes)
|
|
53 {
|
|
54 out.add(_mes);
|
|
55 }
|
|
56
|
|
57 public Message blockingReceive() throws InterruptedException
|
|
58 {
|
|
59 return in.take();
|
|
60 }
|
|
61
|
|
62 public Message tryReceive()
|
|
63 {
|
|
64 return in.poll();
|
|
65 }
|
|
66 }
|
|
67 }
|