diff src/main/java/alice/daemon/AliceDaemon.java @ 345:8f71c3e6f11d

Change directory structure Maven standard
author sugi
date Wed, 16 Apr 2014 18:26:07 +0900
parents
children aefbe41fcf12
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/alice/daemon/AliceDaemon.java	Wed Apr 16 18:26:07 2014 +0900
@@ -0,0 +1,61 @@
+package alice.daemon;
+
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.nio.channels.ServerSocketChannel;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.apache.log4j.WriterAppender;
+
+public class AliceDaemon {
+	
+	private Config conf;
+	private AcceptThread acceptThread;
+	private Logger log = Logger.getLogger(AliceDaemon.class);
+	
+	public AliceDaemon(Config conf) {
+		this.conf = conf;
+		setLogger();
+	}
+	
+	private void setLogger() {
+		Logger root = Logger.getRootLogger();
+		if (conf.level != null)
+			root.setLevel(conf.level);
+		if (conf.logFile == null)
+			return;
+		PatternLayout layout = new PatternLayout();
+		layout.setConversionPattern("%d %-5p %c - %m [%t] (%F:%L)%n");
+		try {
+			FileWriter writer = new FileWriter(conf.logFile);
+			WriterAppender writerAppender = new WriterAppender(layout, writer);
+			root.removeAllAppenders();
+			root.addAppender(writerAppender);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		log.info("configured");
+	}
+	
+	public void listen() {
+		try {
+			ServerSocketChannel ssChannel = ServerSocketChannel.open();
+			ServerSocket ss = ssChannel.socket();
+			ss.setReuseAddress(true);
+			
+            //InetSocketAddress a = new InetSocketAddress("::", conf.localPort);
+            InetSocketAddress a = new InetSocketAddress(InetAddress.getLocalHost(), conf.localPort);
+            //System.out.println(a);
+            ss.bind(a);
+			acceptThread = new AcceptThread(ss, "ACCEPT" + conf.localPort);
+			acceptThread.start();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		
+	}
+}