Advertisement
fensa08

#APS Lab 2/6

Dec 1st, 2019
2,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. Статичко рутирање Problem 2 (3 / 7)
  2.  
  3. Потребно е да се симулира рутирање преку хеш табела. Секој рутер претставува една кофичка од хеш табелата и притоа пакетите на влез ги прима преку еден интерфејс. Бидејќи рутерот, рутирањето на даден пакет го врши користејќи ги статичките рути што тој ги знае, кога ќе добие пакет преку влезниот интерфејс, тој треба да даде одговор дали може да го рутира пакетот до дадениот уред во таа мрежа (postoi или nepostoi). Важно е тоа што сите адреси имаат мрежна маска /24, што значи дека последните 8 бита се наменети за адресирање. Претпоставуваме дека сите адреси се зафатени во таа мрежа, така што до било кој уред од таа мрежа, доколку ја има во рутирачката табела, може да се достави пакетот. Така што доколку во рутирачката табела има 10.10.10.0, тоа значи дека рутерот може да го проследи пакетот до сите уреди во таа мрежа (10.10.10.1- 10.10.10.254).
  4.  
  5. На влез најпрвин се внесува бројот на рутери, потоа најизменично IP адресата на влезниот интерфејс, па во следниот ред IP адресите на мрежите до кој рутерот има статички рути. Потоа се внесува бројот на обиди за рутирање на пакети. Во следните редови најизменично се внесува влезен интерфејс и IP адреса на уред за која треба да се даде одговор дали тој рутер ја познава или не. Име на класта :RoutingHashJava
  6.  
  7.  
  8.  
  9. ====================================================================================================================================
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.util.HashMap;
  15. import java.util.LinkedList;
  16.  
  17.  
  18. class CBHT<K extends Comparable<K>, E> {
  19.  
  20.     // An object of class CBHT is a closed-bucket hash table, containing
  21.     // entries of class MapEntry.
  22.     private SLLNode<MapEntry<K,E>>[] buckets;
  23.  
  24.     @SuppressWarnings("unchecked")
  25.     public CBHT(int m) {
  26.         // Construct an empty CBHT with m buckets.
  27.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  28.     }
  29.  
  30.     private int hash(K key) {
  31.         // Translate key to an index of the array buckets.
  32.         return Math.abs(key.hashCode()) % buckets.length;
  33.     }
  34.  
  35.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  36.         // Find which if any node of this CBHT contains an entry whose key is
  37.         // equal
  38.         // to targetKey. Return a link to that node (or null if there is none).
  39.         int b = hash(targetKey);
  40.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  41.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  42.                 return curr;
  43.         }
  44.         return null;
  45.     }
  46.  
  47.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  48.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  49.         int b = hash(key);
  50.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  51.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  52.                 // Make newEntry replace the existing entry ...
  53.                 curr.element = newEntry;
  54.                 return;
  55.             }
  56.         }
  57.         // Insert newEntry at the front of the 1WLL in bucket b ...
  58.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  59.     }
  60.  
  61.     public void delete(K key) {
  62.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  63.         int b = hash(key);
  64.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  65.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  66.                 if (pred == null)
  67.                     buckets[b] = curr.succ;
  68.                 else
  69.                     pred.succ = curr.succ;
  70.                 return;
  71.             }
  72.         }
  73.     }
  74.  
  75.     public String toString() {
  76.         String temp = "";
  77.         for (int i = 0; i < buckets.length; i++) {
  78.             temp += i + ":";
  79.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  80.                 temp += curr.element.toString() + " ";
  81.             }
  82.             temp += "\n";
  83.         }
  84.         return temp;
  85.     }
  86.  
  87. }
  88.  
  89. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  90.  
  91.     // Each MapEntry object is a pair consisting of a key (a Comparable
  92.     // object) and a value (an arbitrary object).
  93.     K key;
  94.     E value;
  95.  
  96.     public MapEntry (K key, E val) {
  97.         this.key = key;
  98.         this.value = val;
  99.     }
  100.  
  101.     public int compareTo (K that) {
  102.         // Compare this map entry to that map entry.
  103.         @SuppressWarnings("unchecked")
  104.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  105.         return this.key.compareTo(other.key);
  106.     }
  107.  
  108.     public String toString () {
  109.         return "<" + key + "," + value + ">";
  110.     }
  111. }
  112.  
  113.  class SLLNode<E> {
  114.     protected E element;
  115.     protected SLLNode<E> succ;
  116.  
  117.     public SLLNode(E elem, SLLNode<E> succ) {
  118.         this.element = elem;
  119.         this.succ = succ;
  120.     }
  121.  
  122.     @Override
  123.     public String toString() {
  124.         return element.toString();
  125.     }
  126. }
  127.  
  128. public class RoutingHashJava {
  129.  
  130.  
  131.  
  132.     public static void main(String []args) throws IOException {
  133.  
  134.  
  135.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  136.         int n = Integer.parseInt(in.readLine());
  137.  
  138.         CBHT<String, String[]> ht = new CBHT<>(n*n);
  139.  
  140.  
  141.         for(int i = 0; i < n; i++){
  142.  
  143.             String interFace = in.readLine();
  144.             String routes = in.readLine();
  145.             String p[] = routes.split("\\,");
  146.  
  147.             int counter = 0;
  148.             String arr[] = new String[p.length];
  149.             for(int j = 0; j < p.length; j++){
  150.                 String a[] = p[j].split("\\.");
  151.                 String address = a[0]+"."+a[1]+"."+a[2];
  152.                 arr[counter] = address;
  153.                 counter++;
  154.             }
  155.             ht.insert(interFace,arr);
  156.         }
  157.  
  158.         n = Integer.parseInt(in.readLine());
  159.         for(int i = 0; i < n; i++){
  160.             String ruter = in.readLine();
  161.             String address = in.readLine();
  162.             String p[] = address.split("\\.");
  163.             String add = p[0]+ "." + p[1] + "." + p[2];
  164.  
  165.  
  166.             boolean postoi = true;
  167.             SLLNode<MapEntry<String, String[]>> bucket =  ht.search(ruter);
  168.  
  169.             if( bucket == null){
  170.                 System.out.println("ne postoi");
  171.                 continue;
  172.             }
  173.            String[] kofa = bucket.element.value;
  174.  
  175.             for(int j = 0; j < kofa.length;j++){
  176.                if(kofa[j].equals(add)){
  177.                    postoi = false;
  178.                    System.out.println("postoi");
  179.                    break;
  180.                }
  181.            }
  182.  
  183.             if(postoi){
  184.                 System.out.println("ne postoi");
  185.             }
  186.         }
  187.  
  188.  
  189.  
  190.     }
  191.  
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement