Skip to content

Commit

Permalink
- Migrate X,Y,W protocols
Browse files Browse the repository at this point in the history
- Add BufferUtil helper class
  • Loading branch information
Abyss777 committed Jun 6, 2018
1 parent 681b8f4 commit c8028d9
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 100 deletions.
39 changes: 39 additions & 0 deletions src/org/traccar/helper/BufferUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018 Anton Tananaev ([email protected])
* Copyright 2018 Andrey Kunitsyn ([email protected])
*
* 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.helper;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;

public class BufferUtil {

public static int indexOf(String needle, ByteBuf haystack) {
return ByteBufUtil.indexOf(
Unpooled.wrappedBuffer(needle.getBytes(StandardCharsets.US_ASCII)), haystack);
}

public static int indexOf(String needle, ByteBuf haystack, int startIndex, int endIndex) {
int index = ByteBufUtil.indexOf(
Unpooled.wrappedBuffer(needle.getBytes(StandardCharsets.US_ASCII)),
Unpooled.wrappedBuffer(haystack.array(), startIndex, endIndex - startIndex));
return (index != -1) ? (startIndex + index) : -1;
}

}
8 changes: 2 additions & 6 deletions src/org/traccar/protocol/AtrackFrameDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
package org.traccar.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.traccar.BaseFrameDecoder;

import java.nio.charset.StandardCharsets;
import org.traccar.helper.BufferUtil;

