📄 serverconnectionmanager.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.util.Enumeration;/** * This is the server class of the two main classes * {@link net.sf.cscc.ServerConnectionManager} and * {@link net.sf.cscc.ClientConnectionManager} in * this client server communication component. * This class provides the functionality to connect and to exchange * so-called data events between a remote server and a client over a specific * TCP/IP port. A {@link net.sf.cscc.DataEvent} is an event which * sends specific data from a client to server, respectively from a server * to the client.<br> * For doing so, a keep alive connection between the client and the server is kept. * The incoming messages are enqueued in a queue and can be either get by * a pull (the interested clients have to go and look for the data) or a push * (the interested clients will be notified over a oberver pattern) mechanism. * * @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 * / Completion of method comments. * 2003-05-17 SM Comments corrected * 2003-05-20 TR Comments corrected * 2003-06-23 SM Dispose method corrected, stops now the * port listening thread. * 2003-06-24 SM Some corrections of comments. * 2004-12-13 SM Some cleaning of unused things. * 2006-11-28 SM Comments revised. * @version $Date: 2007/07/01 17:04:05 $, $Author: reode_orm $, $Revision: 1.1 $ */public class ServerConnectionManager { /** * The listener waits for connection requests from clients that want to subscribe. * After the subscriber request of a client, it then creates a * {@link net.sf.cscc.ClientConnection} object which handles the * communication over the network. */ private ConnectionPortListener listener; /** * Registry for managing all the connections to the server. */ private ClientConnectionRegistry connectionRegistry = null; /** * This constructor initializes the server connection manager. * * @pre portNumber >= 0 * @post outGoingMessages != null && inComingMessage != null && * connectionRegistry != null && listener != null * @param portNumber TCP/IP port where to listen for connection requests */ public ServerConnectionManager(int portNumber) { connectionRegistry = new ClientConnectionRegistry(); listener = new ConnectionPortListener(portNumber, connectionRegistry); } /** * This method stops the listener, listening for connection requests. * * @pre true * @post true */ public void stopListening() { listener.stopListening(); } /** * This method sends a DataEvent object to a specific client having a * given clientId. * * @pre de != null && clientId != null * @post true * @param de DataEvent object which should be sent to the client. * @param clientId Id of the client which should receive the data event. */ public void sendDataEvent(DataEvent de, Object clientId) { ClientConnection cc = (ClientConnection)connectionRegistry.findConnection(clientId); cc.enqueueDataEvent(de); } /** * This method returns the oldest DataEvent object in the queue. After * returning it, the DataEvent object is removed from the queue. * * @pre true * @post true * @return Returns a DataEvent from the incoming event queue. */ public DataEvent receiveDataEvent() { return connectionRegistry.dequeueDataEvent(); } /** * This method returns the oldest DataEvent object received * from the server. The DataEvent object is left in the event queue * that means calling this method result in returning the same * event when calling this method a second time, if, and only if the * event was not removed in the meantime. * * @pre true * @post true * @return Returns the oldest DataEvent object in the event queue without * removing it from the queue. */ public DataEvent getDataEvent() { return connectionRegistry.getDataEvent(); } /** * This method is used to remove manually the oldest event in the * incoming event queue. * * @pre true * @post true */ public void removeDataEvent() { connectionRegistry.removeDataEvent(); } /** * This method returns true, if there are data events in the incoming event * queue. * * @pre true * @post true * @return Returns true, if there is one or more data events in the incoming * event queue. */ public boolean hasDataEvents() { return connectionRegistry.hasDataEvents(); } /** * This method broadcasts an event to all connected clients. * * @pre true * @post true * @param de DataEvent object which should be broadcasted to all clients. */ public void sendBroadCastEvent(DataEvent de) { Enumeration e = connectionRegistry.clientConnections(); while (e.hasMoreElements()) { ClientConnection cc = (ClientConnection)e.nextElement(); cc.enqueueDataEvent(de); } } /** * An observer which wants to be notified about new messages from clients * can register itself at the observer list. It is notified * automatically about the incoming data events (push model). The observer * must implement the interface {@link net.sf.cscc.DataEventObserver} * or {@link net.sf.cscc.DataEventReceivingObserver}, * which are subinterfaces of {@link net.sf.cscc.DataEventBaseObserver}. * * @pre o != null * @post true * @param o DataEventBaseObserver which wants to be notified if a new data * event from a client is received. */ public void addObserver(DataEventBaseObserver o) { connectionRegistry.addDataObserver(o); } /** * An observer which wants to be notified about communication events * can register itself at the communication observer list. It is notified * automatically about the occurring communication events. A communication * event observer must implement the interface * {@link net.sf.cscc.CommunicationEventObserver}. * @pre o != null * @post true * @param o CommunicationEventObserver which wants to be notified if a * new data event from a client is received. */ public void addObserver(CommunicationEventObserver o) { connectionRegistry.addCommunicationObserver(o); } /** * A communication observer which is already registered at the observer * list, can be unregisterd with this method. If the given observer is * not registered, nothing happens. * * @pre o != null * @post true * @param o CommunicationEventObserver which should be deleted from the * observer list. */ public void removeObserver(CommunicationEventObserver o) { connectionRegistry.removeCommunicationObserver(o); } /** * A data observer which is already registered at the observer list * can be unregisterd with this method. If the given observer is not * registered, nothing happens. * * @pre o != null * @post true * @param o DataEventBaseObserver which should be deleted from the * observer list. */ public void removeObserver(DataEventBaseObserver o) { connectionRegistry.removeDataObserver(o); } /** * This method is used to start the listening process of the server. The * listening process listens at the given TCP/IP {@link #getPortNumber()} for * connection requests of the client. * * @pre true * @post true */ public void beginListening() { listener.beginListening(); } /** * Sets the port number for the connection. The set port number will be active * the next time when {@link #beginListening()} is called. * * @pre portNumber >= 0 * @param portNumber Number to use as port */ public void setPortNumber(int portNumber){ listener.setPortNumber(portNumber); } /** * Gets 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 listener.getPortNumber(); } /** * Releases all occupied ressources by this component, i.e. all the * connections are closed, the listening on the TCP/IP port is stopped and * the internally used threads are stopped. This method should be called * as soon as the ServerConnectionManager instance is not used any more. * * @pre true * @post Test * @since Component version 1.1 / Class Version 1.11 */ public void dispose() { closeAllConnections(); connectionRegistry.stopDataDispatcher(); stopListening(); } /** * Closes a connection to a client. The connection is uniquely identified * by a given clientId. If the connection belonging to the clientId * is not found, nothing happens. * @pre clientId != null * @post true * @param clientId Identification object, which identifies the client * connection uniquely. */ public void closeConnection(Object clientId) { ClientConnection cc = connectionRegistry.findConnection(clientId); if (cc != null) { cc.close(); } } /** * Closes all (currently active, ie. open) connections to the currently * subscribed clients. * * @pre true * @post true */ public void closeAllConnections() { Enumeration e = connectionRegistry.clientConnections(); while (e.hasMoreElements()) { ClientConnection cc = (ClientConnection)e.nextElement(); cc.close(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -