blob: d9b9199dfb454c318fff84c8d71b04132275afc2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @summary Test DatagramChannel's send and receive methods
26 * @author Mike McCloskey
27 */
28
29import java.io.*;
30import java.net.*;
31import java.nio.*;
32import java.nio.channels.*;
33import java.nio.charset.*;
34
35
36public class Connect {
37
38 static PrintStream log = System.err;
39
40 public static void main(String[] args) throws Exception {
41 test();
42 }
43
44 static void test() throws Exception {
45 invoke(new Actor(), new Reactor());
46 }
47
48 static void invoke(Sprintable reader, Sprintable writer) throws Exception {
49
50 Thread writerThread = new Thread(writer);
51 writerThread.start();
52 while (!writer.ready())
53 Thread.sleep(50);
54
55 Thread readerThread = new Thread(reader);
56 readerThread.start();
57
58 writerThread.join();
59 readerThread.join();
60
61 reader.throwException();
62 writer.throwException();
63 }
64
65 public interface Sprintable extends Runnable {
66 public void throwException() throws Exception;
67 public boolean ready();
68 }
69
70 public static class Actor implements Sprintable {
71 Exception e = null;
72
73 public void throwException() throws Exception {
74 if (e != null)
75 throw e;
76 }
77
78 private volatile boolean ready = false;
79
80 public boolean ready() {
81 return ready;
82 }
83
84 public void run() {
85 try {
86 DatagramChannel dc = DatagramChannel.open();
87 ready = true;
88
89 // Send a message
90 ByteBuffer bb = ByteBuffer.allocateDirect(256);
91 bb.put("hello".getBytes());
92 bb.flip();
93 InetAddress address = InetAddress.getLocalHost();
94 InetSocketAddress isa = new InetSocketAddress(address, 8888);
95 dc.connect(isa);
96 dc.write(bb);
97
98 // Try to send to some other address
99 address = InetAddress.getLocalHost();
100 InetSocketAddress bogus = new InetSocketAddress(address, 3333);
101 try {
102 dc.send(bb, bogus);
103 throw new RuntimeException("Allowed bogus send while connected");
104 } catch (IllegalArgumentException iae) {
105 // Correct behavior
106 }
107
108 // Read a reply
109 bb.flip();
110 dc.read(bb);
111 bb.flip();
112 CharBuffer cb = Charset.forName("US-ASCII").
113 newDecoder().decode(bb);
114 log.println("From Reactor: "+isa+ " said " +cb);
115
116 // Clean up
117 dc.disconnect();
118 dc.close();
119 } catch (Exception ex) {
120 e = ex;
121 }
122 }
123 }
124
125 public static class Reactor implements Sprintable {
126 Exception e = null;
127
128 public void throwException() throws Exception {
129 if (e != null)
130 throw e;
131 }
132
133 private volatile boolean ready = false;
134
135 public boolean ready() {
136 return ready;
137 }
138
139 public void run() {
140 try {
141 // Listen for a message
142 DatagramChannel dc = DatagramChannel.open();
143 dc.socket().bind(new InetSocketAddress(8888));
144 ByteBuffer bb = ByteBuffer.allocateDirect(100);
145 ready = true;
146 SocketAddress sa = dc.receive(bb);
147 bb.flip();
148 CharBuffer cb = Charset.forName("US-ASCII").
149 newDecoder().decode(bb);
150 log.println("From Actor: "+sa+ " said " +cb);
151
152 // Reply to sender
153 dc.connect(sa);
154 bb.flip();
155 dc.write(bb);
156
157 // Clean up
158 dc.disconnect();
159 dc.close();
160 } catch (Exception ex) {
161 e = ex;
162 }
163 }
164 }
165}