Mercurial > hg > Database > Alice
view src/main/java/alice/daemon/AliceDaemon.java @ 540:97bd6efa6e69 dispose
ods&ids have AliceDeamon
author | Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 03 Aug 2015 18:39:37 +0900 |
parents | 0832af83583f |
children | ba9e8b32220d |
line wrap: on
line source
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 java.util.HashMap; import alice.codesegment.CodeSegmentManager; import alice.datasegment.Command; import alice.datasegment.CommandType; import alice.datasegment.DataSegment; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.WriterAppender; import org.msgpack.MessagePack; public class AliceDaemon { private Config conf; private AcceptThread acceptThread; private Logger log = Logger.getLogger(AliceDaemon.class); // static public static final MessagePack packer = new MessagePack(); public static DataSegment dataSegment = new DataSegment(); public static HashMap<Integer, CommandType> hash = new HashMap<Integer, CommandType>();//コマンド対応表 public static int lastId = 0;//コマンドの総数 public static CodeSegmentManager codeSegmentManager = new CodeSegmentManager(); public AliceDaemon(Config conf) { this.conf = conf; setLogger(); commandInit();//static } public static CommandType getCommandTypeFromId(int id) { return hash.get(id); } public void commandInit() {//ToDo: 好きなタイミングで呼び出せるように int id = 1; for (CommandType type : CommandType.values()) { type.setID(id); hash.put(id++, type); } } 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); // listen on any address ipv4/ipv6 InetSocketAddress a = new InetSocketAddress("::", conf.localPort); System.out.println("AliceDaemon.listen: bind to " + a); ss.bind(a); acceptThread = new AcceptThread(ss, "ACCEPT" + conf.localPort); acceptThread.start(); } catch (IOException e) { e.printStackTrace(); } } }