diff options
author | Abyss777 <abyss@fox5.ru> | 2016-07-06 14:54:24 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-07-06 14:54:24 +0500 |
commit | 49f00254695ae56aa03c0174f5aadfe1a0e4c836 (patch) | |
tree | c1074e438dcb8aed8c2c2f4d172cb88e75d1bc58 | |
parent | 387d798a8fc66ec6792851a199fc83c4636fc6d8 (diff) | |
download | trackermap-server-49f00254695ae56aa03c0174f5aadfe1a0e4c836.tar.gz trackermap-server-49f00254695ae56aa03c0174f5aadfe1a0e4c836.tar.bz2 trackermap-server-49f00254695ae56aa03c0174f5aadfe1a0e4c836.zip |
Style fixes and optimizations
4 files changed, 14 insertions, 56 deletions
diff --git a/src/org/traccar/protocol/GranitFrameDecoder.java b/src/org/traccar/protocol/GranitFrameDecoder.java deleted file mode 100644 index ad6ddc99d..000000000 --- a/src/org/traccar/protocol/GranitFrameDecoder.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) - * - * 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.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 org.traccar.helper.StringFinder; - -public class GranitFrameDecoder extends FrameDecoder { - - @Override - protected Object decode( - ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - - int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n")); - if (index != -1) { - ChannelBuffer frame = buf.readBytes(index - buf.readerIndex()); - buf.skipBytes(2); - return frame; - } - - return null; - } - -} diff --git a/src/org/traccar/protocol/GranitProtocol.java b/src/org/traccar/protocol/GranitProtocol.java index fb9d10809..07357b786 100644 --- a/src/org/traccar/protocol/GranitProtocol.java +++ b/src/org/traccar/protocol/GranitProtocol.java @@ -17,6 +17,7 @@ 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.traccar.BaseProtocol; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -39,7 +40,7 @@ public class GranitProtocol extends BaseProtocol { TrackerServer server = new TrackerServer(new ServerBootstrap(), this.getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new GranitFrameDecoder()); + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024, true, false)); pipeline.addLast("objectEncoder", new GranitProtocolEncoder()); pipeline.addLast("objectDecoder", new GranitProtocolDecoder(GranitProtocol.this)); } diff --git a/src/org/traccar/protocol/GranitProtocolDecoder.java b/src/org/traccar/protocol/GranitProtocolDecoder.java index 16229dc18..6f334d3c5 100644 --- a/src/org/traccar/protocol/GranitProtocolDecoder.java +++ b/src/org/traccar/protocol/GranitProtocolDecoder.java @@ -39,11 +39,11 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { } public static void appendChecksum(ChannelBuffer buffer, int length) { - buffer.writeByte(0x2a); // asterisk + buffer.writeByte('*'); int checksum = Checksum.xor(buffer.toByteBuffer(0, length)) & 0xFF; String checksumString = String.format("%02X", checksum); buffer.writeBytes(checksumString.getBytes(StandardCharsets.US_ASCII)); - buffer.writeByte(0x0D); buffer.writeByte(0x0A); + buffer.writeByte('\r'); buffer.writeByte('\n'); } private static void sendResponseCurrent(Channel channel, int deviceId, long time) { @@ -132,12 +132,10 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ChannelBuffer buf = (ChannelBuffer) msg; - - if (hasDeviceId() && buf.toString(StandardCharsets.US_ASCII).contains("OK") - || buf.toString(StandardCharsets.US_ASCII).startsWith("ERROR") - || buf.toString(StandardCharsets.US_ASCII).startsWith("+PR") - || buf.toString(StandardCharsets.US_ASCII).startsWith("+IDNT") - || buf.toString(StandardCharsets.US_ASCII).startsWith("+BBMD")) { + String bufString = buf.toString(StandardCharsets.US_ASCII); + if (hasDeviceId() && bufString.contains("OK") || bufString.startsWith("ERROR") + || bufString.startsWith("+PR") || bufString.startsWith("+IDNT") + || bufString.startsWith("+BBMD")) { Position position = new Position(); position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); @@ -145,7 +143,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { position.setTime(new Date()); getLastLocation(position, new Date()); position.setValid(false); - position.set(Position.KEY_RESULT, buf.toString(StandardCharsets.US_ASCII)); + position.set(Position.KEY_RESULT, bufString); return position; } @@ -153,10 +151,9 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { if (buf.readableBytes() < HEADER_LENGTH) { return null; } + buf.skipBytes(HEADER_LENGTH); - String header = buf.readBytes(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); - - if (header.equals("+RRCB~")) { + if (bufString.startsWith("+RRCB~")) { buf.skipBytes(2); //binary length 26 int deviceId = buf.readUnsignedShort(); if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { @@ -175,7 +172,7 @@ public class GranitProtocolDecoder extends BaseProtocolDecoder { decodeStructure(buf, position); return position; - } else if (header.equals("+DDAT~")) { + } else if (bufString.startsWith("+DDAT~")) { buf.skipBytes(2); //binary length int deviceId = buf.readUnsignedShort(); if (!identify(String.valueOf(deviceId), channel, remoteAddress)) { diff --git a/test/org/traccar/protocol/GranitProtocolDecoderTest.java b/test/org/traccar/protocol/GranitProtocolDecoderTest.java index 9b1c188c1..9f1f07bf7 100644 --- a/test/org/traccar/protocol/GranitProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GranitProtocolDecoderTest.java @@ -16,10 +16,10 @@ public class GranitProtocolDecoderTest extends ProtocolTest { "2b525243427e1a003e2934757c57b8b03c38d279b4e61e9bd7006b000000001c00002a45330d0a")); verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, - "2b444441547e84003e290401d01690737c57b8903c383c7fa0e5081b64006b000000001c0000b8803c388e7fe7e5102197006c000000001c0000b8813c38ad7f02e6042035006c000000001d0000b8813c38bf7f13e6001d1e006c000000001d0000b8813c38bf7f13e6001d00006c000000001d0000b8903c38977f34e6091065006c000000001e000014002a39320d0a")); + "2b444441547e84003e290401d01690737c57b8903c383c7fa0e5081b64006b000000001c0000b8803c388e7fe7e5102197006c000000001c0000b8813c38ad7f02e6042035006c000000001d0000b8813c38bf7f13e6001d1e006c000000001d0000b8813c38bf7f13e6001d00006c000000001d0000b8903c38977f34e6091065006c000000001e000014002a39320d0a")); verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, - "2b444441547e84003e290401d41680747c57f8a03c38987f50e6005300006c000000001c0000f8b03c38987f50e6005300006c000000001c0000fefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe14002a43460d0a")); + "2b444441547e84003e290401d41680747c57f8a03c38987f50e6005300006c000000001c0000f8b03c38987f50e6005300006c000000001c0000fefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefefe14002a43460d0a")); //+IDNT: Navigator.04x Firmware version 0712GLN *21 verifyAttributes(decoder, binary(ByteOrder.LITTLE_ENDIAN, |