1 | package geniusweb.partiesserver.repository;
|
---|
2 |
|
---|
3 | import java.net.InetAddress;
|
---|
4 | import java.net.UnknownHostException;
|
---|
5 | import java.util.Date;
|
---|
6 | import java.util.UUID;
|
---|
7 |
|
---|
8 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
10 |
|
---|
11 | import geniusweb.actions.PartyId;
|
---|
12 | import geniusweb.connection.DefaultConnection;
|
---|
13 | import geniusweb.inform.Inform;
|
---|
14 | import geniusweb.partiesserver.RunningPartiesUpdater;
|
---|
15 | import geniusweb.partiesserver.websocket.PartySocket;
|
---|
16 | import geniusweb.party.Party;
|
---|
17 | import 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)
|
---|
27 | public 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 PartySocket 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, 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 | * 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
|
---|
104 | * {@link RunningPartiesUpdater} will keep an eye on the
|
---|
105 | * time and handle removal after time is up.
|
---|
106 | *
|
---|
107 | */
|
---|
108 | public static RunningParty create(Party party, String name,
|
---|
109 | long maxRunTimeMS) {
|
---|
110 | if (maxRunTimeMS <= 0) {
|
---|
111 | throw new IllegalArgumentException("runtime must be >0");
|
---|
112 | }
|
---|
113 | Date now = new Date();
|
---|
114 | return new RunningParty(party,
|
---|
115 | new PartyId("party" + (nr++) + "_" + uniqueid), name, now,
|
---|
116 | new Date(now.getTime() + maxRunTimeMS), null);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * @return unique ID for this running party
|
---|
121 | */
|
---|
122 | @Override
|
---|
123 | public PartyId getID() {
|
---|
124 | return id;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * @return the implementation of this party
|
---|
129 | */
|
---|
130 | public Party getParty() {
|
---|
131 | return party;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * The name, this should match the filename in the repo (without the .jar).
|
---|
136 | *
|
---|
137 | * @return
|
---|
138 | */
|
---|
139 | public String getName() {
|
---|
140 | return name;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * @return the start {@link Date} of this runningparty.
|
---|
145 | */
|
---|
146 | public Date getStartDate() {
|
---|
147 | return startDate;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | *
|
---|
152 | * @return the end date/ deadline for this party
|
---|
153 | */
|
---|
154 | public Date getEndDate() {
|
---|
155 | return endDate;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public int hashCode() {
|
---|
160 | final int prime = 31;
|
---|
161 | int result = 1;
|
---|
162 | result = prime * result + ((id == null) ? 0 : id.hashCode());
|
---|
163 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
164 | result = prime * result + ((party == null) ? 0 : party.hashCode());
|
---|
165 | result = prime * result
|
---|
166 | + ((startDate == null) ? 0 : startDate.hashCode());
|
---|
167 | return result;
|
---|
168 | }
|
---|
169 |
|
---|
170 | @Override
|
---|
171 | public boolean equals(Object obj) {
|
---|
172 | if (this == obj)
|
---|
173 | return true;
|
---|
174 | if (obj == null)
|
---|
175 | return false;
|
---|
176 | if (getClass() != obj.getClass())
|
---|
177 | return false;
|
---|
178 | RunningParty other = (RunningParty) obj;
|
---|
179 | if (id == null) {
|
---|
180 | if (other.id != null)
|
---|
181 | return false;
|
---|
182 | } else if (!id.equals(other.id))
|
---|
183 | return false;
|
---|
184 | if (name == null) {
|
---|
185 | if (other.name != null)
|
---|
186 | return false;
|
---|
187 | } else if (!name.equals(other.name))
|
---|
188 | return false;
|
---|
189 | if (party == null) {
|
---|
190 | if (other.party != null)
|
---|
191 | return false;
|
---|
192 | } else if (!party.equals(other.party))
|
---|
193 | return false;
|
---|
194 | if (startDate == null) {
|
---|
195 | if (other.startDate != null)
|
---|
196 | return false;
|
---|
197 | } else if (!startDate.equals(other.startDate))
|
---|
198 | return false;
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 |
|
---|
202 | @Override
|
---|
203 | public String toString() {
|
---|
204 | return "RunningParty[" + id + "," + startDate + party.getClass() + ","
|
---|
205 | + name + "]";
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * You must have called {@link #connect(DefaultConnection)} before calling
|
---|
210 | * this.
|
---|
211 | *
|
---|
212 | * @param info
|
---|
213 | */
|
---|
214 | public void inform(Inform info) {
|
---|
215 | connection.notifyListeners(info);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Attach RunningParty to a connection. We allow at most 1 connection.
|
---|
220 | *
|
---|
221 | * @param newconn the new connection that has been made with this party.
|
---|
222 | * @return new RunningParty. This must be inserted into the
|
---|
223 | * {@link RunningPartiesRepo} by the caller.
|
---|
224 | * @throws IllegalStateException if the party is already connected.
|
---|
225 | */
|
---|
226 | public RunningParty withConnection(PartySocket 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 | }
|
---|