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

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

Initial Release

File size: 7.4 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.party.Party;
15import geniusweb.party.inform.Inform;
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 DefaultConnection<Inform, Action> 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 start the start date for this party. Usually set to current time.
54 * @param end the end date for this party
55 * @param conn the connection to use, or null if no connection is available
56 * yet.
57 */
58 protected RunningParty(Party party, PartyId id, String name, Date start,
59 Date end, DefaultConnection conn) {
60 if (party == null || id == null || name == null || start == null
61 || end == null) {
62 throw new NullPointerException("arguments must not be null");
63 }
64 this.party = party;
65 this.id = id;
66 this.name = name;
67 this.startDate = start;
68 this.endDate = end;
69 this.connection = conn;
70 }
71
72 /**
73 * Create instance from availableparty
74 *
75 * @param availableparty the {@link AvailableParty}
76 * @param name the (file)name
77 * @param maxlifetimems the maximum life time for this party (ms). The
78 * party will be killed and removed after this point.
79 * This value should be set to the maximum negotiation
80 * time plus the worst time to actual start up of the
81 * negotiation
82 * @throws IllegalAccessException
83 * @throws InstantiationException
84 */
85 public static RunningParty create(AvailableParty availableparty,
86 long maxlifetimems)
87 throws InstantiationException, IllegalAccessException {
88 if (availableparty == null) {
89 throw new IllegalArgumentException(
90 "availableparty must not be null");
91 }
92 return create(availableparty.getPartyClass().newInstance(),
93 availableparty.getName(), maxlifetimems);
94 }
95
96 /**
97 * Create a running party.
98 *
99 * @param party the {@link Party} that is loaded and ready to run.
100 * @param name The name, this should match the filename in the repo
101 * (without the .jar).
102 * @param maxRunTimeMS the maximum runtime for this party (ms)
103 */
104 public static RunningParty create(Party party, String name,
105 long maxRunTimeMS) {
106 if (maxRunTimeMS <= 0) {
107 throw new IllegalArgumentException("runtime must be >0");
108 }
109 Date now = new Date();
110 return new RunningParty(party,
111 new PartyId("party" + (nr++) + "_" + uniqueid), name, now,
112 new Date(now.getTime() + maxRunTimeMS), null);
113 }
114
115 /**
116 * @return unique ID for this running party
117 */
118 @Override
119 public PartyId getID() {
120 return id;
121 }
122
123 /**
124 * @return the implementation of this party
125 */
126 public Party getParty() {
127 return party;
128 }
129
130 /**
131 * The name, this should match the filename in the repo (without the .jar).
132 *
133 * @return
134 */
135 public String getName() {
136 return name;
137 }
138
139 /**
140 * @return the start {@link Date} of this runningparty.
141 */
142 public Date getStartDate() {
143 return startDate;
144 }
145
146 /**
147 *
148 * @return the end date/ deadline for this party
149 */
150 public Date getEndDate() {
151 return endDate;
152 }
153
154 @Override
155 public int hashCode() {
156 final int prime = 31;
157 int result = 1;
158 result = prime * result + ((id == null) ? 0 : id.hashCode());
159 result = prime * result + ((name == null) ? 0 : name.hashCode());
160 result = prime * result + ((party == null) ? 0 : party.hashCode());
161 result = prime * result
162 + ((startDate == null) ? 0 : startDate.hashCode());
163 return result;
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (this == obj)
169 return true;
170 if (obj == null)
171 return false;
172 if (getClass() != obj.getClass())
173 return false;
174 RunningParty other = (RunningParty) obj;
175 if (id == null) {
176 if (other.id != null)
177 return false;
178 } else if (!id.equals(other.id))
179 return false;
180 if (name == null) {
181 if (other.name != null)
182 return false;
183 } else if (!name.equals(other.name))
184 return false;
185 if (party == null) {
186 if (other.party != null)
187 return false;
188 } else if (!party.equals(other.party))
189 return false;
190 if (startDate == null) {
191 if (other.startDate != null)
192 return false;
193 } else if (!startDate.equals(other.startDate))
194 return false;
195 return true;
196 }
197
198 @Override
199 public String toString() {
200 return "RunningParty[" + id + "," + startDate + party.getClass() + ","
201 + name + "]";
202 }
203
204 /**
205 * You must have called {@link #connect(DefaultConnection)} before calling
206 * this.
207 *
208 * @param info
209 */
210 public void inform(Inform info) {
211 connection.notifyChange(info);
212 }
213
214 /**
215 * Attach RunningParty to a connection. We allow at most 1 connection.
216 *
217 * @param newconn the new connection that has been made with this party.
218 * @return new RunningParty. This must be inserted into the
219 * {@link RunningPartiesRepo} by the caller.
220 * @throws IllegalStateException if the party is already connected.
221 */
222 public RunningParty withConnection(
223 DefaultConnection<Inform, Action> newconn) {
224 if (newconn == null)
225 throw new NullPointerException("newconn must be not null");
226 if (this.connection != null) {
227 throw new IllegalStateException(
228 "RunningParty is already connected");
229 }
230 party.connect(newconn);
231 return new RunningParty(party, id, name, startDate, endDate, newconn);
232 }
233
234 /**
235 *
236 * @param terminationTime the new termination time
237 * @return new RunningParty with changed end time (all other things
238 * unchanged)
239 */
240 public RunningParty withEndTime(Date terminationTime) {
241 return new RunningParty(party, id, name, startDate, terminationTime,
242 connection);
243 }
244
245 /**
246 * Get a id that is world-wide unique. We use the IP number here instead of
247 * UUID because UUID usually is just a random number which is (1) long (22
248 * bytes) (2) not guaranteed unique.
249 */
250 private static String getUniqueMachineID() {
251 if (uniqueid == null) {
252 try {
253 uniqueid = InetAddress.getLocalHost().getHostAddress();
254 } catch (UnknownHostException e1) {
255 uniqueid = UUID.randomUUID().toString(); // fallback, should
256 // never be needed
257 }
258 uniqueid = uniqueid.replaceAll("\\.", "_");
259 }
260 return uniqueid;
261 }
262
263}
Note: See TracBrowser for help on using the repository browser.