1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.net.mock;
11  
12  import java.net.DatagramPacket;
13  import java.net.DatagramSocket;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * 
19   * @author Ceki G&uumllcü
20   */
21  public class MockSyslogServer extends Thread {
22  
23    final int loopLen;
24    final int port;
25    
26    List<String> msgList = new ArrayList<String>();
27    boolean finished = false;
28    
29    public MockSyslogServer(int loopLen, int port) {
30      super();
31      this.loopLen = loopLen;
32      this.port = port;
33    }
34  
35    @Override
36    public void run() {
37      //System.out.println("MockSyslogServer listening on port "+port);
38      DatagramSocket socket = null;
39      try {
40        socket = new DatagramSocket(port);
41  
42        for (int i = 0; i < loopLen; i++) {
43          byte[] buf = new byte[2048];
44          DatagramPacket packet = new DatagramPacket(buf, buf.length);
45          //System.out.println("Waiting for message");
46          socket.receive(packet);
47          //System.out.println("Got message");
48          String msg = new String(buf, 0, packet.getLength());
49          msgList.add(msg);
50        }
51      } catch (Exception se) {
52        se.printStackTrace();
53      } finally {
54        if(socket != null) {
55  	  try {socket.close();} catch(Exception e) {}
56        }
57      }
58      finished = true;
59    }
60    
61    public boolean isFinished() {
62      return finished;
63    }
64    
65    public List<String> getMessageList() {
66      return msgList;
67    }
68  }