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

📄 quickserver.java

📁 一个用java编写的服务器,对于学习网络编程的人来说是个很好的例子
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

				processServerHooks(ServerHook.POST_SHUTDOWN);
			}
		}
	} //end of run

	/**
	 * Sets the maximum number of client connection allowed.
	 * @since 1.1
	 * @see #getMaxConnection
	 */
	public void setMaxConnection(long maxConnection) {
		if(getServiceState()==Service.SUSPENDED)
			suspendMaxConnection = maxConnection;
		else
			this.maxConnection = maxConnection;
		logger.finest("Set to "+maxConnection);
	}
	/** 
	 * Returns the maximum number of client connection allowed.
	 * @since 1.1
	 * @see #setMaxConnection
	 */
	public long getMaxConnection() {
		return maxConnection;
	}

	/** 
	 * Returns number of clients connected.
	 * @since 1.1
	 */
	public long getClientCount() {
		if(clientHandlerPool != null) {
			try {
				return getClientHandlerPool().getNumActive();
			} catch(Exception e) {
				return 0;
			}
		}
		return 0;
	}

	/**
	 * Sets the message to be sent to any new client connected after
	 * maximum client connection has reached. 
	 * Default is : <code>-ERR Server Busy. Max Connection Reached</code>
	 * @since 1.1
	 * @see #getMaxConnectionMsg
	 */
	public void setMaxConnectionMsg(String maxConnectionMsg) {
		if(getServiceState() == Service.SUSPENDED)
			suspendMaxConnectionMsg = maxConnectionMsg;
		else
			this.maxConnectionMsg = maxConnectionMsg;
		logger.finest("Set to "+maxConnectionMsg);
	}
	/**
	 * Returns the message to be sent to any new client connected 
	 * after maximum client connection has reached.
	 * @since 1.1
	 * @see #setMaxConnectionMsg
	 */
	public String getMaxConnectionMsg() {
		return maxConnectionMsg;
	}

	/**
	 * Sets the Ip address to bind to. 
	 * @param bindAddr argument can be used on a multi-homed host for a 
	 * QuickServer that will only accept connect requests to one 
	 * of its addresses. If not set, it will default accepting 
	 * connections on any/all local addresses.
	 * @exception java.net.UnknownHostException if no IP address for 
	 * the host could be found
	 * @since 1.1
	 * @see #getBindAddr
	 */
	public void setBindAddr(String bindAddr) 
			throws UnknownHostException {
		ipAddr = InetAddress.getByName(bindAddr);
		logger.finest("Set to "+bindAddr);
	}
	/**
	 * Returns the IP address binding to. 
	 * @since 1.1
	 * @see #setBindAddr
	 */
	public InetAddress getBindAddr() {
		if(ipAddr==null) {
			try	{
				ipAddr = InetAddress.getByName("0.0.0.0");
			} catch(Exception e){
				logger.warning("Unable to create default ip(0.0.0.0) : "+e);
				throw new RuntimeException("Error: Unable to find servers own ip : "+e);
			}			
		}
		return ipAddr;
	}

	/**
	 * Sets the store of objects to QuickServer, it is an array of objects  
	 * that main program or the class that created QuickServer passes to 
	 * the QuickServer. 
	 * @param storeObjects array of objects
	 * @see #getStoreObjects
	 * @since 1.1
	 */
	public void setStoreObjects(Object[] storeObjects) {
		this.storeObjects = storeObjects;
	}

	/**
	 * Returns store of objects from QuickServer, if nothing was set will
	 * return <code>null</code>.
	 * @see #setStoreObjects
	 * @since 1.1
	 */
	public Object[] getStoreObjects() {
		return storeObjects;
	}

	/** 
	 * Set the port to run QSAdminServer on.
	 * @since 1.2
	 */
	public void setQSAdminServerPort(int port) {
		getQSAdminServer().getServer().setPort(port);
	}
	/** 
	 * Returns the port to run QSAdminServer on.
	 * @since 1.2
	 */
	public int getQSAdminServerPort() {
		return getQSAdminServer().getServer().getPort();
	}

	/** 
	 * Set the ClientAuthenticationHandler class of 
	 * QSAdminServer that handles the authentication of a client.
	 * @since 1.2
	 */
	public void setQSAdminServerAuthenticator(String authenticator) {
		getQSAdminServer().getServer().setClientAuthenticationHandler(authenticator);
	}
	/** 
	 * Returns the Authenticator or ClientAuthenticationHandler class of 
	 * QSAdminServer that handles the authentication of a client.
	 * @since 1.2
	 */
	public String getQSAdminServerAuthenticator() {
		return getQSAdminServer().getServer().getAuthenticator();
	}

	/**
	 * Starts QSAdminServer for this QuickServer.
	 * @see org.quickserver.net.qsadmin.QSAdminServer
	 * @param authenticator sets the ClientAuthenticationHandler class that 
	 *   handles the authentication of a client, 
	 *   if null uses {@link org.quickserver.net.qsadmin.Authenticator}.
	 * @param port to run QSAdminServer on
 	 * @exception org.quickserver.net.AppException 
	 *  if Server already running or if it could not load the classes
	 *  [ClientCommandHandler, ClientAuthenticationHandler, ClientData].
	 * @since 1.1
	 */
	public void startQSAdminServer(int port, String authenticator) 
			throws AppException {
		getQSAdminServer().setClientAuthenticationHandler(authenticator);
		getQSAdminServer().startServer(port);
	}
	/**
	 * Starts QSAdminServer for this QuickServer.
	 * @see org.quickserver.net.qsadmin.QSAdminServer
	 * @since 1.2
	 */
	public void startQSAdminServer() throws AppException {
		getQSAdminServer().startServer();
	}

	/**
	 * Returns {@link QSAdminServer} associated with this QuickServer
	 * @since 1.1
	 */
	public QSAdminServer getQSAdminServer() {
		if(adminServer==null)
			adminServer = new QSAdminServer(QuickServer.this);
		return adminServer;
	}

	/**
	 * Sets {@link QSAdminServer} associated with this QuickServer
	 * @since 1.3.3
	 */
	public void setQSAdminServer(QSAdminServer adminServer) {
		if(adminServer==null)
			this.adminServer = adminServer;
	}

	/** 
	 * Returns the closed state of the QuickServer Socket.
	 * @since 1.1
	 */
	public boolean isClosed() {
		if(server==null)
			return true;
		return server.isClosed();
	}

	/** 
	 * Returns the application logger associated with QuickServer.
	 * If it was not set will return QuickServer's own logger.
	 * @since 1.2
	 */
	public Logger getAppLogger() {
		if(appLogger!=null)
			return appLogger;
		return logger;
	}
	/** 
	 * Sets the application logger associated with QuickServer
	 * @since 1.2
	 */
	public void setAppLogger(Logger appLogger) {
		this.appLogger = appLogger;
	}

	/**
     * Sets the ClientObjectHandler class that interacts with 
	 * client sockets to handle java objects.
	 * @param handler object the fully qualified name of the class that 
	 *  implements {@link ClientObjectHandler}
	 * @see #getClientObjectHandler
	 * @since 1.2
     */
	public void setClientObjectHandler(String handler) {
		clientObjectHandlerString = handler;
		logger.finest("Set to "+handler);
	}
	/**
     * Returns the ClientObjectHandler class that interacts with 
	 * client sockets.
	 * @see #setClientObjectHandler
	 * @since 1.2
     */
	public String getClientObjectHandler() {
		return clientObjectHandlerString;
	}

	/**
	 * Sets the console log handler formatter.
	 * @param formatter fully qualified name of the class that implements 
	 * {@link java.util.logging.Formatter}
	 * @since 1.2
	 */
	public void setConsoleLoggingFormatter(String formatter) 
			throws ClassNotFoundException, InstantiationException,
				IllegalAccessException {
		if(formatter==null)
			return;
		consoleLoggingformatter = formatter;

		Formatter conformatter = 
			(Formatter) getClass(formatter, true).newInstance();
		Logger jdkLogger = Logger.getLogger("");
		Handler[] handlers =  jdkLogger.getHandlers();
		for(int index = 0; index < handlers.length; index++ ) {
			if(ConsoleHandler.class.isInstance(handlers[index])) {
				handlers[index].setFormatter(conformatter);
			}
		}
		logger.finest("Set to "+formatter);
	}

	/**
	 * Gets the console log handler formatter.
	 * @since 1.3
	 */
	public String getConsoleLoggingFormatter() {
		return consoleLoggingformatter;
	}

	/**
	 * Sets the console log handler formater to 
	 * {@link org.quickserver.util.logging.MiniFormatter}
	 * @since 1.2
	 */
	public void setConsoleLoggingToMini() {
		try	{
			setConsoleLoggingFormatter("org.quickserver.util.logging.MiniFormatter");
		} catch(Exception e) {
			logger.warning("Setting to logging.MiniFormatter : "+e);
		}
	}

	/**
	 * Sets the console log handler formater to 
	 * {@link org.quickserver.util.logging.MicroFormatter}
	 * @since 1.2
	 */
	public void setConsoleLoggingToMicro() {
		try	{
			setConsoleLoggingFormatter("org.quickserver.util.logging.MicroFormatter");	
		} catch(Exception e) {
			logger.warning("Setting to MicroFormatter : "+e);
		}
	}

	/**
	 * Sets the console log handler level.
	 * @since 1.2
	 */
	public void setConsoleLoggingLevel(Level level) {
		Logger rlogger = Logger.getLogger("");
		Handler[] handlers =  rlogger.getHandlers();
		for(int index = 0; index < handlers.length; index++ ) {
			if(ConsoleHandler.class.isInstance(handlers[index])) {
				handlers[index].setLevel(level);
			}
		}
		if(level==Level.SEVERE)
			consoleLoggingLevel = "SEVERE";
		else if(level==Level.WARNING)
			consoleLoggingLevel = "WARNING";
		else if(level==Level.INFO)
			consoleLoggingLevel = "INFO";
		else if(level==Level.CONFIG)
			consoleLoggingLevel = "CONFIG";
		else if(level==Level.FINE)
			consoleLoggingLevel = "FINE";
		else if(level==Level.FINER)
			consoleLoggingLevel = "FINER";
		else if(level==Level.FINEST)
			consoleLoggingLevel = "FINEST";
		else 
			consoleLoggingLevel = "UNKNOWN";

		logger.fine("Set to "+level);
	}

	/**
	 * Gets the console log handler level.
	 * @since 1.3
	 */
	public String getConsoleLoggingLevel() {
		return consoleLoggingLevel;
	}

	/**
	 * Sets the level for all log handlers.
	 * @since 1.3.1
	 */
	public void setLoggingLevel(Level level) {
		Logger rlogger = Logger.getLogger("");
		Handler[] handlers =  rlogger.getHandlers();
		for(int index = 0; index < handlers.length; index++ ) {
		  handlers[index].setLevel(level);
		}

		if(level==Level.SEVERE)
			loggingLevel = "SEVERE";
		else if(level==Level.WARNING)
			loggingLevel = "WARNING";
		else if(level==Level.INFO)
			loggingLevel = "INFO";
		else if(level==Level.CONFIG)
			loggingLevel = "CONFIG";
		else if(level==Level.FINE)
			loggingLevel = "FINE";
		else if(level==Level.FINER)
			loggingLevel = "FINER";
		else if(level==Level.FINEST)
			loggingLevel = "FINEST";
		else 
			loggingLevel = "UNKNOWN";

		consoleLoggingLevel = loggingLevel;

		logger.fine("Set to "+level);
	}

	//*** Start of Service interface methods
	/**
	 * Returns service error if any.
	 * @since 1.4.7
	 */
	public Throwable getServiceError() {
		return serviceError;
	}

	/**
	 * Initialise and create the service.
	 * @param param of the xml configuration file.
	 * @since 1.2
	 */
	public synchronized boolean initService(Object param[]) {
		serviceError = null;
		try {
			initServer(param);
		} catch(Exception e) {
			serviceError = e;
			return false;
		}
		return true;
	}

	/**
	 * Initialise and create the service.
	 * @param qsConfig QuickServerConfig object.
	 * @since 1.4.6
	 */
	public synchronized boolean initService(QuickServerConfig qsConfig) {
		serviceError = null;
		try {
			initServer(qsConfig);			
		} catch(Exception e) {
			serviceError = e;
			return false;
		}
		return true;
	}
	
	/**
	 * Start the service.
	 * @return true if serivce was stopped from Running state.
	 * @since 1.2
	 */
	public boolean startService() {

⌨️ 快捷键说明

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