001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.net;
015
016import java.io.InputStream;
017import java.net.ServerSocket;
018import java.net.Socket;
019import java.util.ArrayList;
020import java.util.List;
021
022public class ExternalMockSocketServer {
023
024    static final String LOGGINGEVENT = "LoggingEvent";
025    static final String LOGGINGEVENT2 = "LoggingEvent2";
026    static final String MINIMALEXT = "MinimalExt";
027    static final String MINIMALSER = "MinimalSer";
028
029    static final int PORT = 4560;
030
031    // static int loopLen;
032    static int clientNumber;
033
034    static List<String> msgList = new ArrayList<String>();
035    static boolean finished = false;
036
037    String className = LOGGINGEVENT;
038
039    public static void main(String[] args) {
040        if (args.length == 1) {
041            clientNumber = Integer.parseInt(args[0]);
042            // loopLen = Integer.parseInt((args[1]));
043            runServer();
044        } else {
045            usage("Wrong number of arguments.");
046        }
047    }
048
049    static void usage(String msg) {
050        System.err.println(msg);
051        System.err.println("Usage: java " + ExternalMockSocketServer.class.getName() + " loopNumber");
052        System.exit(1);
053    }
054
055    static void runServer() {
056
057        try {
058            System.out.println("Starting Server...");
059            ServerSocket serverSocket = new ServerSocket(PORT);
060            System.out.println("Listening on port " + PORT);
061            for (int j = 0; j < clientNumber; j++) {
062                Socket socket = serverSocket.accept();
063                System.out.println("New client accepted.");
064                System.out.println("Connected to client at " + socket.getInetAddress());
065
066                InputStream is = socket.getInputStream();
067                long sum = 0;
068
069                while (true) {
070                    // this call is blocking
071                    int val = is.read();
072                    if (val == -1) {
073                        break;
074                    }
075                    // if a byte is available, we skip it.
076                    // this allows to pass all available bytes in a quick manner.
077                    int a = is.available();
078                    sum += a + 1;
079                    is.skip(a);
080                }
081                System.out.println(sum / 1000 + " KB");
082            }
083            serverSocket.close();
084        } catch (Exception se) {
085            se.printStackTrace();
086        }
087        System.out.println("Server finished.");
088        finished = true;
089    }
090
091}