|
|
@ -4,6 +4,7 @@ import exception.ProtocolError;
|
|
|
|
import exception.SizeError;
|
|
|
|
import exception.SizeError;
|
|
|
|
import exception.TransmissionError;
|
|
|
|
import exception.TransmissionError;
|
|
|
|
import exception.VersionError;
|
|
|
|
import exception.VersionError;
|
|
|
|
|
|
|
|
import exception.SocketClosed;
|
|
|
|
import remoteException.EmptyDirectory;
|
|
|
|
import remoteException.EmptyDirectory;
|
|
|
|
import remoteException.InternalRemoteError;
|
|
|
|
import remoteException.InternalRemoteError;
|
|
|
|
import remoteException.NotFound;
|
|
|
|
import remoteException.NotFound;
|
|
|
@ -44,9 +45,10 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|
|
|
/** Send a Packet. Socket must be set and connected.
|
|
|
|
/** Send a Packet. Socket must be set and connected.
|
|
|
|
* @param socket Socket used to send Packet.
|
|
|
|
* @param socket Socket used to send Packet.
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws InternalError
|
|
|
|
|
|
|
|
* @throws SocketClosed
|
|
|
|
* @throws IOException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
protected void send(Socket socket) throws InternalError, IOException {
|
|
|
|
protected void send(Socket socket) throws InternalError, SocketClosed, IOException {
|
|
|
|
assert socket != null : "Trying to send a Packet but no socket defined";
|
|
|
|
assert socket != null : "Trying to send a Packet but no socket defined";
|
|
|
|
assert socket.isConnected() : "Trying to send a Packet but socket not connected";
|
|
|
|
assert socket.isConnected() : "Trying to send a Packet but socket not connected";
|
|
|
|
if (socket == null || (!socket.isConnected())) {
|
|
|
|
if (socket == null || (!socket.isConnected())) {
|
|
|
@ -55,17 +57,30 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|
|
|
// generate Packet
|
|
|
|
// generate Packet
|
|
|
|
byte[] packet = toPacket();
|
|
|
|
byte[] packet = toPacket();
|
|
|
|
// send it
|
|
|
|
// send it
|
|
|
|
|
|
|
|
try {
|
|
|
|
OutputStream outputStream = socket.getOutputStream();
|
|
|
|
OutputStream outputStream = socket.getOutputStream();
|
|
|
|
outputStream.write(packet);
|
|
|
|
outputStream.write(packet);
|
|
|
|
outputStream.flush();
|
|
|
|
outputStream.flush();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
// closing socket
|
|
|
|
|
|
|
|
System.err.println("Error: cannot send response, closing socket");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
socket.close();
|
|
|
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
|
|
|
System.err.println("Cannot close socket");
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Send a Request throught socket. Socket must be connected (typically used from client).
|
|
|
|
/** Send a Request throught socket. Socket must be connected (typically used from client).
|
|
|
|
* @param socket Socket. Must be connected.
|
|
|
|
* @param socket Socket. Must be connected.
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws InternalError
|
|
|
|
|
|
|
|
* @throws SocketClosed
|
|
|
|
* @throws IOException
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void sendRequest(Object socket) throws InternalError, IOException {
|
|
|
|
public void sendRequest(Object socket) throws InternalError, IOException, SocketClosed {
|
|
|
|
assert socket instanceof Socket: "Wrong socket type";
|
|
|
|
assert socket instanceof Socket: "Wrong socket type";
|
|
|
|
if (socket instanceof Socket) {
|
|
|
|
if (socket instanceof Socket) {
|
|
|
|
requestSocket = (Socket)socket;
|
|
|
|
requestSocket = (Socket)socket;
|
|
|
@ -82,10 +97,11 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|
|
|
* @throws VersionError
|
|
|
|
* @throws VersionError
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws SizeError
|
|
|
|
* @throws SizeError
|
|
|
|
|
|
|
|
* @throws SocketClosed
|
|
|
|
* @throws IOException
|
|
|
|
* @throws IOException
|
|
|
|
* @return ProtocolP2PPacket received.
|
|
|
|
* @return ProtocolP2PPacket received.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public ProtocolP2PPacketTCP(Object socket) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException {
|
|
|
|
public ProtocolP2PPacketTCP(Object socket) throws TransmissionError, ProtocolError, VersionError, InternalError, SizeError, SocketClosed, IOException {
|
|
|
|
super(socket);
|
|
|
|
super(socket);
|
|
|
|
assert socket instanceof Socket : "Wrong socket type";
|
|
|
|
assert socket instanceof Socket : "Wrong socket type";
|
|
|
|
if (!(socket instanceof Socket)) {
|
|
|
|
if (!(socket instanceof Socket)) {
|
|
|
@ -93,7 +109,18 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Socket ss = (Socket)socket;
|
|
|
|
Socket ss = (Socket)socket;
|
|
|
|
byte[] packet = new byte[1024];
|
|
|
|
byte[] packet = new byte[1024];
|
|
|
|
ss.getInputStream().read(packet);
|
|
|
|
try {
|
|
|
|
|
|
|
|
System.err.println("Reading " + ss.getInputStream().read(packet) + " bytes");
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
System.err.println("Error: cannot read request, closing socket");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
ss.close();
|
|
|
|
|
|
|
|
} catch (IOException e2) {
|
|
|
|
|
|
|
|
System.err.println("Cannot close socket");
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
throw new SocketClosed();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// contruction
|
|
|
|
// contruction
|
|
|
|
boolean protocolError = false;
|
|
|
|
boolean protocolError = false;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -141,8 +168,9 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|
|
|
* @param response Packet to send as a response.
|
|
|
|
* @param response Packet to send as a response.
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws IOException
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
* @throws SocketClosed
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException {
|
|
|
|
public void sendResponse(ProtocolP2PPacket response) throws InternalError, IOException, SocketClosed {
|
|
|
|
assert response instanceof ProtocolP2PPacketTCP: "Wrong Packet type";
|
|
|
|
assert response instanceof ProtocolP2PPacketTCP: "Wrong Packet type";
|
|
|
|
if (response instanceof ProtocolP2PPacketTCP) {
|
|
|
|
if (response instanceof ProtocolP2PPacketTCP) {
|
|
|
|
ProtocolP2PPacketTCP r = (ProtocolP2PPacketTCP) response;
|
|
|
|
ProtocolP2PPacketTCP r = (ProtocolP2PPacketTCP) response;
|
|
|
@ -170,8 +198,9 @@ public class ProtocolP2PPacketTCP extends ProtocolP2PPacket {
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws InternalError
|
|
|
|
* @throws SizeError
|
|
|
|
* @throws SizeError
|
|
|
|
* @throws IOException
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
* @throws SocketClosed
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException {
|
|
|
|
public ProtocolP2PPacket receiveResponse() throws EmptyFile, NotFound, EmptyDirectory, InternalRemoteError, VersionRemoteError, ProtocolRemoteError, TransmissionError, ProtocolError, VersionError, InternalError, SizeError, IOException, SocketClosed {
|
|
|
|
assert requestSocket != null : "Cannot receive response because request packet not sent.";
|
|
|
|
assert requestSocket != null : "Cannot receive response because request packet not sent.";
|
|
|
|
if (requestSocket == null) {
|
|
|
|
if (requestSocket == null) {
|
|
|
|
throw new InternalError();
|
|
|
|
throw new InternalError();
|
|
|
|