Ajout de packet réseau pour PandacubeCore

This commit is contained in:
Marc Baloup 2016-09-09 10:45:52 +02:00
parent dd530bfea9
commit 58e48bf27a
13 changed files with 394 additions and 3 deletions

View File

@ -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)
}

View File

@ -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);
}
}

View File

@ -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
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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; }
}

View File

@ -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) {
}
}

View File

@ -11,7 +11,7 @@ public class PacketServerException extends PacketServer {
private String exception;
public PacketServerException() {
super((byte)0xE0);
super((byte)0xD0);
}
@Override