1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.relacs.rmi;
20
21 import jgroup.core.IID;
22 import jgroup.core.VMID;
23 import jgroup.relacs.types.IIDImpl;
24 import jgroup.relacs.types.VMIDImpl;
25 import jgroup.util.IntList;
26
27
28 /*
29 * FIXME: Not sure that having acknowledgement info piggybacked int
30 * IID is a good idea. Probably we should study some alternative
31 * design.
32 */
33
34 /**
35 * This class is a factory for invocation identifiers. It uses a
36 * sequence number to generate unique identifiers, and keeps track of
37 * invocation identifiers for which a response has been received. This
38 * information is used to generate acknowledgement information sent to
39 * servers in order that they can remove replies from their tables.
40 *
41 * @author Alberto Montresor
42 * @since Jgroup 0.8
43 */
44 public class IIDFactory
45 {
46
47 ////////////////////////////////////////////////////////////////////////////////////////////
48 // Fields
49 ////////////////////////////////////////////////////////////////////////////////////////////
50
51 /** Unique id generated for this factory */
52 private VMID vmid;
53
54 /** Counter used to create unique identifiers */
55 private int counter;
56
57 /**
58 * Set containing the identifiers of the invocations for which a
59 * reply has been received. Because all identifiers are created
60 * through this class, we may safely maintain only the sequence
61 * number of those identifiers, instead of complete <code>IID</code>
62 * objects.
63 */
64 private IntList replies;
65
66 /**
67 * Contains the final value of the first sequence of consecutive
68 * identifiers for which a reply has been received.
69 */
70 private int ack;
71
72
73 ////////////////////////////////////////////////////////////////////////////////////////////
74 // Constructor
75 ////////////////////////////////////////////////////////////////////////////////////////////
76
77 /**
78 * Initializes a new <code>IIDFactory</code> object by setting the
79 * sequence number to 0 and creating an empty replies list. Ack
80 * information is also set to 0.
81 */
82 public IIDFactory()
83 {
84 vmid = VMIDImpl.getLocalVMID();
85 counter = 0;
86 replies = new IntList();
87 ack = 0;
88 }
89
90
91 ////////////////////////////////////////////////////////////////////////////////////////////
92 // Methods
93 ////////////////////////////////////////////////////////////////////////////////////////////
94
95 /**
96 * Creates a new invocation identifier. It increases the sequence
97 * number and constructs a new <code>IIDImpl</code> object with the
98 * new sequence number; acknowledgement information is piggybacked in
99 * the identifier.
100 */
101 public synchronized IID getId()
102 {
103 return new IIDImpl(vmid, ++counter, ack);
104 }
105
106 /**
107 * Adds the given invocation identifier to the set of identifiers for
108 * which a reply has been received.
109 */
110 public synchronized void addReply(IID id)
111 {
112 replies.insert(id.getCounter());
113 ack = replies.getMaxConsecutive();
114 if (ack < 0)
115 ack = 0;
116 }
117
118 } // END IIDFactory