17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*9a70fc3bSMark J. Nelson * Common Development and Distribution License (the "License"). 6*9a70fc3bSMark J. Nelson * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 227c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc. 237c478bd9Sstevel@tonic-gate * All rights reserved. 247c478bd9Sstevel@tonic-gate * 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate // DAAdvertiser.java: Advertise a DA, also handle incoming DAAdverts. 287c478bd9Sstevel@tonic-gate // Author: James Kempf 297c478bd9Sstevel@tonic-gate // Created On: Tue May 19 15:22:04 1998 307c478bd9Sstevel@tonic-gate // Last Modified By: James Kempf 317c478bd9Sstevel@tonic-gate // Last Modified On: Thu Mar 4 10:39:06 1999 327c478bd9Sstevel@tonic-gate // Update Count: 44 337c478bd9Sstevel@tonic-gate // 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate package com.sun.slp; 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate import java.net.*; 387c478bd9Sstevel@tonic-gate import java.util.*; 397c478bd9Sstevel@tonic-gate import java.io.*; 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /** 427c478bd9Sstevel@tonic-gate * This class supplies a regular interval 'heartbeat' DAAdvertisement. 437c478bd9Sstevel@tonic-gate * Implementation specific subclasses handle incoming DAAdverts and 447c478bd9Sstevel@tonic-gate * forwarding of registrations and deregistrations to other DAs. The 457c478bd9Sstevel@tonic-gate * implementation specific subclasses depend on how the server is 467c478bd9Sstevel@tonic-gate * representing DA information internally. 477c478bd9Sstevel@tonic-gate * 487c478bd9Sstevel@tonic-gate * @author James Kempf, Erik Guttman 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate class DAAdvertiser extends Thread { 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate protected DatagramSocket dss = null; 547c478bd9Sstevel@tonic-gate protected InetAddress castAddr = null; 557c478bd9Sstevel@tonic-gate protected InetAddress interfac = null; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate // V2 advertising has the same DAAdvert every time. 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate static private byte[] outbuf = null; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static protected SLPConfig config = null; // Config object. 627c478bd9Sstevel@tonic-gate static protected Hashtable daadv = 637c478bd9Sstevel@tonic-gate new Hashtable(); // Existing advertisers 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate private Boolean done = new Boolean(false); 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate // Initialize the DAAdvertiser on the designated interface. 687c478bd9Sstevel@tonic-gate initializeDAAdvertiserOnInterface(InetAddress interfac)697c478bd9Sstevel@tonic-gate static void initializeDAAdvertiserOnInterface(InetAddress interfac) 707c478bd9Sstevel@tonic-gate throws ServiceLocationException { 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate // If we've got it, return. 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate if (daadv.get(interfac) != null) { 757c478bd9Sstevel@tonic-gate return; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate // Get the config object. 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (config == null) { 827c478bd9Sstevel@tonic-gate config = SLPConfig.getSLPConfig(); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate // Get the SLPv2 DAADvert to send 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate ServiceTable table = ServiceTable.getServiceTable(); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate SLPServerHeaderV2 hdr = 917c478bd9Sstevel@tonic-gate new SLPServerHeaderV2(SrvLocHeader.DAAdvert, 927c478bd9Sstevel@tonic-gate false, 937c478bd9Sstevel@tonic-gate config.getLocale()); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate SDAAdvert msg = 967c478bd9Sstevel@tonic-gate (SDAAdvert)table.makeDAAdvert(hdr, 977c478bd9Sstevel@tonic-gate interfac, 987c478bd9Sstevel@tonic-gate (short)0x0, 997c478bd9Sstevel@tonic-gate config.getSAConfiguredScopes(), 1007c478bd9Sstevel@tonic-gate config); 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate // Create a new DAAdvertiser for this interface, with SLPv2 1037c478bd9Sstevel@tonic-gate // message to send. 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate DAAdvertiser daadv = new DAAdvertiser(interfac, msg.getHeader()); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate // Start thread running. 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate daadv.start(); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate // Used by V1 subclass constructor. 1147c478bd9Sstevel@tonic-gate DAAdvertiser()1157c478bd9Sstevel@tonic-gate DAAdvertiser() { 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate if (config == null) { 1187c478bd9Sstevel@tonic-gate config = SLPConfig.getSLPConfig(); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate // Create a new DAAdvertiser for the interface for default multicast 1257c478bd9Sstevel@tonic-gate // address. Externalize the message and set the instance variable. 1267c478bd9Sstevel@tonic-gate DAAdvertiser(InetAddress interfac, SrvLocHeader hdr)1277c478bd9Sstevel@tonic-gate DAAdvertiser(InetAddress interfac, SrvLocHeader hdr) 1287c478bd9Sstevel@tonic-gate throws ServiceLocationException { 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate // Config may not be initialized if this was called directly from 1317c478bd9Sstevel@tonic-gate // slpd. 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (config == null) { 1347c478bd9Sstevel@tonic-gate config = SLPConfig.getSLPConfig(); 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate // Externalize the DAAdvert. 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate hdr.externalize(baos, true, false); 1437c478bd9Sstevel@tonic-gate outbuf = baos.toByteArray(); 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate // Initialize the networking for default multicast address. 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate initializeNetworking(interfac, config.getMulticastAddress()); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate // Convert advert to bytes, initialize networking. 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate protected void initializeNetworking(InetAddress interfac, InetAddress maddr)1557c478bd9Sstevel@tonic-gate initializeNetworking(InetAddress interfac, 1567c478bd9Sstevel@tonic-gate InetAddress maddr) 1577c478bd9Sstevel@tonic-gate throws ServiceLocationException { 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate // Set the interface and multicast address on this advertiser. 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate this.interfac = interfac; 1627c478bd9Sstevel@tonic-gate this.castAddr = maddr; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate // Get the socket from the listener object corresponding to this 1657c478bd9Sstevel@tonic-gate // interface. The listener will always be started before the 1667c478bd9Sstevel@tonic-gate // DAAdvertiser, otherwise, SAs may start sending registrations 1677c478bd9Sstevel@tonic-gate // before anybody is listening. 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate dss = Listener.returnListenerSocketOnInterface(interfac); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate // If the socket is null, then there is no listener. Open a 1727c478bd9Sstevel@tonic-gate // new socket. Note that any listener opened *afterwards* will 1737c478bd9Sstevel@tonic-gate // not get this socket. 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (dss == null) { 1767c478bd9Sstevel@tonic-gate dss = config.getMulticastSocketOnInterface(interfac, true); 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate } 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate run()1817c478bd9Sstevel@tonic-gate public void run() { 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate // Set the thread name. 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate setName("SLP DA Advertisement"); 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate long heartbeat = config.getAdvertHeartbeatTime() * 1000; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate while (true) { 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate // Send an advert. 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate sendAdvert(); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate // Sleep until next time. 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate try { 1987c478bd9Sstevel@tonic-gate sleep(heartbeat); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate } catch (InterruptedException ie) { 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate // Somebody interrupted us. If we are to exit, then do so. 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate synchronized (done) { 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if (done.booleanValue()) { 2077c478bd9Sstevel@tonic-gate return; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate // Send an unsolicited DAAdvert. 2187c478bd9Sstevel@tonic-gate sendAdvert()2197c478bd9Sstevel@tonic-gate void sendAdvert() { 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate byte[] buf = getOutbuf(); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate DatagramPacket dp = 2247c478bd9Sstevel@tonic-gate new DatagramPacket(buf, 2257c478bd9Sstevel@tonic-gate buf.length, 2267c478bd9Sstevel@tonic-gate castAddr, 2277c478bd9Sstevel@tonic-gate Defaults.iSLPPort); 2287c478bd9Sstevel@tonic-gate try { 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate dss.send(dp); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate } catch (IOException ex) { 2337c478bd9Sstevel@tonic-gate config.writeLog("passive_advert_exception", 2347c478bd9Sstevel@tonic-gate new Object[] {ex.getMessage()}); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate // Tell the listener to refresh the socket. 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate dss = Listener.refreshSocketOnInterface(interfac); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate // Return the buffer for transmission. 2447c478bd9Sstevel@tonic-gate getOutbuf()2457c478bd9Sstevel@tonic-gate protected byte[] getOutbuf() { 2467c478bd9Sstevel@tonic-gate return outbuf; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate // Stop the thread from executing. 2517c478bd9Sstevel@tonic-gate stopThread()2527c478bd9Sstevel@tonic-gate void stopThread() { 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate synchronized (done) { 2557c478bd9Sstevel@tonic-gate done = new Boolean(true); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate this.interrupt(); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate } 263