From 815c3de43ee8943fddb916f27317c37a7a806d65 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 19 Jul 2016 15:11:35 +1200 Subject: Move device id to a session --- src/org/traccar/BaseProtocolDecoder.java | 93 +++++++++++++++++++------------- 1 file changed, 57 insertions(+), 36 deletions(-) (limited to 'src/org/traccar/BaseProtocolDecoder.java') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 59205bc6f..f98617caf 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -16,6 +16,7 @@ package org.traccar; import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.socket.DatagramChannel; import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.Position; @@ -23,6 +24,8 @@ import org.traccar.model.Position; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.util.Date; +import java.util.HashMap; +import java.util.Map; public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @@ -32,47 +35,63 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { return protocol.getName(); } - private long deviceId; - - public boolean hasDeviceId() { - return deviceId != 0; - } - - public long getDeviceId() { + private DeviceSession channelDeviceSession; // connection-based protocols + private Map addressDeviceSessions = new HashMap<>(); // connectionless protocols + + private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) { + long deviceId = 0; + if (uniqueIds.length > 0) { + try { + for (String uniqueId : uniqueIds) { + if (uniqueId != null) { + Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId); + if (device != null) { + deviceId = device.getId(); + break; + } + } + } + } catch (Exception e) { + Log.warning(e); + } + if (deviceId == 0) { + StringBuilder message = new StringBuilder("Unknown device -"); + for (String uniqueId : uniqueIds) { + message.append(" ").append(uniqueId); + } + if (remoteAddress != null) { + message.append(" (").append(((InetSocketAddress) remoteAddress).getHostString()).append(")"); + } + Log.warning(message.toString()); + } + } return deviceId; } - public boolean identify(String uniqueId, Channel channel, SocketAddress remoteAddress, boolean logWarning) { - try { - Device device = Context.getIdentityManager().getDeviceByUniqueId(uniqueId); - if (device != null) { - deviceId = device.getId(); - if (Context.getConnectionManager() != null) { - Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress); - } - return true; + public DeviceSession getDeviceSession(Channel channel, SocketAddress remoteAddress, String... uniqueIds) { + if (channel instanceof DatagramChannel) { + long deviceId = findDeviceId(remoteAddress, uniqueIds); + DeviceSession deviceSession = addressDeviceSessions.get(remoteAddress); + if (deviceSession != null && (deviceSession.getDeviceId() == deviceId || uniqueIds.length == 0)) { + return deviceSession; + } else if (deviceId != 0) { + deviceSession = new DeviceSession(deviceId); + addressDeviceSessions.put(remoteAddress, deviceSession); + return deviceSession; } else { - deviceId = 0; - if (logWarning) { - String message = "Unknown device - " + uniqueId; - if (remoteAddress != null) { - message += " (" + ((InetSocketAddress) remoteAddress).getHostString() + ")"; - } - Log.warning(message); + return null; + } + } else { + if (channelDeviceSession == null) { + long deviceId = findDeviceId(remoteAddress, uniqueIds); + if (deviceId != 0) { + channelDeviceSession = new DeviceSession(deviceId); } - return false; } - } catch (Exception error) { - deviceId = 0; - Log.warning(error); - return false; + return channelDeviceSession; } } - public boolean identify(String uniqueId, Channel channel, SocketAddress remoteAddress) { - return identify(uniqueId, channel, remoteAddress, true); - } - public BaseProtocolDecoder(Protocol protocol) { this.protocol = protocol; } @@ -104,17 +123,19 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @Override protected void onMessageEvent(Channel channel, SocketAddress remoteAddress, Object msg) { - if (hasDeviceId()) { - Context.getConnectionManager().updateDevice(deviceId, Device.STATUS_ONLINE, new Date()); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + Context.getConnectionManager().updateDevice(deviceSession.getDeviceId(), Device.STATUS_ONLINE, new Date()); } } @Override protected Object handleEmptyMessage(Channel channel, SocketAddress remoteAddress, Object msg) { - if (Context.getConfig().getBoolean("database.saveEmpty") && hasDeviceId()) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (Context.getConfig().getBoolean("database.saveEmpty") && deviceSession != null) { Position position = new Position(); position.setProtocol(getProtocolName()); - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); return position; } else { -- cgit v1.2.3