📄 tcpserver.java
字号:
/******************************************************************************* * Copyright (C) 2002, 2003 * ingenieurbuero fuer innovative informationstechnik (iiit) * Dipl.-Ing. Joerg Beckmann, Dortmund, Germany * * 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. * * version $Id: TcpServer.java,v 1.14 2003/04/14 20:41:41 joerg Exp $ ******************************************************************************/package de.iiit.access.server.plugins.server;import de.iiit.access.common.api.*;import de.iiit.access.server.*;import de.iiit.access.server.api.*;import de.iiit.access.server.plugins.parser.*;import de.iiit.xmlconfig.*;import de.iiit.util.*;import java.util.regex.*;import java.util.*;import java.net.*;import java.io.*;import org.apache.log4j.Logger;/** Implements a TCP/IP server for requesting the AccessServer * @version $Revision: 1.14 $ $Date: 2003/04/14 20:41:41 $ */public class TcpServer extends Thread implements ThreadPluginIf{ /** CVS Version Tag */ private static final String vcid = "$Id: TcpServer.java,v 1.14 2003/04/14 20:41:41 joerg Exp $"; private static final String PORT = "Port"; private static final int DEFAULT_PORT = 54321; private int port; private Logger logger = Logger.getLogger(this.getClass()); private boolean running = true; private Vector handlers = new Vector(); private ServerSocket listen = null; private boolean verifyUser = true; private ResolverPluginIf resolver = null; /** Creates a new instance of TcpServer */ public TcpServer() { setName("TcpServer"); logger.info("TcpServer intantiated."); } /** Initializes the server * @param config the configuration of the server. Currently the only parameter is <CODE>Port</CODE> * which defines the port number to listen on for connections. Default is <CODE>1234</CODE> */ public void initialize(Configuration config) { port = config.getIntAttribute(PORT, DEFAULT_PORT); verifyUser = AccessServer.getVerifyUser(); resolver = AccessServer.getResolverPlugin(); logger.info("TcpServer configured on port <" + port + ">."); } /** The main method of the thread. It is called by the Java runtime environment. * This method waits for new connections and creates an instance of the inner class * TcpHandler for each new one. */ public void run() { try { logger.info("Starting TcpServer on port " + port); listen = new ServerSocket(port); while (running) { TcpHandler tcpHandler = new TcpHandler(listen.accept()); handlers.add(tcpHandler); } } catch (IOException e) { if (running) logger.error("Socket error on port " + port, e); } } /** This method is called by the AccessServer when the background threads should * stop because of a shutdown of the AccessServer itself. */ public void shutdown() { running = false; try { listen.close(); } catch (IOException e) { // Do nothing } interrupt(); Enumeration enum = handlers.elements(); while (enum.hasMoreElements()) ((TcpHandler) enum.nextElement()).shutdown(); } /** Implements the handler for each established connection. */ public class TcpHandler extends Thread { /** CVS Version Tag */ private static final String vcid = "$Id: TcpServer.java,v 1.14 2003/04/14 20:41:41 joerg Exp $"; private Socket socket = null; private Logger logger = Logger.getLogger(this.getClass()); private ExpressionEvaluator evaluator = new ExpressionEvaluator(); private Pattern pCommandLine; private String user = null; private boolean localHostFlag = false; private boolean running = true; private boolean ignoreCase = false; private TcpHandler() { } /** Creates a new instance of TcpHandler * @param s The socket to use for the communication with the client. */ public TcpHandler(Socket s) { socket = s; setName("TcpHandler-" + socket.getPort()); start(); } /** This method is called by the AccessServer when the background threads should * stop because of a shutdown of the AccessServer itself. */ protected void shutdown() { running = false; try { socket.close(); } catch (IOException e) { // Do nothing } interrupt(); } private boolean processLine(PrintStream os, String commandLine) { boolean result = true; Matcher mCommandLine = pCommandLine.matcher(commandLine); if (! mCommandLine.find()) { os.println("ERROR 100: Unknown command"); } else { int count = mCommandLine.groupCount(); if (count == 3) { String cmd = mCommandLine.group(1).trim(); String operator = mCommandLine.group(2); String argument = mCommandLine.group(3).trim(); if (ignoreCase) argument = argument.toLowerCase(); if (operator.equals("") && argument.equals("")) { if (cmd.equalsIgnoreCase("HELP")) { os.println("HELP - This help"); os.println("QUIT - Close connection"); os.println("SETUSER=<user> - Set user for expressions"); os.println("EXPR=<expression> - Evaluate expression"); os.println("OK"); } else if (cmd.equalsIgnoreCase("QUIT")) { os.println("Closing connection"); result = false; } else if (localHostFlag && cmd.equalsIgnoreCase("SHUTDOWN")) { os.println("Closing connection and shutting down"); logger.info("Shutdown command recognized"); AccessServer.shutDown(); result = false; } else { os.println("ERROR 100: Unknown command"); } } else if (! operator.equals("=")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -