00001 package org.hfbk.vis;
00002
00003 import java.io.BufferedReader;
00004 import java.io.IOException;
00005 import java.io.InputStreamReader;
00006 import java.io.PrintStream;
00007 import java.net.ServerSocket;
00008 import java.net.Socket;
00009
00010 import org.hfbk.vis.source.Source;
00011
00012 public class VisServer extends Thread{
00013
00014 final static int PORT=8888;
00015
00016 static ServerSocket socket;
00017
00018 Socket connection;
00019
00020 public VisServer(Socket connect) {
00021 this.connection=connect;
00022 start();
00023 }
00024
00025 public void run() {
00026 try {
00027 BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
00028 PrintStream out=new PrintStream(connection.getOutputStream());
00029
00030 String cmd=in.readLine();
00031 String[] args=cmd.split(" ");
00032
00033 if (args[0].equals("GET")){
00034 String[] subargs=args[1].split("\\?");
00035 String script=subargs[0];
00036 if (script.equals("/graph.php")){
00037 handleSourceRequest("http://host"+args[1], out);
00038 }
00039 }
00040
00041 connection.close();
00042
00043 } catch (IOException e) {
00044 throw new RuntimeException (e);
00045 }
00046 }
00047
00048 void handleSourceRequest(String url, PrintStream out){
00049 Source source=Source.getSource(url);
00050
00051 try {
00052 source.join();
00053 } catch (InterruptedException e) {
00054 throw new RuntimeException (e);
00055 }
00056 System.out.println("Haue "+url+" raus!");
00057 out.print("HTTP/1.0 200 OK\r\n");
00058 out.print("Content-type: text/html; charset=utf8\r\n");
00059 out.print("\r\n");
00060 source.graph.dump(out);
00061 }
00062
00063 public static void main(String[] args) throws IOException {
00064
00065 socket=new ServerSocket(PORT);
00066 System.out.println("Running.");
00067 while(true){
00068 Socket connect=socket.accept();
00069 System.out.println(connect.getInetAddress().toString()+" klingelt an.");
00070 new VisServer(connect);
00071 }
00072 }
00073 }