Commit 873e48ec authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

If wait was set to a longer time then session timeout, the session would be...

If wait was set to a longer time then session timeout, the session would be timeout even though there were connections waiting for a response. Did some refactoring so that the session "knows" when all connections are closed and now returns the current time if there are currently connections actively awaiting a response when requested to return the last activity on a connection.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@6874 b35dd754-fafc-0310-a699-88a17e54d16e
parent ab7f66d7
...@@ -109,12 +109,13 @@ public class HttpBindServlet extends HttpServlet { ...@@ -109,12 +109,13 @@ public class HttpBindServlet extends HttpServlet {
private boolean isContinuation(HttpServletRequest request, HttpServletResponse response) private boolean isContinuation(HttpServletRequest request, HttpServletResponse response)
throws IOException throws IOException
{ {
HttpConnection connection = (HttpConnection) request.getAttribute("request-connection"); HttpSession session = (HttpSession) request.getAttribute("request-session");
if (connection == null) { if (session == null) {
return false; return false;
} }
synchronized (connection.getSession()) { synchronized (session) {
respond(response, connection); respond(response, session.getResponse((Long) request.getAttribute("request"))
.getBytes("utf-8"));
} }
return true; return true;
} }
...@@ -162,7 +163,8 @@ public class HttpBindServlet extends HttpServlet { ...@@ -162,7 +163,8 @@ public class HttpBindServlet extends HttpServlet {
else { else {
connection connection
.setContinuation(ContinuationSupport.getContinuation(request, connection)); .setContinuation(ContinuationSupport.getContinuation(request, connection));
request.setAttribute("request-connection", connection); request.setAttribute("request-session", connection.getSession());
request.setAttribute("request", connection.getRequestId());
respond(response, connection); respond(response, connection);
} }
} }
...@@ -218,7 +220,7 @@ public class HttpBindServlet extends HttpServlet { ...@@ -218,7 +220,7 @@ public class HttpBindServlet extends HttpServlet {
response.getOutputStream().write(content); response.getOutputStream().write(content);
} }
private String createEmptyBody() { private static String createEmptyBody() {
Element body = DocumentHelper.createElement("body"); Element body = DocumentHelper.createElement("body");
body.addNamespace("", "http://jabber.org/protocol/httpbind"); body.addNamespace("", "http://jabber.org/protocol/httpbind");
return body.asXML(); return body.asXML();
......
...@@ -42,7 +42,7 @@ public class HttpSession extends ClientSession { ...@@ -42,7 +42,7 @@ public class HttpSession extends ClientSession {
private int wait; private int wait;
private int hold = 0; private int hold = 0;
private String language; private String language;
private final Queue<HttpConnection> connectionQueue = new LinkedList<HttpConnection>(); private final List<HttpConnection> connectionQueue = new LinkedList<HttpConnection>();
private final List<Deliverable> pendingElements = new ArrayList<Deliverable>(); private final List<Deliverable> pendingElements = new ArrayList<Deliverable>();
private final List<Deliverable> sentElements = new ArrayList<Deliverable>(); private final List<Deliverable> sentElements = new ArrayList<Deliverable>();
private boolean isSecure; private boolean isSecure;
...@@ -296,13 +296,38 @@ public class HttpSession extends ClientSession { ...@@ -296,13 +296,38 @@ public class HttpSession extends ClientSession {
/** /**
* Returns the time in milliseconds since the epoch that this session was last active. Activity * Returns the time in milliseconds since the epoch that this session was last active. Activity
* is a request was either made or responded to. * is a request was either made or responded to. If the session is currently active, meaning
* there are connections awaiting a response, the current time is returned.
* *
* @return the time in milliseconds since the epoch that this session was last active. * @return the time in milliseconds since the epoch that this session was last active.
*/ */
public synchronized long getLastActivity() { public synchronized long getLastActivity() {
if (connectionQueue.size() <= 0) {
return lastActivity; return lastActivity;
} }
// The session is currently active, return the current time.
else {
return System.currentTimeMillis();
}
}
public String getResponse(long requestID) {
for (HttpConnection connection : connectionQueue) {
if (connection.getRequestId() == requestID) {
String response;
try {
response = connection.getResponse();
}
catch (HttpBindTimeoutException e) {
response = createEmptyBody();
}
connectionQueue.remove(connection);
fireConnectionClosed(connection);
return response;
}
}
throw new InternalError("Could not locate connection: " + requestID);
}
/** /**
* Sets whether the initial request on the session was secure. * Sets whether the initial request on the session was secure.
...@@ -393,11 +418,11 @@ public class HttpSession extends ClientSession { ...@@ -393,11 +418,11 @@ public class HttpSession extends ClientSession {
// With this connection we need to check if we will have too many connections open, // With this connection we need to check if we will have too many connections open,
// closing any extras. // closing any extras.
while (connectionQueue.size() >= hold) { while (connectionQueue.size() >= hold) {
HttpConnection toClose = connectionQueue.remove(); HttpConnection toClose = connectionQueue.remove(0);
toClose.close(); toClose.close();
fireConnectionClosed(toClose); fireConnectionClosed(toClose);
} }
connectionQueue.offer(connection); connectionQueue.add(connection);
fireConnectionOpened(connection); fireConnectionOpened(connection);
} }
lastRequestID = connection.getRequestId(); lastRequestID = connection.getRequestId();
...@@ -444,12 +469,11 @@ public class HttpSession extends ClientSession { ...@@ -444,12 +469,11 @@ public class HttpSession extends ClientSession {
private void deliver(Deliverable stanza) { private void deliver(Deliverable stanza) {
String deliverable = createDeliverable(Arrays.asList(stanza)); String deliverable = createDeliverable(Arrays.asList(stanza));
boolean delivered = false; boolean delivered = false;
while (!delivered && connectionQueue.size() > 0) { for (HttpConnection connection : connectionQueue) {
HttpConnection connection = connectionQueue.remove();
try { try {
deliver(connection, deliverable); deliver(connection, deliverable);
delivered = true; delivered = true;
fireConnectionClosed(connection); break;
} }
catch (HttpConnectionClosedException e) { catch (HttpConnectionClosedException e) {
/* Connection was closed, try the next one */ /* Connection was closed, try the next one */
...@@ -488,8 +512,7 @@ public class HttpSession extends ClientSession { ...@@ -488,8 +512,7 @@ public class HttpSession extends ClientSession {
failDelivery(); failDelivery();
} }
while (connectionQueue.size() > 0) { for (HttpConnection toClose : connectionQueue) {
HttpConnection toClose = connectionQueue.remove();
toClose.close(); toClose.close();
fireConnectionClosed(toClose); fireConnectionClosed(toClose);
} }
...@@ -511,6 +534,12 @@ public class HttpSession extends ClientSession { ...@@ -511,6 +534,12 @@ public class HttpSession extends ClientSession {
pendingElements.clear(); pendingElements.clear();
} }
private static String createEmptyBody() {
Element body = DocumentHelper.createElement("body");
body.addNamespace("", "http://jabber.org/protocol/httpbind");
return body.asXML();
}
/** /**
* A virtual server connection relates to a http session which its self can relate to many http * A virtual server connection relates to a http session which its self can relate to many http
* connections. * connections.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment