Update Payload and protocolP2P packet to allow ratio

This commit is contained in:
Louis Royer 2020-03-27 17:10:09 +01:00
parent 5fa160d98d
commit d6fbfa6683
3 changed files with 38 additions and 0 deletions

View File

@ -9,6 +9,9 @@ import protocolP2P.DiscoverRequest;
import protocolP2P.DiscoverResponse;
import protocolP2P.Register;
import protocolP2P.Unregister;
import protocolP2P.RatioRequest;
import protocolP2P.RatioResponse;
import protocolP2P.UpdateRatio;
import localException.ProtocolError;
import localException.InternalError;
import localException.TransmissionError;
@ -40,6 +43,9 @@ public class Payload {
assert requestResponseCode != RequestResponseCode.DISCOVER_RESPONSE || (this instanceof DiscoverResponse) : "DISCOVER_RESPONSE must use DiscoverResponse class";
assert requestResponseCode != RequestResponseCode.REGISTER || (this instanceof Register) : "REGISTER must use Register class";
assert requestResponseCode != RequestResponseCode.UNREGISTER || (this instanceof Unregister) : "UNREGISTER must use Unregister class";
assert requestResponseCode != RequestResponseCode.RATIO_REQUEST || (this instanceof RatioRequest) : "RATIO_REQUEST must use RatioRequest class";
assert requestResponseCode != RequestResponseCode.RATIO_RESPONSE || (this instanceof RatioResponse) : "RATIO_RESPONSE must use RatioResponse class";
assert requestResponseCode != RequestResponseCode.UPDATE_RATIO || (this instanceof UpdateRatio) : "UPDATE_RATIO must use UpdateRatio class";
this.requestResponseCode = requestResponseCode;
checkRequestResponseCode(); // this can throw InternalError
}
@ -67,6 +73,9 @@ public class Payload {
assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.DISCOVER_RESPONSE || (this instanceof DiscoverResponse) : "DISCOVER_RESPONSE must use DiscoverResponse class";
assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.REGISTER || (this instanceof Register) : "REGISTER must use Register class";
assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.UNREGISTER || (this instanceof Unregister) : "UNREGISTER must use Unregister class";
assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.RATIO_REQUEST || (this instanceof RatioRequest) : "RATIO_REQUEST must use RatioRequest class";
assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.RATIO_RESPONSE || (this instanceof RatioResponse) : "RATIO_RESPONSE must use RatioResponse class";
assert RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]) != RequestResponseCode.UPDATE_RATIO || (this instanceof UpdateRatio) : "UPDATE_RATIO must use UpdateRatio class";
requestResponseCode = RequestResponseCode.fromCode(packet[RequestResponseCode.RRCODE_POSITION]);
checkRequestResponseCode(); // this can throw InternalError
}
@ -85,6 +94,9 @@ public class Payload {
|| (requestResponseCode == RequestResponseCode.DISCOVER_RESPONSE && !(this instanceof DiscoverResponse))
|| (requestResponseCode == RequestResponseCode.REGISTER && !(this instanceof Register))
|| (requestResponseCode == RequestResponseCode.UNREGISTER && !(this instanceof Unregister))
|| (requestResponseCode == RequestResponseCode.RATIO_REQUEST && !(this instanceof RatioRequest))
|| (requestResponseCode == RequestResponseCode.RATIO_RESPONSE && !(this instanceof RatioResponse))
|| (requestResponseCode == RequestResponseCode.UPDATE_RATIO && !(this instanceof UpdateRatio))
) {
throw new InternalError();
}

View File

@ -18,6 +18,9 @@ import protocolP2P.RequestResponseCode;
import protocolP2P.LoadRequest;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import protocolP2P.RatioRequest;
import protocolP2P.RatioResponse;
import protocolP2P.UpdateRatio;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
@ -141,6 +144,7 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
case HASH_RESPONSE:
case DISCOVER_RESPONSE:
case NOT_A_TRACKER:
case RATIO_RESPONSE:
// we were expecting a request, but we are receiving a response
throw new ProtocolError();
default :
@ -319,6 +323,15 @@ public class ProtocolP2PPacketTCP < T extends Payload > extends ProtocolP2PPacke
case DISCOVER_RESPONSE:
payload = (Payload) new DiscoverResponse(packet);
break;
case RATIO_REQUEST:
payload = (Payload) new RatioRequest(packet);
break;
case RATIO_RESPONSE:
payload = (Payload) new RatioResponse(packet);
break;
case UPDATE_RATIO:
payload = (Payload) new UpdateRatio(packet);
break;
default:
payload = new Payload(packet); // this can throw TransmissionError
break;

View File

@ -19,6 +19,9 @@ import protocolP2P.RequestResponseCode;
import protocolP2P.LoadRequest;
import protocolP2P.FileList;
import protocolP2P.FilePart;
import protocolP2P.RatioRequest;
import protocolP2P.RatioResponse;
import protocolP2P.UpdateRatio;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
@ -134,6 +137,7 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket
case HASH_RESPONSE:
case DISCOVER_RESPONSE:
case NOT_A_TRACKER:
case RATIO_RESPONSE:
// we were expecting a request, but we are receiving a response
throw new ProtocolError();
default :
@ -304,6 +308,15 @@ public class ProtocolP2PPacketUDP < T extends Payload> extends ProtocolP2PPacket
case DISCOVER_RESPONSE:
payload = (Payload) new DiscoverResponse(packet);
break;
case RATIO_REQUEST:
payload = (Payload) new RatioRequest(packet);
break;
case RATIO_RESPONSE:
payload = (Payload) new RatioResponse(packet);
break;
case UPDATE_RATIO:
payload = (Payload) new UpdateRatio(packet);
break;
default:
payload = new Payload(packet); // this can throw TransmissionError
break;