aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/session/ConnectionManager.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-02 17:11:17 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-02 17:11:17 -0700
commit6e1d43d86414281ca562c45488b82808936fc980 (patch)
treeb26800225a5439c86d9fc441f96bb690dde4f77a /src/main/java/org/traccar/session/ConnectionManager.java
parent5032f99c6462ca97f3629abe1faf2f50cc4977fe (diff)
downloadtrackermap-server-6e1d43d86414281ca562c45488b82808936fc980.tar.gz
trackermap-server-6e1d43d86414281ca562c45488b82808936fc980.tar.bz2
trackermap-server-6e1d43d86414281ca562c45488b82808936fc980.zip
Integrate cache manager
Diffstat (limited to 'src/main/java/org/traccar/session/ConnectionManager.java')
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index 5d8a8c606..e01a568aa 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -29,6 +29,7 @@ import org.traccar.handler.events.OverspeedEventHandler;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
import java.net.InetSocketAddress;
@@ -51,6 +52,8 @@ public class ConnectionManager {
private final Map<Long, DeviceSession> sessionsByDeviceId = new ConcurrentHashMap<>();
private final Map<Endpoint, Map<String, DeviceSession>> sessionsByEndpoint = new ConcurrentHashMap<>();
+ private final CacheManager cacheManager;
+
private final Map<Long, Set<UpdateListener>> listeners = new ConcurrentHashMap<>();
private final Map<Long, Timeout> timeouts = new ConcurrentHashMap<>();
@@ -60,6 +63,7 @@ public class ConnectionManager {
deviceTimeout = Context.getConfig().getLong(Keys.STATUS_TIMEOUT) * 1000;
updateDeviceState = Context.getConfig().getBoolean(Keys.STATUS_UPDATE_DEVICE_STATE);
timer = Main.getInjector().getInstance(Timer.class);
+ cacheManager = Main.getInjector().getInstance(CacheManager.class);
}
public DeviceSession getDeviceSession(long deviceId) {
@@ -67,7 +71,8 @@ public class ConnectionManager {
}
public DeviceSession getDeviceSession(
- Protocol protocol, Channel channel, SocketAddress remoteAddress, String... uniqueIds) {
+ Protocol protocol, Channel channel, SocketAddress remoteAddress,
+ String... uniqueIds) throws StorageException {
Endpoint endpoint = new Endpoint(channel, remoteAddress);
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.getOrDefault(
@@ -116,6 +121,7 @@ public class ConnectionManager {
endpointSessions.put(device.getUniqueId(), deviceSession);
sessionsByEndpoint.put(endpoint, endpointSessions);
sessionsByDeviceId.put(device.getId(), deviceSession);
+ cacheManager.addDevice(device.getId());
return deviceSession;
} else {
@@ -125,17 +131,31 @@ public class ConnectionManager {
}
}
- public void removeDeviceSessions(Channel channel) {
+ public void deviceDisconnected(Channel channel) {
Endpoint endpoint = new Endpoint(channel, channel.remoteAddress());
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.remove(endpoint);
if (endpointSessions != null) {
for (DeviceSession deviceSession : endpointSessions.values()) {
updateDevice(deviceSession.getDeviceId(), Device.STATUS_OFFLINE, null);
sessionsByDeviceId.remove(deviceSession.getDeviceId());
+ cacheManager.removeDevice(deviceSession.getDeviceId());
}
}
}
+ public void deviceUnknown(long deviceId) {
+ updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
+ DeviceSession deviceSession = sessionsByDeviceId.remove(deviceId);
+ cacheManager.removeDevice(deviceId);
+ if (deviceSession != null) {
+ Endpoint endpoint = new Endpoint(deviceSession.getChannel(), deviceSession.getRemoteAddress());
+ sessionsByEndpoint.computeIfPresent(endpoint, (e, sessions) -> {
+ sessions.remove(deviceSession.getUniqueId());
+ return sessions.isEmpty() ? null : sessions;
+ });
+ }
+ }
+
public void updateDevice(final long deviceId, String status, Date time) {
Device device = Context.getIdentityManager().getById(deviceId);
if (device == null) {
@@ -181,7 +201,7 @@ public class ConnectionManager {
if (status.equals(Device.STATUS_ONLINE)) {
timeouts.put(deviceId, timer.newTimeout(timeout1 -> {
if (!timeout1.isCancelled()) {
- updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
+ deviceUnknown(deviceId);
}
}, deviceTimeout, TimeUnit.MILLISECONDS));
}