xref: /illumos-gate/usr/src/lib/libslp/javalib/com/sun/slp/PermSARegTable.java (revision 1fceb383a3f0b59711832b9dc4e8329d7f216604)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2001 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  */
29 
30 //  SCCS Status:      %W%	%G%
31 //  PermSARegTable.java: Periodically reregister registrations.
32 //  Author:           James Kempf
33 //  Created On:       Thu May 14 14:11:49 1998
34 //  Last Modified By: James Kempf
35 //  Last Modified On: Thu Jan 28 14:53:43 1999
36 //  Update Count:     36
37 //
38 
39 package com.sun.slp;
40 
41 import java.net.*;
42 import java.io.*;
43 import java.util.*;
44 
45 /**
46  * Periodically reregister advertisments in the SA client.
47  *
48  * @version %R%.%L% %D%
49  * @author Erik Guttman, James Kempf
50  */
51 
52 class PermSARegTable extends Thread {
53 
54     private Hashtable htRegs;
55     private SLPConfig config;
56     private final static long INCREMENT = Defaults.lMaxSleepTime / 2L;
57 								// 9 hours...
58     private final static long SLEEPY_TIME = INCREMENT / 2L;
59 						// 4 hours, more or less...
60 
61     // We use these indicies for record access. We should use a class
62     //  here, but it's another 1K!
63 
64     private final static int TIME = 0;
65     private final static int REG = 1;
66 
67     PermSARegTable(SLPConfig config) {
68 	htRegs = new Hashtable();
69 	this.config = config;
70 	start();
71     }
72 
73     // We just lock the hashtable when we need to update. Otherwise, we
74     //  get into deadlock if an outgoing request is being made when
75     //  somebody else wants to get into this class to look something
76     //  up.
77 
78     void reg(ServiceURL URL, CSrvReg sr) {
79 
80 	// Make up a record for the table.
81 
82 	Object[] rec =
83 	    new Object[] {
84 	    new Long(System.currentTimeMillis() + INCREMENT),
85 		sr};
86 
87 	// Note that we do not account for multiple nonservice: URLs under
88 	// separate type names, because that is disallowed by the protocol.
89 
90 	htRegs.put(URL, rec);
91     }
92 
93     // Remove
94 
95     void dereg(ServiceURL URL) {
96 	htRegs.remove(URL);
97 
98     }
99 
100     // Send off the vector of registations for expired advertisements.
101 
102     private void send(SrvLocMsg reg) {
103 	InetAddress addr = config.getLoopback();
104 
105 	try {
106 	    Transact.transactTCPMsg(addr, reg, true);
107 
108 	} catch (ServiceLocationException ex) {
109 
110 	    config.writeLog("periodic_exception",
111 			    new Object[] {new Short(ex.getErrorCode()),
112 					      ex.getMessage()});
113 	} catch (IllegalArgumentException iae) {
114 	    Assert.slpassert(false, "reregister_bug", new Object[0]);
115 
116 	}
117     }
118 
119     // Walk the registration table, collecting registrations
120     //  to reregister. We synchronize on this method to close
121     //  the window between when the table is walked and
122     //  when the registration is sent
123     //  during which the client may deregister the URL but
124     //  it is reregistered anyway.
125 
126     private synchronized void walk() {
127 	Enumeration e;
128 	long lnow = System.currentTimeMillis();
129 
130 	e = htRegs.keys();
131 
132 	while (e.hasMoreElements()) {
133 	    ServiceURL url = (ServiceURL)e.nextElement();
134 	    Object[] rec = (Object[])htRegs.get(url);
135 	    long xtime = ((Long)rec[TIME]).longValue();
136 
137 	    // If the deadline to refresh passed, then do it.
138 
139 	    if (xtime <= lnow) {
140 		send((SrvLocMsg)rec[REG]);
141 		rec[TIME] = new Long(lnow + INCREMENT);
142 	    }
143 	}
144 
145     }
146 
147     public void run() {
148 
149 	setName("SLP PermSARegTable");
150 
151 	while (true) {
152 
153 	    try {
154 
155 		// Sleep for half the reregistration interval (which itself
156 		//  is half the lifetime of the URLs.
157 
158 		sleep(SLEEPY_TIME);
159 
160 	    } catch (InterruptedException ie) {
161 
162 	    }
163 
164 	    walk();
165 
166 	}
167     }
168 }
169