Advertisement
Kame3

Статичко рутирање lab6.2

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