aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-06-26 19:42:32 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-06-26 19:42:32 +1200
commit86d96506f51fc4c49af0487e00ecd2d5ee02218c (patch)
treec5d639518b6c6fc77a3db94fd9cd81c6778cb0bb
parent88c87b2ce54733c06db810e51a749797ba03e913 (diff)
downloadtrackermap-server-86d96506f51fc4c49af0487e00ecd2d5ee02218c.tar.gz
trackermap-server-86d96506f51fc4c49af0487e00ecd2d5ee02218c.tar.bz2
trackermap-server-86d96506f51fc4c49af0487e00ecd2d5ee02218c.zip
Implement session updates
-rw-r--r--src/org/traccar/database/DataManager.java4
-rw-r--r--src/org/traccar/http/AsyncServlet.java29
2 files changed, 29 insertions, 4 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index e2478ecd6..92f19b903 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -35,6 +35,7 @@ import javax.sql.DataSource;
import org.traccar.Context;
import org.traccar.helper.DriverDelegate;
import org.traccar.helper.Log;
+import org.traccar.http.AsyncServlet;
import org.traccar.http.JsonConverter;
import org.traccar.model.Device;
import org.traccar.model.Permission;
@@ -283,6 +284,8 @@ public class DataManager {
QueryBuilder.create(dataSource, getQuery("database.deleteDevice"))
.setObject(device)
.executeUpdate();
+ Context.getPermissionsManager().refresh();
+ AsyncServlet.sessionRefreshDevice(device.getId());
}
public void linkDevice(long userId, long deviceId) throws SQLException {
@@ -291,6 +294,7 @@ public class DataManager {
.setLong("deviceId", deviceId)
.executeUpdate();
Context.getPermissionsManager().refresh();
+ AsyncServlet.sessionRefreshUser(userId);
}
public Collection<Position> getPositions(long userId, long deviceId, Date from, Date to) throws SQLException {
diff --git a/src/org/traccar/http/AsyncServlet.java b/src/org/traccar/http/AsyncServlet.java
index 5481f4ba3..99929731f 100644
--- a/src/org/traccar/http/AsyncServlet.java
+++ b/src/org/traccar/http/AsyncServlet.java
@@ -18,7 +18,11 @@ package org.traccar.http;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
@@ -51,11 +55,11 @@ public class AsyncServlet extends HttpServlet {
private static final boolean DEBUG_ASYNC = false;
private static final long SESSION_TIMEOUT = 30;
- private static final long REQUEST_TIMEOUT = 10;
+ private static final long REQUEST_TIMEOUT = 20;
private boolean destroyed;
private final long userId;
- private final Collection<Long> devices;
+ private final Set<Long> devices = new HashSet<Long>();
private Timeout sessionTimeout;
private Timeout requestTimeout;
private final Map<Long, Position> positions = new HashMap<Long, Position>();
@@ -70,7 +74,7 @@ public class AsyncServlet extends HttpServlet {
public AsyncSession(long userId, Collection<Long> devices) {
logEvent("create userId: " + userId + " devices: " + devices.size());
this.userId = userId;
- this.devices = devices;
+ this.devices.addAll(devices);
Collection<Position> initialPositions = Context.getDataCache().getInitialState(devices);
for (Position position : initialPositions) {
@@ -80,6 +84,10 @@ public class AsyncServlet extends HttpServlet {
Context.getDataCache().addListener(devices, dataListener);
}
+ public boolean hasDevice(long deviceId) {
+ return devices.contains(deviceId);
+ }
+
private final DataCache.DataCacheListener dataListener = new DataCache.DataCacheListener() {
@Override
public void onUpdate(Position position) {
@@ -171,7 +179,20 @@ public class AsyncServlet extends HttpServlet {
}
- private final Map<Long, AsyncSession> asyncSessions = new HashMap<Long, AsyncSession>();
+ private static final Map<Long, AsyncSession> asyncSessions = new HashMap<Long, AsyncSession>();
+
+ public static void sessionRefreshUser(long userId) {
+ asyncSessions.remove(userId);
+ }
+
+ public static void sessionRefreshDevice(long deviceId) {
+ Iterator<Entry<Long, AsyncSession>> iterator = asyncSessions.entrySet().iterator();
+ while (iterator.hasNext()) {
+ if (iterator.next().getValue().hasDevice(deviceId)) {
+ iterator.remove();
+ }
+ }
+ }
private void async(final AsyncContext context) {