/*
 * Created on Jan 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.prolixtech.jaminid_examples;

import com.prolixtech.jaminid.ContentOracle;
import com.prolixtech.jaminid.Daemon;
import com.prolixtech.jaminid.Request;
import com.prolixtech.jaminid.Response;

/**
 * @author Constantinos Michael
 *
 */
public class HelloWorldServer extends ContentOracle {

    public static final int PORT = 80;
    public static String HELLO =  "<html><body><h1>Hello <a href=\"?details\">World</a>!</h1></body></html>";
    
    private static Daemon daemon;
    
    public String demultiplex(Request connRequest, Response connResponse) {
        if(connRequest.getParamOrNull("details")!=null){
            StringBuffer a = new StringBuffer();
            a.append("<h1>Details about your request</h1>");
            a.append("<h3>Location</h3>");
            a.append(connRequest.getLocation()); 
            a.append("<h3>Parameters</h3>");
            a.append(connRequest.getParamMap());
            a.append("<h3>Client</h3>");
            a.append("IP Address: ");
            a.append(connRequest.getIPAddressString());
            a.append("<BR>");
            a.append(connRequest.getHeader());
            
            return a.toString();
        }
        if(connRequest.getLocation().endsWith("exit")){
            System.out.println("Exiting!");
            daemon.tearDown();
        }
        
        return HELLO;
        
    }
    
    
    
    public static void main(String[] args) {
        
        HelloWorldServer s = new HelloWorldServer();
        daemon = new Daemon(PORT, s);
        System.out.println("The webserver is now running! Visit http://localhost:" + PORT + " through your favorite web browser.");
        
    }
    
}
