1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
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 }