xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/StreamListener.java (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2001 by Sun Microsystems, Inc.
23  * All rights reserved.
24  *
25  */
26 
27 //  StreamListener.java: Listen to stream socket, spawn request to
28 //			  handle incoming request.
29 //  Author:           James Kempf
30 //  Created On:       Mon May 18 13:31:40 1998
31 //  Last Modified By: James Kempf
32 //  Last Modified On: Tue Jan 12 11:03:30 1999
33 //  Update Count:     24
34 //
35 
36 package com.sun.slp;
37 
38 import java.util.*;
39 import java.net.*;
40 import java.io.*;
41 
42 /**
43  * Listen on the SLP port for clients requesting a stream connection. Spawn
44  * a request handler to handle the connection.
45  *
46  * @author James Kempf, Erik Guttman
47  */
48 
49 class StreamListener extends Thread {
50 
51     private ServerSocket serverSocket = null;		// The listening socket
52     private InetAddress interfac = null;		// The interface.
53 
54     static private SLPConfig config   = null;		// Config object
55     static private Hashtable listeners = new Hashtable(); // Stream listeners
56 
57     // Initialize a stream (TCP) listener on an interface.
58 
initializeStreamListenerOnInterface(InetAddress interfac)59     static void initializeStreamListenerOnInterface(InetAddress interfac)
60 	throws ServiceLocationException {
61 
62 	// If we've got it, return.
63 
64 	if (listeners.get(interfac) != null) {
65 	    return;
66 
67 	}
68 
69 	// Get config object.
70 
71 	if (config == null) {
72 	    config = SLPConfig.getSLPConfig();
73 
74 	}
75 
76 	// Create the new stream listener.
77 
78 	StreamListener listener = new StreamListener(interfac);
79 
80 	// Start it running.
81 
82 	listener.start();
83 
84     }
85 
StreamListener(InetAddress interfac)86     private StreamListener(InetAddress interfac)
87 	throws ServiceLocationException {
88 
89 	int qn = config.getServerSocketQueueLength();
90 
91 	// Create a server socket for incoming Stream connections.
92 
93 	try {
94 	    serverSocket = new ServerSocket(Defaults.iSLPPort,
95 					    qn,
96 					    interfac);
97 
98 	} catch (IOException ex) {
99 	    throw new ServiceLocationException(
100 				ServiceLocationException.NETWORK_INIT_FAILED,
101 				"socket_creation_failure",
102 				new Object[] {interfac, ex.getMessage()});
103 	}
104 
105 	// Record.
106 
107 	listeners.put(interfac, this);
108 
109 	this.interfac = interfac;
110 
111     }
112 
113     // Listen to port 427, accept incoming connections, pass the socket
114     //  off to a new RequestHandler to process.
115 
run()116     public void run() {
117 
118 	setName("SLP Stream Listener");
119 
120 	long lLastIOE = 0;
121 
122 	// Loop, blocking on acceptence of connections.
123 
124 	while (true) {
125 
126 	    Socket s = null;
127 
128 	    try {
129 		s = serverSocket.accept(); // will block here
130 
131 		if (config.traceMsg() && s != null) {
132 		    config.writeLog("sl_incoming",
133 				    new Object[] {
134 			s.getInetAddress().toString(),
135 			    interfac});
136 		}
137 
138 		// Set socket timeout in case something goes wrong.
139 
140 		if (s != null) {
141 		    s.setSoTimeout(config.getTCPTimeout());
142 
143 		}
144 
145 	    } catch (SocketException ex) {
146 		if (config.traceMsg()) {
147 		    config.writeLog("sl_sock_timeout",
148 				    new Object[] {
149 			s.getInetAddress().toString(),
150 			    interfac,
151 			    ex.getMessage()});
152 
153 		}
154 
155 		continue;
156 
157 	    } catch (IOException ex) {
158 		long lThisIOE = System.currentTimeMillis();
159 
160 		Assert.slpassert((lThisIOE - lLastIOE >= 250),
161 			      "sls_repeat_failure",
162 			      new Object[0]);
163 
164 		lLastIOE = lThisIOE;
165 		continue;
166 	    }
167 
168 	    RequestHandler rh = new RequestHandler(s, interfac, config);
169 	    rh.start();
170 
171 	}
172     }
173 }
174