!636 import java.util.*; import java.io.*; import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.security.KeyStore; import java.text.SimpleDateFormat; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; import javax.net.ssl.*; main { static class MyHTTPD extends NanoHTTPD { /** * Constructs an HTTP server on given port. */ public MyHTTPD(int port) throws IOException { super(port); } @Override public Response serve( String uri, Method method, Map header, Map parms, Map files ) { System.out.println( method + " '222" + uri + "' " ); String ip = header.get("remote-addr"); if (!ip.equals("127.0.0.1")) peers.add(ip); if (uri.equals("/k.mp4")) return serveBinary("/home/stefan/k.mp4"); String msg = "

Hello server

\n"; if ( parms.get("username") == null ) msg += "
\n" + "

Your name:

\n" + "
\n"; else msg += "

Hello, " + parms.get("username") + "!

"; msg += "uri: " + uri + "
"; new List links; for (String p : peers) links.add("" + p + ""); msg += "All known peers: " + join(", ", links); msg += "\n"; return newFixedLengthResponse(msg); } static String join(String glue, Iterable list) { new StringBuilder buf; boolean first = true; for (String s : list) { if (first) first = false; else buf.append(glue); buf.append(s); } return buf.toString(); } Response serveBinary(String path) { System.out.println("Serving " + path); long length = new File(path).length(); System.out.println("File size: " + length); FileInputStream fis = null; try { fis = new FileInputStream(path); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new Response(Status.OK, "application/binary", fis, length); } } private static MyHTTPD server; private static int port = 8888; static new (Tree)Set peers; psvm { server = new MyHTTPD(port); server.start(); System.out.println("HTTP server started (listening on port " + port + "!)"); printMyIPs(); server.join(); // keep main thread alive } static void printMyIPs() { String ip; try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = interfaces.nextElement(); // filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback() || !iface.isUp()) continue; Enumeration addresses = iface.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address) { // we still stick to the old stuff... until the new stuff comes along! ip = addr.getHostAddress(); System.out.println("\nPlease visit http://" + ip + ":" + port + " to see me!"); } } } } catch (Throwable e) { e.printStackTrace(); } } } !include #1000433 // class NanoHTTP