Advertisement
duplicityyy

[АПС] - Статичко рутирање

Dec 17th, 2020
1,543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. Статичко рутирање Problem 2 (0 / 3)
  2. Потребно е да се симулира рутирање преку хеш табела. Секој рутер претставува една кофичка од хеш табелата и притоа пакетите на влез ги прима преку еден интерфејс. Бидејќи рутерот, рутирањето на даден пакет го врши користејќи ги статичките рути што тој ги знае, кога ќе добие пакет преку влезниот интерфејс, тој треба да даде одговор дали може да го рутира пакетот до дадениот уред во таа мрежа (postoi или nepostoi). Важно е тоа што сите адреси имаат мрежна маска /24, што значи дека последните 8 бита се наменети за адресирање. Претпоставуваме дека сите адреси се зафатени во таа мрежа, така што до било кој уред од таа мрежа, доколку ја има во рутирачката табела, може да се достави пакетот. Така што доколку во рутирачката табела има 10.10.10.0, тоа значи дека рутерот може да го проследи пакетот до сите уреди во таа мрежа (10.10.10.1- 10.10.10.254).
  3.  
  4. На влез најпрвин се внесува бројот на рутери, потоа најизменично IP адресата на влезниот интерфејс, па во следниот ред IP адресите на мрежите до кој рутерот има статички рути. Потоа се внесува бројот на обиди за рутирање на пакети. Во следните редови најизменично се внесува влезен интерфејс и IP адреса на уред за која треба да се даде одговор дали тој рутер ја познава или не. Име на класта :RoutingHashJava
  5.  
  6. Влез:
  7. 5
  8. 10.10.1.3
  9. 34.55.34.0,123.3.3.0,67.5.5.0,122.45.4.0
  10. 14.4.12.4
  11. 167.12.145.10,45.55.5.0,23.2.3.0
  12. 12.14.3.3
  13. 146.6.7.0,55.55.43.0,23.66.5.4.0
  14. 55.5.5.5
  15. 12.1.1.0
  16. 34.5.5.1
  17. 14.4.4.0
  18. 2
  19. 10.10.1.3
  20. 34.44.34.12
  21. 55.5.5.5
  22. 12.1.1.233
  23. Излез:
  24. ne postoi
  25. postoi
  26.  
  27. Влез:
  28. 4
  29. 57.9.7.2
  30. 152.234.5.0,43.43.56.0,76.45.234.0,123.34.5.0
  31. 43.45.3.5
  32. 253.252.23.0,45.54.23.0,32.17.71.0,87.56.3.0
  33. 11.5.4.7
  34. 32.12.45.0,76.34.6.0
  35. 123.23.4.4
  36. 123.12.3.0,67.4.65.0,56.54.12.0,245.43.45.0,4.3.5.0
  37. 4
  38. 123.23.4.4
  39. 56.54.12.7
  40. 43.53.5.2
  41. 56.54.12.5
  42. 34.34.5.6
  43. 4.5.6.7
  44. 234.17.4.6
  45. 12.12.12.1
  46. Излез:
  47. postoi
  48. ne postoi
  49. ne postoi
  50. ne postoi
  51.  
  52. ___________________________________________________________________________________________________________________________________
  53.  
  54. import java.io.BufferedReader;
  55. import java.io.IOException;
  56. import java.io.InputStreamReader;
  57.  
  58. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  59.  
  60.     // Each MapEntry object is a pair consisting of a key (a Comparable
  61.     // object) and a value (an arbitrary object).
  62.     K key;
  63.     E value;
  64.  
  65.     public MapEntry (K key, E val) {
  66.         this.key = key;
  67.         this.value = val;
  68.     }
  69.    
  70.     public int compareTo (K that) {
  71.     // Compare this map entry to that map entry.
  72.         @SuppressWarnings("unchecked")
  73.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  74.         return this.key.compareTo(other.key);
  75.     }
  76.  
  77.     public String toString () {
  78.         return "<" + key + "," + value + ">";
  79.     }
  80. }
  81.  
  82. class CBHT<K extends Comparable<K>, E> {
  83.  
  84.     // An object of class CBHT is a closed-bucket hash table, containing
  85.     // entries of class MapEntry.
  86.     private SLLNode<MapEntry<K,E>>[] buckets;
  87.  
  88.     @SuppressWarnings("unchecked")
  89.     public CBHT(int m) {
  90.         // Construct an empty CBHT with m buckets.
  91.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  92.     }
  93.  
  94.     private int hash(K key) {
  95.         // Translate key to an index of the array buckets.
  96.         return Math.abs(key.hashCode()) % buckets.length;
  97.     }
  98.  
  99.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  100.         // Find which if any node of this CBHT contains an entry whose key is
  101.         // equal
  102.         // to targetKey. Return a link to that node (or null if there is none).
  103.         int b = hash(targetKey);
  104.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  105.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  106.                 return curr;
  107.         }
  108.         return null;
  109.     }
  110.  
  111.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  112.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  113.         int b = hash(key);
  114.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  115.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  116.                 // Make newEntry replace the existing entry ...
  117.                 curr.element = newEntry;
  118.                 return;
  119.             }
  120.         }
  121.         // Insert newEntry at the front of the 1WLL in bucket b ...
  122.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  123.     }
  124.  
  125.     public void delete(K key) {
  126.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  127.         int b = hash(key);
  128.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  129.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  130.                 if (pred == null)
  131.                     buckets[b] = curr.succ;
  132.                 else
  133.                     pred.succ = curr.succ;
  134.                 return;
  135.             }
  136.         }
  137.     }
  138.  
  139.     public String toString() {
  140.         String temp = "";
  141.         for (int i = 0; i < buckets.length; i++) {
  142.             temp += i + ":";
  143.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  144.                 temp += curr.element.toString() + " ";
  145.             }
  146.             temp += "\n";
  147.         }
  148.         return temp;
  149.     }
  150.  
  151. }
  152.  
  153.  
  154. class SLLNode<E> {
  155.     protected E element;
  156.     protected SLLNode<E> succ;
  157.  
  158.     public SLLNode(E elem, SLLNode<E> succ) {
  159.         this.element = elem;
  160.         this.succ = succ;
  161.     }
  162.  
  163.     @Override
  164.     public String toString() {
  165.         return element.toString();
  166.     }
  167. }
  168.  
  169. public class RoutingHashJava {
  170.     public static void main(String[] args) throws NumberFormatException, IOException {
  171.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  172.         int N = Integer.parseInt(br.readLine());
  173.         CBHT<String, String> koficka = new CBHT<String, String>(N);
  174.  
  175.         for (int i = 0; i < N; i++) {
  176.             String prva = br.readLine();
  177.             String vtora = br.readLine();
  178.             koficka.insert(prva, vtora);
  179.         }
  180.  
  181.         //System.out.println(koficka);
  182.        
  183.         N = Integer.parseInt(br.readLine());
  184.         for (int i = 0; i < N; i++) {
  185.             String interfejs = br.readLine();
  186.             String[] ip = br.readLine().split("\\.");
  187.             SLLNode<MapEntry<String, String>> me = koficka.search(interfejs);
  188.             if(me != null) {
  189.                 String interf = me.element.key;
  190.                 String[] ip_adresi = me.element.value.split("\\,");
  191.                 int br_pati = 0;
  192.                 for(int x =0;x<ip_adresi.length;x++) {
  193.                     String[] delcinja_ip = ip_adresi[x].split("\\.");
  194.                     if(ip[0].equals(delcinja_ip[0]) && ip[1].equals(delcinja_ip[1]) && ip[2].equals(delcinja_ip[2])) {
  195.                         System.out.println("postoi");
  196.                         br_pati++;
  197.                         break;
  198.                     }
  199.                 }
  200.                 if(br_pati < 1) {
  201.                     System.out.println("ne postoi");
  202.                 }
  203.             }else {
  204.                 System.out.println("ne postoi");
  205.             }
  206.         }
  207.         br.close();
  208.     }
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement