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

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

Faster example parties

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