10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7298SMark.J.Nelson@Sun.COM  * Common Development and Distribution License (the "License").
6*7298SMark.J.Nelson@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
230Sstevel@tonic-gate  * All rights reserved.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate //  ActiveDiscoverer.java: Object to perform active DA discovery.
280Sstevel@tonic-gate //  Author:           James Kempf
290Sstevel@tonic-gate //  Created On:       Thu Sep  3 08:45:21 1998
300Sstevel@tonic-gate //  Last Modified By: James Kempf
310Sstevel@tonic-gate //  Last Modified On: Thu Jan 28 15:45:45 1999
320Sstevel@tonic-gate //  Update Count:     32
330Sstevel@tonic-gate //
340Sstevel@tonic-gate 
350Sstevel@tonic-gate package com.sun.slp;
360Sstevel@tonic-gate 
370Sstevel@tonic-gate import java.util.*;
380Sstevel@tonic-gate import java.net.*;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * The ActiveDiscover does active discovery DA discovery by periodically
420Sstevel@tonic-gate  * sending out a SrvRqst for "service:directory-agent". Replies are
430Sstevel@tonic-gate  * entered into the DA table.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * @author James Kempf
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate class ActiveDiscoverer extends Thread {
490Sstevel@tonic-gate 
500Sstevel@tonic-gate     // Config object.
510Sstevel@tonic-gate 
520Sstevel@tonic-gate     static private SLPConfig config = null;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate     // Message for active DA discovery.
550Sstevel@tonic-gate 
560Sstevel@tonic-gate     private CSrvMsg activeMsg = null;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate     // Version of protocol to use for advertisements.
590Sstevel@tonic-gate 
600Sstevel@tonic-gate     private int version = 0;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate     // DATable where discovered DAs are recorded.
630Sstevel@tonic-gate 
640Sstevel@tonic-gate     private ServerDATable table = null;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate     // Scopes to advertise for.
670Sstevel@tonic-gate 
680Sstevel@tonic-gate     private Vector useScopes = null;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate     // Address on which to advertise.
710Sstevel@tonic-gate 
720Sstevel@tonic-gate     private InetAddress address = null;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate     // Create an active discoverer.
750Sstevel@tonic-gate 
ActiveDiscoverer(int version, ServerDATable table, Vector useScopes, InetAddress address)760Sstevel@tonic-gate     ActiveDiscoverer(int version,
770Sstevel@tonic-gate 		     ServerDATable table,
780Sstevel@tonic-gate 		     Vector useScopes,
790Sstevel@tonic-gate 		     InetAddress address) {
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	this.version = version;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	this.table = table;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	this.useScopes = useScopes;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	this.address = address;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (config == null) {
900Sstevel@tonic-gate 	    config = SLPConfig.getSLPConfig();
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate     }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate     // Do an initial active discovery then start a thread to
970Sstevel@tonic-gate     //  do one periodically.
980Sstevel@tonic-gate 
start()990Sstevel@tonic-gate     public void start() {
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	// Initial sleepy time.
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	long sleepyTime = config.getRandomWait();
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	// Create a message for active discovery.
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	try {
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	    activeMsg = new CSrvMsg(config.getLocale(),
1100Sstevel@tonic-gate 				    Defaults.DA_SERVICE_TYPE,
1110Sstevel@tonic-gate 				    useScopes,
1120Sstevel@tonic-gate 				    "");
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	} catch (ServiceLocationException ex) {
1150Sstevel@tonic-gate 	    Assert.slpassert(false,
1160Sstevel@tonic-gate 			  "sdat_active_err",
1170Sstevel@tonic-gate 			  new Object[] {
1180Sstevel@tonic-gate 		new Integer(ex.getErrorCode()),
1190Sstevel@tonic-gate 		    ex.getMessage()});
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	// Initialize preconfigured DAs.
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	addPreconfiguredDAs();
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	// Do an initial round of active discovery, waiting for
1280Sstevel@tonic-gate 	//  a random period first. Only do it if active
1290Sstevel@tonic-gate 	//  discovery is on.
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	if (config.getActiveDiscoveryInterval() > 0) {
1320Sstevel@tonic-gate 	    try {
1330Sstevel@tonic-gate 		Thread.currentThread().sleep(sleepyTime);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	    } catch (InterruptedException ex) {
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	    }
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	    activeDiscovery();
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	} else {
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	    // Report that active discovery is off.
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	    config.writeLog("ad_active_off",
1460Sstevel@tonic-gate 			    new Object[0]);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	// Start the active discovery thread.
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	super.start();
1530Sstevel@tonic-gate     }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate     // Implement the Runnable interface for a thread to start.
1580Sstevel@tonic-gate 
run()1590Sstevel@tonic-gate     public void run() {
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	// Set the Thread name.
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	Thread.currentThread().setName("SLP Active DA Discovery");
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	// Sleepy time until discovery.
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	long sleepyTime = config.getActiveDiscoveryInterval() * 1000;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	// If the sleep time is zero, then active discovery is turned off.
1700Sstevel@tonic-gate 	//  Use the service URL maximum lifetime.
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (sleepyTime <= 0) {
1730Sstevel@tonic-gate 	    sleepyTime = (ServiceURL.LIFETIME_MAXIMUM / 2) * 1000;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	// Register ourselves at startup if we are a DA. We may not be
1780Sstevel@tonic-gate 	//  listening for the active discovery message at startup
1790Sstevel@tonic-gate 	//  because the listener thread goes on-line last of all.
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (config.isDA()) {
1820Sstevel@tonic-gate 	    Vector interfaces = config.getInterfaces();
1830Sstevel@tonic-gate 	    int i, n = interfaces.size();
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	    for (i = 0; i < n; i++) {
1860Sstevel@tonic-gate 		InetAddress interfac = (InetAddress)interfaces.elementAt(i);
1870Sstevel@tonic-gate 		ServiceURL url = new ServiceURL(Defaults.DA_SERVICE_TYPE +
1880Sstevel@tonic-gate 						"://" +
1890Sstevel@tonic-gate 						interfac.getHostAddress(),
1900Sstevel@tonic-gate 						ServiceURL.LIFETIME_MAXIMUM);
1910Sstevel@tonic-gate 		Vector scopes = config.getSAConfiguredScopes();
1920Sstevel@tonic-gate 		long timestamp = 0; // later adverts will cause replacement,
1930Sstevel@tonic-gate 				    // but noforwarding because it is to us...
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 		String mySPIs = System.getProperty("sun.net.slp.SPIs");
1960Sstevel@tonic-gate 		mySPIs = mySPIs == null ? "" : mySPIs;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 		table.recordNewDA(url,
1990Sstevel@tonic-gate 				  scopes,
2000Sstevel@tonic-gate 				  timestamp,
2010Sstevel@tonic-gate 				  version,
2020Sstevel@tonic-gate 				  config.getDAAttributes(),
2030Sstevel@tonic-gate 				  mySPIs);
2040Sstevel@tonic-gate 	    }
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	// Sleep, then perform active discovery or polling of preconfigured
2080Sstevel@tonic-gate 	//  DAs when we awake.
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	do {
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	    try {
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 		sleep(sleepyTime);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		if (config.getActiveDiscoveryInterval() > 0) {
2170Sstevel@tonic-gate 		    activeDiscovery();
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		} else {
2200Sstevel@tonic-gate 		    addPreconfiguredDAs();
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	    } catch (InterruptedException ex) {
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	    }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	} while (true);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate     }
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate     // Perform active DA discovery.
2330Sstevel@tonic-gate 
activeDiscovery()2340Sstevel@tonic-gate     synchronized private void activeDiscovery() {
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	// Set the previous responders list to null. Otherwise,
2370Sstevel@tonic-gate 	//  the previous responders from the last time we did
2380Sstevel@tonic-gate 	//  this may interfere.
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	SrvLocHeader hdr = activeMsg.getHeader();
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	hdr.previousResponders.removeAllElements();
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	// Perform the active discovery message transaction.
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	try {
2480Sstevel@tonic-gate 	    Transact.transactActiveAdvertRequest(Defaults.DA_SERVICE_TYPE,
2490Sstevel@tonic-gate 						 activeMsg,
2500Sstevel@tonic-gate 						 table);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	} catch (ServiceLocationException ex) {
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	    config.writeLog("ad_multi_error",
2550Sstevel@tonic-gate 			    new Object[] { new Integer(ex.getErrorCode()),
2560Sstevel@tonic-gate 					       ex.getMessage() });
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate     }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate     // Add preconfigured DAs to the DA table. Note that we poll the
2630Sstevel@tonic-gate     // preconfigured DAs once every 9 hours to make sure they are still around.
2640Sstevel@tonic-gate 
addPreconfiguredDAs()2650Sstevel@tonic-gate     synchronized private void addPreconfiguredDAs() {
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	Vector daAddrs = config.getPreconfiguredDAs();
2680Sstevel@tonic-gate 	int i, n = daAddrs.size();
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	// Go through the DA addresses, contacting them for their
2710Sstevel@tonic-gate 	// information. Better not be any SLPv1 DAs there.
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	for (i = 0; i < n; i++) {
2740Sstevel@tonic-gate 	    InetAddress daAddr = (InetAddress)daAddrs.elementAt(i);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	    // Use a TCP connection. DAs must support TCP so why not?
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	    SrvLocMsg reply = null;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	    try {
2810Sstevel@tonic-gate 		reply = Transact.transactTCPMsg(daAddr, activeMsg, false);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	    } catch (ServiceLocationException ex) {
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		if (config.traceDrop()) {
2860Sstevel@tonic-gate 		    config.writeLog("ad_trans_error", new Object[] {
2870Sstevel@tonic-gate 			new Integer(ex.getErrorCode()),
2880Sstevel@tonic-gate 			    daAddr,
2890Sstevel@tonic-gate 			    ex.getMessage() });
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		continue;
2930Sstevel@tonic-gate 	    }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	    // Report if there's an error in configuration.
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	    if (!(reply instanceof CDAAdvert)) {
2980Sstevel@tonic-gate 		if (config.traceDrop()) {
2990Sstevel@tonic-gate 		    config.writeLog("ad_preconfig_not_advert",
3000Sstevel@tonic-gate 				    new Object[] { daAddr, reply });
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		continue;
3050Sstevel@tonic-gate 	    }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	    CDAAdvert advert = (CDAAdvert)reply;
3090Sstevel@tonic-gate 	    SrvLocHeader hdr = advert.getHeader();
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	    // We need to make the URL long lived if active
3120Sstevel@tonic-gate 	    // discovery is off. Otherwise, we let the DA time out like all the
3130Sstevel@tonic-gate 	    // rest.
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	    if (config.getActiveDiscoveryInterval() <= 0) {
3160Sstevel@tonic-gate 		advert.URL =
3170Sstevel@tonic-gate 		    new ServiceURL(advert.URL.toString(),
3180Sstevel@tonic-gate 				   ServiceURL.LIFETIME_MAXIMUM);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	    }
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	    // Add the scopes to the configured scopes. Scopes from configured
3230Sstevel@tonic-gate 	    //  DAs count as configured scopes.
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	    config.addPreconfiguredDAScopes(hdr.scopes);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	    // Record it. Note that we don't have to forward here
3280Sstevel@tonic-gate 	    //  because it's the very beginning.
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	    table.recordNewDA(advert.URL,
3310Sstevel@tonic-gate 			      hdr.scopes,
3320Sstevel@tonic-gate 			      advert.timestamp,
3330Sstevel@tonic-gate 			      hdr.version,
3340Sstevel@tonic-gate 			      advert.attrs,
3350Sstevel@tonic-gate 			      advert.spis);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate     }
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate }
341