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 // PermSARegTable.java: Periodically reregister registrations. 280Sstevel@tonic-gate // Author: James Kempf 290Sstevel@tonic-gate // Created On: Thu May 14 14:11:49 1998 300Sstevel@tonic-gate // Last Modified By: James Kempf 310Sstevel@tonic-gate // Last Modified On: Thu Jan 28 14:53:43 1999 320Sstevel@tonic-gate // Update Count: 36 330Sstevel@tonic-gate // 340Sstevel@tonic-gate 350Sstevel@tonic-gate package com.sun.slp; 360Sstevel@tonic-gate 370Sstevel@tonic-gate import java.net.*; 380Sstevel@tonic-gate import java.io.*; 390Sstevel@tonic-gate import java.util.*; 400Sstevel@tonic-gate 410Sstevel@tonic-gate /** 420Sstevel@tonic-gate * Periodically reregister advertisments in the SA client. 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * @author Erik Guttman, James Kempf 450Sstevel@tonic-gate */ 460Sstevel@tonic-gate 470Sstevel@tonic-gate class PermSARegTable extends Thread { 480Sstevel@tonic-gate 490Sstevel@tonic-gate private Hashtable htRegs; 500Sstevel@tonic-gate private SLPConfig config; 510Sstevel@tonic-gate private final static long INCREMENT = Defaults.lMaxSleepTime / 2L; 520Sstevel@tonic-gate // 9 hours... 530Sstevel@tonic-gate private final static long SLEEPY_TIME = INCREMENT / 2L; 540Sstevel@tonic-gate // 4 hours, more or less... 550Sstevel@tonic-gate 560Sstevel@tonic-gate // We use these indicies for record access. We should use a class 570Sstevel@tonic-gate // here, but it's another 1K! 580Sstevel@tonic-gate 590Sstevel@tonic-gate private final static int TIME = 0; 600Sstevel@tonic-gate private final static int REG = 1; 610Sstevel@tonic-gate PermSARegTable(SLPConfig config)620Sstevel@tonic-gate PermSARegTable(SLPConfig config) { 630Sstevel@tonic-gate htRegs = new Hashtable(); 640Sstevel@tonic-gate this.config = config; 650Sstevel@tonic-gate start(); 660Sstevel@tonic-gate } 670Sstevel@tonic-gate 680Sstevel@tonic-gate // We just lock the hashtable when we need to update. Otherwise, we 690Sstevel@tonic-gate // get into deadlock if an outgoing request is being made when 700Sstevel@tonic-gate // somebody else wants to get into this class to look something 710Sstevel@tonic-gate // up. 720Sstevel@tonic-gate reg(ServiceURL URL, CSrvReg sr)730Sstevel@tonic-gate void reg(ServiceURL URL, CSrvReg sr) { 740Sstevel@tonic-gate 750Sstevel@tonic-gate // Make up a record for the table. 760Sstevel@tonic-gate 770Sstevel@tonic-gate Object[] rec = 780Sstevel@tonic-gate new Object[] { 790Sstevel@tonic-gate new Long(System.currentTimeMillis() + INCREMENT), 800Sstevel@tonic-gate sr}; 810Sstevel@tonic-gate 820Sstevel@tonic-gate // Note that we do not account for multiple nonservice: URLs under 830Sstevel@tonic-gate // separate type names, because that is disallowed by the protocol. 840Sstevel@tonic-gate 850Sstevel@tonic-gate htRegs.put(URL, rec); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate // Remove 890Sstevel@tonic-gate dereg(ServiceURL URL)900Sstevel@tonic-gate void dereg(ServiceURL URL) { 910Sstevel@tonic-gate htRegs.remove(URL); 920Sstevel@tonic-gate 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate // Send off the vector of registations for expired advertisements. 960Sstevel@tonic-gate send(SrvLocMsg reg)970Sstevel@tonic-gate private void send(SrvLocMsg reg) { 980Sstevel@tonic-gate InetAddress addr = config.getLoopback(); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate try { 1010Sstevel@tonic-gate Transact.transactTCPMsg(addr, reg, true); 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate } catch (ServiceLocationException ex) { 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate config.writeLog("periodic_exception", 1060Sstevel@tonic-gate new Object[] {new Short(ex.getErrorCode()), 1070Sstevel@tonic-gate ex.getMessage()}); 1080Sstevel@tonic-gate } catch (IllegalArgumentException iae) { 1090Sstevel@tonic-gate Assert.slpassert(false, "reregister_bug", new Object[0]); 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate // Walk the registration table, collecting registrations 1150Sstevel@tonic-gate // to reregister. We synchronize on this method to close 1160Sstevel@tonic-gate // the window between when the table is walked and 1170Sstevel@tonic-gate // when the registration is sent 1180Sstevel@tonic-gate // during which the client may deregister the URL but 1190Sstevel@tonic-gate // it is reregistered anyway. 1200Sstevel@tonic-gate walk()1210Sstevel@tonic-gate private synchronized void walk() { 1220Sstevel@tonic-gate Enumeration e; 1230Sstevel@tonic-gate long lnow = System.currentTimeMillis(); 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate e = htRegs.keys(); 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate while (e.hasMoreElements()) { 1280Sstevel@tonic-gate ServiceURL url = (ServiceURL)e.nextElement(); 1290Sstevel@tonic-gate Object[] rec = (Object[])htRegs.get(url); 1300Sstevel@tonic-gate long xtime = ((Long)rec[TIME]).longValue(); 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate // If the deadline to refresh passed, then do it. 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate if (xtime <= lnow) { 1350Sstevel@tonic-gate send((SrvLocMsg)rec[REG]); 1360Sstevel@tonic-gate rec[TIME] = new Long(lnow + INCREMENT); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate run()1420Sstevel@tonic-gate public void run() { 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate setName("SLP PermSARegTable"); 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate while (true) { 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate try { 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate // Sleep for half the reregistration interval (which itself 1510Sstevel@tonic-gate // is half the lifetime of the URLs. 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate sleep(SLEEPY_TIME); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate } catch (InterruptedException ie) { 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate walk(); 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate } 164