mirror of
https://github.com/traccar/traccar.git
synced 2025-01-09 04:07:38 +08:00
Remove protocol detector classes
This commit is contained in:
parent
d8552cb297
commit
b9e28d25c8
@ -194,7 +194,6 @@
|
||||
|
||||
<!-- PROTOCOL CONFIG -->
|
||||
|
||||
<!--<entry key='detector.port'>5000</entry>-->
|
||||
<entry key='gps103.port'>5001</entry>
|
||||
<entry key='tk103.port'>5002</entry>
|
||||
<entry key='gl100.port'>5003</entry>
|
||||
|
@ -1,122 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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;
|
||||
|
||||
import org.jboss.netty.buffer.ChannelBuffer;
|
||||
import org.jboss.netty.channel.Channel;
|
||||
import org.jboss.netty.channel.ChannelHandlerContext;
|
||||
import org.jboss.netty.channel.ChannelPipeline;
|
||||
import org.jboss.netty.channel.MessageEvent;
|
||||
import org.jboss.netty.channel.SimpleChannelHandler;
|
||||
import org.jboss.netty.handler.codec.frame.FrameDecoder;
|
||||
import org.jboss.netty.handler.codec.string.StringDecoder;
|
||||
import org.traccar.helper.Log;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.List;
|
||||
|
||||
public class DetectorHandler extends SimpleChannelHandler {
|
||||
|
||||
private final List<TrackerServer> serverList;
|
||||
|
||||
private boolean showFailed;
|
||||
|
||||
DetectorHandler(List<TrackerServer> serverList) {
|
||||
this.serverList = serverList;
|
||||
}
|
||||
|
||||
public void checkPipeline(String protocol, ChannelPipeline pipeline, ChannelBuffer buf) throws Exception {
|
||||
Object tmp = buf.duplicate();
|
||||
|
||||
// Frame decoder
|
||||
FrameDecoder frameDecoder = (FrameDecoder) pipeline.get("frameDecoder");
|
||||
if (frameDecoder != null) {
|
||||
try {
|
||||
Method method = frameDecoder.getClass().getDeclaredMethod(
|
||||
"decode", ChannelHandlerContext.class, Channel.class, ChannelBuffer.class);
|
||||
method.setAccessible(true);
|
||||
tmp = method.invoke(frameDecoder, null, null, tmp);
|
||||
} catch (NoSuchMethodException error) {
|
||||
Method method = frameDecoder.getClass().getSuperclass().getDeclaredMethod(
|
||||
"decode", ChannelHandlerContext.class, Channel.class, ChannelBuffer.class);
|
||||
method.setAccessible(true);
|
||||
tmp = method.invoke(frameDecoder, null, null, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
// String decoder
|
||||
if (pipeline.get("stringDecoder") != null) {
|
||||
StringDecoder stringDecoder = new StringDecoder();
|
||||
if (tmp != null) {
|
||||
try {
|
||||
Method method = stringDecoder.getClass().getDeclaredMethod(
|
||||
"decode", ChannelHandlerContext.class, Channel.class, Object.class);
|
||||
method.setAccessible(true);
|
||||
tmp = method.invoke(stringDecoder, null, null, tmp);
|
||||
} catch (NoSuchMethodException error) {
|
||||
Method method = stringDecoder.getClass().getSuperclass().getDeclaredMethod(
|
||||
"decode", ChannelHandlerContext.class, Channel.class, Object.class);
|
||||
method.setAccessible(true);
|
||||
tmp = method.invoke(stringDecoder, null, null, tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Protocol decoder
|
||||
BaseProtocolDecoder protocolDecoder = (BaseProtocolDecoder) pipeline.get("objectDecoder");
|
||||
if (tmp != null) {
|
||||
try {
|
||||
Method method = protocolDecoder.getClass().getDeclaredMethod(
|
||||
"decode", ChannelHandlerContext.class, Channel.class, SocketAddress.class, Object.class);
|
||||
method.setAccessible(true);
|
||||
tmp = method.invoke(protocolDecoder, null, null, null, tmp);
|
||||
} catch (NoSuchMethodException error) {
|
||||
Method method = protocolDecoder.getClass().getSuperclass().getDeclaredMethod(
|
||||
"decode", ChannelHandlerContext.class, Channel.class, SocketAddress.class, Object.class);
|
||||
method.setAccessible(true);
|
||||
tmp = method.invoke(protocolDecoder, null, null, null, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
if (tmp != null) {
|
||||
Log.info("Protocol " + protocol + " possible match");
|
||||
} else if (showFailed) {
|
||||
Log.info("Protocol " + protocol + " no match");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
|
||||
|
||||
if (e.getMessage() instanceof ChannelBuffer) {
|
||||
ChannelBuffer buf = (ChannelBuffer) e.getMessage();
|
||||
|
||||
for (TrackerServer server : serverList) {
|
||||
try {
|
||||
if (!server.getProtocol().equals("detector")) {
|
||||
checkPipeline(server.getProtocol(), server.getPipelineFactory().getPipeline(), buf);
|
||||
}
|
||||
} catch (Exception error) {
|
||||
if (showFailed) {
|
||||
Log.info("Protocol " + server.getProtocol() + " error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -68,8 +68,6 @@ public class ServerManager {
|
||||
initProtocolServer((BaseProtocol) protocolClass.newInstance());
|
||||
}
|
||||
}
|
||||
|
||||
initProtocolDetector();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
@ -88,30 +86,8 @@ public class ServerManager {
|
||||
GlobalTimer.release();
|
||||
}
|
||||
|
||||
private boolean isProtocolEnabled(String protocol) {
|
||||
return Context.getConfig().hasKey(protocol + ".port");
|
||||
}
|
||||
|
||||
private void initProtocolDetector() {
|
||||
String protocol = "detector";
|
||||
if (isProtocolEnabled(protocol)) {
|
||||
serverList.add(new TrackerServer(new ServerBootstrap(), protocol) {
|
||||
@Override
|
||||
protected void addSpecificHandlers(ChannelPipeline pipeline) {
|
||||
pipeline.addLast("detectorHandler", new DetectorHandler(serverList));
|
||||
}
|
||||
});
|
||||
serverList.add(new TrackerServer(new ConnectionlessBootstrap(), protocol) {
|
||||
@Override
|
||||
protected void addSpecificHandlers(ChannelPipeline pipeline) {
|
||||
pipeline.addLast("detectorHandler", new DetectorHandler(serverList));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void initProtocolServer(final Protocol protocol) {
|
||||
if (isProtocolEnabled(protocol.getName())) {
|
||||
if (Context.getConfig().hasKey(protocol.getName() + ".port")) {
|
||||
protocol.initTrackerServers(serverList);
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +0,0 @@
|
||||
package org.traccar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DetectorHandlerTest {
|
||||
|
||||
@Test
|
||||
public void testCheckPipeline() throws Exception {
|
||||
|
||||
/*ChannelPipeline pipeline = Channels.pipeline();
|
||||
pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, "\r\n", "\n", ";"));
|
||||
pipeline.addLast("stringDecoder", new StringDecoder());
|
||||
pipeline.addLast("stringEncoder", new StringEncoder());
|
||||
pipeline.addLast("objectDecoder", new Gps103ProtocolDecoder(null));
|
||||
|
||||
DetectorHandler.checkPipeline("gps103", pipeline, ChannelBuffers.copiedBuffer(
|
||||
"imei:869039001186913,tracker,1308282156,0,F,215630.000,A,5602.11015,N,9246.30767,E,1.4,,175.9,;", Charset.defaultCharset()));*/
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user