From 58e48bf27a17291d571446b7ef3ab4ba6377b611 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 9 Sep 2016 10:45:52 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20packet=20r=C3=A9seau=20pour=20Pand?= =?UTF-8?q?acubeCore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fr/pandacube/java/Pandacube.java | 2 +- .../java/util/network/packet/Packet.java | 25 ++++++- .../packets/core_slave/PacketClientClose.java | 22 ++++++ .../PacketClientDeclareProcess.java | 39 ++++++++++ .../PacketClientProcessQueryResponse.java | 61 ++++++++++++++++ .../packets/core_slave/PacketServerClose.java | 22 ++++++ .../PacketServerConnectSuccess.java | 22 ++++++ ...PacketServerProcessDeclarationConfirm.java | 32 ++++++++ .../core_slave/PacketServerProcessInput.java | 42 +++++++++++ .../core_slave/PacketServerProcessQuery.java | 73 +++++++++++++++++++ .../global/PacketClientAuthenticate.java | 35 +++++++++ .../global/PacketServerCantAuthenticate.java | 20 +++++ .../packets/global/PacketServerException.java | 2 +- 13 files changed, 394 insertions(+), 3 deletions(-) create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientClose.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientDeclareProcess.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientProcessQueryResponse.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerClose.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerConnectSuccess.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessDeclarationConfirm.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessInput.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessQuery.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/global/PacketClientAuthenticate.java create mode 100644 src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCantAuthenticate.java diff --git a/src/fr/pandacube/java/Pandacube.java b/src/fr/pandacube/java/Pandacube.java index d503eb2..77ef761 100644 --- a/src/fr/pandacube/java/Pandacube.java +++ b/src/fr/pandacube/java/Pandacube.java @@ -8,6 +8,6 @@ public class Pandacube { public static final int NETWORK_TCP_BUFFER_SIZE = 1024 * 1024; - public static final int NETWORK_TIMEOUT = 30 * 1000; // 30 secondes + public static final int NETWORK_TIMEOUT = 0; // no timeout (milli-seconds) } diff --git a/src/fr/pandacube/java/util/network/packet/Packet.java b/src/fr/pandacube/java/util/network/packet/Packet.java index 35f8399..8f9984f 100644 --- a/src/fr/pandacube/java/util/network/packet/Packet.java +++ b/src/fr/pandacube/java/util/network/packet/Packet.java @@ -9,6 +9,16 @@ import fr.pandacube.java.Pandacube; import fr.pandacube.java.util.Log; 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.packets.core_slave.PacketClientClose; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketClientDeclareProcess; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketClientProcessQueryResponse; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerClose; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerConnectSuccess; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessDeclarationConfirm; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessInput; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessQuery; +import fr.pandacube.java.util.network.packet.packets.global.PacketClientAuthenticate; +import fr.pandacube.java.util.network.packet.packets.global.PacketServerCantAuthenticate; import fr.pandacube.java.util.network.packet.packets.global.PacketServerException; import fr.pandacube.java.util.network.packet.packets.web.PacketClientWebRequest; import fr.pandacube.java.util.network.packet.packets.web.PacketServerWebResponse; @@ -85,9 +95,22 @@ public abstract class Packet implements ByteSerializable { /* * Ajout des types de packets (client + serveur) */ + addPacket(PacketClientClose.class); + addPacket(PacketClientDeclareProcess.class); + addPacket(PacketClientProcessQueryResponse.class); + addPacket(PacketServerClose.class); + addPacket(PacketServerConnectSuccess.class); + addPacket(PacketServerProcessDeclarationConfirm.class); + addPacket(PacketServerProcessInput.class); + addPacket(PacketServerProcessQuery.class); + + addPacket(PacketClientAuthenticate.class); + addPacket(PacketServerCantAuthenticate.class); + addPacket(PacketServerException.class); + addPacket(PacketClientWebRequest.class); addPacket(PacketServerWebResponse.class); - addPacket(PacketServerException.class); + } } diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientClose.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientClose.java new file mode 100644 index 0000000..d424a97 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientClose.java @@ -0,0 +1,22 @@ +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.bytebuffer.ByteBuffer; + +public class PacketClientClose extends PacketClient { + + public PacketClientClose() { + super((byte)0x31); + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + // no data + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + // no data + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientDeclareProcess.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientDeclareProcess.java new file mode 100644 index 0000000..aba01d4 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientDeclareProcess.java @@ -0,0 +1,39 @@ +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.bytebuffer.ByteBuffer; + +public class PacketClientDeclareProcess extends PacketClient { + + private String processName; + private String type; + + public PacketClientDeclareProcess() { + super((byte)0x30); + } + + public void setProcessName(String pN) { + processName = pN; + } + + public void setType(String t) { + type = t; + } + + public String getProcessName() { return processName; } + public String getType() { return type; } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putString(processName); + buffer.putString(type); + + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + processName = buffer.getString(); + type = buffer.getString(); + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientProcessQueryResponse.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientProcessQueryResponse.java new file mode 100644 index 0000000..e2f90f5 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketClientProcessQueryResponse.java @@ -0,0 +1,61 @@ +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.bytebuffer.ByteBuffer; +import fr.pandacube.java.util.network.packet.packets.core_slave.PacketServerProcessQuery.QueryType; + +public class PacketClientProcessQueryResponse extends PacketClient { + + private QueryType type; + private int queryId; + private byte[] responseData = null; + + + public PacketClientProcessQueryResponse() { + super((byte)0x32); + } + + public QueryType getType() { return type; } + public int getQueryId() { return queryId; } + public byte[] getResponseData() { return responseData; } + + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putInt(type.ordinal()); + buffer.putInt(queryId); + buffer.putSizedByteArray(responseData); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + type = QueryType.values()[buffer.getInt()]; + queryId = buffer.getInt(); + responseData = buffer.getSizedByteArray(); + } + + + public static PacketClientProcessQueryResponse destroyResponse(int queryId) { + PacketClientProcessQueryResponse q = new PacketClientProcessQueryResponse(); + q.type = QueryType.DESTROY; + q.queryId = queryId; + return q; + } + + public static PacketClientProcessQueryResponse isAliveResponse(int queryId, boolean resp) { + PacketClientProcessQueryResponse q = new PacketClientProcessQueryResponse(); + q.type = QueryType.IS_ALIVE; + q.queryId = queryId; + q.responseData = new byte[] {(byte)(resp ? 1 : 0)}; + return q; + } + + public static PacketClientProcessQueryResponse exitStatusResponse(int queryId, int resp) { + PacketClientProcessQueryResponse q = new PacketClientProcessQueryResponse(); + q.type = QueryType.EXIT_STATUS; + q.queryId = queryId; + q.responseData = new ByteBuffer(4, CHARSET).putInt(resp).array(); + return q; + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerClose.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerClose.java new file mode 100644 index 0000000..d0814d4 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerClose.java @@ -0,0 +1,22 @@ +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.bytebuffer.ByteBuffer; + +public class PacketServerClose extends PacketServer { + + public PacketServerClose() { + super((byte)0xB0); + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + // no data + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + // no data + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerConnectSuccess.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerConnectSuccess.java new file mode 100644 index 0000000..f107f27 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerConnectSuccess.java @@ -0,0 +1,22 @@ +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.bytebuffer.ByteBuffer; + +public class PacketServerConnectSuccess extends PacketServer { + + public PacketServerConnectSuccess() { + super((byte)0xB2); + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + // no data + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + // no data + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessDeclarationConfirm.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessDeclarationConfirm.java new file mode 100644 index 0000000..03741c6 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessDeclarationConfirm.java @@ -0,0 +1,32 @@ +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.bytebuffer.ByteBuffer; + +public class PacketServerProcessDeclarationConfirm extends PacketServer { + + private String serverName; + + public PacketServerProcessDeclarationConfirm() { + super((byte)0xB1); + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putString(serverName); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + serverName = buffer.getString(); + } + + public String getServerName() { + return serverName; + } + + public void setProcessName(String name) { + serverName = name; + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessInput.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessInput.java new file mode 100644 index 0000000..dec06d6 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessInput.java @@ -0,0 +1,42 @@ +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.bytebuffer.ByteBuffer; + +public class PacketServerProcessInput extends PacketServer { + + private String serverName; + private byte[] dataToSend; + + public PacketServerProcessInput() { + super((byte)0xB3); + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putString(serverName); + buffer.putSizedByteArray(dataToSend); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + serverName = buffer.getString(); + dataToSend = buffer.getSizedByteArray(); + } + + public String getServerName() { + return serverName; + } + + public void setServerName(String serverName) { + this.serverName = serverName; + } + + public byte[] getDataToSend() { + return dataToSend; + } + + public void setDataToSend(byte[] dataToSend) { + this.dataToSend = dataToSend; + } +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessQuery.java b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessQuery.java new file mode 100644 index 0000000..2fbfac0 --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/core_slave/PacketServerProcessQuery.java @@ -0,0 +1,73 @@ +package fr.pandacube.java.util.network.packet.packets.core_slave; + +import fr.pandacube.java.util.RandomUtil; +import fr.pandacube.java.util.network.packet.PacketServer; +import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; + +public class PacketServerProcessQuery extends PacketServer { + + private String processName; + private QueryType type; + private int queryId = RandomUtil.rand.nextInt(); + private byte[] queryData = null; + + + public PacketServerProcessQuery() { + super((byte)0xB4); + } + + public String getProcessName() { return processName; } + public QueryType getType() { return type; } + public int getQueryId() { return queryId; } + public byte[] getQueryData() { return queryData; } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putInt(type.ordinal()); + buffer.putInt(queryId); + buffer.putSizedByteArray(queryData); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + type = QueryType.values()[buffer.getInt()]; + queryId = buffer.getInt(); + queryData = buffer.getSizedByteArray(); + } + + + public static PacketServerProcessQuery startQuery(String processName) { + PacketServerProcessQuery q = new PacketServerProcessQuery(); + q.processName = processName; + q.type = QueryType.START; + return q; + } + + public static PacketServerProcessQuery destroyQuery(String processName, boolean wait) { + PacketServerProcessQuery q = new PacketServerProcessQuery(); + q.processName = processName; + q.type = QueryType.DESTROY; + q.queryData = new byte[] {(byte)(wait ? 1 : 0)}; + return q; + } + + public static PacketServerProcessQuery isAliveQuery(String processName) { + PacketServerProcessQuery q = new PacketServerProcessQuery(); + q.processName = processName; + q.type = QueryType.IS_ALIVE; + return q; + } + + public static PacketServerProcessQuery exitStatusQuery(String processName) { + PacketServerProcessQuery q = new PacketServerProcessQuery(); + q.processName = processName; + q.type = QueryType.EXIT_STATUS; + return q; + } + + + public enum QueryType { + START, DESTROY, IS_ALIVE, EXIT_STATUS; + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/global/PacketClientAuthenticate.java b/src/fr/pandacube/java/util/network/packet/packets/global/PacketClientAuthenticate.java new file mode 100644 index 0000000..55f82ed --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/global/PacketClientAuthenticate.java @@ -0,0 +1,35 @@ +package fr.pandacube.java.util.network.packet.packets.global; + +import fr.pandacube.java.util.network.packet.PacketClient; +import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; + +public class PacketClientAuthenticate extends PacketClient { + + private String password; + private String additionalData = ""; + + public PacketClientAuthenticate() { + super((byte)0x50); + } + + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + buffer.putString(password); + buffer.putString(additionalData); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + password = buffer.getString(); + additionalData = buffer.getString(); + } + + + public String getPassword() { return password; } + public void setPassword(String p) { password = p; } + + public String getAdditionalData() { return additionalData; } + public void setAdditionalData(String data) { additionalData = data; } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCantAuthenticate.java b/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCantAuthenticate.java new file mode 100644 index 0000000..8a471ff --- /dev/null +++ b/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerCantAuthenticate.java @@ -0,0 +1,20 @@ +package fr.pandacube.java.util.network.packet.packets.global; + +import fr.pandacube.java.util.network.packet.PacketServer; +import fr.pandacube.java.util.network.packet.bytebuffer.ByteBuffer; + +public class PacketServerCantAuthenticate extends PacketServer { + + public PacketServerCantAuthenticate() { + super((byte)0xD1); + } + + @Override + public void deserializeFromByteBuffer(ByteBuffer buffer) { + } + + @Override + public void serializeToByteBuffer(ByteBuffer buffer) { + } + +} diff --git a/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerException.java b/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerException.java index 279d13a..4c7b802 100644 --- a/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerException.java +++ b/src/fr/pandacube/java/util/network/packet/packets/global/PacketServerException.java @@ -11,7 +11,7 @@ public class PacketServerException extends PacketServer { private String exception; public PacketServerException() { - super((byte)0xE0); + super((byte)0xD0); } @Override