1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
*
*
* An "echo" service designed to be used with inetd. It can be configured in
* inetd.conf to be used by any of the following types of services :-
*
* stream tcp nowait
* stream tcp6 nowait
* stream tcp wait
* stream tcp6 wait
* dgram udp wait
* dgram udp6 wait
*
* If configured as a "tcp nowait" service then inetd will launch a
* VM to run the EchoService each time that a client connects to
* the TCP port. The EchoService simply echos any messages it
* receives from the client and shuts if the client closes the
* connection.
*
* If configured as a "tcp wait" service then inetd will launch a VM
* to run the EchoService when a client connects to the port. When
* launched the EchoService takes over the listener socket. It
* terminates when all clients have disconnected and the service
* is idle for a few seconds.
*
* If configured as a "udp wait" service then a VM will be launched for
* each UDP packet to the configured port. System.inheritedChannel()
* will return a DatagramChannel. The echo service here will terminate after
* echoing the UDP packet back to the client.
*
* The service closes the inherited network channel when complete. To
* facilate testing that the channel is closed the "tcp nowait" service
* can close the connection after a given number of bytes.
*/
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
public class EchoService {
private static void doIt(SocketChannel sc, int closeAfter, int delay) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(1024);
int total = 0;
for (;;) {
bb.clear();
int n = sc.read(bb);
if (n < 0) {
break;
}
total += n;
// echo
bb.flip();
sc.write(bb);
// close after X bytes?
if (closeAfter > 0 && total >= closeAfter) {
break;
}
}
sc.close();
if (delay > 0) {
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException x) { }
}
}
private static void doIt(DatagramChannel dc) throws IOException {
ByteBuffer bb = ByteBuffer.allocate(1024);
SocketAddress sa = dc.receive(bb);
bb.flip();
dc.send(bb, sa);
dc.close();
}
// A worker thread to service a single connection
// The class maintains a count of the number of worker threads so
// can the service can terminate then all clients disconnect.
static class Worker implements Runnable {
private static int count = 0;
private static Object lock = new Object();
public static int count() {
synchronized (lock) {
return count;
}
}
private SocketChannel sc;
Worker(SocketChannel sc) {
this.sc = sc;
synchronized (lock) {
count++;
}
}
public void run() {
try {
doIt(sc, -1, -1);
} catch (IOException x) {
} finally {
synchronized (lock) {
count--;
}
}
}
}
public static void main(String args[]) throws IOException {
Channel c = System.inheritedChannel();
if (c == null) {
return;
}
// tcp nowait
if (c instanceof SocketChannel) {
int closeAfter = 0;
int delay = 0;
if (args.length > 0) {
closeAfter = Integer.parseInt(args[0]);
}
if (args.length > 1) {
delay = Integer.parseInt(args[1]);
}
doIt((SocketChannel)c, closeAfter, delay);
}
// tcp wait - in this case we take over the listener socket
// In this test case we create a thread to service each connection
// and terminate after all clients are gone.
//
if (c instanceof ServerSocketChannel) {
ServerSocketChannel ssc = (ServerSocketChannel)c;
ssc.configureBlocking(false);
Selector sel = ssc.provider().openSelector();
SelectionKey sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
SocketChannel sc;
int count = 0;
for (;;) {
sel.select(5000);
if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) {
Worker w = new Worker(sc);
(new Thread(w)).start();
} else {
// if all clients have disconnected then we die as well.
if (Worker.count() == 0) {
break;
}
}
}
ssc.close();
}
// udp wait
if (c instanceof DatagramChannel) {
doIt((DatagramChannel)c);
}
// linger?
if (args.length > 0) {
int delay = Integer.parseInt(args[0]);
try {
Thread.currentThread().sleep(delay);
} catch (InterruptedException x) { }
}
}
}
|