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// ActiveDiscoverer.java: Object to perform active DA discovery.
2N/A// Author: James Kempf
2N/A// Created On: Thu Sep 3 08:45:21 1998
2N/A// Last Modified By: James Kempf
2N/A// Last Modified On: Thu Jan 28 15:45:45 1999
2N/A// Update Count: 32
2N/A//
2N/A
2N/Apackage com.sun.slp;
2N/A
2N/Aimport java.util.*;
2N/Aimport java.net.*;
2N/A
2N/A/*
2N/A * The ActiveDiscover does active discovery DA discovery by periodically
2N/A * sending out a SrvRqst for "service:directory-agent". Replies are
2N/A * entered into the DA table.
2N/A *
2N/A * @author James Kempf
2N/A */
2N/A
2N/Aclass ActiveDiscoverer extends Thread {
2N/A
2N/A // Config object.
2N/A
2N/A static private SLPConfig config = null;
2N/A
2N/A // Message for active DA discovery.
2N/A
2N/A private CSrvMsg activeMsg = null;
2N/A
2N/A // Version of protocol to use for advertisements.
2N/A
2N/A private int version = 0;
2N/A
2N/A // DATable where discovered DAs are recorded.
2N/A
2N/A private ServerDATable table = null;
2N/A
2N/A // Scopes to advertise for.
2N/A
2N/A private Vector useScopes = null;
2N/A
2N/A // Address on which to advertise.
2N/A
2N/A private InetAddress address = null;
2N/A
2N/A // Create an active discoverer.
2N/A
2N/A ActiveDiscoverer(int version,
2N/A ServerDATable table,
2N/A Vector useScopes,
2N/A InetAddress address) {
2N/A
2N/A this.version = version;
2N/A
2N/A this.table = table;
2N/A
2N/A this.useScopes = useScopes;
2N/A
2N/A this.address = address;
2N/A
2N/A if (config == null) {
2N/A config = SLPConfig.getSLPConfig();
2N/A
2N/A }
2N/A
2N/A }
2N/A
2N/A // Do an initial active discovery then start a thread to
2N/A // do one periodically.
2N/A
2N/A public void start() {
2N/A
2N/A // Initial sleepy time.
2N/A
2N/A long sleepyTime = config.getRandomWait();
2N/A
2N/A // Create a message for active discovery.
2N/A
2N/A try {
2N/A
2N/A activeMsg = new CSrvMsg(config.getLocale(),
2N/A Defaults.DA_SERVICE_TYPE,
2N/A useScopes,
2N/A "");
2N/A
2N/A } catch (ServiceLocationException ex) {
2N/A Assert.slpassert(false,
2N/A "sdat_active_err",
2N/A new Object[] {
2N/A new Integer(ex.getErrorCode()),
2N/A ex.getMessage()});
2N/A
2N/A }
2N/A
2N/A // Initialize preconfigured DAs.
2N/A
2N/A addPreconfiguredDAs();
2N/A
2N/A // Do an initial round of active discovery, waiting for
2N/A // a random period first. Only do it if active
2N/A // discovery is on.
2N/A
2N/A if (config.getActiveDiscoveryInterval() > 0) {
2N/A try {
2N/A Thread.currentThread().sleep(sleepyTime);
2N/A
2N/A } catch (InterruptedException ex) {
2N/A
2N/A }
2N/A
2N/A activeDiscovery();
2N/A
2N/A } else {
2N/A
2N/A // Report that active discovery is off.
2N/A
2N/A config.writeLog("ad_active_off",
2N/A new Object[0]);
2N/A
2N/A }
2N/A
2N/A // Start the active discovery thread.
2N/A
2N/A super.start();
2N/A }
2N/A
2N/A
2N/A
2N/A // Implement the Runnable interface for a thread to start.
2N/A
2N/A public void run() {
2N/A
2N/A // Set the Thread name.
2N/A
2N/A Thread.currentThread().setName("SLP Active DA Discovery");
2N/A
2N/A // Sleepy time until discovery.
2N/A
2N/A long sleepyTime = config.getActiveDiscoveryInterval() * 1000;
2N/A
2N/A // If the sleep time is zero, then active discovery is turned off.
2N/A // Use the service URL maximum lifetime.
2N/A
2N/A if (sleepyTime <= 0) {
2N/A sleepyTime = (ServiceURL.LIFETIME_MAXIMUM / 2) * 1000;
2N/A
2N/A }
2N/A
2N/A // Register ourselves at startup if we are a DA. We may not be
2N/A // listening for the active discovery message at startup
2N/A // because the listener thread goes on-line last of all.
2N/A
2N/A if (config.isDA()) {
2N/A Vector interfaces = config.getInterfaces();
2N/A int i, n = interfaces.size();
2N/A
2N/A for (i = 0; i < n; i++) {
2N/A InetAddress interfac = (InetAddress)interfaces.elementAt(i);
2N/A ServiceURL url = new ServiceURL(Defaults.DA_SERVICE_TYPE +
2N/A "://" +
2N/A interfac.getHostAddress(),
2N/A ServiceURL.LIFETIME_MAXIMUM);
2N/A Vector scopes = config.getSAConfiguredScopes();
2N/A long timestamp = 0; // later adverts will cause replacement,
2N/A // but noforwarding because it is to us...
2N/A
2N/A String mySPIs = System.getProperty("sun.net.slp.SPIs");
2N/A mySPIs = mySPIs == null ? "" : mySPIs;
2N/A
2N/A table.recordNewDA(url,
2N/A scopes,
2N/A timestamp,
2N/A version,
2N/A config.getDAAttributes(),
2N/A mySPIs);
2N/A }
2N/A }
2N/A
2N/A // Sleep, then perform active discovery or polling of preconfigured
2N/A // DAs when we awake.
2N/A
2N/A do {
2N/A
2N/A try {
2N/A
2N/A sleep(sleepyTime);
2N/A
2N/A if (config.getActiveDiscoveryInterval() > 0) {
2N/A activeDiscovery();
2N/A
2N/A } else {
2N/A addPreconfiguredDAs();
2N/A
2N/A }
2N/A
2N/A } catch (InterruptedException ex) {
2N/A
2N/A }
2N/A
2N/A } while (true);
2N/A
2N/A }
2N/A
2N/A // Perform active DA discovery.
2N/A
2N/A synchronized private void activeDiscovery() {
2N/A
2N/A // Set the previous responders list to null. Otherwise,
2N/A // the previous responders from the last time we did
2N/A // this may interfere.
2N/A
2N/A SrvLocHeader hdr = activeMsg.getHeader();
2N/A
2N/A hdr.previousResponders.removeAllElements();
2N/A
2N/A
2N/A // Perform the active discovery message transaction.
2N/A
2N/A try {
2N/A Transact.transactActiveAdvertRequest(Defaults.DA_SERVICE_TYPE,
2N/A activeMsg,
2N/A table);
2N/A
2N/A } catch (ServiceLocationException ex) {
2N/A
2N/A config.writeLog("ad_multi_error",
2N/A new Object[] { new Integer(ex.getErrorCode()),
2N/A ex.getMessage() });
2N/A
2N/A }
2N/A
2N/A }
2N/A
2N/A // Add preconfigured DAs to the DA table. Note that we poll the
2N/A // preconfigured DAs once every 9 hours to make sure they are still around.
2N/A
2N/A synchronized private void addPreconfiguredDAs() {
2N/A
2N/A Vector daAddrs = config.getPreconfiguredDAs();
2N/A int i, n = daAddrs.size();
2N/A
2N/A // Go through the DA addresses, contacting them for their
2N/A // information. Better not be any SLPv1 DAs there.
2N/A
2N/A for (i = 0; i < n; i++) {
2N/A InetAddress daAddr = (InetAddress)daAddrs.elementAt(i);
2N/A
2N/A // Use a TCP connection. DAs must support TCP so why not?
2N/A
2N/A SrvLocMsg reply = null;
2N/A
2N/A try {
2N/A reply = Transact.transactTCPMsg(daAddr, activeMsg, false);
2N/A
2N/A } catch (ServiceLocationException ex) {
2N/A
2N/A if (config.traceDrop()) {
2N/A config.writeLog("ad_trans_error", new Object[] {
2N/A new Integer(ex.getErrorCode()),
2N/A daAddr,
2N/A ex.getMessage() });
2N/A }
2N/A
2N/A continue;
2N/A }
2N/A
2N/A // Report if there's an error in configuration.
2N/A
2N/A if (!(reply instanceof CDAAdvert)) {
2N/A if (config.traceDrop()) {
2N/A config.writeLog("ad_preconfig_not_advert",
2N/A new Object[] { daAddr, reply });
2N/A
2N/A }
2N/A
2N/A continue;
2N/A }
2N/A
2N/A
2N/A CDAAdvert advert = (CDAAdvert)reply;
2N/A SrvLocHeader hdr = advert.getHeader();
2N/A
2N/A // We need to make the URL long lived if active
2N/A // discovery is off. Otherwise, we let the DA time out like all the
2N/A // rest.
2N/A
2N/A if (config.getActiveDiscoveryInterval() <= 0) {
2N/A advert.URL =
2N/A new ServiceURL(advert.URL.toString(),
2N/A ServiceURL.LIFETIME_MAXIMUM);
2N/A
2N/A }
2N/A
2N/A // Add the scopes to the configured scopes. Scopes from configured
2N/A // DAs count as configured scopes.
2N/A
2N/A config.addPreconfiguredDAScopes(hdr.scopes);
2N/A
2N/A // Record it. Note that we don't have to forward here
2N/A // because it's the very beginning.
2N/A
2N/A table.recordNewDA(advert.URL,
2N/A hdr.scopes,
2N/A advert.timestamp,
2N/A hdr.version,
2N/A advert.attrs,
2N/A advert.spis);
2N/A
2N/A }
2N/A }
2N/A
2N/A}