📄 connectionportlistener.java
字号:
/* * Copyright (c) 2003 - 2007, Silvio Meier and Tobias Reinhard * * All rights reserved. * * Redistribution and use in source and binary forms, * with or without modification, are permitted provided * that the following conditions are met: * * o Redistributions of source code must retain the above * copyright notice, this list of conditions and the * following disclaimer. * o Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the * following disclaimer in the documentation and/or other * materials provided with the distribution. * o The names of its contributors may not be used to endorse * or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package net.sf.cscc;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;/** * This class is a network listener, listening for new connection requests to * the server. If a new request arrives this class creates an instance of * {@link net.sf.cscc.ClientConnection} and adds it to the * {@link net.sf.cscc.ClientConnectionRegistry} object. * * @author Silvio Meier (SM) * @copyright Silvio Meier, Tobias Reinhard, 2003 * @history 2003-05-09 SM First Version * 2003-05-14 SM working on additional methods and events, * correction of some comments. * 2003-05-15 SM methods for setting and getting port number * 2003-05-16 SM bug removed (null pointer when connection * has broken) / Completion of method comments. * 2003-05-17 SM Comments corrected * 2003-05-20 TR Comments corrected * 2003-06-26 SM Some null pointer exception occurred. * Listener Thread is now only started if no * exception occurs during the opening of the * listening socket if connection was terminiated * unexpectedly. Now this bug is removed. * 2004-12-13 SM Some cleaning of unused Elements. * 2006-11-02 SM Comments revised. * @version $Date: 2007/07/01 17:04:06 $, $Author: reode_orm $, $Revision: 1.1 $ * */class ConnectionPortListener extends Thread { /** * This variable is used for generating new ClientConnection ids * by incrementing an integer counter by one, every time when a * new ClientConnection object is instantiated. */ protected static int currentConnectionID = 0; /** * This is the socket listening for client connection requests. */ private ServerSocket serverSocket = null; /** * The port number which is used for listening for the client connections. */ private int port = 0; /** * This flag is true if the thread is listening on a given TCP/IP * port number. In this case the {@link #run()} method is processing * the requests for new connection from the communication interface. */ private boolean listening; /** * This instance variable holds a reference to the * {@link net.sf.cscc.ClientConnectionRegistry} object at the server * side. This object is used to manage all currently open connections * to clients. */ private ClientConnectionRegistry connectionRegistry; /** * Creates a new network listener which listens at a specific * TCP/IP port. The listening thread is started by calling the * method {@link #beginListening()}. * @pre connectionRegistry != null * @post true * @param portNumber Number of the TCP/IP port to listen for new * connection requests of the client. */ public ConnectionPortListener(int portNumber, ClientConnectionRegistry connectionRegistry) { this.port = portNumber; this.connectionRegistry = connectionRegistry; } /** * This method listens for new communication requests. * If a request arrives, the method creates a new * {@link net.sf.cscc.ClientConnection} object * which handles the communication between the server and the client. * The connection is registered at the * {@link net.sf.cscc.ClientConnectionRegistry}. * @pre true * @post listening == false * @obligation This method may not be called directly. It will be * called by the thread which starts up the listening process. */ public void run() { listening = true; while (listening) { try { // wait (blocked) for connection and if a new connection is detected... Socket newSocket = serverSocket.accept(); // generate a unique ID for the client connection Integer i = new Integer(currentConnectionID); // create a new client connection ClientConnection newClient = new ClientConnection(i, newSocket, connectionRegistry); // set the id to the client connection newClient.setClientID(i); // register connection connectionRegistry.registerConnection(i, newClient); // notification about new client connectionRegistry.notifyCommunicationEventObservers( new CommunicationEvent(CommunicationEvent.CONNECTION_ESTABLISHED, i)); // increase Id currentConnectionID++; } catch (IOException e) { // do nothing finish thread } } } /** * This method returns true, if there is currently a listening thread for * connection requests. * @pre true * @post true * @return Returns true if currently, there is a listening thread listening * for client connection requests. */ public boolean isListening() { return listening; } /** * Starts the listening thread by creating a new server socket listening * for new connections. * @pre listening == false * @post listening == true && serverSocket != null * @throws net.sf.cscc.CsccException if listener can not be * correctly started. */ public void beginListening() { try { // notify communication observers about the start of the port listening connectionRegistry.notifyCommunicationEventObservers( new CommunicationEvent(CommunicationEvent.CONNECTION_START_LISTENING)); serverSocket = new ServerSocket(port); // starts the listening thread this.start(); } catch (IOException e) { throw new CsccException(e.getMessage()); } } /** * This method stops the listening thread. * * @pre true * @post listening == false && serverSocket == null * @throws net.sf.cscc.CsccException if listener is not * correctly stopped. */ public void stopListening() { listening = false; try { if (serverSocket != null) serverSocket.close(); //Thread.yield(); connectionRegistry.notifyCommunicationEventObservers( new CommunicationEvent(CommunicationEvent.CONNECTION_STOP_LISTENING)); serverSocket = null; } catch (IOException e) { throw new CsccException(e.getMessage()); } } /** * Sets the port number for the connection. The set port number will be * used the next time when {@link #beginListening()} is called. * @pre port >= 0 * @param portNumber Number to use as port */ public void setPortNumber(int portNumber) { this.port = portNumber; } /** * Returns the currently set port number. * * @pre true * @post true * * @return Number of the TCP/IP port used for connections between client and * server. */ public int getPortNumber() { return port; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -