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.mock;
15  
16  import java.net.DatagramPacket;
17  import java.net.DatagramSocket;
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * 
23   * @author Ceki Gülcü
24   */
25  public class MockSyslogServer extends Thread {
26  
27      final int loopLen;
28      final int port;
29  
30      List<byte[]> msgList = new ArrayList<byte[]>();
31      boolean finished = false;
32  
33      public MockSyslogServer(int loopLen, int port) {
34          super();
35          this.loopLen = loopLen;
36          this.port = port;
37      }
38  
39      @Override
40      public void run() {
41          // System.out.println("MockSyslogServer listening on port "+port);
42          DatagramSocket socket = null;
43          try {
44              socket = new DatagramSocket(port);
45  
46              for (int i = 0; i < loopLen; i++) {
47                  byte[] buf = new byte[65536];
48                  DatagramPacket packet = new DatagramPacket(buf, buf.length);
49                  // System.out.println("Waiting for message");
50                  socket.receive(packet);
51                  byte[] out = new byte[packet.getLength()];
52                  System.arraycopy(buf, 0, out, 0, out.length);
53                  msgList.add(out);
54              }
55          } catch (Exception se) {
56              se.printStackTrace();
57          } finally {
58              if (socket != null) {
59                  try {
60                      socket.close();
61                  } catch (Exception e) {
62                  }
63              }
64          }
65          finished = true;
66      }
67  
68      public boolean isFinished() {
69          return finished;
70      }
71  
72      public List<byte[]> getMessageList() {
73          return msgList;
74      }
75  }