jdk/test/java/nio/channels/DatagramChannel/Connect.java
changeset 5788 50d4ad1a780f
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
5787:a0af7b8e80ed 5788:50d4ad1a780f
    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 Actor(), new Reactor());
    45         Reactor r = new Reactor();
       
    46         Actor a = new Actor(r.port());
       
    47         invoke(a, r);
    46     }
    48     }
    47 
    49 
    48     static void invoke(Sprintable reader, Sprintable writer) throws Exception {
    50     static void invoke(Sprintable reader, Sprintable writer) throws Exception {
    49 
    51 
    50         Thread writerThread = new Thread(writer);
    52         Thread writerThread = new Thread(writer);
    51         writerThread.start();
    53         writerThread.start();
    52         while (!writer.ready())
       
    53             Thread.sleep(50);
       
    54 
    54 
    55         Thread readerThread = new Thread(reader);
    55         Thread readerThread = new Thread(reader);
    56         readerThread.start();
    56         readerThread.start();
    57 
    57 
    58         writerThread.join();
    58         writerThread.join();
    62         writer.throwException();
    62         writer.throwException();
    63     }
    63     }
    64 
    64 
    65     public interface Sprintable extends Runnable {
    65     public interface Sprintable extends Runnable {
    66         public void throwException() throws Exception;
    66         public void throwException() throws Exception;
    67         public boolean ready();
       
    68     }
    67     }
    69 
    68 
    70     public static class Actor implements Sprintable {
    69     public static class Actor implements Sprintable {
       
    70         final int port;
    71         Exception e = null;
    71         Exception e = null;
       
    72 
       
    73         Actor(int port) {
       
    74             this.port = port;
       
    75         }
    72 
    76 
    73         public void throwException() throws Exception {
    77         public void throwException() throws Exception {
    74             if (e != null)
    78             if (e != null)
    75                 throw e;
    79                 throw e;
    76         }
    80         }
    77 
    81 
    78         private volatile boolean ready = false;
       
    79 
       
    80         public boolean ready() {
       
    81             return ready;
       
    82         }
       
    83 
       
    84         public void run() {
    82         public void run() {
    85             try {
    83             try {
    86                 DatagramChannel dc = DatagramChannel.open();
    84                 DatagramChannel dc = DatagramChannel.open();
    87                 ready = true;
       
    88 
    85 
    89                 // Send a message
    86                 // Send a message
    90                 ByteBuffer bb = ByteBuffer.allocateDirect(256);
    87                 ByteBuffer bb = ByteBuffer.allocateDirect(256);
    91                 bb.put("hello".getBytes());
    88                 bb.put("hello".getBytes());
    92                 bb.flip();
    89                 bb.flip();
    93                 InetAddress address = InetAddress.getLocalHost();
    90                 InetAddress address = InetAddress.getLocalHost();
    94                 InetSocketAddress isa = new InetSocketAddress(address, 8888);
    91                 InetSocketAddress isa = new InetSocketAddress(address, port);
    95                 dc.connect(isa);
    92                 dc.connect(isa);
    96                 dc.write(bb);
    93                 dc.write(bb);
    97 
    94 
    98                 // Try to send to some other address
    95                 // Try to send to some other address
    99                 address = InetAddress.getLocalHost();
    96                 address = InetAddress.getLocalHost();
   121             }
   118             }
   122         }
   119         }
   123     }
   120     }
   124 
   121 
   125     public static class Reactor implements Sprintable {
   122     public static class Reactor implements Sprintable {
       
   123         final DatagramChannel dc;
   126         Exception e = null;
   124         Exception e = null;
       
   125 
       
   126         Reactor() throws IOException {
       
   127             dc = DatagramChannel.open().bind(new InetSocketAddress(0));
       
   128         }
       
   129 
       
   130         int port() {
       
   131             return dc.socket().getLocalPort();
       
   132         }
   127 
   133 
   128         public void throwException() throws Exception {
   134         public void throwException() throws Exception {
   129             if (e != null)
   135             if (e != null)
   130                 throw e;
   136                 throw e;
   131         }
   137         }
   132 
   138 
   133         private volatile boolean ready = false;
       
   134 
       
   135         public boolean ready() {
       
   136             return ready;
       
   137         }
       
   138 
       
   139         public void run() {
   139         public void run() {
   140             try {
   140             try {
   141                 // Listen for a message
   141                 // Listen for a message
   142                 DatagramChannel dc = DatagramChannel.open();
       
   143                 dc.socket().bind(new InetSocketAddress(8888));
       
   144                 ByteBuffer bb = ByteBuffer.allocateDirect(100);
   142                 ByteBuffer bb = ByteBuffer.allocateDirect(100);
   145                 ready = true;
       
   146                 SocketAddress sa = dc.receive(bb);
   143                 SocketAddress sa = dc.receive(bb);
   147                 bb.flip();
   144                 bb.flip();
   148                 CharBuffer cb = Charset.forName("US-ASCII").
   145                 CharBuffer cb = Charset.forName("US-ASCII").
   149                 newDecoder().decode(bb);
   146                 newDecoder().decode(bb);
   150                 log.println("From Actor: "+sa+ " said " +cb);
   147                 log.println("From Actor: "+sa+ " said " +cb);