public class AtrackFrameDecoder extends BaseFrameDecoder {

Expand Down Expand Up @@ -51,8 +48,7 @@ protected Object decode(

} else {

ByteBuf delimiter = Unpooled.wrappedBuffer("\r\n".getBytes(StandardCharsets.US_ASCII));
int endIndex = ByteBufUtil.indexOf(delimiter, buf);
int endIndex = BufferUtil.indexOf("\r\n", buf);
if (endIndex > 0) {
return buf.readBytes(endIndex - buf.readerIndex() + 2);
}
Expand Down
6 changes: 2 additions & 4 deletions src/org/traccar/protocol/EnforaProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.traccar.protocol;

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.helper.BufferUtil;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
Expand Down Expand Up @@ -83,8 +82,7 @@ protected Object decode(
}

// Find NMEA sentence
ByteBuf header = Unpooled.wrappedBuffer("GPRMC".getBytes(StandardCharsets.US_ASCII));
int start = ByteBufUtil.indexOf(header, buf);
int start = BufferUtil.indexOf("GPRMC", buf);
if (start == -1) {
return null;
}
Expand Down
17 changes: 9 additions & 8 deletions src/org/traccar/protocol/WatchFrameDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@
*/
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.FrameDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.nio.charset.StandardCharsets;

public class WatchFrameDecoder extends FrameDecoder {
import org.traccar.BaseFrameDecoder;

public class WatchFrameDecoder extends BaseFrameDecoder {

@Override
protected Object decode(
ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') + 1;
if (idIndex <= 0) {
Expand Down Expand Up @@ -56,7 +57,7 @@ protected Object decode(
int length = Integer.parseInt(
buf.toString(lengthIndex, payloadIndex - lengthIndex, StandardCharsets.US_ASCII), 16);
if (buf.readableBytes() >= payloadIndex + 1 + length + 1) {
ChannelBuffer frame = ChannelBuffers.dynamicBuffer();
ByteBuf frame = Unpooled.buffer();
int endIndex = buf.readerIndex() + payloadIndex + 1 + length + 1;
while (buf.readerIndex() < endIndex) {
byte b = buf.readByte();
Expand Down
12 changes: 6 additions & 6 deletions src/org/traccar/protocol/WatchProtocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 - 2017 Anton Tananaev ([email protected])
* Copyright 2015 - 2018 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,13 @@
*/
package org.traccar.protocol;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.traccar.BaseProtocol;
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;
import org.traccar.model.Command;

import io.netty.handler.codec.string.StringEncoder;

import java.util.List;

public class WatchProtocol extends BaseProtocol {
Expand All @@ -47,9 +47,9 @@ public WatchProtocol() {

@Override
public void initTrackerServers(List<TrackerServer> 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 WatchFrameDecoder());
pipeline.addLast("stringEncoder", new StringEncoder());
pipeline.addLast("objectEncoder", new WatchProtocolEncoder());
Expand Down
10 changes: 5 additions & 5 deletions src/org/traccar/protocol/WatchProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package org.traccar.protocol;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.Context;
import org.traccar.DeviceSession;
Expand Down Expand Up @@ -176,7 +176,7 @@ public String getManufacturer() {
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

ChannelBuffer buf = (ChannelBuffer) msg;
ByteBuf buf = (ByteBuf) msg;

buf.skipBytes(1); // header
manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII);
Expand Down Expand Up @@ -225,7 +225,7 @@ protected Object decode(

sendResponse(channel, id, index, "LK");

if (buf.readable()) {
if (buf.isReadable()) {
String[] values = buf.toString(StandardCharsets.US_ASCII).split(",");
if (values.length >= 3) {
Position position = new Position(getProtocolName());
Expand Down Expand Up @@ -259,7 +259,7 @@ protected Object decode(

} else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) {

if (buf.readable()) {
if (buf.isReadable()) {

Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
Expand Down
6 changes: 3 additions & 3 deletions src/org/traccar/protocol/WatchProtocolEncoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 - 2017 Anton Tananaev ([email protected])
* Copyright 2016 - 2018 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
*/
package org.traccar.protocol;

import org.jboss.netty.channel.Channel;
import io.netty.channel.Channel;
import org.traccar.StringProtocolEncoder;
import org.traccar.helper.DataConverter;
import org.traccar.helper.Log;
Expand Down Expand Up @@ -47,7 +47,7 @@ protected String formatCommand(Channel channel, Command command, String format,
boolean hasIndex = false;
String manufacturer = "CS";
if (channel != null) {
WatchProtocolDecoder decoder = channel.getPipeline().get(WatchProtocolDecoder.class);
WatchProtocolDecoder decoder = channel.pipeline().get(WatchProtocolDecoder.class);
if (decoder != null) {
hasIndex = decoder.getHasIndex();
manufacturer = decoder.getManufacturer();
Expand Down
8 changes: 2 additions & 6 deletions src/org/traccar/protocol/WondexFrameDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
*/
package org.traccar.protocol;

import java.nio.charset.StandardCharsets;

import org.traccar.BaseFrameDecoder;
import org.traccar.NetworkMessage;
import org.traccar.helper.BufferUtil;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

Expand All @@ -49,8 +46,7 @@ protected Object decode(

} else {

ByteBuf delimiter = Unpooled.wrappedBuffer("\r\n".getBytes(StandardCharsets.US_ASCII));
int index = ByteBufUtil.indexOf(delimiter, buf);
int index = BufferUtil.indexOf("\r\n", buf);
if (index != -1) {
ByteBuf frame = buf.readBytes(index - buf.readerIndex());
buf.skipBytes(2);
Expand Down
23 changes: 12 additions & 11 deletions src/org/traccar/protocol/XexunFrameDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 - 2016 Anton Tananaev ([email protected])
* Copyright 2012 - 2018 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,31 +15,32 @@
*/
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;
import org.traccar.BaseFrameDecoder;
import org.traccar.helper.BufferUtil;

public class XexunFrameDecoder extends FrameDecoder {
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

public class XexunFrameDecoder 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() < 80) {
return null;
}

int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GPRMC"));
int beginIndex = BufferUtil.indexOf("GPRMC", buf);
if (beginIndex == -1) {
beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GNRMC"));
beginIndex = BufferUtil.indexOf("GNRMC", buf);
if (beginIndex == -1) {
return null;
}
}

int identifierIndex = buf.indexOf(beginIndex, buf.writerIndex(), new StringFinder("imei:"));
int identifierIndex = BufferUtil.indexOf("imei:", buf, beginIndex, buf.writerIndex());
if (identifierIndex == -1) {
return null;
}
Expand Down
16 changes: 8 additions & 8 deletions src/org/traccar/protocol/XexunProtocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015 - 2016 Anton Tananaev ([email protected])
* Copyright 2015 - 2018 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,16 +15,16 @@
*/
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 org.traccar.BaseProtocol;
import org.traccar.Context;
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;
import org.traccar.model.Command;

import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.util.List;

public class XexunProtocol extends BaseProtocol {
Expand All @@ -38,9 +38,9 @@ public XexunProtocol() {

@Override
public void initTrackerServers(List<TrackerServer> 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) {
boolean full = Context.getConfig().getBoolean(getName() + ".extended");
if (full) {
pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); // tracker bug \n\r
Expand Down
4 changes: 2 additions & 2 deletions src/org/traccar/protocol/XexunProtocolDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012 - 2017 Anton Tananaev ([email protected])
* Copyright 2012 - 2018 Anton Tananaev ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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.DateBuilder;
Expand Down
Loading

0 comments on commit c8028d9

Please sign in to comment.