aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/BaseProtocolDecoder.java
diff options
context:
space:
mode:
authorninioe <ninioe@gmail.com>2016-07-19 15:20:15 +0300
committerninioe <ninioe@gmail.com>2016-07-19 15:20:15 +0300
commit20f603076210073e122c720e21a3e8999478b11a (patch)
tree668a50e6c55601c2420bf0f4b986ed0b194c2e32 /src/org/traccar/BaseProtocolDecoder.java
parente954e95547d05a34bbd36e3aa5003f4ddaf2ccd0 (diff)
parent00e2f8a3575855a8b863e245005b8f83b3314bfb (diff)
downloadtrackermap-server-20f603076210073e122c720e21a3e8999478b11a.tar.gz
trackermap-server-20f603076210073e122c720e21a3e8999478b11a.tar.bz2
trackermap-server-20f603076210073e122c720e21a3e8999478b11a.zip
Merge remote-tracking branch 'refs/remotes/tananaev/master'
Diffstat (limited to 'src/org/traccar/BaseProtocolDecoder.java')
-rw-r--r--src/org/traccar/BaseProtocolDecoder.java129
1 files changed, 79 insertions, 50 deletions
diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java
index 9a253e351..46832a4ca 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,87 +35,113 @@ 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();
+ 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);
if (Context.getConnectionManager() != null) {
Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress);
}
- return true;
+ return deviceSession;
} else {
- deviceId = 0;
- if (logWarning) {
- String message = "Unknown device - " + uniqueId;
- if (remoteAddress != null) {
- message += " (" + ((InetSocketAddress) remoteAddress).getHostString() + ")";
+ return null;
+ }
+ } else {
+ if (channelDeviceSession == null) {
+ long deviceId = findDeviceId(remoteAddress, uniqueIds);
+ if (deviceId != 0) {
+ channelDeviceSession = new DeviceSession(deviceId);
+ if (Context.getConnectionManager() != null) {
+ Context.getConnectionManager().addActiveDevice(deviceId, protocol, channel, remoteAddress);
}
- Log.warning(message);
}
- 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;
}
public void getLastLocation(Position position, Date deviceTime) {
- position.setOutdated(true);
-
- Position last = Context.getIdentityManager().getLastPosition(getDeviceId());
- if (last != null) {
- position.setFixTime(last.getFixTime());
- position.setValid(last.getValid());
- position.setLatitude(last.getLatitude());
- position.setLongitude(last.getLongitude());
- position.setAltitude(last.getAltitude());
- position.setSpeed(last.getSpeed());
- position.setCourse(last.getCourse());
- } else {
- position.setFixTime(new Date(0));
- }
+ if (position.getDeviceId() != 0) {
+ position.setOutdated(true);
+
+ Position last = Context.getIdentityManager().getLastPosition(position.getDeviceId());
+ if (last != null) {
+ position.setFixTime(last.getFixTime());
+ position.setValid(last.getValid());
+ position.setLatitude(last.getLatitude());
+ position.setLongitude(last.getLongitude());
+ position.setAltitude(last.getAltitude());
+ position.setSpeed(last.getSpeed());
+ position.setCourse(last.getCourse());
+ } else {
+ position.setFixTime(new Date(0));
+ }
- if (deviceTime != null) {
- position.setDeviceTime(deviceTime);
- } else {
- position.setDeviceTime(new Date());
+ if (deviceTime != null) {
+ position.setDeviceTime(deviceTime);
+ } else {
+ position.setDeviceTime(new Date());
+ }
}
}
@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 {