source: src/main/java/geniusweb/partiesserver/repository/RunningParty.java@ 27

Last change on this file since 27 was 27, checked in by bart, 4 years ago

some minor fixes

File size: 8.1 KB
Line 
1package geniusweb.partiesserver.repository;
2
3import java.net.InetAddress;
4import java.net.UnknownHostException;
5import java.util.Date;
6import java.util.UUID;
7
8import com.fasterxml.jackson.annotation.JsonAutoDetect;
9import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
10
11import geniusweb.actions.PartyId;
12import geniusweb.inform.Inform;
13import geniusweb.partiesserver.RunningPartiesUpdater;
14import geniusweb.partiesserver.websocket.PartySocket;
15import geniusweb.party.Party;
16import tudelft.utilities.repository.RepoElement;
17
18/**
19 * Class that contains a running party. Adds a start/end times and generates
20 * party ID. Also stores original filename of the party.
21 *
22 * This object can not be deserialized here because running parties have to be
23 * created.
24 */
25@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
26public class RunningParty implements RepoElement<PartyId> {
27 private static int nr = 1; // counter for generating unique new parties.
28 private static String uniqueid = getUniqueMachineID(); // world-wide unique
29 // ID for this
30 // number. For ID
31 // generation.
32
33 private final PartyId id;
34 private final Date startDate;
35 private final Date endDate; // agent will be declared dead and removed after
36 // this
37 private final String name;
38 /*
39 * set when someone contacts our socket. null if not set.
40 */
41 private final transient PartySocket connection;
42
43 /**
44 * The actually running party
45 */
46 private transient final Party party;
47
48 /**
49 * Only available for testing.
50 *
51 * @param party the new party
52 * @param id the new id for the party.
53 * @param name the name, this should match the filename
54 * @param start the start date for this party. Usually set to current time.
55 * @param end the end date for this party
56 * @param conn the connection to use, or null if no connection is available
57 * yet.
58 */
59 protected RunningParty(Party party, PartyId id, String name, Date start,
60 Date end, PartySocket conn) {
61 if (party == null || id == null || name == null || start == null
62 || end == null) {
63 throw new NullPointerException("arguments must not be null");
64 }
65 this.party = party;
66 this.id = id;
67 this.name = name;
68 this.startDate = start;
69 this.endDate = end;
70 this.connection = conn;
71 }
72
73 /**
74 * @param availableparty the {@link AvailableParty}
75 * @param maxlifetimems the maximum life time for this party (ms). The
76 * party will be killed and removed after this point.
77 * This value should be set to the maximum negotiation
78 * time plus the worst time to actual start up of the
79 * negotiation
80 * @return a new RunningParty being an instance of availableparty
81 * @throws IllegalAccessException if the class or its nullary constructor is
82 * not accessible.
83 *
84 * @throws InstantiationException if this Class represents an abstract
85 * class, an interface, an array class, a
86 * primitive type, or void; or if the class
87 * has no nullary constructor; or if the
88 * instantiation fails for some other reason.
89 *
90 */
91 public static RunningParty create(AvailableParty availableparty,
92 long maxlifetimems)
93 throws InstantiationException, IllegalAccessException {
94 if (availableparty == null) {
95 throw new IllegalArgumentException(
96 "availableparty must not be null");
97 }
98 return create(availableparty.getPartyClass().newInstance(),
99 availableparty.getName(), maxlifetimems);
100 }
101
102 /**
103 * Create a running party.
104 *
105 * @param party the {@link Party} that is loaded and ready to run.
106 * @param name The name, this should match the filename in the repo
107 * (without the .jar).
108 * @param maxRunTimeMS the maximum runtime for this party (ms). The
109 * {@link RunningPartiesUpdater} will keep an eye on the
110 * time and handle removal after time is up.
111 * @return new RunningParty created from the Party instance.
112 *
113 */
114 private static RunningParty create(Party party, String name,
115 long maxRunTimeMS) {
116 if (maxRunTimeMS <= 0) {
117 throw new IllegalArgumentException("runtime must be >0");
118 }
119 Date now = new Date();
120 return new RunningParty(party,
121 new PartyId("party" + (nr++) + "_" + uniqueid), name, now,
122 new Date(now.getTime() + maxRunTimeMS), null);
123 }
124
125 /**
126 * @return unique ID for this running party
127 */
128 @Override
129 public PartyId getID() {
130 return id;
131 }
132
133 /**
134 * @return the implementation of this party
135 */
136 public Party getParty() {
137 return party;
138 }
139
140 /**
141 * @return The name, this should match the filename in the repo (without the
142 * .jar).
143 *
144 */
145 public String getName() {
146 return name;
147 }
148
149 /**
150 * @return the start {@link Date} of this runningparty.
151 */
152 public Date getStartDate() {
153 return startDate;
154 }
155
156 /**
157 *
158 * @return the end date/ deadline for this party
159 */
160 public Date getEndDate() {
161 return endDate;
162 }
163
164 @Override
165 public int hashCode() {
166 final int prime = 31;
167 int result = 1;
168 result = prime * result + ((id == null) ? 0 : id.hashCode());
169 result = prime * result + ((name == null) ? 0 : name.hashCode());
170 result = prime * result + ((party == null) ? 0 : party.hashCode());
171 result = prime * result
172 + ((startDate == null) ? 0 : startDate.hashCode());
173 return result;
174 }
175
176 @Override
177 public boolean equals(Object obj) {
178 if (this == obj)
179 return true;
180 if (obj == null)
181 return false;
182 if (getClass() != obj.getClass())
183 return false;
184 RunningParty other = (RunningParty) obj;
185 if (id == null) {
186 if (other.id != null)
187 return false;
188 } else if (!id.equals(other.id))
189 return false;
190 if (name == null) {
191 if (other.name != null)
192 return false;
193 } else if (!name.equals(other.name))
194 return false;
195 if (party == null) {
196 if (other.party != null)
197 return false;
198 } else if (!party.equals(other.party))
199 return false;
200 if (startDate == null) {
201 if (other.startDate != null)
202 return false;
203 } else if (!startDate.equals(other.startDate))
204 return false;
205 return true;
206 }
207
208 @Override
209 public String toString() {
210 return "RunningParty[" + id + "," + startDate + party.getClass() + ","
211 + name + "]";
212 }
213
214 /**
215 * You must have called {@link #withConnection(PartySocket)} before calling
216 * this.
217 *
218 * @param info the {@link Inform} to be sent to the {@link #connection}
219 */
220 public void inform(Inform info) {
221 connection.notifyListeners(info);
222 }
223
224 /**
225 * Attach RunningParty to a connection. We allow at most 1 connection.
226 *
227 * @param newconn the new connection that has been made with this party.
228 * @return new RunningParty. This must be inserted into the
229 * {@link RunningPartiesRepo} by the caller.
230 * @throws IllegalStateException if the party is already connected.
231 */
232 public RunningParty withConnection(PartySocket newconn) {
233 if (newconn == null)
234 throw new NullPointerException("newconn must be not null");
235 if (this.connection != null) {
236 throw new IllegalStateException(
237 "RunningParty is already connected");
238 }
239 party.connect(newconn);
240 return new RunningParty(party, id, name, startDate, endDate, newconn);
241 }
242
243 /**
244 *
245 * @param terminationTime the new termination time
246 * @return new RunningParty with changed end time (all other things
247 * unchanged)
248 */
249 public RunningParty withEndTime(Date terminationTime) {
250 return new RunningParty(party, id, name, startDate, terminationTime,
251 connection);
252 }
253
254 /**
255 * Get a id that is world-wide unique. We use the IP number here instead of
256 * UUID because UUID usually is just a random number which is (1) long (22
257 * bytes) (2) not guaranteed unique.
258 */
259 private static String getUniqueMachineID() {
260 if (uniqueid == null) {
261 try {
262 uniqueid = InetAddress.getLocalHost().getHostAddress();
263 } catch (UnknownHostException e1) {
264 uniqueid = UUID.randomUUID().toString(); // fallback, should
265 // never be needed
266 }
267 uniqueid = uniqueid.replaceAll("\\.", "_");
268 }
269 return uniqueid;
270 }
271
272}
Note: See TracBrowser for help on using the repository browser.