Refactoring noms de classes des packets

This commit is contained in:
Marc Baloup 2017-01-15 22:27:57 +01:00
parent 48bbec7ff0
commit 20ed0fee2c
19 changed files with 114 additions and 95 deletions

View File

@ -25,7 +25,7 @@ import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.PacketException; import fr.pandacube.java.util.network.packet.PacketException;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.ResponseCallback; import fr.pandacube.java.util.network.packet.ResponseCallback;
import fr.pandacube.java.util.network.packet.packets.global.PacketServerException; import fr.pandacube.java.util.network.packet.packets.global.PacketD0ServerException;
import org.javatuples.Pair; import org.javatuples.Pair;
public class TCPClient extends Thread implements Closeable { public class TCPClient extends Thread implements Closeable {
@ -83,10 +83,10 @@ public class TCPClient extends Thread implements Closeable {
if (!(p instanceof PacketServer)) if (!(p instanceof PacketServer))
throw new PacketException(p.getClass().getCanonicalName() + " is not a subclass of PacketServer"); throw new PacketException(p.getClass().getCanonicalName() + " is not a subclass of PacketServer");
if (p instanceof PacketServerException) { if (p instanceof PacketD0ServerException) {
try { try {
listener.onServerException(this, ((PacketServerException)p).getExceptionString()); listener.onServerException(this, ((PacketD0ServerException)p).getExceptionString());
} catch (Exception e) { } catch (Exception e) {
Log.severe("Exception while calling TCPClientListener.onServerException()", e); Log.severe("Exception while calling TCPClientListener.onServerException()", e);
} }

View File

@ -9,20 +9,20 @@ import fr.pandacube.java.Pandacube;
import fr.pandacube.java.util.Log; import fr.pandacube.java.util.Log;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteSerializable; import fr.pandacube.java.util.network.packet.bytebuffer.ByteSerializable;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketClientClose; import fr.pandacube.java.util.network.packet.packets.core_slave.Packet31ClientClose;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketClientDeclareProcess; import fr.pandacube.java.util.network.packet.packets.core_slave.Packet30ClientDeclareProcess;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketClientProcessQueryResponse; import fr.pandacube.java.util.network.packet.packets.core_slave.Packet32ClientProcessQueryResponse;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerClose; import fr.pandacube.java.util.network.packet.packets.core_slave.PacketB0ServerClose;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerConnectSuccess; import fr.pandacube.java.util.network.packet.packets.core_slave.PacketB2ServerConnectSuccess;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessDeclarationConfirm; import fr.pandacube.java.util.network.packet.packets.core_slave.PacketB1ServerProcessDeclarationConfirm;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessInput; import fr.pandacube.java.util.network.packet.packets.core_slave.PacketB3ServerProcessInput;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessQuery; import fr.pandacube.java.util.network.packet.packets.core_slave.PacketB4ServerProcessQuery;
import fr.pandacube.java.util.network.packet.packets.global.PacketClientAuthenticate; import fr.pandacube.java.util.network.packet.packets.global.Packet50ClientAuthenticate;
import fr.pandacube.java.util.network.packet.packets.global.PacketClientLogRecord; import fr.pandacube.java.util.network.packet.packets.global.Packet51ClientLogRecord;
import fr.pandacube.java.util.network.packet.packets.global.PacketServerCantAuthenticate; import fr.pandacube.java.util.network.packet.packets.global.PacketD1ServerCantAuthenticate;
import fr.pandacube.java.util.network.packet.packets.global.PacketServerException; import fr.pandacube.java.util.network.packet.packets.global.PacketD0ServerException;
import fr.pandacube.java.util.network.packet.packets.web.PacketClientWebRequest; import fr.pandacube.java.util.network.packet.packets.web.Packet00ClientWebRequest;
import fr.pandacube.java.util.network.packet.packets.web.PacketServerWebResponse; import fr.pandacube.java.util.network.packet.packets.web.Packet80ServerWebResponse;
/** <pre> /** <pre>
* Identification des packets réseaux * Identification des packets réseaux
@ -96,22 +96,22 @@ public abstract class Packet implements ByteSerializable {
/* /*
* Ajout des types de packets (client + serveur) * Ajout des types de packets (client + serveur)
*/ */
addPacket(PacketClientClose.class); addPacket(Packet31ClientClose.class);
addPacket(PacketClientDeclareProcess.class); addPacket(Packet30ClientDeclareProcess.class);
addPacket(PacketClientProcessQueryResponse.class); addPacket(Packet32ClientProcessQueryResponse.class);
addPacket(PacketServerClose.class); addPacket(PacketB0ServerClose.class);
addPacket(PacketServerConnectSuccess.class); addPacket(PacketB2ServerConnectSuccess.class);
addPacket(PacketServerProcessDeclarationConfirm.class); addPacket(PacketB1ServerProcessDeclarationConfirm.class);
addPacket(PacketServerProcessInput.class); addPacket(PacketB3ServerProcessInput.class);
addPacket(PacketServerProcessQuery.class); addPacket(PacketB4ServerProcessQuery.class);
addPacket(PacketClientAuthenticate.class); addPacket(Packet50ClientAuthenticate.class);
addPacket(PacketClientLogRecord.class); addPacket(Packet51ClientLogRecord.class);
addPacket(PacketServerCantAuthenticate.class); addPacket(PacketD1ServerCantAuthenticate.class);
addPacket(PacketServerException.class); addPacket(PacketD0ServerException.class);
addPacket(PacketClientWebRequest.class); addPacket(Packet00ClientWebRequest.class);
addPacket(PacketServerWebResponse.class); addPacket(Packet80ServerWebResponse.class);
} }

View File

@ -2,37 +2,31 @@ package fr.pandacube.java.util.network.packet.bytebuffer;
import java.util.Arrays; import java.util.Arrays;
public class Array8Bit { public class Array8Bit implements ByteSerializable {
public static final int BIT_COUNT = 8; public static final int BIT_COUNT = Byte.SIZE;
private boolean[] values = new boolean[BIT_COUNT]; private boolean[] values = new boolean[BIT_COUNT];
public Array8Bit(byte b) {
fromByte(b);
}
/** /**
* * @param bits (index 0 is the lowest significant bit)
* @param b unsigned integer value. Lowest significant bit will be used.
*/ */
public Array8Bit(int b) {
for (int i = 0; i<BIT_COUNT; i++) {
values[i] = (b % 2 == 1);
b >>= 1;
}
}
public Array8Bit(boolean[] bits) { public Array8Bit(boolean[] bits) {
if (bits == null || bits.length != BIT_COUNT) if (bits == null || bits.length != BIT_COUNT)
throw new IllegalArgumentException("bits is null or bits.length != "+BIT_COUNT); throw new IllegalArgumentException("bits is null or bits.length != "+BIT_COUNT);
values = Arrays.copyOf(bits, BIT_COUNT); values = Arrays.copyOf(bits, BIT_COUNT);
} }
/** /**
* i = 0 is the lowest significant bit * i = 0 is the lowest significant bit
* @param i * @param i
* @return * @return
*/ */
public boolean getValue(int i) { public boolean getBit(int i) {
return values[i]; return values[i];
} }
@ -41,12 +35,23 @@ public class Array8Bit {
* @param i * @param i
* @param b * @param b
*/ */
public void setValue(int i, boolean b) { public void setBit(int i, boolean b) {
values[i] = b; values[i] = b;
} }
public byte getValuesAsByte() {
public void fromByte(byte b) {
int mask = 1;
for (int i = 0; i < BIT_COUNT; i++) {
values[i] = (b & mask) != 0;
mask <<= 1;
}
}
public byte toByte() {
byte b = 0; byte b = 0;
for (int i=BIT_COUNT-1; i>=0; i--) { for (int i=BIT_COUNT-1; i>=0; i--) {
b <<= 1; b <<= 1;
@ -55,4 +60,18 @@ public class Array8Bit {
return b; return b;
} }
@Override
public void deserializeFromByteBuffer(ByteBuffer buffer) {
fromByte(buffer.getByte());
}
@Override
public void serializeToByteBuffer(ByteBuffer buffer) {
buffer.putByte(toByte());
}
} }

View File

@ -3,12 +3,12 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketClient; import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketClientDeclareProcess extends PacketClient { public class Packet30ClientDeclareProcess extends PacketClient {
private String processName; private String processName;
private String type; private String type;
public PacketClientDeclareProcess() { public Packet30ClientDeclareProcess() {
super((byte)0x30); super((byte)0x30);
} }

View File

@ -3,9 +3,9 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketClient; import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketClientClose extends PacketClient { public class Packet31ClientClose extends PacketClient {
public PacketClientClose() { public Packet31ClientClose() {
super((byte)0x31); super((byte)0x31);
} }

View File

@ -2,16 +2,16 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketClient; import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessQuery.QueryType; import fr.pandacube.java.util.network.packet.packets.core_slave.PacketB4ServerProcessQuery.QueryType;
public class PacketClientProcessQueryResponse extends PacketClient { public class Packet32ClientProcessQueryResponse extends PacketClient {
private QueryType type; private QueryType type;
private int queryId; private int queryId;
private byte[] responseData = null; private byte[] responseData = null;
public PacketClientProcessQueryResponse() { public Packet32ClientProcessQueryResponse() {
super((byte)0x32); super((byte)0x32);
} }
@ -35,23 +35,23 @@ public class PacketClientProcessQueryResponse extends PacketClient {
} }
public static PacketClientProcessQueryResponse destroyResponse(int queryId) { public static Packet32ClientProcessQueryResponse destroyResponse(int queryId) {
PacketClientProcessQueryResponse q = new PacketClientProcessQueryResponse(); Packet32ClientProcessQueryResponse q = new Packet32ClientProcessQueryResponse();
q.type = QueryType.DESTROY; q.type = QueryType.DESTROY;
q.queryId = queryId; q.queryId = queryId;
return q; return q;
} }
public static PacketClientProcessQueryResponse isAliveResponse(int queryId, boolean resp) { public static Packet32ClientProcessQueryResponse isAliveResponse(int queryId, boolean resp) {
PacketClientProcessQueryResponse q = new PacketClientProcessQueryResponse(); Packet32ClientProcessQueryResponse q = new Packet32ClientProcessQueryResponse();
q.type = QueryType.IS_ALIVE; q.type = QueryType.IS_ALIVE;
q.queryId = queryId; q.queryId = queryId;
q.responseData = new byte[] {(byte)(resp ? 1 : 0)}; q.responseData = new byte[] {(byte)(resp ? 1 : 0)};
return q; return q;
} }
public static PacketClientProcessQueryResponse exitStatusResponse(int queryId, int resp) { public static Packet32ClientProcessQueryResponse exitStatusResponse(int queryId, int resp) {
PacketClientProcessQueryResponse q = new PacketClientProcessQueryResponse(); Packet32ClientProcessQueryResponse q = new Packet32ClientProcessQueryResponse();
q.type = QueryType.EXIT_STATUS; q.type = QueryType.EXIT_STATUS;
q.queryId = queryId; q.queryId = queryId;
q.responseData = new ByteBuffer(4, CHARSET).putInt(resp).array(); q.responseData = new ByteBuffer(4, CHARSET).putInt(resp).array();

View File

@ -3,9 +3,9 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerClose extends PacketServer { public class PacketB0ServerClose extends PacketServer {
public PacketServerClose() { public PacketB0ServerClose() {
super((byte)0xB0); super((byte)0xB0);
} }

View File

@ -3,11 +3,11 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerProcessDeclarationConfirm extends PacketServer { public class PacketB1ServerProcessDeclarationConfirm extends PacketServer {
private String serverName; private String serverName;
public PacketServerProcessDeclarationConfirm() { public PacketB1ServerProcessDeclarationConfirm() {
super((byte)0xB1); super((byte)0xB1);
} }

View File

@ -3,9 +3,9 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerConnectSuccess extends PacketServer { public class PacketB2ServerConnectSuccess extends PacketServer {
public PacketServerConnectSuccess() { public PacketB2ServerConnectSuccess() {
super((byte)0xB2); super((byte)0xB2);
} }

View File

@ -3,12 +3,12 @@ package fr.pandacube.java.util.network.packet.packets.core_slave;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerProcessInput extends PacketServer { public class PacketB3ServerProcessInput extends PacketServer {
private String serverName; private String serverName;
private byte[] dataToSend; private byte[] dataToSend;
public PacketServerProcessInput() { public PacketB3ServerProcessInput() {
super((byte)0xB3); super((byte)0xB3);
} }

View File

@ -4,7 +4,7 @@ import fr.pandacube.java.util.RandomUtil;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerProcessQuery extends PacketServer { public class PacketB4ServerProcessQuery extends PacketServer {
private String processName; private String processName;
private QueryType type; private QueryType type;
@ -12,7 +12,7 @@ public class PacketServerProcessQuery extends PacketServer {
private byte[] queryData = null; private byte[] queryData = null;
public PacketServerProcessQuery() { public PacketB4ServerProcessQuery() {
super((byte)0xB4); super((byte)0xB4);
} }
@ -36,30 +36,30 @@ public class PacketServerProcessQuery extends PacketServer {
} }
public static PacketServerProcessQuery startQuery(String processName) { public static PacketB4ServerProcessQuery startQuery(String processName) {
PacketServerProcessQuery q = new PacketServerProcessQuery(); PacketB4ServerProcessQuery q = new PacketB4ServerProcessQuery();
q.processName = processName; q.processName = processName;
q.type = QueryType.START; q.type = QueryType.START;
return q; return q;
} }
public static PacketServerProcessQuery destroyQuery(String processName, boolean wait) { public static PacketB4ServerProcessQuery destroyQuery(String processName, boolean wait) {
PacketServerProcessQuery q = new PacketServerProcessQuery(); PacketB4ServerProcessQuery q = new PacketB4ServerProcessQuery();
q.processName = processName; q.processName = processName;
q.type = QueryType.DESTROY; q.type = QueryType.DESTROY;
q.queryData = new byte[] {(byte)(wait ? 1 : 0)}; q.queryData = new byte[] {(byte)(wait ? 1 : 0)};
return q; return q;
} }
public static PacketServerProcessQuery isAliveQuery(String processName) { public static PacketB4ServerProcessQuery isAliveQuery(String processName) {
PacketServerProcessQuery q = new PacketServerProcessQuery(); PacketB4ServerProcessQuery q = new PacketB4ServerProcessQuery();
q.processName = processName; q.processName = processName;
q.type = QueryType.IS_ALIVE; q.type = QueryType.IS_ALIVE;
return q; return q;
} }
public static PacketServerProcessQuery exitStatusQuery(String processName) { public static PacketB4ServerProcessQuery exitStatusQuery(String processName) {
PacketServerProcessQuery q = new PacketServerProcessQuery(); PacketB4ServerProcessQuery q = new PacketB4ServerProcessQuery();
q.processName = processName; q.processName = processName;
q.type = QueryType.EXIT_STATUS; q.type = QueryType.EXIT_STATUS;
return q; return q;

View File

@ -3,12 +3,12 @@ package fr.pandacube.java.util.network.packet.packets.global;
import fr.pandacube.java.util.network.packet.PacketClient; import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketClientAuthenticate extends PacketClient { public class Packet50ClientAuthenticate extends PacketClient {
private String password; private String password;
private String additionalData = ""; private String additionalData = "";
public PacketClientAuthenticate() { public Packet50ClientAuthenticate() {
super((byte)0x50); super((byte)0x50);
} }

View File

@ -3,7 +3,7 @@ package fr.pandacube.java.util.network.packet.packets.global;
import fr.pandacube.java.util.network.packet.PacketClient; import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketClientLogRecord extends PacketClient { public class Packet51ClientLogRecord extends PacketClient {
private long time; private long time;
private String level; private String level;
@ -11,7 +11,7 @@ public class PacketClientLogRecord extends PacketClient {
private String message; private String message;
private String throwable; private String throwable;
public PacketClientLogRecord() { public Packet51ClientLogRecord() {
super((byte)0x51); super((byte)0x51);
} }

View File

@ -6,11 +6,11 @@ import java.io.StringWriter;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerException extends PacketServer { public class PacketD0ServerException extends PacketServer {
private String exception; private String exception;
public PacketServerException() { public PacketD0ServerException() {
super((byte)0xD0); super((byte)0xD0);
} }

View File

@ -3,9 +3,9 @@ package fr.pandacube.java.util.network.packet.packets.global;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerCantAuthenticate extends PacketServer { public class PacketD1ServerCantAuthenticate extends PacketServer {
public PacketServerCantAuthenticate() { public PacketD1ServerCantAuthenticate() {
super((byte)0xD1); super((byte)0xD1);
} }

View File

@ -3,13 +3,13 @@ package fr.pandacube.java.util.network.packet.packets.global;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerCommand extends PacketServer { public class PacketD2ServerCommand extends PacketServer {
private String command; private String command;
private boolean async; private boolean async;
private boolean returnResult; private boolean returnResult;
public PacketServerCommand() { public PacketD2ServerCommand() {
super((byte)0xD2); super((byte)0xD2);
} }

View File

@ -3,12 +3,12 @@ package fr.pandacube.java.util.network.packet.packets.web;
import fr.pandacube.java.util.network.packet.PacketClient; import fr.pandacube.java.util.network.packet.PacketClient;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketClientWebRequest extends PacketClient { public class Packet00ClientWebRequest extends PacketClient {
private String password; private String password;
private String jsonData; private String jsonData;
public PacketClientWebRequest() { public Packet00ClientWebRequest() {
super((byte)0x00); super((byte)0x00);
} }

View File

@ -3,11 +3,11 @@ package fr.pandacube.java.util.network.packet.packets.web;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
public class PacketServerWebResponse extends PacketServer { public class Packet80ServerWebResponse extends PacketServer {
private String jsonData; private String jsonData;
public PacketServerWebResponse() { public Packet80ServerWebResponse() {
super((byte)0x80); super((byte)0x80);
} }

View File

@ -32,7 +32,7 @@ import fr.pandacube.java.util.network.packet.PacketException;
import fr.pandacube.java.util.network.packet.PacketServer; import fr.pandacube.java.util.network.packet.PacketServer;
import fr.pandacube.java.util.network.packet.ResponseCallback; import fr.pandacube.java.util.network.packet.ResponseCallback;
import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer;
import fr.pandacube.java.util.network.packet.packets.global.PacketServerException; import fr.pandacube.java.util.network.packet.packets.global.PacketD0ServerException;
import org.javatuples.Pair; import org.javatuples.Pair;
/** /**
@ -158,7 +158,7 @@ public class TCPServer extends Thread implements Closeable {
} catch (Exception e) { } catch (Exception e) {
Log.severe("Exception while handling packet. This exception will be sent to the client with PacketServerException packet.", e); Log.severe("Exception while handling packet. This exception will be sent to the client with PacketServerException packet.", e);
PacketServerException packet = new PacketServerException(); PacketD0ServerException packet = new PacketD0ServerException();
packet.setException(e); packet.setException(e);
send(packet); send(packet);
} }