Filip_Markoski

[ADS] Static Routing

Nov 22nd, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Arrays;
  7. import java.util.Date;
  8. import java.util.Scanner;
  9.  
  10. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  11.  
  12.     // Each MapEntry object is a pair consisting of a key (a Comparable
  13.     // object) and a value (an arbitrary object).
  14.     K key;
  15.     E value;
  16.  
  17.     public MapEntry(K key, E val) {
  18.         this.key = key;
  19.         this.value = val;
  20.     }
  21.  
  22.     public int compareTo(K that) {
  23.         // Compare this map entry to that map entry.
  24.         @SuppressWarnings("unchecked")
  25.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  26.         return this.key.compareTo(other.key);
  27.     }
  28.  
  29.     public String toString() {
  30.         return "<" + key + "," + value + ">";
  31.     }
  32. }
  33.  
  34. class SLLNode<E> {
  35.     protected E element;
  36.     protected SLLNode<E> succ;
  37.  
  38.     public SLLNode(E elem, SLLNode<E> succ) {
  39.         this.element = elem;
  40.         this.succ = succ;
  41.     }
  42.  
  43.     @Override
  44.     public String toString() {
  45.         return element.toString();
  46.     }
  47. }
  48.  
  49. class CBHT<K extends Comparable<K>, E> {
  50.  
  51.     // An object of class CBHT is a closed-bucket hash table, containing
  52.     // entries of class MapEntry.
  53.     private SLLNode<MapEntry<K, E>>[] buckets;
  54.  
  55.     @SuppressWarnings("unchecked")
  56.     public CBHT(int m) {
  57.         // Construct an empty CBHT with m buckets.
  58.         buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  59.     }
  60.  
  61.     private int hash(K key) {
  62.         // Translate key to an index of the array buckets.
  63.         return Math.abs(key.hashCode()) % buckets.length;
  64.     }
  65.  
  66.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  67.         // Find which if any node of this CBHT contains an entry whose key is
  68.         // equal
  69.         // to targetKey. Return a link to that node (or null if there is none).
  70.         int b = hash(targetKey);
  71.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  72.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  73.                 return curr;
  74.         }
  75.         return null;
  76.     }
  77.  
  78.     public void insert(K key, E val) {        // Insert the entry <key, val> into this CBHT.
  79.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  80.         int b = hash(key);
  81.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  82.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  83.                 // Make newEntry replace the existing entry ...
  84.                 curr.element = newEntry;
  85.                 return;
  86.             }
  87.         }
  88.         // Insert newEntry at the front of the 1WLL in bucket b ...
  89.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  90.     }
  91.  
  92.     public void delete(K key) {
  93.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  94.         int b = hash(key);
  95.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  96.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  97.                 if (pred == null)
  98.                     buckets[b] = curr.succ;
  99.                 else
  100.                     pred.succ = curr.succ;
  101.                 return;
  102.             }
  103.         }
  104.     }
  105.  
  106.     public String toString() {
  107.         String temp = "";
  108.         for (int i = 0; i < buckets.length; i++) {
  109.             temp += i + ":";
  110.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  111.                 temp += curr.element.toString() + " ";
  112.             }
  113.             temp += "\n";
  114.         }
  115.         return temp;
  116.     }
  117.  
  118.  
  119.     public SLLNode<MapEntry<K, E>> mySearch(K targetKey) {
  120.  
  121.         int b = hash(targetKey);
  122.         System.out.println(String.format("b = %d", b));
  123.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  124.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  125.                 return curr;
  126.         }
  127.         return null;
  128.     }
  129.  
  130. }
  131.  
  132. public class RoutingHashJava {
  133.  
  134.     public static void main(String[] args) throws IOException, ParseException {
  135.         Scanner scanner = new Scanner(System.in);
  136.  
  137.         int numberOfRouters = scanner.nextInt();
  138.  
  139.         CBHT<String, String[]> table = new CBHT<>(2 * numberOfRouters);
  140.  
  141.         for (int i = 1; i <= numberOfRouters; i++) {
  142.             String routerInterface = scanner.next();
  143.             scanner.nextLine(); // Fire off to go to next line
  144.             String routingTable = scanner.nextLine();
  145.             String parts[] = routingTable.split(",");
  146.  
  147.             table.insert(routerInterface, parts);
  148.         }
  149.  
  150.  
  151.         int triesToRoutePackets = scanner.nextInt();
  152.         for (int i = 0; i < triesToRoutePackets; i++) {
  153.  
  154.             String inputInterface = scanner.next();
  155.             String ipAddress = scanner.next();
  156.  
  157.             SLLNode<MapEntry<String, String[]>> searchNode = table.search(inputInterface);
  158.  
  159.             if (searchNode != null) {
  160.                 String subnetMasks[] = searchNode.element.value;
  161.                 boolean valid = false;
  162.                 for (int j = 0; j < subnetMasks.length; j++) {
  163.  
  164.                     String ipAddressParts[] = ipAddress.split("\\.");
  165.                     String subnetParts[] = subnetMasks[j].split("\\.");
  166.  
  167.                     for (int k = 0; k < subnetParts.length - 1; k++) {
  168.  
  169.                         if (ipAddressParts[k].equals(subnetParts[k])) {
  170.                             valid = true;
  171.                         } else {
  172.                             valid = false;
  173.                             break;
  174.                         }
  175.                     }
  176.  
  177.                     if (valid) {
  178.                         System.out.println("postoi"); break;
  179.                     }
  180.                     /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("ddd:ddd:ddd:ddd");
  181.                     Date date = simpleDateFormat.parse(subnetMasks[j]);
  182.                     System.out.println(date);*/
  183.                 }
  184.                  if (!valid){
  185.                     System.out.println("ne postoi");
  186.                 }
  187.             } else {
  188.                 System.out.println("ne postoi");
  189.             }
  190.         }
  191.  
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment