View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.net;
15  
16  import java.io.InputStream;
17  import java.net.ServerSocket;
18  import java.net.Socket;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  public class ExternalMockSocketServer {
23  
24      static final String LOGGINGEVENT = "LoggingEvent";
25      static final String LOGGINGEVENT2 = "LoggingEvent2";
26      static final String MINIMALEXT = "MinimalExt";
27      static final String MINIMALSER = "MinimalSer";
28  
29      static final int PORT = 4560;
30  
31      // static int loopLen;
32      static int clientNumber;
33  
34      static List<String> msgList = new ArrayList<String>();
35      static boolean finished = false;
36  
37      String className = LOGGINGEVENT;
38  
39      public static void main(String[] args) {
40          if (args.length == 1) {
41              clientNumber = Integer.parseInt(args[0]);
42              // loopLen = Integer.parseInt((args[1]));
43              runServer();
44          } else {
45              usage("Wrong number of arguments.");
46          }
47      }
48  
49      static void usage(String msg) {
50          System.err.println(msg);
51          System.err.println("Usage: java " + ExternalMockSocketServer.class.getName() + " loopNumber");
52          System.exit(1);
53      }
54  
55      static void runServer() {
56  
57          try {
58              System.out.println("Starting Server...");
59              ServerSocket serverSocket = new ServerSocket(PORT);
60              System.out.println("Listening on port " + PORT);
61              for (int j = 0; j < clientNumber; j++) {
62                  Socket socket = serverSocket.accept();
63                  System.out.println("New client accepted.");
64                  System.out.println("Connected to client at " + socket.getInetAddress());
65  
66                  InputStream is = socket.getInputStream();
67                  long sum = 0;
68  
69                  while (true) {
70                      // this call is blocking
71                      int val = is.read();
72                      if (val == -1) {
73                          break;
74                      }
75                      // if a byte is available, we skip it.
76                      // this allows to pass all available bytes in a quick manner.
77                      int a = is.available();
78                      sum += a + 1;
79                      is.skip(a);
80                  }
81                  System.out.println(sum / 1000 + " KB");
82              }
83              serverSocket.close();
84          } catch (Exception se) {
85              se.printStackTrace();
86          }
87          System.out.println("Server finished.");
88          finished = true;
89      }
90  
91  }