xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/PermSARegTable.java (revision 9a70fc3be3b1e966bf78825cdb8d509963a6f0a1)
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) 2001 by Sun Microsystems, Inc.
237c478bd9Sstevel@tonic-gate  * All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate //  PermSARegTable.java: Periodically reregister registrations.
287c478bd9Sstevel@tonic-gate //  Author:           James Kempf
297c478bd9Sstevel@tonic-gate //  Created On:       Thu May 14 14:11:49 1998
307c478bd9Sstevel@tonic-gate //  Last Modified By: James Kempf
317c478bd9Sstevel@tonic-gate //  Last Modified On: Thu Jan 28 14:53:43 1999
327c478bd9Sstevel@tonic-gate //  Update Count:     36
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.io.*;
397c478bd9Sstevel@tonic-gate import java.util.*;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /**
427c478bd9Sstevel@tonic-gate  * Periodically reregister advertisments in the SA client.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * @author Erik Guttman, James Kempf
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate class PermSARegTable extends Thread {
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate     private Hashtable htRegs;
507c478bd9Sstevel@tonic-gate     private SLPConfig config;
517c478bd9Sstevel@tonic-gate     private final static long INCREMENT = Defaults.lMaxSleepTime / 2L;
527c478bd9Sstevel@tonic-gate 								// 9 hours...
537c478bd9Sstevel@tonic-gate     private final static long SLEEPY_TIME = INCREMENT / 2L;
547c478bd9Sstevel@tonic-gate 						// 4 hours, more or less...
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate     // We use these indicies for record access. We should use a class
577c478bd9Sstevel@tonic-gate     //  here, but it's another 1K!
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate     private final static int TIME = 0;
607c478bd9Sstevel@tonic-gate     private final static int REG = 1;
617c478bd9Sstevel@tonic-gate 
PermSARegTable(SLPConfig config)627c478bd9Sstevel@tonic-gate     PermSARegTable(SLPConfig config) {
637c478bd9Sstevel@tonic-gate 	htRegs = new Hashtable();
647c478bd9Sstevel@tonic-gate 	this.config = config;
657c478bd9Sstevel@tonic-gate 	start();
667c478bd9Sstevel@tonic-gate     }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate     // We just lock the hashtable when we need to update. Otherwise, we
697c478bd9Sstevel@tonic-gate     //  get into deadlock if an outgoing request is being made when
707c478bd9Sstevel@tonic-gate     //  somebody else wants to get into this class to look something
717c478bd9Sstevel@tonic-gate     //  up.
727c478bd9Sstevel@tonic-gate 
reg(ServiceURL URL, CSrvReg sr)737c478bd9Sstevel@tonic-gate     void reg(ServiceURL URL, CSrvReg sr) {
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	// Make up a record for the table.
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	Object[] rec =
787c478bd9Sstevel@tonic-gate 	    new Object[] {
797c478bd9Sstevel@tonic-gate 	    new Long(System.currentTimeMillis() + INCREMENT),
807c478bd9Sstevel@tonic-gate 		sr};
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	// Note that we do not account for multiple nonservice: URLs under
837c478bd9Sstevel@tonic-gate 	// separate type names, because that is disallowed by the protocol.
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	htRegs.put(URL, rec);
867c478bd9Sstevel@tonic-gate     }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate     // Remove
897c478bd9Sstevel@tonic-gate 
dereg(ServiceURL URL)907c478bd9Sstevel@tonic-gate     void dereg(ServiceURL URL) {
917c478bd9Sstevel@tonic-gate 	htRegs.remove(URL);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate     }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate     // Send off the vector of registations for expired advertisements.
967c478bd9Sstevel@tonic-gate 
send(SrvLocMsg reg)977c478bd9Sstevel@tonic-gate     private void send(SrvLocMsg reg) {
987c478bd9Sstevel@tonic-gate 	InetAddress addr = config.getLoopback();
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	try {
1017c478bd9Sstevel@tonic-gate 	    Transact.transactTCPMsg(addr, reg, true);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	} catch (ServiceLocationException ex) {
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	    config.writeLog("periodic_exception",
1067c478bd9Sstevel@tonic-gate 			    new Object[] {new Short(ex.getErrorCode()),
1077c478bd9Sstevel@tonic-gate 					      ex.getMessage()});
1087c478bd9Sstevel@tonic-gate 	} catch (IllegalArgumentException iae) {
1097c478bd9Sstevel@tonic-gate 	    Assert.slpassert(false, "reregister_bug", new Object[0]);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate     }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate     // Walk the registration table, collecting registrations
1157c478bd9Sstevel@tonic-gate     //  to reregister. We synchronize on this method to close
1167c478bd9Sstevel@tonic-gate     //  the window between when the table is walked and
1177c478bd9Sstevel@tonic-gate     //  when the registration is sent
1187c478bd9Sstevel@tonic-gate     //  during which the client may deregister the URL but
1197c478bd9Sstevel@tonic-gate     //  it is reregistered anyway.
1207c478bd9Sstevel@tonic-gate 
walk()1217c478bd9Sstevel@tonic-gate     private synchronized void walk() {
1227c478bd9Sstevel@tonic-gate 	Enumeration e;
1237c478bd9Sstevel@tonic-gate 	long lnow = System.currentTimeMillis();
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	e = htRegs.keys();
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	while (e.hasMoreElements()) {
1287c478bd9Sstevel@tonic-gate 	    ServiceURL url = (ServiceURL)e.nextElement();
1297c478bd9Sstevel@tonic-gate 	    Object[] rec = (Object[])htRegs.get(url);
1307c478bd9Sstevel@tonic-gate 	    long xtime = ((Long)rec[TIME]).longValue();
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	    // If the deadline to refresh passed, then do it.
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	    if (xtime <= lnow) {
1357c478bd9Sstevel@tonic-gate 		send((SrvLocMsg)rec[REG]);
1367c478bd9Sstevel@tonic-gate 		rec[TIME] = new Long(lnow + INCREMENT);
1377c478bd9Sstevel@tonic-gate 	    }
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate     }
1417c478bd9Sstevel@tonic-gate 
run()1427c478bd9Sstevel@tonic-gate     public void run() {
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	setName("SLP PermSARegTable");
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	while (true) {
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	    try {
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		// Sleep for half the reregistration interval (which itself
1517c478bd9Sstevel@tonic-gate 		//  is half the lifetime of the URLs.
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		sleep(SLEEPY_TIME);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	    } catch (InterruptedException ie) {
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	    }
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	    walk();
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate     }
1637c478bd9Sstevel@tonic-gate }
164