From 4d88fa05bb1b673d676ff92b0b2b53bfba19cece Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 9 Jun 2018 09:35:25 +1200 Subject: Migrate remaining protocols --- src/org/traccar/protocol/T55Protocol.java | 20 ++++----- src/org/traccar/protocol/T55ProtocolDecoder.java | 7 +-- src/org/traccar/protocol/T57FrameDecoder.java | 14 +++--- src/org/traccar/protocol/T57Protocol.java | 13 +++--- src/org/traccar/protocol/T57ProtocolDecoder.java | 4 +- src/org/traccar/protocol/T800xProtocol.java | 11 +++-- src/org/traccar/protocol/T800xProtocolDecoder.java | 46 ++++++++----------- src/org/traccar/protocol/T800xProtocolEncoder.java | 10 ++--- src/org/traccar/protocol/TaipProtocol.java | 18 ++++---- src/org/traccar/protocol/TaipProtocolDecoder.java | 7 +-- src/org/traccar/protocol/TelemaxProtocol.java | 13 +++--- .../traccar/protocol/TelemaxProtocolDecoder.java | 2 +- src/org/traccar/protocol/ThinkRaceProtocol.java | 11 +++-- .../traccar/protocol/ThinkRaceProtocolDecoder.java | 17 +++---- src/org/traccar/protocol/Tk102Protocol.java | 11 +++-- src/org/traccar/protocol/Tk102ProtocolDecoder.java | 23 +++++----- src/org/traccar/protocol/Tk103FrameDecoder.java | 14 +++--- src/org/traccar/protocol/Tk103Protocol.java | 18 ++++---- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 4 +- src/org/traccar/protocol/TramigoFrameDecoder.java | 45 +++++++------------ src/org/traccar/protocol/TramigoProtocol.java | 14 +++--- .../traccar/protocol/TramigoProtocolDecoder.java | 52 ++++++++++++---------- 22 files changed, 172 insertions(+), 202 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/T55Protocol.java b/src/org/traccar/protocol/T55Protocol.java index 402fd46b8..b69cfab6f 100644 --- a/src/org/traccar/protocol/T55Protocol.java +++ b/src/org/traccar/protocol/T55Protocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,13 +15,11 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ConnectionlessBootstrap; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; +import io.netty.handler.codec.LineBasedFrameDecoder; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import java.util.List; @@ -34,18 +32,18 @@ public class T55Protocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectDecoder", new T55ProtocolDecoder(T55Protocol.this)); } }); - serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) { + serverList.add(new TrackerServer(true, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectDecoder", new T55ProtocolDecoder(T55Protocol.this)); diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index dfe0d541f..8aa7fae8f 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -15,17 +15,18 @@ */ package org.traccar.protocol; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.socket.DatagramChannel; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.channels.DatagramChannel; import java.util.Date; import java.util.regex.Pattern; @@ -102,7 +103,7 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { if (deviceSession != null && channel != null && !(channel instanceof DatagramChannel) && Context.getIdentityManager().lookupAttributeBoolean( deviceSession.getDeviceId(), getProtocolName() + ".ack", false, true)) { - channel.write("OK1\r\n"); + channel.writeAndFlush(new NetworkMessage("OK1\r\n", remoteAddress)); } Parser parser = new Parser(PATTERN_GPRMC, sentence); diff --git a/src/org/traccar/protocol/T57FrameDecoder.java b/src/org/traccar/protocol/T57FrameDecoder.java index b3b456516..844043088 100644 --- a/src/org/traccar/protocol/T57FrameDecoder.java +++ b/src/org/traccar/protocol/T57FrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 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. @@ -15,18 +15,18 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.frame.FrameDecoder; +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; import java.nio.charset.StandardCharsets; -public class T57FrameDecoder extends FrameDecoder { +public class T57FrameDecoder extends BaseFrameDecoder { @Override protected Object decode( - ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; diff --git a/src/org/traccar/protocol/T57Protocol.java b/src/org/traccar/protocol/T57Protocol.java index 5149929e9..9f3dd62a3 100644 --- a/src/org/traccar/protocol/T57Protocol.java +++ b/src/org/traccar/protocol/T57Protocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 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. @@ -15,11 +15,10 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import java.util.List; @@ -32,9 +31,9 @@ public class T57Protocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new T57FrameDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); diff --git a/src/org/traccar/protocol/T57ProtocolDecoder.java b/src/org/traccar/protocol/T57ProtocolDecoder.java index 65dfc46e3..441f8cf8a 100644 --- a/src/org/traccar/protocol/T57ProtocolDecoder.java +++ b/src/org/traccar/protocol/T57ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 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. @@ -15,7 +15,7 @@ */ package org.traccar.protocol; -import org.jboss.netty.channel.Channel; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.Parser; diff --git a/src/org/traccar/protocol/T800xProtocol.java b/src/org/traccar/protocol/T800xProtocol.java index 830ff4de6..26f34ee77 100644 --- a/src/org/traccar/protocol/T800xProtocol.java +++ b/src/org/traccar/protocol/T800xProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,10 +15,9 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -34,9 +33,9 @@ public class T800xProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 3, 2, -5, 0)); pipeline.addLast("objectEncoder", new T800xProtocolEncoder()); pipeline.addLast("objectDecoder", new T800xProtocolDecoder(T800xProtocol.this)); diff --git a/src/org/traccar/protocol/T800xProtocolDecoder.java b/src/org/traccar/protocol/T800xProtocolDecoder.java index 7bc780c83..d91fe9d42 100644 --- a/src/org/traccar/protocol/T800xProtocolDecoder.java +++ b/src/org/traccar/protocol/T800xProtocolDecoder.java @@ -15,11 +15,13 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.BcdUtil; import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; @@ -29,7 +31,6 @@ import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; -import java.nio.ByteOrder; public class T800xProtocolDecoder extends BaseProtocolDecoder { @@ -43,21 +44,15 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ALARM = 0x04; public static final int MSG_COMMAND = 0x81; - private static float readSwappedFloat(ChannelBuffer buf) { - byte[] bytes = new byte[4]; - buf.readBytes(bytes); - return ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, bytes).readFloat(); - } - - private void sendResponse(Channel channel, short header, int type, ChannelBuffer imei) { + private void sendResponse(Channel channel, short header, int type, ByteBuf imei) { if (channel != null) { - ChannelBuffer response = ChannelBuffers.directBuffer(15); + ByteBuf response = Unpooled.buffer(15); response.writeShort(header); response.writeByte(type); response.writeShort(response.capacity()); // length response.writeShort(0x0001); // index response.writeBytes(imei); - channel.write(response); + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } } @@ -84,16 +79,16 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ChannelBuffer buf = (ChannelBuffer) msg; + ByteBuf buf = (ByteBuf) msg; short header = buf.readShort(); int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // length int index = buf.readUnsignedShort(); - ChannelBuffer imei = buf.readBytes(8); + ByteBuf imei = buf.readBytes(8); DeviceSession deviceSession = getDeviceSession( - channel, remoteAddress, ChannelBuffers.hexDump(imei).substring(1)); + channel, remoteAddress, ByteBufUtil.hexDump(imei).substring(1)); if (deviceSession == null) { return null; } @@ -154,29 +149,24 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { position.setValid(!BitUtil.check(status, 7)); position.setTime(dateBuilder.getDate()); - position.setAltitude(readSwappedFloat(buf)); - position.setLongitude(readSwappedFloat(buf)); - position.setLatitude(readSwappedFloat(buf)); - position.setSpeed(UnitsConverter.knotsFromKph( - BcdUtil.readInteger(buf, 4) * 0.1)); + position.setAltitude(buf.readFloatLE()); + position.setLongitude(buf.readFloatLE()); + position.setLatitude(buf.readFloatLE()); + position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4) * 0.1)); position.setCourse(buf.readUnsignedShort()); } else { getLastLocation(position, dateBuilder.getDate()); - byte[] array = new byte[16]; - buf.readBytes(array); - ChannelBuffer swapped = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, array); - - int mcc = swapped.readUnsignedShort(); - int mnc = swapped.readUnsignedShort(); + int mcc = buf.readUnsignedShortLE(); + int mnc = buf.readUnsignedShortLE(); if (mcc != 0xffff && mnc != 0xffff) { Network network = new Network(); for (int i = 0; i < 3; i++) { network.addCellTower(CellTower.from( - mcc, mnc, swapped.readUnsignedShort(), swapped.readUnsignedShort())); + mcc, mnc, buf.readUnsignedShortLE(), buf.readUnsignedShortLE())); } position.setNetwork(network); } diff --git a/src/org/traccar/protocol/T800xProtocolEncoder.java b/src/org/traccar/protocol/T800xProtocolEncoder.java index 038a5e51a..ba17c9d75 100644 --- a/src/org/traccar/protocol/T800xProtocolEncoder.java +++ b/src/org/traccar/protocol/T800xProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 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. @@ -15,8 +15,8 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import org.traccar.BaseProtocolEncoder; import org.traccar.helper.DataConverter; import org.traccar.helper.Log; @@ -30,9 +30,9 @@ public class T800xProtocolEncoder extends BaseProtocolEncoder { public static final int MODE_BROADCAST = 0x02; public static final int MODE_FORWARD = 0x03; - private ChannelBuffer encodeContent(Command command, String content) { + private ByteBuf encodeContent(Command command, String content) { - ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); + ByteBuf buf = Unpooled.buffer(); buf.writeByte('#'); buf.writeByte('#'); diff --git a/src/org/traccar/protocol/TaipProtocol.java b/src/org/traccar/protocol/TaipProtocol.java index cbfc44122..bfecf9b39 100644 --- a/src/org/traccar/protocol/TaipProtocol.java +++ b/src/org/traccar/protocol/TaipProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,13 +15,11 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ConnectionlessBootstrap; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; import org.traccar.CharacterDelimiterFrameDecoder; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import java.util.List; @@ -34,18 +32,18 @@ public class TaipProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, '<')); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectDecoder", new TaipProtocolDecoder(TaipProtocol.this)); } }); - serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) { + serverList.add(new TrackerServer(true, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectDecoder", new TaipProtocolDecoder(TaipProtocol.this)); diff --git a/src/org/traccar/protocol/TaipProtocolDecoder.java b/src/org/traccar/protocol/TaipProtocolDecoder.java index ea5492462..31b435bce 100644 --- a/src/org/traccar/protocol/TaipProtocolDecoder.java +++ b/src/org/traccar/protocol/TaipProtocolDecoder.java @@ -15,9 +15,10 @@ */ package org.traccar.protocol; -import org.jboss.netty.channel.Channel; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; @@ -271,9 +272,9 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { if (messageIndex != null) { String response = ">ACK;ID=" + uniqueId + ";" + messageIndex + ";*"; response += String.format("%02X", Checksum.xor(response)) + "<"; - channel.write(response, remoteAddress); + channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } else { - channel.write(uniqueId, remoteAddress); + channel.writeAndFlush(new NetworkMessage(uniqueId, remoteAddress)); } } return position; diff --git a/src/org/traccar/protocol/TelemaxProtocol.java b/src/org/traccar/protocol/TelemaxProtocol.java index 5dee95c8e..f2208741a 100644 --- a/src/org/traccar/protocol/TelemaxProtocol.java +++ b/src/org/traccar/protocol/TelemaxProtocol.java @@ -15,12 +15,11 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; +import io.netty.handler.codec.LineBasedFrameDecoder; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import java.util.List; @@ -33,9 +32,9 @@ public class TelemaxProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("stringDecoder", new StringDecoder()); diff --git a/src/org/traccar/protocol/TelemaxProtocolDecoder.java b/src/org/traccar/protocol/TelemaxProtocolDecoder.java index a52a83133..fd2f03e1c 100644 --- a/src/org/traccar/protocol/TelemaxProtocolDecoder.java +++ b/src/org/traccar/protocol/TelemaxProtocolDecoder.java @@ -15,7 +15,7 @@ */ package org.traccar.protocol; -import org.jboss.netty.channel.Channel; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; diff --git a/src/org/traccar/protocol/ThinkRaceProtocol.java b/src/org/traccar/protocol/ThinkRaceProtocol.java index 98f43e2e3..644e16ca7 100644 --- a/src/org/traccar/protocol/ThinkRaceProtocol.java +++ b/src/org/traccar/protocol/ThinkRaceProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,10 +15,9 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import java.util.List; @@ -31,9 +30,9 @@ public class ThinkRaceProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2 + 12 + 1 + 1, 2, 2, 0)); pipeline.addLast("objectDecoder", new ThinkRaceProtocolDecoder(ThinkRaceProtocol.this)); } diff --git a/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java b/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java index f7dd18f51..dd1f34254 100644 --- a/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java +++ b/src/org/traccar/protocol/ThinkRaceProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,11 +15,12 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.BitUtil; import org.traccar.model.CellTower; import org.traccar.model.Network; @@ -52,10 +53,10 @@ public class ThinkRaceProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ChannelBuffer buf = (ChannelBuffer) msg; + ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header - ChannelBuffer id = buf.readBytes(12); + ByteBuf id = buf.readBytes(12); buf.readUnsignedByte(); // separator int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // length @@ -68,7 +69,7 @@ public class ThinkRaceProtocolDecoder extends BaseProtocolDecoder { String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession != null && channel != null) { - ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + ByteBuf response = Unpooled.buffer(); response.writeByte(0x48); response.writeByte(0x52); // header response.writeBytes(id); response.writeByte(0x2c); // separator @@ -76,7 +77,7 @@ public class ThinkRaceProtocolDecoder extends BaseProtocolDecoder { response.writeShort(0x0002); // length response.writeShort(0x8000); response.writeShort(0x0000); // checksum - channel.write(response); + channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } } diff --git a/src/org/traccar/protocol/Tk102Protocol.java b/src/org/traccar/protocol/Tk102Protocol.java index 962f2401b..2eff262f0 100644 --- a/src/org/traccar/protocol/Tk102Protocol.java +++ b/src/org/traccar/protocol/Tk102Protocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,10 +15,9 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; +import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import java.util.List; @@ -31,9 +30,9 @@ public class Tk102Protocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 1 + 1 + 10, 1, 1, 0)); pipeline.addLast("objectDecoder", new Tk102ProtocolDecoder(Tk102Protocol.this)); } diff --git a/src/org/traccar/protocol/Tk102ProtocolDecoder.java b/src/org/traccar/protocol/Tk102ProtocolDecoder.java index 50dd45676..1419000be 100644 --- a/src/org/traccar/protocol/Tk102ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk102ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -15,11 +15,12 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -59,16 +60,16 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder { .text(")") .compile(); - private void sendResponse(Channel channel, int type, ChannelBuffer dataSequence, ChannelBuffer content) { + private void sendResponse(Channel channel, int type, ByteBuf dataSequence, ByteBuf content) { if (channel != null) { - ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + ByteBuf response = Unpooled.buffer(); response.writeByte('['); response.writeByte(type); response.writeBytes(dataSequence); response.writeByte(content.readableBytes()); response.writeBytes(content); response.writeByte(']'); - channel.write(response); + channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } } @@ -76,16 +77,16 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ChannelBuffer buf = (ChannelBuffer) msg; + ByteBuf buf = (ByteBuf) msg; buf.skipBytes(1); // header int type = buf.readUnsignedByte(); - ChannelBuffer dataSequence = buf.readBytes(10); + ByteBuf dataSequence = buf.readBytes(10); int length = buf.readUnsignedByte(); if (type == MSG_LOGIN_REQUEST || type == MSG_LOGIN_REQUEST_2) { - ChannelBuffer data = buf.readBytes(length); + ByteBuf data = buf.readBytes(length); String id; if (type == MSG_LOGIN_REQUEST) { @@ -95,7 +96,7 @@ public class Tk102ProtocolDecoder extends BaseProtocolDecoder { } if (getDeviceSession(channel, remoteAddress, id) != null) { - ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + ByteBuf response = Unpooled.buffer(); response.writeByte(MODE_GPRS); response.writeBytes(data); sendResponse(channel, MSG_LOGIN_RESPONSE, dataSequence, response); diff --git a/src/org/traccar/protocol/Tk103FrameDecoder.java b/src/org/traccar/protocol/Tk103FrameDecoder.java index 390e38906..3ad1cd2c9 100644 --- a/src/org/traccar/protocol/Tk103FrameDecoder.java +++ b/src/org/traccar/protocol/Tk103FrameDecoder.java @@ -1,6 +1,6 @@ /* * Copyright 2017 Valerii Vyshniak (val@val.one) - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 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. @@ -16,16 +16,16 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.frame.FrameDecoder; +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; -public class Tk103FrameDecoder extends FrameDecoder { +public class Tk103FrameDecoder extends BaseFrameDecoder { @Override protected Object decode( - ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; diff --git a/src/org/traccar/protocol/Tk103Protocol.java b/src/org/traccar/protocol/Tk103Protocol.java index e23982c74..ff31f16bc 100644 --- a/src/org/traccar/protocol/Tk103Protocol.java +++ b/src/org/traccar/protocol/Tk103Protocol.java @@ -1,6 +1,6 @@ /* * Copyright 2017 Christoph Krey (c@ckrey.de) - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -16,12 +16,10 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ConnectionlessBootstrap; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; +import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -53,9 +51,9 @@ public class Tk103Protocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new Tk103FrameDecoder()); pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); @@ -63,9 +61,9 @@ public class Tk103Protocol extends BaseProtocol { pipeline.addLast("objectDecoder", new Tk103ProtocolDecoder(Tk103Protocol.this)); } }); - serverList.add(new TrackerServer(new ConnectionlessBootstrap(), getName()) { + serverList.add(new TrackerServer(true, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectEncoder", new Tk103ProtocolEncoder()); diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index 22a73469f..6253bc127 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -15,7 +15,7 @@ */ package org.traccar.protocol; -import org.jboss.netty.channel.Channel; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; diff --git a/src/org/traccar/protocol/TramigoFrameDecoder.java b/src/org/traccar/protocol/TramigoFrameDecoder.java index 20992c04b..6c7e639ac 100644 --- a/src/org/traccar/protocol/TramigoFrameDecoder.java +++ b/src/org/traccar/protocol/TramigoFrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,45 +15,32 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; +import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import org.traccar.BaseFrameDecoder; -import java.nio.ByteOrder; - -public class TramigoFrameDecoder extends LengthFieldBasedFrameDecoder { - - public TramigoFrameDecoder() { - super(1024, 6, 2, -8, 0); - } +public class TramigoFrameDecoder extends BaseFrameDecoder { @Override - protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { + protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 20) { return null; } - // Swap byte order for legacy protocol + int length; if (buf.getUnsignedByte(buf.readerIndex()) == 0x80) { - int length = buf.readableBytes(); - byte[] bytes = new byte[length]; - buf.getBytes(buf.readerIndex(), bytes); - - ChannelBuffer result = (ChannelBuffer) super.decode( - ctx, channel, ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, bytes)); - if (result != null) { - buf.skipBytes(result.readableBytes()); - } - return result; + length = buf.getUnsignedShort(buf.readerIndex() + 6); + } else { + length = buf.getUnsignedShortLE(buf.readerIndex() + 6); + } + + if (length >= buf.readableBytes()) { + return buf.readBytes(length); } - return super.decode(ctx, channel, buf); + return null; } } diff --git a/src/org/traccar/protocol/TramigoProtocol.java b/src/org/traccar/protocol/TramigoProtocol.java index 28673c97b..74c673967 100644 --- a/src/org/traccar/protocol/TramigoProtocol.java +++ b/src/org/traccar/protocol/TramigoProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -15,12 +15,10 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; import org.traccar.BaseProtocol; +import org.traccar.PipelineBuilder; import org.traccar.TrackerServer; -import java.nio.ByteOrder; import java.util.List; public class TramigoProtocol extends BaseProtocol { @@ -31,15 +29,13 @@ public class TramigoProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - TrackerServer server = new TrackerServer(new ServerBootstrap(), getName()) { + serverList.add(new TrackerServer(false, getName()) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { + protected void addProtocolHandlers(PipelineBuilder pipeline) { pipeline.addLast("frameDecoder", new TramigoFrameDecoder()); pipeline.addLast("objectDecoder", new TramigoProtocolDecoder(TramigoProtocol.this)); } - }; - server.setEndianness(ByteOrder.LITTLE_ENDIAN); - serverList.add(server); + }); } } diff --git a/src/org/traccar/protocol/TramigoProtocolDecoder.java b/src/org/traccar/protocol/TramigoProtocolDecoder.java index 2605346a9..a54a4de68 100644 --- a/src/org/traccar/protocol/TramigoProtocolDecoder.java +++ b/src/org/traccar/protocol/TramigoProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2014 - 2018 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. @@ -15,11 +15,12 @@ */ package org.traccar.protocol; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.channel.Channel; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.NetworkMessage; import org.traccar.helper.DateUtil; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -49,16 +50,18 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - ChannelBuffer buf = (ChannelBuffer) msg; + ByteBuf buf = (ByteBuf) msg; int protocol = buf.readUnsignedByte(); + boolean legacy = protocol == 0x80; + buf.readUnsignedByte(); // version id - int index = buf.readUnsignedShort(); - int type = buf.readUnsignedShort(); + int index = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE(); + int type = legacy ? buf.readUnsignedShort() : buf.readUnsignedShortLE(); buf.readUnsignedShort(); // length buf.readUnsignedShort(); // mask buf.readUnsignedShort(); // checksum - long id = buf.readUnsignedInt(); + long id = legacy ? buf.readUnsignedInt() : buf.readUnsignedIntLE(); buf.readUnsignedInt(); // time Position position = new Position(getProtocolName()); @@ -75,36 +78,37 @@ public class TramigoProtocolDecoder extends BaseProtocolDecoder { // need to send ack? - buf.readUnsignedShort(); // report trigger - buf.readUnsignedShort(); // state flag + buf.readUnsignedShortLE(); // report trigger + buf.readUnsignedShortLE(); // state flag - position.setLatitude(buf.readUnsignedInt() * 0.0000001); - position.setLongitude(buf.readUnsignedInt() * 0.0000001); + position.setLatitude(buf.readUnsignedIntLE() * 0.0000001); + position.setLongitude(buf.readUnsignedIntLE() * 0.0000001); - position.set(Position.KEY_RSSI, buf.readUnsignedShort()); - position.set(Position.KEY_SATELLITES, buf.readUnsignedShort()); - position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShort()); - position.set("gpsAntennaStatus", buf.readUnsignedShort()); + position.set(Position.KEY_RSSI, buf.readUnsignedShortLE()); + position.set(Position.KEY_SATELLITES, buf.readUnsignedShortLE()); + position.set(Position.KEY_SATELLITES_VISIBLE, buf.readUnsignedShortLE()); + position.set("gpsAntennaStatus", buf.readUnsignedShortLE()); - position.setSpeed(buf.readUnsignedShort() * 0.194384); - position.setCourse(buf.readUnsignedShort()); + position.setSpeed(buf.readUnsignedShortLE() * 0.194384); + position.setCourse(buf.readUnsignedShortLE()); - position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); + position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); - position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); + position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE()); - position.set(Position.KEY_CHARGE, buf.readUnsignedShort()); + position.set(Position.KEY_CHARGE, buf.readUnsignedShortLE()); - position.setTime(new Date(buf.readUnsignedInt() * 1000)); + position.setTime(new Date(buf.readUnsignedIntLE() * 1000)); // parse other data return position; - } else if (protocol == 0x80) { + } else if (legacy) { if (channel != null) { - channel.write(ChannelBuffers.copiedBuffer("gprs,ack," + index, StandardCharsets.US_ASCII)); + channel.writeAndFlush(new NetworkMessage( + Unpooled.copiedBuffer("gprs,ack," + index, StandardCharsets.US_ASCII), remoteAddress)); } String sentence = buf.toString(StandardCharsets.US_ASCII); -- cgit v1.2.3