cobweb/src/com/spookyinternet/cobweb/CobwebServer.java

44 lines
1.3 KiB
Java

package com.spookyinternet.cobweb;
import java.net.*;
import java.util.*;
public class CobwebServer {
private ServerSocket srv;
private Map<String, List<HttpRoute>> glances;
private Map<String, List<HttpRoute>> routes;
public CobwebServer(int port, byte[] host) throws Exception {
this.srv = new ServerSocket(port, 64, InetAddress.getByAddress(host));
this.glances = new HashMap<String, List<HttpRoute>>();
this.routes = new HashMap<String, List<HttpRoute>>();
}
public void inspect(String method, String path, HttpInspectFunction fn) throws Exception {
String norm_method = method.toLowerCase();
if(this.glances.get(norm_method) == null) {
this.glances.put(norm_method, new ArrayList<HttpRoute>());
}
this.glances.get(norm_method).add(new HttpRoute(path, fn));
}
public void on(String method, String path, HttpRequestFunction fn) throws Exception {
String norm_method = method.toLowerCase();
if(this.routes.get(norm_method) == null) {
this.routes.put(norm_method, new ArrayList<HttpRoute>());
}
this.routes.get(norm_method).add(new HttpRoute(path, fn));
}
public void run() throws Exception {
while(true) {
Socket cli = this.srv.accept();
new HttpThread(cli, this.glances, this.routes).start();
}
}
}