aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-14 17:06:44 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-14 17:06:44 -0700
commit415ba3ddb0f770b829c997beb3e575ffb6e195ec (patch)
tree82864fbaf0126b310d3d93abe40160bb371e52c6 /src/main/java/org/traccar
parente43169b2677ea9f07154247ce7ff726d046e59e2 (diff)
downloadtrackermap-server-415ba3ddb0f770b829c997beb3e575ffb6e195ec.tar.gz
trackermap-server-415ba3ddb0f770b829c997beb3e575ffb6e195ec.tar.bz2
trackermap-server-415ba3ddb0f770b829c997beb3e575ffb6e195ec.zip
Inject connection manager
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r--src/main/java/org/traccar/Context.java11
-rw-r--r--src/main/java/org/traccar/MainEventHandler.java8
-rw-r--r--src/main/java/org/traccar/MainModule.java6
-rw-r--r--src/main/java/org/traccar/api/AsyncSocket.java8
-rw-r--r--src/main/java/org/traccar/api/AsyncSocketServlet.java7
-rw-r--r--src/main/java/org/traccar/database/DeviceManager.java11
-rw-r--r--src/main/java/org/traccar/session/ConnectionManager.java76
7 files changed, 87 insertions, 40 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java
index 62ad01c24..a66cde464 100644
--- a/src/main/java/org/traccar/Context.java
+++ b/src/main/java/org/traccar/Context.java
@@ -78,12 +78,6 @@ public final class Context {
return deviceManager;
}
- private static ConnectionManager connectionManager;
-
- public static ConnectionManager getConnectionManager() {
- return connectionManager;
- }
-
private static PermissionsManager permissionsManager;
public static PermissionsManager getPermissionsManager() {
@@ -114,15 +108,14 @@ public final class Context {
if (dataManager != null) {
usersManager = new UsersManager(dataManager);
groupsManager = new GroupsManager(dataManager);
- deviceManager = new DeviceManager(dataManager);
+ deviceManager = new DeviceManager(
+ config, dataManager, Main.getInjector().getInstance(ConnectionManager.class));
}
identityManager = deviceManager;
permissionsManager = new PermissionsManager(dataManager, usersManager);
- connectionManager = new ConnectionManager();
-
initEventsModule();
}
diff --git a/src/main/java/org/traccar/MainEventHandler.java b/src/main/java/org/traccar/MainEventHandler.java
index bb84a09d2..d4a0fae6c 100644
--- a/src/main/java/org/traccar/MainEventHandler.java
+++ b/src/main/java/org/traccar/MainEventHandler.java
@@ -28,6 +28,7 @@ import org.traccar.database.StatisticsManager;
import org.traccar.helper.DateUtil;
import org.traccar.helper.NetworkUtil;
import org.traccar.model.Position;
+import org.traccar.session.ConnectionManager;
import org.traccar.session.cache.CacheManager;
import org.traccar.storage.StorageException;
@@ -44,8 +45,11 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
private final Set<String> connectionlessProtocols = new HashSet<>();
private final Set<String> logAttributes = new LinkedHashSet<>();
+ private final ConnectionManager connectionManager;
+
@Inject
- public MainEventHandler() {
+ public MainEventHandler(ConnectionManager connectionManager) {
+ this.connectionManager = connectionManager;
String connectionlessProtocolList = Context.getConfig().getString(Keys.STATUS_IGNORE_OFFLINE);
if (connectionlessProtocolList != null) {
connectionlessProtocols.addAll(Arrays.asList(connectionlessProtocolList.split("[, ]")));
@@ -132,7 +136,7 @@ public class MainEventHandler extends ChannelInboundHandlerAdapter {
if (BasePipelineFactory.getHandler(ctx.pipeline(), HttpRequestDecoder.class) == null
&& !connectionlessProtocols.contains(ctx.pipeline().get(BaseProtocolDecoder.class).getProtocolName())) {
- Context.getConnectionManager().deviceDisconnected(ctx.channel());
+ connectionManager.deviceDisconnected(ctx.channel());
}
}
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java
index 73fe0c460..306bc9e7b 100644
--- a/src/main/java/org/traccar/MainModule.java
+++ b/src/main/java/org/traccar/MainModule.java
@@ -35,7 +35,6 @@ import org.traccar.database.NotificationManager;
import org.traccar.database.UsersManager;
import org.traccar.helper.SanitizerModule;
import org.traccar.notification.EventForwarder;
-import org.traccar.session.ConnectionManager;
import org.traccar.database.DataManager;
import org.traccar.database.DeviceManager;
import org.traccar.database.IdentityManager;
@@ -136,11 +135,6 @@ public class MainModule extends AbstractModule {
}
@Provides
- public static ConnectionManager provideConnectionManager() {
- return Context.getConnectionManager();
- }
-
- @Provides
public static Client provideClient() {
return ClientBuilder.newClient().register(
(ContextResolver<ObjectMapper>) clazz -> Main.getInjector().getInstance(ObjectMapper.class));
diff --git a/src/main/java/org/traccar/api/AsyncSocket.java b/src/main/java/org/traccar/api/AsyncSocket.java
index 2b866176b..3239d36c4 100644
--- a/src/main/java/org/traccar/api/AsyncSocket.java
+++ b/src/main/java/org/traccar/api/AsyncSocket.java
@@ -41,10 +41,12 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U
private static final String KEY_EVENTS = "events";
private final ObjectMapper objectMapper;
+ private final ConnectionManager connectionManager;
private final long userId;
- public AsyncSocket(ObjectMapper objectMapper, long userId) {
+ public AsyncSocket(ObjectMapper objectMapper, ConnectionManager connectionManager, long userId) {
this.objectMapper = objectMapper;
+ this.connectionManager = connectionManager;
this.userId = userId;
}
@@ -56,14 +58,14 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U
data.put(KEY_POSITIONS, Context.getDeviceManager().getInitialState(userId));
sendData(data);
- Context.getConnectionManager().addListener(userId, this);
+ connectionManager.addListener(userId, this);
}
@Override
public void onWebSocketClose(int statusCode, String reason) {
super.onWebSocketClose(statusCode, reason);
- Context.getConnectionManager().removeListener(userId, this);
+ connectionManager.removeListener(userId, this);
}
@Override
diff --git a/src/main/java/org/traccar/api/AsyncSocketServlet.java b/src/main/java/org/traccar/api/AsyncSocketServlet.java
index 4e55dfebf..317ec469e 100644
--- a/src/main/java/org/traccar/api/AsyncSocketServlet.java
+++ b/src/main/java/org/traccar/api/AsyncSocketServlet.java
@@ -21,6 +21,7 @@ import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;
import org.traccar.Context;
import org.traccar.api.resource.SessionResource;
import org.traccar.config.Keys;
+import org.traccar.session.ConnectionManager;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
@@ -29,10 +30,12 @@ import java.time.Duration;
public class AsyncSocketServlet extends JettyWebSocketServlet {
private final ObjectMapper objectMapper;
+ private final ConnectionManager connectionManager;
@Inject
- public AsyncSocketServlet(ObjectMapper objectMapper) {
+ public AsyncSocketServlet(ObjectMapper objectMapper, ConnectionManager connectionManager) {
this.objectMapper = objectMapper;
+ this.connectionManager = connectionManager;
}
@Override
@@ -41,7 +44,7 @@ public class AsyncSocketServlet extends JettyWebSocketServlet {
factory.setCreator((req, resp) -> {
if (req.getSession() != null) {
long userId = (Long) ((HttpSession) req.getSession()).getAttribute(SessionResource.USER_ID_KEY);
- return new AsyncSocket(objectMapper, userId);
+ return new AsyncSocket(objectMapper, connectionManager, userId);
} else {
return null;
}
diff --git a/src/main/java/org/traccar/database/DeviceManager.java b/src/main/java/org/traccar/database/DeviceManager.java
index 20f179f2e..973cf3f68 100644
--- a/src/main/java/org/traccar/database/DeviceManager.java
+++ b/src/main/java/org/traccar/database/DeviceManager.java
@@ -32,6 +32,7 @@ import org.traccar.Context;
import org.traccar.config.Keys;
import org.traccar.model.Command;
import org.traccar.model.Device;
+import org.traccar.session.ConnectionManager;
import org.traccar.session.DeviceState;
import org.traccar.model.DeviceAccumulators;
import org.traccar.model.Group;
@@ -45,6 +46,7 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceManager.class);
private final Config config;
+ private final ConnectionManager connectionManager;
private final long dataRefreshDelay;
private Map<String, Device> devicesByUniqueId;
@@ -54,9 +56,10 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
private final Map<Long, DeviceState> deviceStates = new ConcurrentHashMap<>();
- public DeviceManager(DataManager dataManager) {
+ public DeviceManager(Config config, DataManager dataManager, ConnectionManager connectionManager) {
super(dataManager, Device.class);
- this.config = Context.getConfig();
+ this.config = config;
+ this.connectionManager = connectionManager;
try {
writeLock();
if (devicesByUniqueId == null) {
@@ -287,9 +290,7 @@ public class DeviceManager extends BaseObjectManager<Device> implements Identity
positions.put(position.getDeviceId(), position);
- if (Context.getConnectionManager() != null) {
- Context.getConnectionManager().updatePosition(position);
- }
+ connectionManager.updatePosition(position);
}
}
diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java
index e01a568aa..ab3c36734 100644
--- a/src/main/java/org/traccar/session/ConnectionManager.java
+++ b/src/main/java/org/traccar/session/ConnectionManager.java
@@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory;
import org.traccar.Context;
import org.traccar.Main;
import org.traccar.Protocol;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.handler.events.MotionEventHandler;
import org.traccar.handler.events.OverspeedEventHandler;
@@ -30,8 +31,14 @@ 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.Storage;
import org.traccar.storage.StorageException;
+import org.traccar.storage.query.Columns;
+import org.traccar.storage.query.Condition;
+import org.traccar.storage.query.Request;
+import javax.inject.Inject;
+import javax.inject.Singleton;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Date;
@@ -42,6 +49,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
+@Singleton
public class ConnectionManager {
private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class);
@@ -52,18 +60,23 @@ public class ConnectionManager {
private final Map<Long, DeviceSession> sessionsByDeviceId = new ConcurrentHashMap<>();
private final Map<Endpoint, Map<String, DeviceSession>> sessionsByEndpoint = new ConcurrentHashMap<>();
+ private final Config config;
private final CacheManager cacheManager;
+ private final Storage storage;
+ private final Timer timer;
private final Map<Long, Set<UpdateListener>> listeners = new ConcurrentHashMap<>();
private final Map<Long, Timeout> timeouts = new ConcurrentHashMap<>();
- private final Timer timer;
-
- public 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);
+ @Inject
+ public ConnectionManager(
+ Config config, CacheManager cacheManager, Storage storage, Timer timer) {
+ this.config = config;
+ this.cacheManager = cacheManager;
+ this.storage = storage;
+ this.timer = timer;
+ deviceTimeout = config.getLong(Keys.STATUS_TIMEOUT) * 1000;
+ updateDeviceState = config.getBoolean(Keys.STATUS_UPDATE_DEVICE_STATE);
}
public DeviceSession getDeviceSession(long deviceId) {
@@ -91,7 +104,8 @@ public class ConnectionManager {
Device device = null;
try {
for (String uniqueId : uniqueIds) {
- device = Context.getIdentityManager().getByUniqueId(uniqueId);
+ device = storage.getObject(Device.class, new Request(
+ new Columns.All(), new Condition.Equals("uniqueId", "uniqueId", uniqueId)));
if (device != null) {
break;
}
@@ -100,8 +114,8 @@ public class ConnectionManager {
LOGGER.warn("Find device error", e);
}
- if (device == null && Context.getConfig().getBoolean(Keys.DATABASE_REGISTER_UNKNOWN)) {
- device = Context.getIdentityManager().addUnknownDevice(uniqueIds[0]);
+ if (device == null && config.getBoolean(Keys.DATABASE_REGISTER_UNKNOWN)) {
+ device = addUnknownDevice(uniqueIds[0]);
}
if (device != null && !device.getDisabled()) {
@@ -131,6 +145,34 @@ public class ConnectionManager {
}
}
+ private Device addUnknownDevice(String uniqueId) {
+ Device device = new Device();
+ device.setName(uniqueId);
+ device.setUniqueId(uniqueId);
+ device.setCategory(config.getString(Keys.DATABASE_REGISTER_UNKNOWN_DEFAULT_CATEGORY));
+
+ long defaultGroupId = config.getLong(Keys.DATABASE_REGISTER_UNKNOWN_DEFAULT_GROUP_ID);
+ if (defaultGroupId != 0) {
+ device.setGroupId(defaultGroupId);
+ }
+
+ try {
+ device.setId(storage.addObject(device, new Request(new Columns.Exclude("id"))));
+
+ LOGGER.info("Automatically registered device " + uniqueId);
+
+ if (defaultGroupId != 0) {
+ Context.getPermissionsManager().refreshDeviceAndGroupPermissions();
+ Context.getPermissionsManager().refreshAllExtendedPermissions();
+ }
+
+ return device;
+ } catch (StorageException e) {
+ LOGGER.warn("Automatic device registration error", e);
+ return null;
+ }
+ }
+
public void deviceDisconnected(Channel channel) {
Endpoint endpoint = new Endpoint(channel, channel.remoteAddress());
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.remove(endpoint);
@@ -156,10 +198,18 @@ public class ConnectionManager {
}
}
- public void updateDevice(final long deviceId, String status, Date time) {
- Device device = Context.getIdentityManager().getById(deviceId);
+ public void updateDevice(long deviceId, String status, Date time) {
+ Device device = cacheManager.getObject(Device.class, deviceId);
if (device == null) {
- return;
+ try {
+ device = storage.getObject(Device.class, new Request(
+ new Columns.All(), new Condition.Equals("id", "id", deviceId)));
+ } catch (StorageException e) {
+ LOGGER.warn("Failed to get device", e);
+ }
+ if (device == null) {
+ return;
+ }
}
String oldStatus = device.getStatus();