Skip to content

Commit

Permalink
Move server init into constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
tananaev committed Oct 17, 2018
1 parent ae32084 commit 61d4c1f
Show file tree
Hide file tree
Showing 186 changed files with 229 additions and 1,293 deletions.
12 changes: 12 additions & 0 deletions src/org/traccar/BaseProtocol.java
Expand Up @@ -24,13 +24,16 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

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 StringProtocolEncoder textCommandEncoder = null;

Expand All @@ -48,6 +51,15 @@ public String getName() {
return name;
}

protected void addServer(TrackerServer server) {
serverList.add(server);
}

@Override
public Collection<TrackerServer> getServerList() {
return serverList;
}

public void setSupportedDataCommands(String... commands) {
supportedDataCommands.addAll(Arrays.asList(commands));
}
Expand Down
5 changes: 2 additions & 3 deletions src/org/traccar/Protocol.java
Expand Up @@ -19,18 +19,17 @@
import org.traccar.model.Command;

import java.util.Collection;
import java.util.List;

public interface Protocol {

String getName();

Collection<TrackerServer> getServerList();

Collection<String> getSupportedDataCommands();

void sendDataCommand(ActiveDevice activeDevice, Command command);

void initTrackerServers(List<TrackerServer> serverList);

Collection<String> getSupportedTextCommands();

void sendTextCommand(String destAddress, Command command) throws Exception;
Expand Down
15 changes: 5 additions & 10 deletions src/org/traccar/ServerManager.java
Expand Up @@ -70,10 +70,11 @@ public ServerManager() throws Exception {

for (String name : names) {
Class protocolClass = Class.forName(packageName + '.' + name);
if (BaseProtocol.class.isAssignableFrom(protocolClass)) {
BaseProtocol baseProtocol = (BaseProtocol) protocolClass.newInstance();
initProtocolServer(baseProtocol);
protocolList.put(baseProtocol.getName(), baseProtocol);
if (BaseProtocol.class.isAssignableFrom(protocolClass)
&& Context.getConfig().hasKey(BaseProtocol.nameFromClass(protocolClass) + ".port")) {
BaseProtocol protocol = (BaseProtocol) protocolClass.newInstance();
serverList.addAll(protocol.getServerList());
protocolList.put(protocol.getName(), protocol);
}
}
}
Expand All @@ -99,10 +100,4 @@ public void stop() {
GlobalTimer.release();
}

private void initProtocolServer(final Protocol protocol) {
if (Context.getConfig().hasKey(protocol.getName() + ".port")) {
protocol.initTrackerServers(serverList);
}
}

}
7 changes: 1 addition & 6 deletions src/org/traccar/protocol/AdmProtocol.java
Expand Up @@ -23,19 +23,14 @@
import org.traccar.model.Command;

import java.nio.ByteOrder;
import java.util.List;

public class AdmProtocol extends BaseProtocol {

public AdmProtocol() {
setSupportedDataCommands(
Command.TYPE_GET_DEVICE_STATUS,
Command.TYPE_CUSTOM);
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 1024, 2, 1, -3, 0, true));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AisProtocol.java
Expand Up @@ -20,16 +20,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AisProtocol extends BaseProtocol {

public AisProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new StringDecoder());
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AlematicsProtocol.java
Expand Up @@ -21,16 +21,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AlematicsProtocol extends BaseProtocol {

public AlematicsProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new AlematicsFrameDecoder(1024));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AnytrekProtocol.java
Expand Up @@ -20,16 +20,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AnytrekProtocol extends BaseProtocol {

public AnytrekProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 2, 2, 2, 0));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/ApelProtocol.java
Expand Up @@ -21,16 +21,10 @@
import org.traccar.TrackerServer;

import java.nio.ByteOrder;
import java.util.List;

public class ApelProtocol extends BaseProtocol {

public ApelProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN, 1024, 2, 2, 4, 0, true));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AplicomProtocol.java
Expand Up @@ -19,16 +19,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AplicomProtocol extends BaseProtocol {

public AplicomProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new AplicomFrameDecoder());
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AppelloProtocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AppelloProtocol extends BaseProtocol {

public AppelloProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LineBasedFrameDecoder(1024));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AppletProtocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AppletProtocol extends BaseProtocol {

public AppletProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new HttpResponseEncoder());
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/AquilaProtocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AquilaProtocol extends BaseProtocol {

public AquilaProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LineBasedFrameDecoder(1024));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/Ardi01Protocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class Ardi01Protocol extends BaseProtocol {

public Ardi01Protocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LineBasedFrameDecoder(1024));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/ArknavProtocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class ArknavProtocol extends BaseProtocol {

public ArknavProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new CharacterDelimiterFrameDecoder(1024, '\r'));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/ArknavX8Protocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class ArknavX8Protocol extends BaseProtocol {

public ArknavX8Protocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new CharacterDelimiterFrameDecoder(1024, ';'));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/ArnaviProtocol.java
Expand Up @@ -22,16 +22,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class ArnaviProtocol extends BaseProtocol {

public ArnaviProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LineBasedFrameDecoder(1024));
Expand Down
10 changes: 2 additions & 8 deletions src/org/traccar/protocol/AstraProtocol.java
Expand Up @@ -20,23 +20,17 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class AstraProtocol extends BaseProtocol {

public AstraProtocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 1, 2, -3, 0));
pipeline.addLast(new AstraProtocolDecoder(AstraProtocol.this));
}
});
serverList.add(new TrackerServer(true, getName()) {
addServer(new TrackerServer(true, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new AstraProtocolDecoder(AstraProtocol.this));
Expand Down
8 changes: 1 addition & 7 deletions src/org/traccar/protocol/At2000Protocol.java
Expand Up @@ -19,16 +19,10 @@
import org.traccar.PipelineBuilder;
import org.traccar.TrackerServer;

import java.util.List;

public class At2000Protocol extends BaseProtocol {

public At2000Protocol() {
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new At2000FrameDecoder());
Expand Down
10 changes: 2 additions & 8 deletions src/org/traccar/protocol/AtrackProtocol.java
Expand Up @@ -20,26 +20,20 @@
import org.traccar.TrackerServer;
import org.traccar.model.Command;

import java.util.List;

public class AtrackProtocol extends BaseProtocol {

public AtrackProtocol() {
setSupportedDataCommands(
Command.TYPE_CUSTOM);
}

@Override
public void initTrackerServers(List<TrackerServer> serverList) {
serverList.add(new TrackerServer(false, getName()) {
addServer(new TrackerServer(false, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new AtrackFrameDecoder());
pipeline.addLast(new AtrackProtocolEncoder());
pipeline.addLast(new AtrackProtocolDecoder(AtrackProtocol.this));
}
});
serverList.add(new TrackerServer(true, getName()) {
addServer(new TrackerServer(true, getName()) {
@Override
protected void addProtocolHandlers(PipelineBuilder pipeline) {
pipeline.addLast(new AtrackProtocolEncoder());
Expand Down

0 comments on commit 61d4c1f

Please sign in to comment.