diff options
Diffstat (limited to 'src/org/traccar/TrackerServer.java')
-rw-r--r-- | src/org/traccar/TrackerServer.java | 101 |
1 files changed, 42 insertions, 59 deletions
diff --git a/src/org/traccar/TrackerServer.java b/src/org/traccar/TrackerServer.java index 7e0a6f0c6..3a1e1c4e8 100644 --- a/src/org/traccar/TrackerServer.java +++ b/src/org/traccar/TrackerServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 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,55 +15,58 @@ */ package org.traccar; -import org.jboss.netty.bootstrap.Bootstrap; -import org.jboss.netty.bootstrap.ConnectionlessBootstrap; -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.buffer.HeapChannelBufferFactory; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.channel.ChannelPipelineFactory; -import org.jboss.netty.channel.group.ChannelGroup; -import org.jboss.netty.channel.group.ChannelGroupFuture; -import org.jboss.netty.channel.group.DefaultChannelGroup; +import io.netty.bootstrap.AbstractBootstrap; +import io.netty.bootstrap.Bootstrap; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.Channel; +import io.netty.channel.group.ChannelGroup; +import io.netty.channel.group.DefaultChannelGroup; +import io.netty.channel.socket.nio.NioDatagramChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.util.concurrent.GlobalEventExecutor; import java.net.InetSocketAddress; -import java.nio.ByteOrder; public abstract class TrackerServer { - private final Bootstrap bootstrap; - private final String protocol; + private final boolean datagram; + private final AbstractBootstrap bootstrap; - public boolean isConnectionless() { - return bootstrap instanceof ConnectionlessBootstrap; + public boolean isDatagram() { + return datagram; } - public String getProtocol() { - return protocol; - } - - public TrackerServer(Bootstrap bootstrap, String protocol) { - this.bootstrap = bootstrap; - this.protocol = protocol; - - if (bootstrap instanceof ServerBootstrap) { - bootstrap.setFactory(GlobalChannelFactory.getFactory()); - } else if (bootstrap instanceof ConnectionlessBootstrap) { - bootstrap.setFactory(GlobalChannelFactory.getDatagramFactory()); - } + public TrackerServer(boolean datagram, String protocol) { + this.datagram = datagram; address = Context.getConfig().getString(protocol + ".address"); port = Context.getConfig().getInteger(protocol + ".port"); - bootstrap.setPipelineFactory(new BasePipelineFactory(this, protocol) { + BasePipelineFactory pipelineFactory = new BasePipelineFactory(this, protocol) { @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { - TrackerServer.this.addSpecificHandlers(pipeline); + protected void addProtocolHandlers(PipelineBuilder pipeline) { + TrackerServer.this.addProtocolHandlers(pipeline); } - }); + }; + + if (datagram) { + + this.bootstrap = new Bootstrap() + .group(EventLoopGroupFactory.getWorkerGroup()) + .channel(NioDatagramChannel.class) + .handler(pipelineFactory); + + } else { + + this.bootstrap = new ServerBootstrap() + .group(EventLoopGroupFactory.getBossGroup(), EventLoopGroupFactory.getWorkerGroup()) + .channel(NioServerSocketChannel.class) + .childHandler(pipelineFactory); + + } } - protected abstract void addSpecificHandlers(ChannelPipeline pipeline); + protected abstract void addProtocolHandlers(PipelineBuilder pipeline); private int port; @@ -85,26 +88,13 @@ public abstract class TrackerServer { this.address = address; } - public void setEndianness(ByteOrder byteOrder) { - bootstrap.setOption("bufferFactory", new HeapChannelBufferFactory(byteOrder)); - bootstrap.setOption("child.bufferFactory", new HeapChannelBufferFactory(byteOrder)); - } - - private final ChannelGroup allChannels = new DefaultChannelGroup(); + private final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); public ChannelGroup getChannelGroup() { - return allChannels; - } - - public void setPipelineFactory(ChannelPipelineFactory pipelineFactory) { - bootstrap.setPipelineFactory(pipelineFactory); + return channelGroup; } - public ChannelPipelineFactory getPipelineFactory() { - return bootstrap.getPipelineFactory(); - } - - public void start() { + public void start() throws Exception { InetSocketAddress endpoint; if (address == null) { endpoint = new InetSocketAddress(port); @@ -112,21 +102,14 @@ public abstract class TrackerServer { endpoint = new InetSocketAddress(address, port); } - Channel channel = null; - if (bootstrap instanceof ServerBootstrap) { - channel = ((ServerBootstrap) bootstrap).bind(endpoint); - } else if (bootstrap instanceof ConnectionlessBootstrap) { - channel = ((ConnectionlessBootstrap) bootstrap).bind(endpoint); - } - + Channel channel = bootstrap.bind(endpoint).sync().channel(); if (channel != null) { getChannelGroup().add(channel); } } public void stop() { - ChannelGroupFuture future = getChannelGroup().close(); - future.awaitUninterruptibly(); + channelGroup.close().awaitUninterruptibly(); } } |