diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-07-19 15:11:35 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2016-07-19 15:11:35 +1200 |
commit | 815c3de43ee8943fddb916f27317c37a7a806d65 (patch) | |
tree | a38e4f6bf80c35ccc83ae9d4c4d3d88e0d837b00 /src/org/traccar/BaseProtocolDecoder.java | |
parent | 4054777eac379ce07560c44901ada659aad062ba (diff) | |
download | trackermap-server-815c3de43ee8943fddb916f27317c37a7a806d65.tar.gz trackermap-server-815c3de43ee8943fddb916f27317c37a7a806d65.tar.bz2 trackermap-server-815c3de43ee8943fddb916f27317c37a7a806d65.zip |
Move device id to a session
Diffstat (limited to 'src/org/traccar/BaseProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/BaseProtocolDecoder.java | 93 |
1 files changed, 57 insertions, 36 deletions
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<SocketAddress, DeviceSession> 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 { |