package com.chenyang.nse.bussiness.tools.syslog;

import java.net.SocketAddress;
import org.graylog2.syslog4j.server.SyslogServer;
import org.graylog2.syslog4j.server.SyslogServerConfigIF;
import org.graylog2.syslog4j.server.SyslogServerEventIF;
import org.graylog2.syslog4j.server.SyslogServerIF;
import org.graylog2.syslog4j.server.SyslogServerSessionEventHandlerIF;

public class MySyslogServer {
   private static final String HOST = "192.168.2.218";
   private static final int PORT = 40000;

   private void receiveSyslogMessage() throws InterruptedException {
      SyslogServerIF server = SyslogServer.getInstance("udp");
      SyslogServerConfigIF config = server.getConfig();
      config.setHost("192.168.2.218");
      config.setPort(40000);
      config.addEventHandler(new SyslogServerSessionEventHandlerIF() {
         public Object sessionOpened(SyslogServerIF syslogServerIF, SocketAddress socketAddress) {
            return null;
         }

         public void event(Object o, SyslogServerIF syslogServerIF, SocketAddress socketAddress, SyslogServerEventIF syslogServerEventIF) {
            System.out.println("receive from:" + socketAddress + "\tmessage" + syslogServerEventIF.getMessage());
         }

         public void exception(Object o, SyslogServerIF syslogServerIF, SocketAddress socketAddress, Exception e) {
         }

         public void sessionClosed(Object o, SyslogServerIF syslogServerIF, SocketAddress socketAddress, boolean b) {
         }

         public void initialize(SyslogServerIF syslogServerIF) {
         }

         public void destroy(SyslogServerIF syslogServerIF) {
         }
      });
      SyslogServer.getThreadedInstance("udp");
   }

   public static void main(String[] args) throws InterruptedException {
      (new MySyslogServer()).receiveSyslogMessage();
   }
}
