View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    * 
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package net.jini.lookup;
20  
21  import net.jini.core.lease.Lease;
22  
23  /**
24   * Data structure used to group together the lease and event sequence
25   * number. For each LookupCache, there is a HashMap that maps a ProxyReg to
26   * an EventReg.
27   *
28   * EventReg's object lock is also used to maintain atomicity of
29   * lookup service state.
30   */
31  final class EventReg {
32      /* The Event source from the event registration */
33      final Object source;
34      /* The Event ID */
35      final long eventID;
36      /* The current event sequence number for the Service template */
37      private long seqNo;
38      /* The Event notification lease */
39      final Lease lease;
40      /* Event Suspension */
41      boolean suspended;
42      /* Discarded Registrar */
43      boolean discarded;
44  
45      EventReg(Object source, long eventID, long seqNo, Lease lease) {
46  	this.source = source;
47  	this.eventID = eventID;
48  	this.seqNo = seqNo;
49  	this.lease = lease;
50      }
51  
52      /**
53       * @param seqNo the seqNo to set, return a positive delta if successful,
54       * otherwise a negative value indicates failure.
55       */
56      long updateSeqNo(long seqNo) {
57  	assert Thread.holdsLock(this);
58  	long difference;
59  	difference = seqNo - this.seqNo;
60  	if (difference > 0) {
61  	    this.seqNo = seqNo;
62  	    return difference;
63  	} else {
64  	    return difference; // Return a negative or zero value
65  	}
66      }
67      
68      boolean nonContiguousEvent(long seqNo){
69          assert Thread.holdsLock(this);
70          long difference = seqNo - this.seqNo;
71          this.notifyAll();
72          return difference != 1;
73      }
74      
75      void suspendEvents() {
76          suspended = true;
77      }
78      
79      boolean eventsSuspended(){
80          return suspended;
81      }
82      
83      void releaseEvents() {
84          suspended = false;
85      }
86      
87      boolean discard() {
88          if (discarded) return false;
89          discarded = true;
90          return true;
91      }
92      
93      boolean discarded(){
94          return discarded;
95      }
96      
97  } //end class ServiceDiscoveryManager.EventReg