]> git.somenet.org - pub/jan/dslab.git/blob - ue2/src/Client/Main.java
all the dslab stuff
[pub/jan/dslab.git] / ue2 / src / Client / Main.java
1 package Client;\r
2 \r
3 import java.io.*;\r
4 import java.util.*;\r
5 import java.rmi.*;\r
6 import java.rmi.registry.*;\r
7 \r
8 \r
9 public class Main{\r
10   static private BufferedReader $con = null;\r
11   static private HashMap $cache = null;\r
12   static private Nameserver.MyDNS $root = null;\r
13   static private Registry $reg = null;\r
14   static private Properties $prop = new Properties();\r
15 \r
16 \r
17 static public void main(String[] $args)throws Exception{\r
18   $con = new BufferedReader (new InputStreamReader(System.in));\r
19   $cache = new HashMap();\r
20 \r
21   InputStream $in = ClassLoader.getSystemResourceAsStream("registry.properties");\r
22   if ($in == null){\r
23     System.err.println("ERROR: Properties file not found!");\r
24     System.exit(1);\r
25   }\r
26   $prop.load($in);\r
27   $in.close();\r
28 \r
29   //get registry\r
30   $reg = LocateRegistry.getRegistry($prop.getProperty("registry.host"), Integer.parseInt($prop.getProperty("registry.port")));\r
31   $root = (Nameserver.MyDNS) $reg.lookup("rmi://"+$prop.getProperty("registry.host")+":"+$prop.getProperty("registry.port")+"/"+$prop.getProperty("ro_id"));\r
32 \r
33   while($con != null)try{\r
34     System.out.println("Enter url (or '!exit' or !cc)");\r
35     auswertung($con.readLine().trim());\r
36   }catch(Exception $e){$e.printStackTrace();} //Eingaben auswerten\r
37 }\r
38 \r
39 static public void auswertung(String $eingraw)throws Exception{\r
40   if($eingraw.equals("") || $eingraw == null)return; // Ignore null-commands\r
41   String[] $eing = $eingraw.split("\\s+"); //regex: whitespace character one and more\r
42 \r
43   if($eing[0].equalsIgnoreCase("!exit")){\r
44     System.exit(0);\r
45   }\r
46   if($eing[0].equalsIgnoreCase("!cc")){\r
47     $cache = new HashMap();\r
48     return;\r
49   }\r
50   try{getFile($eingraw.trim());}catch(Exception $e){System.out.println($e.getMessage());}\r
51 }\r
52 \r
53 static public void getFile(String $addr)throws Exception{\r
54   System.out.println("reqest File:"+$addr);\r
55   if($addr.charAt(0)=='/'){\r
56     System.out.println("Hostname not given!");\r
57     return;\r
58   }\r
59   if($addr.indexOf('/')== -1){\r
60     System.out.println("File not given!");\r
61     return;\r
62   }\r
63   String $tempa[] = $addr.split("/");\r
64   String $hostp[] = $tempa[0].split("\\.");\r
65   String $path = $addr.replaceFirst($tempa[0],"");\r
66 \r
67   System.out.println($tempa[0]);\r
68   System.out.println($path);\r
69 \r
70   //requestor is allways starting at root, requested allways at last hostpart\r
71   Nameserver.MyBase $requestto = $root;\r
72   String $requested = "";\r
73   int $reqcnt = 0;\r
74   for(int $i = $hostp.length-1; $i >= 0; $i--){ //do not request last part - as it is the fileserver!\r
75     Nameserver.MyBase $tempobj = (Nameserver.MyBase)$cache.get($hostp[$i]+"."+$requested);\r
76     if($tempobj == null){ //object not cached - request!\r
77       if($requestto.getMyType() != 1){\r
78         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]);\r
79         return;\r
80       }\r
81       $reqcnt++;\r
82       $tempobj = (Nameserver.MyBase)((Nameserver.MyDNS)$requestto).getServiceRecord($hostp[$i]);\r
83       $cache.put($hostp[$i]+"."+$requested, $tempobj);\r
84     }\r
85     $requestto = $tempobj;\r
86     $requested = $hostp[$i]+"."+$requested;\r
87   }\r
88 \r
89   System.out.println($reqcnt+" reqests done to find: "+$requested);\r
90   if($requestto.getMyType() != 2){\r
91     System.out.println("Never look for a penis on a girls body. Your requested host ist not a Fileserver: "+ $requested);\r
92     return;\r
93   }\r
94   System.out.println(((Fileserver.MyFS)$requestto).readFile($path));\r
95 }\r
96 }