Skip to content

Commit

Permalink
Code sexyfication.
Browse files Browse the repository at this point in the history
  • Loading branch information
xmamo committed Nov 12, 2015
1 parent 8abd0e5 commit d72d5ad
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 60 deletions.
6 changes: 3 additions & 3 deletions src/main/java/co/virtualdragon/vanillaVotifier/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ public static interface RconConfig {
List<String> getCommands();
}

public static class AbstractRconConfig implements RconConfig {
public static class VanillaVotifierRconConfig implements RconConfig {

private InetSocketAddress inetSocketAddress;
private String password;
private ArrayList<String> commands;

public AbstractRconConfig(InetSocketAddress inetSocketAddress, String password) {
public VanillaVotifierRconConfig(InetSocketAddress inetSocketAddress, String password) {
this(inetSocketAddress, password, new ArrayList<String>());
}

public AbstractRconConfig(InetSocketAddress inetSocketAddress, String password, ArrayList<String> commands) {
public VanillaVotifierRconConfig(InetSocketAddress inetSocketAddress, String password, ArrayList<String> commands) {
this.inetSocketAddress = inetSocketAddress;
this.password = password;
if (commands == null) {
Expand Down
95 changes: 61 additions & 34 deletions src/main/java/co/virtualdragon/vanillaVotifier/Rcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import java.io.UnsupportedEncodingException;

public interface Rcon {

RconConfig getRconConfig();

void setRconConfig(RconConfig rconConfig);

int getRequestId();
Expand All @@ -32,56 +32,110 @@ public interface Rcon {

boolean isConnected();

Packet logIn() throws UnsupportedEncodingException, IOException;
VanillaVotifierPacket logIn() throws UnsupportedEncodingException, IOException;

VanillaVotifierPacket sendRequest(VanillaVotifierPacket request) throws UnsupportedEncodingException, IOException;

public static interface Packet {

int getLength();

void setLength(int length);

int getRequestId();

void setRequestId(int requestId);

Type getType();

void setType(Type type);

Packet sendRequest(Packet request) throws UnsupportedEncodingException, IOException;
String getPayload();

void setPayload(String payload);

public static enum Type {

COMMAND_RESPONSE(0), COMMAND(2), LOG_IN(3);

private int i;

private Type(int i) {
this.i = i;
}

public int toInt() {
return i;
}

public static Type fromInt(int i) {
if (i == 0) {
return COMMAND_RESPONSE;
} else if (i == 2) {
return COMMAND;
} else if (i == 3) {
return LOG_IN;
} else {
throw new IllegalArgumentException("i has to be equal to 0, 2, or 3!");
}
}
}
}

public static class Packet {
public static class VanillaVotifierPacket implements Packet {

private int length;
private int requestId;
private Type type;
private String payload;

public Packet(int requestId, Type type, String payload) {
public VanillaVotifierPacket(int requestId, Type type, String payload) {
this(Integer.SIZE / 8 + Integer.SIZE / 8 + payload.length() + Byte.SIZE / 8 * 2, requestId, type, payload);
}

public Packet(int length, int requestId, Type type, String payload) {
public VanillaVotifierPacket(int length, int requestId, Type type, String payload) {
this.length = length;
this.requestId = requestId;
this.type = type;
this.payload = payload;
}

@Override
public int getLength() {
return length;
}

@Override
public void setLength(int length) {
this.length = length;
}

@Override
public int getRequestId() {
return requestId;
}

@Override
public void setRequestId(int requestId) {
this.requestId = requestId;
}

@Override
public Type getType() {
return type;
}

@Override
public void setType(Type type) {
this.type = type;
}

@Override
public String getPayload() {
return payload;
}

@Override
public void setPayload(String payload) {
this.payload = payload;
}
Expand All @@ -90,32 +144,5 @@ public void setPayload(String payload) {
public String toString() {
return length + "\t" + requestId + "\t" + type.toInt() + "\t" + payload;
}

public static enum Type {

COMMAND_RESPONSE(0), COMMAND(2), LOG_IN(3);

private int i;

private Type(int i) {
this.i = i;
}

public int toInt() {
return i;
}

public static Type fromInt(int i) {
if (i == 0) {
return COMMAND_RESPONSE;
} else if (i == 2) {
return COMMAND;
} else if (i == 3) {
return LOG_IN;
} else {
throw new IllegalArgumentException("i has to be equal to 0, 2, or 3!");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.net.InetSocketAddress;
import java.security.KeyPair;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
Expand Down Expand Up @@ -122,7 +121,7 @@ public void load() throws Exception {
rconConfigs = new ArrayList<RconConfig>();
for (int i = 0; i < config.getJSONArray("rcon-list").length(); i++) {
JSONObject jsonObject = config.getJSONArray("rcon-list").getJSONObject(i);
JsonRconConfig rconConfig = new JsonRconConfig(new InetSocketAddress(jsonObject.getString("ip"), jsonObject.getInt("port")), jsonObject.getString("password"));
VanillaVotifierRconConfig rconConfig = new VanillaVotifierRconConfig(new InetSocketAddress(jsonObject.getString("ip"), jsonObject.getInt("port")), jsonObject.getString("password"));
for (int j = 0; j < jsonObject.getJSONArray("commands").length(); j++) {
rconConfig.getCommands().add(jsonObject.getJSONArray("commands").getString(j));
}
Expand Down Expand Up @@ -286,15 +285,4 @@ public void save() throws IOException {
privatePemWriter.flush();
privatePemWriter.close();
}

public static class JsonRconConfig extends AbstractRconConfig {

public JsonRconConfig(InetSocketAddress inetSocketAddress, String password) {
super(inetSocketAddress, password);
}

public JsonRconConfig(InetSocketAddress inetSocketAddress, String password, ArrayList<String> commands) {
super(inetSocketAddress, password, commands);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import co.virtualdragon.vanillaVotifier.CommandSender;
import co.virtualdragon.vanillaVotifier.Rcon;
import co.virtualdragon.vanillaVotifier.Rcon.Packet;
import co.virtualdragon.vanillaVotifier.Rcon.VanillaVotifierPacket;
import co.virtualdragon.vanillaVotifier.Votifier;
import java.net.SocketException;

Expand All @@ -35,15 +35,15 @@ public String sendCommand(Rcon rcon, String command) throws Exception {
if (!rcon.isConnected()) {
rcon.connect();
}
Packet packet = null;
VanillaVotifierPacket packet = null;
try {
packet = rcon.sendRequest(new Packet(rcon.getRequestId(), Packet.Type.LOG_IN, rcon.getRconConfig().getPassword()));
packet = rcon.sendRequest(new VanillaVotifierPacket(rcon.getRequestId(), VanillaVotifierPacket.Type.LOG_IN, rcon.getRconConfig().getPassword()));
} catch (SocketException e) {
rcon.connect();
packet = rcon.sendRequest(new Packet(rcon.getRequestId(), Packet.Type.LOG_IN, rcon.getRconConfig().getPassword()));
packet = rcon.sendRequest(new VanillaVotifierPacket(rcon.getRequestId(), VanillaVotifierPacket.Type.LOG_IN, rcon.getRconConfig().getPassword()));
}
if (packet.getRequestId() != -1) {
return rcon.sendRequest(new Packet(rcon.getRequestId(), Packet.Type.COMMAND, command)).getPayload();
return rcon.sendRequest(new VanillaVotifierPacket(rcon.getRequestId(), VanillaVotifierPacket.Type.COMMAND, command)).getPayload();
} else {
throw new Exception("Invalid password.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import co.virtualdragon.vanillaVotifier.Config.RconConfig;
import co.virtualdragon.vanillaVotifier.Rcon;
import co.virtualdragon.vanillaVotifier.Rcon.Packet;
import co.virtualdragon.vanillaVotifier.Rcon.Packet.Type;
import co.virtualdragon.vanillaVotifier.Rcon.VanillaVotifierPacket;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
Expand Down Expand Up @@ -74,12 +74,12 @@ public int getRequestId() {
}

@Override
public Packet logIn() throws UnsupportedEncodingException, IOException {
return sendRequest(new Packet(requestId, Type.LOG_IN, rconConfig.getPassword()));
public VanillaVotifierPacket logIn() throws UnsupportedEncodingException, IOException {
return sendRequest(new VanillaVotifierPacket(requestId, Type.LOG_IN, rconConfig.getPassword()));
}

@Override
public Packet sendRequest(Packet request) throws UnsupportedEncodingException, IOException {
public VanillaVotifierPacket sendRequest(VanillaVotifierPacket request) throws UnsupportedEncodingException, IOException {
byte[] requestBytes = new byte[request.getLength() + Integer.SIZE / 8];
ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);
requestBuffer.order(ByteOrder.LITTLE_ENDIAN);
Expand All @@ -106,6 +106,6 @@ public Packet sendRequest(Packet request) throws UnsupportedEncodingException, I
responseBuffer.get(responsePayload);
responseBuffer.get();
responseBuffer.get();
return new Packet(responseLength, responseRequestId, responseType, new String(responsePayload, "UTF-8"));
return new VanillaVotifierPacket(responseLength, responseRequestId, responseType, new String(responsePayload, "UTF-8"));
}
}

0 comments on commit d72d5ad

Please sign in to comment.