source: src/main/java/geniusweb/partiesserver/RunParty.java@ 39

Last change on this file since 39 was 39, checked in by bart, 3 years ago

Added time-dependent parties for python and simpleRunner-GUI for java

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