1 | package geniusweb.partiesserver;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.net.UnknownHostException;
|
---|
5 | import java.util.logging.Level;
|
---|
6 |
|
---|
7 | import javax.management.MalformedObjectNameException;
|
---|
8 | import javax.servlet.ServletException;
|
---|
9 | import javax.servlet.http.HttpServlet;
|
---|
10 | import javax.servlet.http.HttpServletRequest;
|
---|
11 | import javax.servlet.http.HttpServletResponse;
|
---|
12 |
|
---|
13 | import geniusweb.partiesserver.repository.AvailablePartiesRepo;
|
---|
14 | import geniusweb.partiesserver.repository.AvailableParty;
|
---|
15 | import geniusweb.partiesserver.repository.RunningPartiesRepo;
|
---|
16 | import geniusweb.partiesserver.repository.RunningParty;
|
---|
17 | import tudelft.utilities.logging.ReportToLogger;
|
---|
18 | import tudelft.utilities.logging.Reporter;
|
---|
19 | import tudelft.utilities.repository.NoResourcesNowException;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Servlet implementation class RunParty. It's behind the url
|
---|
23 | * partiesserver/run/NAME where NAME is the name of the party that needs to be
|
---|
24 | * run.
|
---|
25 | */
|
---|
26 | public class RunParty extends HttpServlet {
|
---|
27 | private static final int DEFAULT_TIMEOUT = 3000; // ms
|
---|
28 | private static final long serialVersionUID = 1L;
|
---|
29 | private final Reporter log;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @see HttpServlet#HttpServlet()
|
---|
33 | */
|
---|
34 | public RunParty() {
|
---|
35 | this(new ReportToLogger("partiesserver"));
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | *
|
---|
40 | * @param reporter the reporter where we log issues to.
|
---|
41 | */
|
---|
42 | public RunParty(Reporter reporter) {
|
---|
43 | this.log = reporter;
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * See
|
---|
48 | * {@link HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)}.
|
---|
49 | * Returns
|
---|
50 | * <ul>
|
---|
51 | * <li>URL with websocket address to contact the now running agent.
|
---|
52 | * <li>SC_NOT_FOUND (404) if attempt is made to access non-running party.
|
---|
53 | * <li>SC_SERVICE_UNAVAILABLE (503) if there are no free slots to run the
|
---|
54 | * party.
|
---|
55 | * <li>SC_EXPECTATION_FAILED (417) if something else goes wrong during
|
---|
56 | * instantiating the party (then see logs for more details)
|
---|
57 | * </ul>
|
---|
58 | */
|
---|
59 | @Override
|
---|
60 | protected void doGet(HttpServletRequest request,
|
---|
61 | HttpServletResponse response) throws ServletException, IOException {
|
---|
62 | String name = request.getPathInfo().substring(1);
|
---|
63 | log.log(Level.INFO, "request to run " + name);
|
---|
64 | System.setSecurityManager(new SimpleSecurityManager());
|
---|
65 | AvailableParty party = AvailablePartiesRepo.instance().get(name);
|
---|
66 | if (party == null) {
|
---|
67 | log.log(Level.INFO, "party " + name + " does not exist");
|
---|
68 | response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | // try to avoid needless creation of the party.
|
---|
72 | if (RunningPartiesRepo.instance().availableSlots() <= 0) {
|
---|
73 | tryLater(response, name);
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | RunningParty running;
|
---|
78 | try {
|
---|
79 | running = RunningParty.create(party, DEFAULT_TIMEOUT);
|
---|
80 | } catch (Throwable e1) {
|
---|
81 | log.log(Level.WARNING, "Failed to instantiate party " + name, e1);
|
---|
82 | response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED,
|
---|
83 | "Failed to instantiate party " + name + ":"
|
---|
84 | + e1.getMessage()
|
---|
85 | + ". See partiesserver log for stack trace");
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // in the mean time, another party may have taken the slot anyway...
|
---|
90 | // the chances that this happens is small though.
|
---|
91 | try {
|
---|
92 | RunningPartiesRepo.instance().put(running);
|
---|
93 | } catch (NoResourcesNowException e) {
|
---|
94 | running.getParty().terminate();
|
---|
95 | tryLater(response, name);
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | log.log(Level.INFO, "started " + name);
|
---|
99 | String url;
|
---|
100 | try {
|
---|
101 | url = "ws://" + Constants.getIpAddressAndPort()
|
---|
102 | + request.getContextPath() + "/party/" + running.getID();
|
---|
103 | } catch (MalformedObjectNameException | UnknownHostException e) {
|
---|
104 | throw new IOException("Failure creating websocket address", e);
|
---|
105 | }
|
---|
106 | response.getWriter().append(url);
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
|
---|
112 | * response)
|
---|
113 | */
|
---|
114 | @Override
|
---|
115 | protected void doPost(HttpServletRequest request,
|
---|
116 | HttpServletResponse response) throws ServletException, IOException {
|
---|
117 | // TODO Auto-generated method stub
|
---|
118 | doGet(request, response);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Reply with 503 plus message that the caller should try later
|
---|
123 | *
|
---|
124 | * @param response the response container
|
---|
125 | * @param name the name of the party that was attempted to launch
|
---|
126 | * @throws IOException
|
---|
127 | */
|
---|
128 | private void tryLater(HttpServletResponse response, String name)
|
---|
129 | throws IOException {
|
---|
130 | long time = RunningPartiesRepo.instance().estimateCleanupTime()
|
---|
131 | .getTime();
|
---|
132 | log.log(Level.INFO, "There are no free slots to run the party " + name
|
---|
133 | + ". suggesting " + time);
|
---|
134 | // sendError does not always work as expected, avoid it.
|
---|
135 | response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
---|
136 | response.getWriter().append("retry later at " + time);
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|