diff options
Diffstat (limited to 'src/org/traccar/BaseProtocolEncoder.java')
-rw-r--r-- | src/org/traccar/BaseProtocolEncoder.java | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/src/org/traccar/BaseProtocolEncoder.java b/src/org/traccar/BaseProtocolEncoder.java index 2c8a81868..ed71b5551 100644 --- a/src/org/traccar/BaseProtocolEncoder.java +++ b/src/org/traccar/BaseProtocolEncoder.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,14 +15,15 @@ */ package org.traccar; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPromise; import org.traccar.helper.Log; import org.traccar.model.Command; import org.traccar.model.Device; -public abstract class BaseProtocolEncoder extends OneToOneEncoder { +public abstract class BaseProtocolEncoder extends ChannelOutboundHandlerAdapter { protected String getUniqueId(long deviceId) { return Context.getIdentityManager().getById(deviceId).getUniqueId(); @@ -41,15 +42,17 @@ public abstract class BaseProtocolEncoder extends OneToOneEncoder { } @Override - protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - if (msg instanceof Command) { - Command command = (Command) msg; - Object encodedCommand = encodeCommand(command); + NetworkMessage networkMessage = (NetworkMessage) msg; + + if (networkMessage.getMessage() instanceof Command) { + + Command command = (Command) networkMessage.getMessage(); + Object encodedCommand = encodeCommand(ctx.channel(), command); - // Log command StringBuilder s = new StringBuilder(); - s.append(String.format("[%08X] ", channel.getId())); + s.append("[").append(ctx.channel().id().asShortText()).append("] "); s.append("id: ").append(getUniqueId(command.getDeviceId())).append(", "); s.append("command type: ").append(command.getType()).append(" "); if (encodedCommand != null) { @@ -59,12 +62,21 @@ public abstract class BaseProtocolEncoder extends OneToOneEncoder { } Log.info(s.toString()); - return encodedCommand; + ctx.write(new NetworkMessage(encodedCommand, networkMessage.getRemoteAddress()), promise); + + } else { + + super.write(ctx, msg, promise); + } + } - return msg; + protected Object encodeCommand(Channel channel, Command command) { + return encodeCommand(command); } - protected abstract Object encodeCommand(Command command); + protected Object encodeCommand(Command command) { + return null; + } } |