package Client;

import java.io.*;
import java.util.*;
import java.rmi.*;
import java.rmi.registry.*;


public class Main{
  static private BufferedReader $con = null;
  static private HashMap $cache = null;
  static private Nameserver.MyDNS $root = null;
  static private Registry $reg = null;
  static private Properties $prop = new Properties();


static public void main(String[] $args)throws Exception{
  $con = new BufferedReader (new InputStreamReader(System.in));
  $cache = new HashMap();

  InputStream $in = ClassLoader.getSystemResourceAsStream("registry.properties");
  if ($in == null){
    System.err.println("ERROR: Properties file not found!");
    System.exit(1);
  }
  $prop.load($in);
  $in.close();

  //get registry
  $reg = LocateRegistry.getRegistry($prop.getProperty("registry.host"), Integer.parseInt($prop.getProperty("registry.port")));
  $root = (Nameserver.MyDNS) $reg.lookup("rmi://"+$prop.getProperty("registry.host")+":"+$prop.getProperty("registry.port")+"/"+$prop.getProperty("ro_id"));

  while($con != null)try{
    System.out.println("Enter url (or '!exit' or !cc)");
    auswertung($con.readLine().trim());
  }catch(Exception $e){$e.printStackTrace();} //Eingaben auswerten
}

static public void auswertung(String $eingraw)throws Exception{
  if($eingraw.equals("") || $eingraw == null)return; // Ignore null-commands
  String[] $eing = $eingraw.split("\\s+"); //regex: whitespace character one and more

  if($eing[0].equalsIgnoreCase("!exit")){
    System.exit(0);
  }
  if($eing[0].equalsIgnoreCase("!cc")){
    $cache = new HashMap();
    return;
  }
  try{getFile($eingraw.trim());}catch(Exception $e){System.out.println($e.getMessage());}
}

static public void getFile(String $addr)throws Exception{
  System.out.println("reqest File:"+$addr);
  if($addr.charAt(0)=='/'){
    System.out.println("Hostname not given!");
    return;
  }
  if($addr.indexOf('/')== -1){
    System.out.println("File not given!");
    return;
  }
  String $tempa[] = $addr.split("/");
  String $hostp[] = $tempa[0].split("\\.");
  String $path = $addr.replaceFirst($tempa[0],"");

  System.out.println($tempa[0]);
  System.out.println($path);

  //requestor is allways starting at root, requested allways at last hostpart
  Nameserver.MyBase $requestto = $root;
  String $requested = "";
  int $reqcnt = 0;
  for(int $i = $hostp.length-1; $i >= 0; $i--){ //do not request last part - as it is the fileserver!
    Nameserver.MyBase $tempobj = (Nameserver.MyBase)$cache.get($hostp[$i]+"."+$requested);
    if($tempobj == null){ //object not cached - request!
      if($requestto.getMyType() != 1){
        System.out.println("Never look for a penis on a girls body. Your requested host ist not a Nameserver: "+$requested+". Your requested hostname:"+$tempa[0]);
        return;
      }
      $reqcnt++;
      $tempobj = (Nameserver.MyBase)((Nameserver.MyDNS)$requestto).getServiceRecord($hostp[$i]);
      $cache.put($hostp[$i]+"."+$requested, $tempobj);
    }
    $requestto = $tempobj;
    $requested = $hostp[$i]+"."+$requested;
  }

  System.out.println($reqcnt+" reqests done to find: "+$requested);
  if($requestto.getMyType() != 2){
    System.out.println("Never look for a penis on a girls body. Your requested host ist not a Fileserver: "+ $requested);
    return;
  }
  System.out.println(((Fileserver.MyFS)$requestto).readFile($path));
}
}