⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listener.java

📁 JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼图/椎图功能.
💻 JAVA
字号:
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Created on 18.10.2003
 */

package freecs.core;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.Vector;

import freecs.Server;
import freecs.util.TrafficMonitor;

/**
 * @author Manfred Andres
 *
 * freecs.core
 */
public class Listener extends Thread {
	private static final Listener l = new Listener();
	private Selector sel;
	private ServerSocketChannel ssc;
	
	public Listener() {
		try {
			sel = SelectorProvider.provider().openSelector();
		} catch (Exception e) {
			Server.debug ("Unable to start Listener!", e, Server.MSG_ERROR, Server.LVL_HALT);
		}
	}
	
	public static void startListener() throws IOException {
		if (l.ssc==null)
			l.initSSC ();
		if (!l.isAlive()) {
			l.setPriority(MAX_PRIORITY);
			l.start();
		}
	}

	public void initSSC () throws IOException {
		 try {
			 InetSocketAddress isa = new InetSocketAddress(Server.srv.getLocalHost(), Integer.parseInt(Server.srv.getProperty("port")));
			 Server.srv.lh = isa.getAddress();
			 ssc = Server.srv.getSSC();
			 ssc.socket().bind(isa, Server.srv.SERVER_SOCKET_BACKLOG);
			 ssc.configureBlocking(false);
			 Server.srv.allowedLoginHosts = new Vector();
			 Server.srv.allowedLoginHosts.addElement(isa.getAddress());
			 SelectionKey sk = ssc.register(sel, SelectionKey.OP_ACCEPT);
		 } catch (IOException ioe) {
			 Server.debug("Server: ", ioe, Server.MSG_ERROR, Server.LVL_HALT);
		 }
	}
	
	public void run () {
		while (Server.srv.isRunning()) {
			try {
				Server.log ("Listener: loopstart", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
				while (sel.selectNow() == 0) try {
					Thread.sleep(100);
				} catch (InterruptedException ie) { }
			} catch (IOException ioe) {
				Server.debug ("Listener.run: ", ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
				if (!ssc.isOpen()) try {
					ssc.socket().close();
					initSSC();
				} catch (IOException iioe) {
					Server.debug ("MAJOR ERROR ON REOPENING LISTENER!", iioe, Server.MSG_ERROR, Server.LVL_MAJOR);
					break;
				}
			} catch (Exception e) {
				Server.debug ("Listener.run: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
			}
			for (Iterator i = sel.selectedKeys().iterator(); i.hasNext(); ) try {
				SelectionKey ck = (SelectionKey) i.next();
				i.remove();
				if (ck.isAcceptable()) {
					accept(ck);
				} else {
					Server.log ("Listener.run: SelectionKey has not Accept in interestOps! " + ck.toString(), Server.MSG_STATE, Server.LVL_MAJOR);
				}
			} catch (CancelledKeyException cke) {
				Server.debug ("Listener.run:", cke, Server.MSG_STATE, Server.LVL_VERBOSE);
			}
		}
		for (Iterator i = sel.keys().iterator(); i.hasNext(); ) {
			SelectionKey ck = (SelectionKey) i.next();
			try {
				ck.channel().close();
				ck.cancel();
				i.remove();
			} catch (Exception e) {
				Server.debug ("Listener.final cleanup: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
			}
		}
	}

	private void accept (SelectionKey sk) {
		if (sk == null) return;
		ServerSocketChannel ssc = (ServerSocketChannel) sk.channel();
		SocketChannel sc;
		try {
			sc = ssc.accept();
			if (sc == null) 
				return;
			InetAddress ia = sc.socket ().getInetAddress ();
		   // check if this host is banned for the listener
		   // FIXME: This is considered ALPHA: 
		   // the traffic-monitor does the banning, this is untested
			if (Server.srv.isBanned (ia)) {
			   StringBuffer tsb = new StringBuffer ("CentralSelector.accept: refusing connection to banned host: ").append (ia.toString ());
			   Server.log (tsb.toString (), Server.MSG_TRAFFIC, Server.LVL_MINOR);
			   sc.close();
			   return;
			}
			if (Server.srv.USE_TRAFFIC_MONITOR) {
			   if (!TrafficMonitor.tm.mayPass (ia)) {
				  StringBuffer tsb = new StringBuffer ("CentralSelector.accept: TrafficMonitor is refusing connection to banned host: ").append (ia.toString ());
				  Server.log (tsb.toString (), Server.MSG_TRAFFIC, Server.LVL_MINOR);
				  sc.close();
				  Server.srv.banHost (ia, Server.srv.HOST_BAN_DURATION);
				  return;
			   }
			}
			CentralSelector.cSel.registerSC (sc, Server.REQUEST_TYPE_HTTP);
		} catch (Exception e) {
			Server.debug ("CentralSelector.accept: Exception encountered during accept: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -