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