2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2001 by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A *
2N/A */
2N/A
2N/A// StreamListener.java: Listen to stream socket, spawn request to
2N/A// handle incoming request.
2N/A// Author: James Kempf
2N/A// Created On: Mon May 18 13:31:40 1998
2N/A// Last Modified By: James Kempf
2N/A// Last Modified On: Tue Jan 12 11:03:30 1999
2N/A// Update Count: 24
2N/A//
2N/A
2N/Apackage com.sun.slp;
2N/A
2N/Aimport java.util.*;
2N/Aimport java.net.*;
2N/Aimport java.io.*;
2N/A
2N/A/**
2N/A * Listen on the SLP port for clients requesting a stream connection. Spawn
2N/A * a request handler to handle the connection.
2N/A *
2N/A * @author James Kempf, Erik Guttman
2N/A */
2N/A
2N/Aclass StreamListener extends Thread {
2N/A
2N/A private ServerSocket serverSocket = null; // The listening socket
2N/A private InetAddress interfac = null; // The interface.
2N/A
2N/A static private SLPConfig config = null; // Config object
2N/A static private Hashtable listeners = new Hashtable(); // Stream listeners
2N/A
2N/A // Initialize a stream (TCP) listener on an interface.
2N/A
2N/A static void initializeStreamListenerOnInterface(InetAddress interfac)
2N/A throws ServiceLocationException {
2N/A
2N/A // If we've got it, return.
2N/A
2N/A if (listeners.get(interfac) != null) {
2N/A return;
2N/A
2N/A }
2N/A
2N/A // Get config object.
2N/A
2N/A if (config == null) {
2N/A config = SLPConfig.getSLPConfig();
2N/A
2N/A }
2N/A
2N/A // Create the new stream listener.
2N/A
2N/A StreamListener listener = new StreamListener(interfac);
2N/A
2N/A // Start it running.
2N/A
2N/A listener.start();
2N/A
2N/A }
2N/A
2N/A private StreamListener(InetAddress interfac)
2N/A throws ServiceLocationException {
2N/A
2N/A int qn = config.getServerSocketQueueLength();
2N/A
2N/A // Create a server socket for incoming Stream connections.
2N/A
2N/A try {
2N/A serverSocket = new ServerSocket(Defaults.iSLPPort,
2N/A qn,
2N/A interfac);
2N/A
2N/A } catch (IOException ex) {
2N/A throw new ServiceLocationException(
2N/A ServiceLocationException.NETWORK_INIT_FAILED,
2N/A "socket_creation_failure",
2N/A new Object[] {interfac, ex.getMessage()});
2N/A }
2N/A
2N/A // Record.
2N/A
2N/A listeners.put(interfac, this);
2N/A
2N/A this.interfac = interfac;
2N/A
2N/A }
2N/A
2N/A // Listen to port 427, accept incoming connections, pass the socket
2N/A // off to a new RequestHandler to process.
2N/A
2N/A public void run() {
2N/A
2N/A setName("SLP Stream Listener");
2N/A
2N/A long lLastIOE = 0;
2N/A
2N/A // Loop, blocking on acceptence of connections.
2N/A
2N/A while (true) {
2N/A
2N/A Socket s = null;
2N/A
2N/A try {
2N/A s = serverSocket.accept(); // will block here
2N/A
2N/A if (config.traceMsg() && s != null) {
2N/A config.writeLog("sl_incoming",
2N/A new Object[] {
2N/A s.getInetAddress().toString(),
2N/A interfac});
2N/A }
2N/A
2N/A // Set socket timeout in case something goes wrong.
2N/A
2N/A if (s != null) {
2N/A s.setSoTimeout(config.getTCPTimeout());
2N/A
2N/A }
2N/A
2N/A } catch (SocketException ex) {
2N/A if (config.traceMsg()) {
2N/A config.writeLog("sl_sock_timeout",
2N/A new Object[] {
2N/A s.getInetAddress().toString(),
2N/A interfac,
2N/A ex.getMessage()});
2N/A
2N/A }
2N/A
2N/A continue;
2N/A
2N/A } catch (IOException ex) {
2N/A long lThisIOE = System.currentTimeMillis();
2N/A
2N/A Assert.slpassert((lThisIOE - lLastIOE >= 250),
2N/A "sls_repeat_failure",
2N/A new Object[0]);
2N/A
2N/A lLastIOE = lThisIOE;
2N/A continue;
2N/A }
2N/A
2N/A RequestHandler rh = new RequestHandler(s, interfac, config);
2N/A rh.start();
2N/A
2N/A }
2N/A }
2N/A}