diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-12-28 23:09:40 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2022-01-14 21:01:16 -0800 |
commit | c1121777969fc5f183e325ad672b581cbe881895 (patch) | |
tree | 8d874f0244ceb68c7bc9f440f44b20cca882fdad /src/main/java/org/traccar | |
parent | 839751e76e329adb573150644bd6198beba0d3b6 (diff) | |
download | trackermap-server-c1121777969fc5f183e325ad672b581cbe881895.tar.gz trackermap-server-c1121777969fc5f183e325ad672b581cbe881895.tar.bz2 trackermap-server-c1121777969fc5f183e325ad672b581cbe881895.zip |
Extract connector interface
Diffstat (limited to 'src/main/java/org/traccar')
-rw-r--r-- | src/main/java/org/traccar/BasePipelineFactory.java | 10 | ||||
-rw-r--r-- | src/main/java/org/traccar/BaseProtocol.java | 10 | ||||
-rw-r--r-- | src/main/java/org/traccar/Protocol.java | 2 | ||||
-rw-r--r-- | src/main/java/org/traccar/ServerManager.java | 16 | ||||
-rw-r--r-- | src/main/java/org/traccar/TrackerConnector.java | 30 | ||||
-rw-r--r-- | src/main/java/org/traccar/TrackerServer.java | 29 | ||||
-rw-r--r-- | src/main/java/org/traccar/handler/OpenChannelHandler.java | 14 |
7 files changed, 70 insertions, 41 deletions
diff --git a/src/main/java/org/traccar/BasePipelineFactory.java b/src/main/java/org/traccar/BasePipelineFactory.java index c9f3a2346..88a1bc713 100644 --- a/src/main/java/org/traccar/BasePipelineFactory.java +++ b/src/main/java/org/traccar/BasePipelineFactory.java @@ -54,12 +54,12 @@ import java.util.Map; public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { - private final TrackerServer server; + private final TrackerConnector connector; private final String protocol; private int timeout; - public BasePipelineFactory(TrackerServer server, String protocol) { - this.server = server; + public BasePipelineFactory(TrackerConnector connector, String protocol) { + this.connector = connector; this.protocol = protocol; timeout = Context.getConfig().getInteger(Keys.PROTOCOL_TIMEOUT.withPrefix(protocol)); if (timeout == 0) { @@ -97,10 +97,10 @@ public abstract class BasePipelineFactory extends ChannelInitializer<Channel> { protected void initChannel(Channel channel) { final ChannelPipeline pipeline = channel.pipeline(); - if (timeout > 0 && !server.isDatagram()) { + if (timeout > 0 && !connector.isDatagram()) { pipeline.addLast(new IdleStateHandler(timeout, 0, 0)); } - pipeline.addLast(new OpenChannelHandler(server)); + pipeline.addLast(new OpenChannelHandler(connector)); pipeline.addLast(new NetworkMessageHandler()); pipeline.addLast(new StandardLoggingHandler(protocol)); diff --git a/src/main/java/org/traccar/BaseProtocol.java b/src/main/java/org/traccar/BaseProtocol.java index bd3391822..2fca9432e 100644 --- a/src/main/java/org/traccar/BaseProtocol.java +++ b/src/main/java/org/traccar/BaseProtocol.java @@ -35,7 +35,7 @@ public abstract class BaseProtocol implements Protocol { private final String name; private final Set<String> supportedDataCommands = new HashSet<>(); private final Set<String> supportedTextCommands = new HashSet<>(); - private final List<TrackerServer> serverList = new LinkedList<>(); + private final List<TrackerConnector> connectorList = new LinkedList<>(); private StringProtocolEncoder textCommandEncoder = null; @@ -54,12 +54,14 @@ public abstract class BaseProtocol implements Protocol { } protected void addServer(TrackerServer server) { - serverList.add(server); + connectorList.add(server); } + // TODO addClient + @Override - public Collection<TrackerServer> getServerList() { - return serverList; + public Collection<TrackerConnector> getConnectorList() { + return connectorList; } public void setSupportedDataCommands(String... commands) { diff --git a/src/main/java/org/traccar/Protocol.java b/src/main/java/org/traccar/Protocol.java index aea69b353..bc9c99557 100644 --- a/src/main/java/org/traccar/Protocol.java +++ b/src/main/java/org/traccar/Protocol.java @@ -25,7 +25,7 @@ public interface Protocol { String getName(); - Collection<TrackerServer> getServerList(); + Collection<TrackerConnector> getConnectorList(); Collection<String> getSupportedDataCommands(); diff --git a/src/main/java/org/traccar/ServerManager.java b/src/main/java/org/traccar/ServerManager.java index 935a821aa..45ac656f3 100644 --- a/src/main/java/org/traccar/ServerManager.java +++ b/src/main/java/org/traccar/ServerManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2021 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. @@ -39,7 +39,7 @@ public class ServerManager { private static final Logger LOGGER = LoggerFactory.getLogger(ServerManager.class); - private final List<TrackerServer> serverList = new LinkedList<>(); + private final List<TrackerConnector> connectorList = new LinkedList<>(); private final Map<String, BaseProtocol> protocolList = new ConcurrentHashMap<>(); private void loadPackage(String packageName) throws IOException, URISyntaxException, ReflectiveOperationException { @@ -75,7 +75,7 @@ public class ServerManager { if (BaseProtocol.class.isAssignableFrom(protocolClass) && Context.getConfig().hasKey( Keys.PROTOCOL_PORT.withPrefix(BaseProtocol.nameFromClass(protocolClass)))) { BaseProtocol protocol = (BaseProtocol) protocolClass.getDeclaredConstructor().newInstance(); - serverList.addAll(protocol.getServerList()); + connectorList.addAll(protocol.getConnectorList()); protocolList.put(protocol.getName(), protocol); } } @@ -90,18 +90,18 @@ public class ServerManager { } public void start() throws Exception { - for (TrackerServer server: serverList) { + for (TrackerConnector connector: connectorList) { try { - server.start(); + connector.start(); } catch (BindException e) { - LOGGER.warn("Port {} is disabled due to conflict", server.getPort()); + LOGGER.warn("Port disabled due to conflict", e); } } } public void stop() { - for (TrackerServer server: serverList) { - server.stop(); + for (TrackerConnector connector: connectorList) { + connector.stop(); } GlobalTimer.release(); } diff --git a/src/main/java/org/traccar/TrackerConnector.java b/src/main/java/org/traccar/TrackerConnector.java new file mode 100644 index 000000000..9fc5e0f62 --- /dev/null +++ b/src/main/java/org/traccar/TrackerConnector.java @@ -0,0 +1,30 @@ +/* + * Copyright 2021 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. + * 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 io.netty.channel.group.ChannelGroup; + +public interface TrackerConnector { + + boolean isDatagram(); + + ChannelGroup getChannelGroup(); + + void start() throws Exception; + + void stop(); + +} diff --git a/src/main/java/org/traccar/TrackerServer.java b/src/main/java/org/traccar/TrackerServer.java index 59ba123e2..7b25e5cc5 100644 --- a/src/main/java/org/traccar/TrackerServer.java +++ b/src/main/java/org/traccar/TrackerServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2021 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. @@ -28,11 +28,19 @@ import org.traccar.config.Keys; import java.net.InetSocketAddress; -public abstract class TrackerServer { +public abstract class TrackerServer implements TrackerConnector { private final boolean datagram; + + @SuppressWarnings("rawtypes") private final AbstractBootstrap bootstrap; + private final int port; + private final String address; + + private final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); + + @Override public boolean isDatagram() { return datagram; } @@ -69,32 +77,20 @@ public abstract class TrackerServer { protected abstract void addProtocolHandlers(PipelineBuilder pipeline); - private int port; - public int getPort() { return port; } - public void setPort(int port) { - this.port = port; - } - - private String address; - public String getAddress() { return address; } - public void setAddress(String address) { - this.address = address; - } - - private final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); - + @Override public ChannelGroup getChannelGroup() { return channelGroup; } + @Override public void start() throws Exception { InetSocketAddress endpoint; if (address == null) { @@ -109,6 +105,7 @@ public abstract class TrackerServer { } } + @Override public void stop() { channelGroup.close().awaitUninterruptibly(); } diff --git a/src/main/java/org/traccar/handler/OpenChannelHandler.java b/src/main/java/org/traccar/handler/OpenChannelHandler.java index d09d617ab..e416f35ae 100644 --- a/src/main/java/org/traccar/handler/OpenChannelHandler.java +++ b/src/main/java/org/traccar/handler/OpenChannelHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2019 - 2021 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. @@ -17,26 +17,26 @@ package org.traccar.handler; import io.netty.channel.ChannelDuplexHandler; import io.netty.channel.ChannelHandlerContext; -import org.traccar.TrackerServer; +import org.traccar.TrackerConnector; public class OpenChannelHandler extends ChannelDuplexHandler { - private final TrackerServer server; + private final TrackerConnector connector; - public OpenChannelHandler(TrackerServer server) { - this.server = server; + public OpenChannelHandler(TrackerConnector connector) { + this.connector = connector; } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); - server.getChannelGroup().add(ctx.channel()); + connector.getChannelGroup().add(ctx.channel()); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { super.channelInactive(ctx); - server.getChannelGroup().remove(ctx.channel()); + connector.getChannelGroup().remove(ctx.channel()); } } |