/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*/
public class EchoService {
int total = 0;
for (;;) {
if (n < 0) {
break;
}
total += n;
// echo
// close after X bytes?
break;
}
}
if (delay > 0) {
try {
} catch (InterruptedException x) { }
}
}
}
// 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.
public static int count() {
synchronized (lock) {
return count;
}
}
synchronized (lock) {
count++;
}
}
public void run() {
try {
} catch (IOException x) {
} finally {
synchronized (lock) {
count--;
}
}
}
}
if (c == null) {
return;
}
// tcp nowait
if (c instanceof SocketChannel) {
int closeAfter = 0;
int delay = 0;
}
}
}
// 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) {
ssc.configureBlocking(false);
int count = 0;
for (;;) {
} else {
// if all clients have disconnected then we die as well.
break;
}
}
}
}
// udp wait
if (c instanceof DatagramChannel) {
doIt((DatagramChannel)c);
}
// linger?
try {
} catch (InterruptedException x) { }
}
}
}