|
|
@ -28,6 +28,7 @@ import protocolP2P.SizeResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.Socket;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
|
|
/** Representation of packet.
|
|
|
|
/** Representation of packet.
|
|
|
|
* @author Louis Royer
|
|
|
|
* @author Louis Royer
|
|
|
@ -113,20 +114,15 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
|
|
|
|
throw new InternalError();
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Socket ss = (Socket)socket;
|
|
|
|
Socket ss = (Socket)socket;
|
|
|
|
byte[] packet = new byte[1024];
|
|
|
|
byte[] packetHeader = new byte[Payload.PAYLOAD_START_POSITION];
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (-1 == ss.getInputStream().read(packet)) {
|
|
|
|
if (-1 == ss.getInputStream().read(packetHeader, 0, Payload.PAYLOAD_START_POSITION)) {
|
|
|
|
throw new IOException();
|
|
|
|
throw new IOException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
int payloadSize = Payload.getPayloadSize(packetHeader);
|
|
|
|
// Error: cannot read request, closing socket
|
|
|
|
byte[] packet = Arrays.copyOf(packetHeader, Payload.PAYLOAD_START_POSITION + payloadSize);
|
|
|
|
try {
|
|
|
|
if (-1 == ss.getInputStream().read(packet, Payload.PAYLOAD_START_POSITION, payloadSize)) {
|
|
|
|
ss.close();
|
|
|
|
throw new IOException();
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
|
|
|
System.err.println("Cannot close socket");
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// contruction
|
|
|
|
// contruction
|
|
|
|
boolean protocolError = false;
|
|
|
|
boolean protocolError = false;
|
|
|
@ -175,6 +171,16 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
|
|
|
|
if (protocolError) {
|
|
|
|
if (protocolError) {
|
|
|
|
throw new ProtocolError();
|
|
|
|
throw new ProtocolError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
// Error: cannot read request, closing socket
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
ss.close();
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
|
|
|
System.err.println("Cannot close socket");
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Send a Response to a Request (typically used from server).
|
|
|
|
/** Send a Response to a Request (typically used from server).
|
|
|
@ -221,20 +227,15 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
|
|
|
|
throw new InternalError();
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// reception
|
|
|
|
// reception
|
|
|
|
byte[] packet = new byte[8192];
|
|
|
|
byte[] packetHeader = new byte[Payload.PAYLOAD_START_POSITION];
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (-1== requestSocket.getInputStream().read(packet)) {
|
|
|
|
if (-1 == requestSocket.getInputStream().read(packetHeader, 0, Payload.PAYLOAD_START_POSITION)) {
|
|
|
|
throw new IOException();
|
|
|
|
throw new IOException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
int payloadSize = Payload.getPayloadSize(packetHeader);
|
|
|
|
// Error: cannot read request, closing socket
|
|
|
|
byte[] packet = Arrays.copyOf(packetHeader, Payload.PAYLOAD_START_POSITION + payloadSize);
|
|
|
|
try {
|
|
|
|
if (-1 == requestSocket.getInputStream().read(packet, Payload.PAYLOAD_START_POSITION, payloadSize)) {
|
|
|
|
requestSocket.close();
|
|
|
|
throw new IOException();
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
|
|
|
System.err.println("Cannot close socket");
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// contruction
|
|
|
|
// contruction
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -276,6 +277,16 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
|
|
|
|
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
|
|
|
|
(new ProtocolP2PPacketTCP<Payload>(new Payload(RequestResponseCode.INTERNAL_ERROR))).send(requestSocket);
|
|
|
|
throw e;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
// Error: cannot read request, closing socket
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
requestSocket.close();
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
|
|
|
System.err.println("Cannot close socket");
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Private constructor with packet as byte[] parameter (typically used when receiving Packet response).
|
|
|
|
/** Private constructor with packet as byte[] parameter (typically used when receiving Packet response).
|
|
|
|