1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*
* Copyright 2015 - 2024 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.session;
import io.netty.channel.Channel;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Protocol;
import org.traccar.broadcast.BroadcastInterface;
import org.traccar.broadcast.BroadcastService;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.database.DeviceLookupService;
import org.traccar.database.NotificationManager;
import org.traccar.model.BaseModel;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.LogRecord;
import org.traccar.model.Position;
import org.traccar.model.User;
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 jakarta.inject.Inject;
import jakarta.inject.Singleton;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Singleton
public class ConnectionManager implements BroadcastInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionManager.class);
private final long deviceTimeout;
private final boolean showUnknownDevices;
private final Map<Long, DeviceSession> sessionsByDeviceId = new ConcurrentHashMap<>();
private final Map<ConnectionKey, Map<String, DeviceSession>> sessionsByEndpoint = new ConcurrentHashMap<>();
private final Map<ConnectionKey, String> unknownByEndpoint = new ConcurrentHashMap<>();
private final Config config;
private final CacheManager cacheManager;
private final Storage storage;
private final NotificationManager notificationManager;
private final Timer timer;
private final BroadcastService broadcastService;
private final DeviceLookupService deviceLookupService;
private final Map<Long, Set<UpdateListener>> listeners = new HashMap<>();
private final Map<Long, Set<Long>> userDevices = new HashMap<>();
private final Map<Long, Set<Long>> deviceUsers = new HashMap<>();
private final Map<Long, Timeout> timeouts = new ConcurrentHashMap<>();
@Inject
public ConnectionManager(
Config config, CacheManager cacheManager, Storage storage,
NotificationManager notificationManager, Timer timer, BroadcastService broadcastService,
DeviceLookupService deviceLookupService) {
this.config = config;
this.cacheManager = cacheManager;
this.storage = storage;
this.notificationManager = notificationManager;
this.timer = timer;
this.broadcastService = broadcastService;
this.deviceLookupService = deviceLookupService;
deviceTimeout = config.getLong(Keys.STATUS_TIMEOUT);
showUnknownDevices = config.getBoolean(Keys.WEB_SHOW_UNKNOWN_DEVICES);
broadcastService.registerListener(this);
}
public DeviceSession getDeviceSession(long deviceId) {
return sessionsByDeviceId.get(deviceId);
}
public DeviceSession getDeviceSession(
Protocol protocol, Channel channel, SocketAddress remoteAddress,
String... uniqueIds) throws Exception {
ConnectionKey connectionKey = new ConnectionKey(channel, remoteAddress);
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.getOrDefault(
connectionKey, new ConcurrentHashMap<>());
uniqueIds = Arrays.stream(uniqueIds).filter(Objects::nonNull).toArray(String[]::new);
if (uniqueIds.length > 0) {
for (String uniqueId : uniqueIds) {
DeviceSession deviceSession = endpointSessions.get(uniqueId);
if (deviceSession != null) {
return deviceSession;
}
}
} else {
return endpointSessions.values().stream().findAny().orElse(null);
}
Device device = deviceLookupService.lookup(uniqueIds);
String firstUniqueId = uniqueIds[0];
if (device == null && config.getBoolean(Keys.DATABASE_REGISTER_UNKNOWN)) {
if (firstUniqueId.matches(config.getString(Keys.DATABASE_REGISTER_UNKNOWN_REGEX))) {
device = addUnknownDevice(firstUniqueId);
}
}
if (device != null) {
unknownByEndpoint.remove(connectionKey);
device.checkDisabled();
DeviceSession oldSession = sessionsByDeviceId.remove(device.getId());
if (oldSession != null) {
Map<String, DeviceSession> oldEndpointSessions = sessionsByEndpoint.get(oldSession.getConnectionKey());
if (oldEndpointSessions != null && oldEndpointSessions.size() > 1) {
oldEndpointSessions.remove(device.getUniqueId());
} else {
sessionsByEndpoint.remove(oldSession.getConnectionKey());
}
}
DeviceSession deviceSession = new DeviceSession(
device.getId(), device.getUniqueId(), device.getModel(), protocol, channel, remoteAddress);
endpointSessions.put(device.getUniqueId(), deviceSession);
sessionsByEndpoint.put(connectionKey, endpointSessions);
sessionsByDeviceId.put(device.getId(), deviceSession);
if (oldSession == null) {
cacheManager.addDevice(device.getId());
}
return deviceSession;
} else {
unknownByEndpoint.put(connectionKey, firstUniqueId);
LOGGER.warn("Unknown device - " + String.join(" ", uniqueIds)
+ " (" + ((InetSocketAddress) remoteAddress).getHostString() + ")");
return null;
}
}
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 " + uniqueId);
return device;
} catch (StorageException e) {
LOGGER.warn("Automatic registration failed", e);
return null;
}
}
public void deviceDisconnected(Channel channel, boolean supportsOffline) {
SocketAddress remoteAddress = channel.remoteAddress();
if (remoteAddress != null) {
ConnectionKey connectionKey = new ConnectionKey(channel, remoteAddress);
Map<String, DeviceSession> endpointSessions = sessionsByEndpoint.remove(connectionKey);
if (endpointSessions != null) {
for (DeviceSession deviceSession : endpointSessions.values()) {
if (supportsOffline) {
updateDevice(deviceSession.getDeviceId(), Device.STATUS_OFFLINE, null);
}
sessionsByDeviceId.remove(deviceSession.getDeviceId());
cacheManager.removeDevice(deviceSession.getDeviceId());
}
}
unknownByEndpoint.remove(connectionKey);
}
}
public void deviceUnknown(long deviceId) {
updateDevice(deviceId, Device.STATUS_UNKNOWN, null);
removeDeviceSession(deviceId);
}
private void removeDeviceSession(long deviceId) {
DeviceSession deviceSession = sessionsByDeviceId.remove(deviceId);
if (deviceSession != null) {
cacheManager.removeDevice(deviceId);
sessionsByEndpoint.computeIfPresent(deviceSession.getConnectionKey(), (e, sessions) -> {
sessions.remove(deviceSession.getUniqueId());
return sessions.isEmpty() ? null : sessions;
});
}
}
public void updateDevice(long deviceId, String status, Date time) {
Device device = cacheManager.getObject(Device.class, deviceId);
if (device == null) {
try {
device = storage.getObject(Device.class, new Request(
new Columns.All(), new Condition.Equals("id", deviceId)));
} catch (StorageException e) {
LOGGER.warn("Failed to get device", e);
}
if (device == null) {
return;
}
}
String oldStatus = device.getStatus();
device.setStatus(status);
if (!status.equals(oldStatus)) {
String eventType;
Map<Event, Position> events = new HashMap<>();
switch (status) {
case Device.STATUS_ONLINE:
eventType = Event.TYPE_DEVICE_ONLINE;
break;
case Device.STATUS_UNKNOWN:
eventType = Event.TYPE_DEVICE_UNKNOWN;
break;
default:
eventType = Event.TYPE_DEVICE_OFFLINE;
break;
}
events.put(new Event(eventType, deviceId), null);
notificationManager.updateEvents(events);
}
if (time != null) {
device.setLastUpdate(time);
}
Timeout timeout = timeouts.remove(deviceId);
if (timeout != null) {
timeout.cancel();
}
if (status.equals(Device.STATUS_ONLINE)) {
timeouts.put(deviceId, timer.newTimeout(timeout1 -> {
if (!timeout1.isCancelled()) {
deviceUnknown(deviceId);
}
}, deviceTimeout, TimeUnit.SECONDS));
}
try {
storage.updateObject(device, new Request(
new Columns.Include("status", "lastUpdate"),
new Condition.Equals("id", deviceId)));
} catch (StorageException e) {
LOGGER.warn("Update device status error", e);
}
updateDevice(true, device);
}
public synchronized void sendKeepalive() {
for (Set<UpdateListener> userListeners : listeners.values()) {
for (UpdateListener listener : userListeners) {
listener.onKeepalive();
}
}
}
@Override
public synchronized void updateDevice(boolean local, Device device) {
if (local) {
broadcastService.updateDevice(true, device);
} else if (Device.STATUS_ONLINE.equals(device.getStatus())) {
timeouts.remove(device.getId());
removeDeviceSession(device.getId());
}
for (long userId : deviceUsers.getOrDefault(device.getId(), Collections.emptySet())) {
if (listeners.containsKey(userId)) {
for (UpdateListener listener : listeners.get(userId)) {
listener.onUpdateDevice(device);
}
}
}
}
@Override
public synchronized void updatePosition(boolean local, Position position) {
if (local) {
broadcastService.updatePosition(true, position);
}
for (long userId : deviceUsers.getOrDefault(position.getDeviceId(), Collections.emptySet())) {
if (listeners.containsKey(userId)) {
for (UpdateListener listener : listeners.get(userId)) {
listener.onUpdatePosition(position);
}
}
}
}
@Override
public synchronized void updateEvent(boolean local, long userId, Event event) {
if (local) {
broadcastService.updateEvent(true, userId, event);
}
if (listeners.containsKey(userId)) {
for (UpdateListener listener : listeners.get(userId)) {
listener.onUpdateEvent(event);
}
}
}
@Override
public synchronized <T1 extends BaseModel, T2 extends BaseModel> void invalidatePermission(
boolean local, Class<T1> clazz1, long id1, Class<T2> clazz2, long id2, boolean link) {
if (link && clazz1.equals(User.class) && clazz2.equals(Device.class)) {
if (listeners.containsKey(id1)) {
userDevices.get(id1).add(id2);
deviceUsers.put(id2, new HashSet<>(List.of(id1)));
}
}
}
public synchronized void updateLog(LogRecord record) {
var sessions = sessionsByEndpoint.getOrDefault(record.getConnectionKey(), Map.of());
if (sessions.isEmpty()) {
String unknownUniqueId = unknownByEndpoint.get(record.getConnectionKey());
if (unknownUniqueId != null && showUnknownDevices) {
record.setUniqueId(unknownUniqueId);
listeners.values().stream()
.flatMap(Set::stream)
.forEach((listener) -> listener.onUpdateLog(record));
}
} else {
var firstEntry = sessions.entrySet().iterator().next();
record.setUniqueId(firstEntry.getKey());
record.setDeviceId(firstEntry.getValue().getDeviceId());
for (long userId : deviceUsers.getOrDefault(record.getDeviceId(), Set.of())) {
for (UpdateListener listener : listeners.getOrDefault(userId, Set.of())) {
listener.onUpdateLog(record);
}
}
}
}
public interface UpdateListener {
void onKeepalive();
void onUpdateDevice(Device device);
void onUpdatePosition(Position position);
void onUpdateEvent(Event event);
void onUpdateLog(LogRecord record);
}
public synchronized void addListener(long userId, UpdateListener listener) throws StorageException {
var set = listeners.get(userId);
if (set == null) {
set = new HashSet<>();
listeners.put(userId, set);
var devices = storage.getObjects(Device.class, new Request(
new Columns.Include("id"), new Condition.Permission(User.class, userId, Device.class)));
userDevices.put(userId, devices.stream().map(BaseModel::getId).collect(Collectors.toSet()));
devices.forEach(device -> deviceUsers.computeIfAbsent(device.getId(), id -> new HashSet<>()).add(userId));
}
set.add(listener);
}
public synchronized void removeListener(long userId, UpdateListener listener) {
var set = listeners.get(userId);
set.remove(listener);
if (set.isEmpty()) {
listeners.remove(userId);
userDevices.remove(userId).forEach(deviceId -> deviceUsers.computeIfPresent(deviceId, (x, userIds) -> {
userIds.remove(userId);
return userIds.isEmpty() ? null : userIds;
}));
}
}
}
|