public static void main(String[] args) throws Exception { if (args.length != 4) { System.out.println("Usage: SampleServer <keystore> <password> <root file> <port>"); return; } String keystoreFile = args[0]; String password = args[1]; String root = args[2]; int port = Integer.parseInt(args[3]); SSLContext sslContext = sslContext(keystoreFile, password); SampleServer server = new SampleServer(sslContext, root, port); server.run(); }
private MockResponse fileToResponse(String path, File file) throws IOException { return new MockResponse() .setStatus("HTTP/1.1 200") .setBody(fileToBytes(file)) .addHeader("content-type: " + contentType(path)); }
private void drainQueue() throws Exception { for (HttpUrl url; (url = queue.take()) != null; ) { if (!fetchedUrls.add(url)) { continue; } Thread currentThread = Thread.currentThread(); String originalName = currentThread.getName(); currentThread.setName("Crawler " + url.toString()); try { fetch(url); } catch (IOException e) { System.out.printf("XXX: %s %s%n", url, e); } finally { currentThread.setName(originalName); } } }
public class Main { public static void main(String[] args) throws Exception { //Create a server //Server has to be only one and multiple clients can connect it SampleServer server = new SampleServer(5555); server.startServer(); //Creating one client Socket soc = new Socket("localhost", 5555); //Similarly create n-number of clients and connect to the same port as that of server //server.getClientList(); will help you get the list of the clients connected to the server } }
public static void main(String[] args) throws IOException { if (args.length != 2) { System.out.println("Usage: Crawler <cache dir> <root>"); return; } int threadCount = 20; long cacheByteCount = 1024L * 1024L * 100L; Cache cache = new Cache(new File(args[0]), cacheByteCount); OkHttpClient client = new OkHttpClient.Builder() .cache(cache) .build(); Crawler crawler = new Crawler(client); crawler.queue.add(HttpUrl.get(args[1])); crawler.parallelDrainQueue(threadCount); } }
@Override public MockResponse dispatch(RecordedRequest request) { String path = request.getPath(); try { if (!path.startsWith("/") || path.contains("..")) throw new FileNotFoundException(); File file = new File(root + path); return file.isDirectory() ? directoryToResponse(path, file) : fileToResponse(path, file); } catch (FileNotFoundException e) { return new MockResponse() .setStatus("HTTP/1.1 404") .addHeader("content-type: text/plain; charset=utf-8") .setBody("NOT FOUND: " + path); } catch (IOException e) { return new MockResponse() .setStatus("HTTP/1.1 500") .addHeader("content-type: text/plain; charset=utf-8") .setBody("SERVER ERROR: " + e); } }