diff options
author | Haidong Ji | 2019-01-23 16:14:16 -0600 |
---|---|---|
committer | Haidong Ji | 2019-01-23 16:14:16 -0600 |
commit | 29bb48a8cc8122871d8728be6fd8b853e52568bd (patch) | |
tree | 8dbd314ce0526b55a2b19243253a69a5d674b8c6 /src | |
parent | ca7416db5fb06ab354903ebd57e94cf87797becd (diff) |
Interim checkin. Network packet processing not done yet.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/NetworkPacket.java | 83 | ||||
-rw-r--r-- | src/test/NetworkPacketTest.java | 59 |
2 files changed, 142 insertions, 0 deletions
diff --git a/src/main/NetworkPacket.java b/src/main/NetworkPacket.java new file mode 100644 index 0000000..805dcf5 --- /dev/null +++ b/src/main/NetworkPacket.java @@ -0,0 +1,83 @@ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + +public class NetworkPacket { + + public static class Request { + public int arrivalTime; + public int processTime; + + public Request(int arrivalTime, int processTime) { + this.arrivalTime = arrivalTime; + this.processTime = processTime; + } + + } + + public static class Response { + public boolean dropped; + public int startTime; + + public Response(boolean dropped, int startTime) { + this.dropped = dropped; + this.startTime = startTime; + } + } + + public static class Buffer { + private int size; + private ArrayList<Integer> finishTime = new ArrayList<Integer>(); + + public Buffer(int size) { + this.size = size; + } + public Response processRequests(Request request) { + // write your code here + if (this.size == 0) + return new Response(false,-1); + else { + if (finishTime.size() == 0) + } + } + } + private static ArrayList<Request> ReadQueries(Scanner scanner) throws IOException { + int requests_count = scanner.nextInt(); + ArrayList<Request> requests = new ArrayList<Request>(); + for (int i = 0; i < requests_count; ++i) { + int arrival_time = scanner.nextInt(); + int process_time = scanner.nextInt(); + requests.add(new Request(arrival_time, process_time)); + } + return requests; + } + + public static ArrayList<Response> processRequests(ArrayList<Request> requests, Buffer buf) { + ArrayList<Response> responses = new ArrayList<>(); + for (int i = 0; i < requests.size(); i++) { + responses.add(buf.processRequests(requests.get(i))); + } + return responses; + } + private static void PrintResponses(ArrayList<Response> responses) { + for (int i = 0; i < responses.size(); ++i) { + Response response = responses.get(i); + if (response.dropped) { + System.out.println(-1); + } else { + System.out.println(response.startTime); + } + } + } + + public static void main(String[] args) throws IOException { + Scanner scanner = new Scanner(System.in); + + int buffer_max_size = scanner.nextInt(); + Buffer buffer = new Buffer(buffer_max_size); + + ArrayList<Request> requests = ReadQueries(scanner); + ArrayList<Response> responses = processRequests(requests, buffer); + PrintResponses(responses); + } +} diff --git a/src/test/NetworkPacketTest.java b/src/test/NetworkPacketTest.java new file mode 100644 index 0000000..fca3d60 --- /dev/null +++ b/src/test/NetworkPacketTest.java @@ -0,0 +1,59 @@ +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class NetworkPacketTest { + @Test + void test() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList<NetworkPacket.Request> requests = new ArrayList<>(); + ArrayList<NetworkPacket.Response> responses = NetworkPacket.processRequests(requests, buf); + assertEquals(0, responses.size()); + } + + @Test + void test1() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList<NetworkPacket.Request> requests = new ArrayList<>(); + requests.add(new NetworkPacket.Request(0, 1)); + ArrayList<NetworkPacket.Response> responses = NetworkPacket.processRequests(requests, buf); + assertEquals(1, responses.size()); + assertFalse(responses.get(0).dropped); + assertEquals(0, responses.get(0).startTime); + } + + @Test + void test2() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList<NetworkPacket.Request> requests = new ArrayList<>(); + requests.add(new NetworkPacket.Request(0, 1)); + requests.add(new NetworkPacket.Request(0, 1)); + ArrayList<NetworkPacket.Response> responses = NetworkPacket.processRequests(requests, buf); + assertEquals(2, responses.size()); + assertFalse(responses.get(0).dropped); + assertEquals(0, responses.get(0).startTime); + assertTrue(responses.get(1).dropped); + } + + @Test + void test3() { + int bufferMaxSize = 1; + NetworkPacket.Buffer buf = new NetworkPacket.Buffer(bufferMaxSize); + ArrayList<NetworkPacket.Request> requests = new ArrayList<>(); + requests.add(new NetworkPacket.Request(0, 1)); + requests.add(new NetworkPacket.Request(1, 1)); + ArrayList<NetworkPacket.Response> responses = NetworkPacket.processRequests(requests, buf); + assertEquals(2, responses.size()); + assertFalse(responses.get(0).dropped); + assertEquals(0, responses.get(0).startTime); + assertFalse(responses.get(1).dropped); + assertEquals(1, responses.get(1).startTime); + } +} |