40 public static void main(String[] args) throws Exception { |
40 public static void main(String[] args) throws Exception { |
41 test(); |
41 test(); |
42 } |
42 } |
43 |
43 |
44 static void test() throws Exception { |
44 static void test() throws Exception { |
45 invoke(new ClassicReader(), new ClassicWriter()); |
45 ClassicReader classicReader; |
|
46 NioReader nioReader; |
|
47 |
|
48 classicReader = new ClassicReader(); |
|
49 invoke(classicReader, new ClassicWriter(classicReader.port())); |
46 log.println("Classic RW: OK"); |
50 log.println("Classic RW: OK"); |
47 |
51 |
48 invoke(new ClassicReader(), new NioWriter()); |
52 classicReader = new ClassicReader(); |
|
53 invoke(classicReader, new NioWriter(classicReader.port())); |
49 log.println("Classic R, Nio W: OK"); |
54 log.println("Classic R, Nio W: OK"); |
50 |
55 |
51 invoke(new NioReader(), new ClassicWriter()); |
56 nioReader = new NioReader(); |
|
57 invoke(nioReader, new ClassicWriter(nioReader.port())); |
52 log.println("Classic W, Nio R: OK"); |
58 log.println("Classic W, Nio R: OK"); |
53 |
59 |
54 invoke(new NioReader(), new NioWriter()); |
60 nioReader = new NioReader(); |
|
61 invoke(nioReader, new NioWriter(nioReader.port())); |
55 log.println("Nio RW: OK"); |
62 log.println("Nio RW: OK"); |
56 } |
63 } |
57 |
64 |
58 static void invoke(Sprintable reader, Sprintable writer) throws Exception { |
65 static void invoke(Sprintable reader, Sprintable writer) throws Exception { |
59 Thread readerThread = new Thread(reader); |
66 Thread readerThread = new Thread(reader); |
73 public interface Sprintable extends Runnable { |
80 public interface Sprintable extends Runnable { |
74 public void throwException() throws Exception; |
81 public void throwException() throws Exception; |
75 } |
82 } |
76 |
83 |
77 public static class ClassicWriter implements Sprintable { |
84 public static class ClassicWriter implements Sprintable { |
78 Exception e = null; |
85 final int port; |
|
86 Exception e = null; |
|
87 |
|
88 ClassicWriter(int port) { |
|
89 this.port = port; |
|
90 } |
79 |
91 |
80 public void throwException() throws Exception { |
92 public void throwException() throws Exception { |
81 if (e != null) |
93 if (e != null) |
82 throw e; |
94 throw e; |
83 } |
95 } |
87 DatagramSocket ds = new DatagramSocket(); |
99 DatagramSocket ds = new DatagramSocket(); |
88 String dataString = "hello"; |
100 String dataString = "hello"; |
89 byte[] data = dataString.getBytes(); |
101 byte[] data = dataString.getBytes(); |
90 InetAddress address = InetAddress.getLocalHost(); |
102 InetAddress address = InetAddress.getLocalHost(); |
91 DatagramPacket dp = new DatagramPacket(data, data.length, |
103 DatagramPacket dp = new DatagramPacket(data, data.length, |
92 address, 8888); |
104 address, port); |
93 ds.send(dp); |
105 ds.send(dp); |
94 Thread.sleep(50); |
106 Thread.sleep(50); |
95 ds.send(dp); |
107 ds.send(dp); |
96 } catch (Exception ex) { |
108 } catch (Exception ex) { |
97 e = ex; |
109 e = ex; |
98 } |
110 } |
99 } |
111 } |
100 } |
112 } |
101 |
113 |
102 public static class NioWriter implements Sprintable { |
114 public static class NioWriter implements Sprintable { |
103 Exception e = null; |
115 final int port; |
|
116 Exception e = null; |
|
117 |
|
118 NioWriter(int port) { |
|
119 this.port = port; |
|
120 } |
104 |
121 |
105 public void throwException() throws Exception { |
122 public void throwException() throws Exception { |
106 if (e != null) |
123 if (e != null) |
107 throw e; |
124 throw e; |
108 } |
125 } |
112 DatagramChannel dc = DatagramChannel.open(); |
129 DatagramChannel dc = DatagramChannel.open(); |
113 ByteBuffer bb = ByteBuffer.allocateDirect(256); |
130 ByteBuffer bb = ByteBuffer.allocateDirect(256); |
114 bb.put("hello".getBytes()); |
131 bb.put("hello".getBytes()); |
115 bb.flip(); |
132 bb.flip(); |
116 InetAddress address = InetAddress.getLocalHost(); |
133 InetAddress address = InetAddress.getLocalHost(); |
117 InetSocketAddress isa = new InetSocketAddress(address, 8888); |
134 InetSocketAddress isa = new InetSocketAddress(address, port); |
118 dc.send(bb, isa); |
135 dc.send(bb, isa); |
119 Thread.sleep(50); |
136 Thread.sleep(50); |
120 dc.send(bb, isa); |
137 dc.send(bb, isa); |
121 } catch (Exception ex) { |
138 } catch (Exception ex) { |
122 e = ex; |
139 e = ex; |
123 } |
140 } |
124 } |
141 } |
125 } |
142 } |
126 |
143 |
127 public static class ClassicReader implements Sprintable { |
144 public static class ClassicReader implements Sprintable { |
128 Exception e = null; |
145 final DatagramSocket ds; |
|
146 Exception e = null; |
|
147 |
|
148 ClassicReader() throws IOException { |
|
149 this.ds = new DatagramSocket(); |
|
150 } |
|
151 |
|
152 int port() { |
|
153 return ds.getLocalPort(); |
|
154 } |
129 |
155 |
130 public void throwException() throws Exception { |
156 public void throwException() throws Exception { |
131 if (e != null) |
157 if (e != null) |
132 throw e; |
158 throw e; |
133 } |
159 } |
134 |
160 |
135 public void run() { |
161 public void run() { |
136 try { |
162 try { |
137 byte[] buf = new byte[256]; |
163 byte[] buf = new byte[256]; |
138 DatagramPacket dp = new DatagramPacket(buf, buf.length); |
164 DatagramPacket dp = new DatagramPacket(buf, buf.length); |
139 DatagramSocket ds = new DatagramSocket(8888); |
|
140 ds.receive(dp); |
165 ds.receive(dp); |
141 String received = new String(dp.getData()); |
166 String received = new String(dp.getData()); |
142 log.println(received); |
167 log.println(received); |
143 ds.close(); |
168 ds.close(); |
144 } catch (Exception ex) { |
169 } catch (Exception ex) { |
146 } |
171 } |
147 } |
172 } |
148 } |
173 } |
149 |
174 |
150 public static class NioReader implements Sprintable { |
175 public static class NioReader implements Sprintable { |
151 Exception e = null; |
176 final DatagramChannel dc; |
152 |
177 Exception e = null; |
153 public void throwException() throws Exception { |
178 |
154 if (e != null) |
179 NioReader() throws IOException { |
155 throw e; |
180 this.dc = DatagramChannel.open().bind(new InetSocketAddress(0)); |
156 } |
181 } |
157 |
182 |
158 public void run() { |
183 int port() { |
159 try { |
184 return dc.socket().getLocalPort(); |
160 DatagramChannel dc = DatagramChannel.open(); |
185 } |
161 dc.socket().bind(new InetSocketAddress(8888)); |
186 |
|
187 public void throwException() throws Exception { |
|
188 if (e != null) |
|
189 throw e; |
|
190 } |
|
191 |
|
192 public void run() { |
|
193 try { |
162 ByteBuffer bb = ByteBuffer.allocateDirect(100); |
194 ByteBuffer bb = ByteBuffer.allocateDirect(100); |
163 SocketAddress sa = dc.receive(bb); |
195 SocketAddress sa = dc.receive(bb); |
164 bb.flip(); |
196 bb.flip(); |
165 CharBuffer cb = Charset.forName("US-ASCII"). |
197 CharBuffer cb = Charset.forName("US-ASCII"). |
166 newDecoder().decode(bb); |
198 newDecoder().decode(bb); |