aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-04-16 11:47:03 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2016-04-16 11:47:03 +1200
commitef25655365eb97d29295f8e0cb720b86a29c5e26 (patch)
tree49662363442f0af20b05b324fa4b663995d51fc7 /src
parentc463798c9e1d72c0c51cd4321826df1ff8609a5d (diff)
downloadtraccar-server-ef25655365eb97d29295f8e0cb720b86a29c5e26.tar.gz
traccar-server-ef25655365eb97d29295f8e0cb720b86a29c5e26.tar.bz2
traccar-server-ef25655365eb97d29295f8e0cb720b86a29c5e26.zip
Bind async connection to user id
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/api/AsyncSocket.java10
-rw-r--r--src/org/traccar/api/resource/PositionResource.java3
-rw-r--r--src/org/traccar/database/ConnectionManager.java52
-rw-r--r--src/org/traccar/database/PermissionsManager.java14
4 files changed, 42 insertions, 37 deletions
diff --git a/src/org/traccar/api/AsyncSocket.java b/src/org/traccar/api/AsyncSocket.java
index 5353a8d85..35e760613 100644
--- a/src/org/traccar/api/AsyncSocket.java
+++ b/src/org/traccar/api/AsyncSocket.java
@@ -33,26 +33,26 @@ public class AsyncSocket extends WebSocketAdapter implements ConnectionManager.U
private static final String KEY_DEVICES = "devices";
private static final String KEY_POSITIONS = "positions";
- private Collection<Long> devices;
+ private long userId;
public AsyncSocket(long userId) {
- devices = Context.getPermissionsManager().getDevicePermissions(userId);
+ this.userId = userId;
}
@Override
public void onWebSocketConnect(Session session) {
super.onWebSocketConnect(session);
- sendData(KEY_POSITIONS, Context.getConnectionManager().getInitialState(devices));
+ sendData(KEY_POSITIONS, Context.getConnectionManager().getInitialState(userId));
- Context.getConnectionManager().addListener(devices, this);
+ Context.getConnectionManager().addListener(userId, this);
}
@Override
public void onWebSocketClose(int statusCode, String reason) {
super.onWebSocketClose(statusCode, reason);
- Context.getConnectionManager().removeListener(devices, this);
+ Context.getConnectionManager().removeListener(userId, this);
}
@Override
diff --git a/src/org/traccar/api/resource/PositionResource.java b/src/org/traccar/api/resource/PositionResource.java
index 9c174625c..2da517d4f 100644
--- a/src/org/traccar/api/resource/PositionResource.java
+++ b/src/org/traccar/api/resource/PositionResource.java
@@ -39,8 +39,7 @@ public class PositionResource extends BaseResource {
@QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
throws SQLException {
if (deviceId == 0) {
- return Context.getConnectionManager().getInitialState(
- Context.getPermissionsManager().getDevicePermissions(getUserId()));
+ return Context.getConnectionManager().getInitialState(getUserId());
} else {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
return Context.getDataManager().getPositions(
diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java
index e03fd1663..e91aa2d5b 100644
--- a/src/org/traccar/database/ConnectionManager.java
+++ b/src/org/traccar/database/ConnectionManager.java
@@ -112,9 +112,11 @@ public class ConnectionManager {
Log.warning(error);
}
- if (listeners.containsKey(deviceId)) {
- for (UpdateListener listener : listeners.get(deviceId)) {
- listener.onUpdateDevice(device);
+ for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) {
+ if (listeners.containsKey(userId)) {
+ for (UpdateListener listener : listeners.get(userId)) {
+ listener.onUpdateDevice(device);
+ }
}
}
}
@@ -123,9 +125,11 @@ public class ConnectionManager {
long deviceId = position.getDeviceId();
positions.put(deviceId, position);
- if (listeners.containsKey(deviceId)) {
- for (UpdateListener listener : listeners.get(deviceId)) {
- listener.onUpdatePosition(position);
+ for (long userId : Context.getPermissionsManager().getDeviceUsers(deviceId)) {
+ if (listeners.containsKey(userId)) {
+ for (UpdateListener listener : listeners.get(userId)) {
+ listener.onUpdatePosition(position);
+ }
}
}
}
@@ -134,13 +138,13 @@ public class ConnectionManager {
return positions.get(deviceId);
}
- public synchronized Collection<Position> getInitialState(Collection<Long> devices) {
+ public synchronized Collection<Position> getInitialState(long userId) {
List<Position> result = new LinkedList<>();
- for (long device : devices) {
- if (positions.containsKey(device)) {
- result.add(positions.get(device));
+ for (long deviceId : Context.getPermissionsManager().getDevicePermissions(userId)) {
+ if (positions.containsKey(deviceId)) {
+ result.add(positions.get(deviceId));
}
}
@@ -152,30 +156,18 @@ public class ConnectionManager {
void onUpdatePosition(Position position);
}
- public void addListener(Collection<Long> devices, UpdateListener listener) {
- for (long deviceId : devices) {
- addListener(deviceId, listener);
- }
- }
-
- public synchronized void addListener(long deviceId, UpdateListener listener) {
- if (!listeners.containsKey(deviceId)) {
- listeners.put(deviceId, new HashSet<UpdateListener>());
- }
- listeners.get(deviceId).add(listener);
- }
-
- public void removeListener(Collection<Long> devices, UpdateListener listener) {
- for (long deviceId : devices) {
- removeListener(deviceId, listener);
+ public synchronized void addListener(long userId, UpdateListener listener) {
+ if (!listeners.containsKey(userId)) {
+ listeners.put(userId, new HashSet<UpdateListener>());
}
+ listeners.get(userId).add(listener);
}
- public synchronized void removeListener(long deviceId, UpdateListener listener) {
- if (!listeners.containsKey(deviceId)) {
- listeners.put(deviceId, new HashSet<UpdateListener>());
+ public synchronized void removeListener(long userId, UpdateListener listener) {
+ if (!listeners.containsKey(userId)) {
+ listeners.put(userId, new HashSet<UpdateListener>());
}
- listeners.get(deviceId).remove(listener);
+ listeners.get(userId).remove(listener);
}
}
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index dc37bbc84..57307b58a 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -38,6 +38,7 @@ public class PermissionsManager {
private final Map<Long, Set<Long>> groupPermissions = new HashMap<>();
private final Map<Long, Set<Long>> devicePermissions = new HashMap<>();
+ private final Map<Long, Set<Long>> deviceUsers = new HashMap<>();
public Set<Long> getGroupPermissions(long userId) {
if (!groupPermissions.containsKey(userId)) {
@@ -53,6 +54,13 @@ public class PermissionsManager {
return devicePermissions.get(userId);
}
+ public Set<Long> getDeviceUsers(long deviceId) {
+ if (!deviceUsers.containsKey(deviceId)) {
+ deviceUsers.put(deviceId, new HashSet<Long>());
+ }
+ return deviceUsers.get(deviceId);
+ }
+
public PermissionsManager(DataManager dataManager) {
this.dataManager = dataManager;
refresh();
@@ -88,6 +96,12 @@ public class PermissionsManager {
} catch (SQLException error) {
Log.warning(error);
}
+
+ for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
+ for (long deviceId : entry.getValue()) {
+ getDeviceUsers(deviceId).add(entry.getKey());
+ }
+ }
}
public boolean isAdmin(long userId) {