From 49f00254695ae56aa03c0174f5aadfe1a0e4c836 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 6 Jul 2016 14:54:24 +0500 Subject: Style fixes and optimizations --- src/org/traccar/protocol/GranitFrameDecoder.java | 40 ---------------------- src/org/traccar/protocol/GranitProtocol.java | 3 +- .../traccar/protocol/GranitProtocolDecoder.java | 23 ++++++------- 3 files changed, 12 insertions(+), 54 deletions(-) delete mode 100644 src/org/traccar/protocol/GranitFrameDecoder.java (limited to 'src') 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)) { -- cgit v1.2.3