Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write in JAVA! I have 3 classes done so far: Class #1: import java.util.*; import java.util.concurrent.atomic.AtomicInteger; /* * Contain a packet that traverses the

image text in transcribed

Please write in JAVA!

I have 3 classes done so far:

Class #1:

import java.util.*; import java.util.concurrent.atomic.AtomicInteger; /* * Contain a packet that traverses the network */ class Packet { // The final router private int destination; // The origin router private int source; // The path this packet takes private LinkedList path = new LinkedList(); /* * Instantiate a packet, given source and destination */ Packet(int s, int d) { source = s; destination = d; } /* * Return the packet's source. */ int getSource() { return source; } /* * Return the packet's destination. */ int getDestination() { return destination; } /* * Record the current location as the packet traverses the network. */ void Record(int router) { path.add(router); } /* * Print the route the packet took through the network. */ void Print() { System.out.println("Packet source=" + source + " destination=" + destination); System.out.print(" path: "); for (int i = 0; i  

Class #2:

/* * This runnable routes packets as they traverse the network. */ class Router implements Runnable { private LinkedList list = new LinkedList(); private int routes[]; private Router routers[]; private int routerNum; private boolean end = false; Router(int rts[], Router rtrs[], int num) { routes = rts; routers = rtrs; routerNum = num; } /* * Add a packet to this router. * Students should add some comments on how this works. * */ public void addWork(Packet p) { } /* * This is not called from class Router, but from routing.main() to * indicate no more packets will ever be added to the network. */ public synchronized void end() { } /* * This is not called from class Router, but from the routing class * to indicate that the network has no packets in it currently (but * may receive more packets later. */ public synchronized void networkEmpty() { } /* * Process packets. Add some details on how this works. */ public void run() { } } 

Class #3:

/* * Simulate a network of routers, with each router as a thread, passing packets from source to destination. */ public class routing { /* * This is the routing table. It implements the function f(r, c), where r corresponds to the row, and c corresponds to the column. * r is the current router, and c is the destination of the packet. f(r, c) gives the next router for the shortest path route. */ private static int routingTable[][] = { // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 { -1, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13}, // 0 { 2, -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 27, 2, 2, 2, 2}, // 1 { 18, 1, -1, 18, 18, 18, 18, 18, 18, 9, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 1, 18, 18, 18, 18}, // 2 { 10, 10, 10, -1, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, // 3 { 24, 24, 24, 24, -1, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24}, // 4 { 12, 10, 10, 10, 12, -1, 12, 10, 10, 10, 10, 10, 12, 12, 12, 12, 10, 10, 10, 12, 12, 10, 10, 12, 12, 12, 12, 10, 12, 10, 10, 10}, // 5 { 15, 15, 15, 15, 15, 15, -1, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 19, 20, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15}, // 6 { 22, 22, 22, 22, 22, 22, 22, -1, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22}, // 7 { 21, 21, 21, 21, 21, 21, 21, 21, -1, 21, 21, 21, 21, 21, 21, 21, 21, 17, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 17, 21}, // 8 { 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, // 9 { 18, 18, 18, 3, 18, 5, 18, 21, 21, 18, -1, 18, 5, 18, 18, 18, 21, 21, 18, 18, 18, 21, 21, 18, 18, 18, 18, 18, 18, 21, 21, 31}, //10 { 23, 18, 18, 18, 23, 18, 23, 18, 18, 18, 18, -1, 23, 23, 23, 23, 18, 18, 18, 23, 23, 18, 18, 23, 23, 23, 23, 18, 23, 18, 18, 18}, //11 { 23, 5, 5, 5, 23, 5, 23, 5, 5, 5, 5, 23, -1, 23, 23, 23, 5, 5, 5, 23, 23, 5, 5, 23, 23, 23, 23, 5, 23, 5, 5, 5}, //12 { 0, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, -1, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23}, //13 { 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, -1, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23}, //14 { 25, 25, 25, 25, 25, 25, 6, 25, 25, 25, 25, 25, 25, 25, 25, -1, 25, 25, 25, 6, 6, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25}, //15 { 21, 21, 21, 21, 21, 21, 21, 22, 21, 21, 21, 21, 21, 21, 21, 21, -1, 21, 21, 21, 21, 21, 22, 21, 21, 21, 21, 21, 21, 29, 21, 21}, //16 { 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, -1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 30, 8}, //17 { 11, 2, 2, 10, 11, 10, 11, 10, 10, 2, 10, 11, 10, 11, 11, 11, 10, 10, -1, 11, 11, 10, 10, 11, 11, 11, 11, 2, 11, 10, 10, 10}, //18 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, -1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, //19 { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, -1, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}, //20 { 10, 10, 10, 10, 10, 10, 10, 16, 8, 10, 10, 10, 10, 10, 10, 10, 16, 8, 10, 10, 10, -1, 16, 10, 10, 10, 10, 10, 10, 16, 8, 10}, //21 { 16, 16, 16, 16, 16, 16, 16, 7, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, -1, 16, 16, 16, 16, 16, 16, 16, 16, 16}, //22 { 13, 11, 11, 12, 28, 12, 28, 12, 12, 11, 12, 11, 12, 13, 14, 28, 12, 12, 11, 28, 28, 12, 12, -1, 28, 28, 28, 11, 28, 12, 12, 12}, //23 { 26, 26, 26, 26, 4, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, -1, 26, 26, 26, 26, 26, 26, 26}, //24 { 26, 26, 26, 26, 26, 26, 15, 26, 26, 26, 26, 26, 26, 26, 26, 15, 26, 26, 26, 15, 15, 26, 26, 26, 26, -1, 26, 26, 26, 26, 26, 26}, //25 { 28, 28, 28, 28, 24, 28, 25, 28, 28, 28, 28, 28, 28, 28, 28, 25, 28, 28, 28, 25, 25, 28, 28, 28, 24, 25, -1, 28, 28, 28, 28, 28}, //26 { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1}, //27 { 23, 23, 23, 23, 26, 23, 26, 23, 23, 23, 23, 23, 23, 23, 23, 26, 23, 23, 23, 26, 26, 23, 23, 23, 26, 26, 26, 23, -1, 23, 23, 23}, //28 { 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, -1, 16, 16}, //29 { 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, -1, 17}, //30 { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, -1}, //31 }; public static AtomicInteger packetCount = new AtomicInteger(0); // A table of routers, one for each vertex in the network static Router router[] = new Router[routingTable.length]; /* * This can be called by another class to determine the number of packets * in the network. */ static public int getPacketCount() { return packetCount.get(); } static private void incPacketCount() { packetCount.getAndIncrement(); } /* * This can be called by another class to decrement the packet count, and * if there are no packets left, let the network know. * */ static public void decPacketCount() { if ((packetCount.decrementAndGet()) == 0) { for (int i = 0; i  packetList = new LinkedList(); // A reference to a packet, that will be introduced to the network. Packet p; // A random number generator, for generating random packets Random rand = new Random(); /* * Create a router and corresponding thread, and start the thread */ for (int i = 0; i  

50 points Your task is to develop a system that routes packets from source to destination among a series of routers on a network. You are provided code that contains a routing table, and generates packets with a source and a destination. Each router on the network must forward each packet to the next node on the way to its destination, recording that the packet traversed that router. The routing table for each router provides the appropriate next node. Three classes are provided to you. The routing class contains the main method, which performs set up, and generates a number of packets to traverse the network. Each packet generated is an instance of the Packet class. The Packet class holds the source and destination of the packet, as well as a list of the routers the packet has traversed. The Packet class also contains a static variable indicating the number of Packets currently traversing the network. Each router is represented by an instance of Runnable router, and a single corresponding thread. The main method contains the routing table for the entire network. When it is constructed, each router recelves its own portion of the routing table. The routing table for each router is an integer array in which the index is the destination router, and the value at that index is the next router that should receive the packet on the way to its destination Your task is to implement the Router class. This is the class that forwards packets to the next router on the way to their destination. As you can see, Router implements Runnable. The main() method calls the addWorko method in Router, once it has created the Packet. The addWork() method enqueues the work to that particular thread. The end() method indicates to Router that , once there are no more packets in the network, it should return. The run() method (from Runnable) does the following: waits until the queue has some contents, then records the number of the current router in the packet. If the router is not the packet's destination, it forwards that packet to the appropriate next router in the routing table by calling the addWork() method for that instance of Router, with the packet as the argument. Since there is one instance of Router for each router in the network, addWork() must add to the queue that corresponds to the correct router. Under no circumstances should any router process a packet more than once. If this is the destination router for the network, the router should Record itself , but should not forward the packet any further. Router should use the monitors we discussed in class to manage its synchronization This program should use no explicit locks. In addition, it should use none of the Java concurrent data structures. Instead, it should rely on the 'synchronized" keyword in Router to ensure synchronization between threads. Note that data structures such as ArrayList may be used concurrently, in a read-only manner. Be advised that you must be careful not to synchronize any more than necessary; in particular, you should not be synchronized when you call addWork() on another node, as this is likely to result in circular locking. No modifications to any of the code, except the implementation Router are necessary Program Submission Turn your code in using the Program Submission Instructions in the Course Materials. Objectives: - Process all packets, and route them correctly to their destination. - Correctly print packet routes. - Exit correctly and cleanly. - Use no timeouts, sleeps or timers. - Use monitors correctly and properly. Uses wait() and notify() or await() and signal(). - Does not busy-wait. Does not recheck any variable as fast as a processor can allow, or use sleep to delay. - Use no other synchronization - Properly use threads and queues. Improperly laid out code, or code that doesn't meet the description of the assigment will be penalized, in addition to the above categories. 50 points Your task is to develop a system that routes packets from source to destination among a series of routers on a network. You are provided code that contains a routing table, and generates packets with a source and a destination. Each router on the network must forward each packet to the next node on the way to its destination, recording that the packet traversed that router. The routing table for each router provides the appropriate next node. Three classes are provided to you. The routing class contains the main method, which performs set up, and generates a number of packets to traverse the network. Each packet generated is an instance of the Packet class. The Packet class holds the source and destination of the packet, as well as a list of the routers the packet has traversed. The Packet class also contains a static variable indicating the number of Packets currently traversing the network. Each router is represented by an instance of Runnable router, and a single corresponding thread. The main method contains the routing table for the entire network. When it is constructed, each router recelves its own portion of the routing table. The routing table for each router is an integer array in which the index is the destination router, and the value at that index is the next router that should receive the packet on the way to its destination Your task is to implement the Router class. This is the class that forwards packets to the next router on the way to their destination. As you can see, Router implements Runnable. The main() method calls the addWorko method in Router, once it has created the Packet. The addWork() method enqueues the work to that particular thread. The end() method indicates to Router that , once there are no more packets in the network, it should return. The run() method (from Runnable) does the following: waits until the queue has some contents, then records the number of the current router in the packet. If the router is not the packet's destination, it forwards that packet to the appropriate next router in the routing table by calling the addWork() method for that instance of Router, with the packet as the argument. Since there is one instance of Router for each router in the network, addWork() must add to the queue that corresponds to the correct router. Under no circumstances should any router process a packet more than once. If this is the destination router for the network, the router should Record itself , but should not forward the packet any further. Router should use the monitors we discussed in class to manage its synchronization This program should use no explicit locks. In addition, it should use none of the Java concurrent data structures. Instead, it should rely on the 'synchronized" keyword in Router to ensure synchronization between threads. Note that data structures such as ArrayList may be used concurrently, in a read-only manner. Be advised that you must be careful not to synchronize any more than necessary; in particular, you should not be synchronized when you call addWork() on another node, as this is likely to result in circular locking. No modifications to any of the code, except the implementation Router are necessary Program Submission Turn your code in using the Program Submission Instructions in the Course Materials. Objectives: - Process all packets, and route them correctly to their destination. - Correctly print packet routes. - Exit correctly and cleanly. - Use no timeouts, sleeps or timers. - Use monitors correctly and properly. Uses wait() and notify() or await() and signal(). - Does not busy-wait. Does not recheck any variable as fast as a processor can allow, or use sleep to delay. - Use no other synchronization - Properly use threads and queues. Improperly laid out code, or code that doesn't meet the description of the assigment will be penalized, in addition to the above categories

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis In Microsoft Excel

Authors: Alex Holloway

1st Edition

B0CCCPKTTX, 979-8852388452

More Books

Students also viewed these Databases questions