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

📄 clientconnectionregistry.java

📁 自己写的一个聊天的小程序 请多多指教
💻 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;import java.util.Hashtable;/** * This class holds a list of all client connections which are currently * open to the server. It also provides several data structures for * the server, e.g. the incoming event (message) queue * which is based on the class {@link net.sf.cscc.Queue} and the * {@link net.sf.cscc.DataEventObservable} * class which is responsible for notifying observers about data events * that occurred. * * *	@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-17 SM Comments corrected *                      2003-06-04 SM Now the incoming data events are dispatched *                      by the dispatcher {@link net.sf.cscc.DataEvent}. *                      2003-06-10 SM Additional description *                      2006-10-25 SM Comments revised. *	@version            $Date: 2007/07/01 17:04:05 $, $Author: reode_orm $, $Revision: 1.1 $ */class ClientConnectionRegistry {    /**     * This data event obervable informs observers, if data events occurred,     * ie. if a data event is enqueued into the incoming event queue of the     * server.     */    private DataEventObservable dataObservable;    /**     * This obervable informs observers when communication events occurred.     */    private CommunicationEventObservable communicationObservable;    /**     * Dispatches the incoming data events to the interested     * observers.     */    private DataDispatcher dataDispatcher;    /**     *  Queue to store the incoming events.     */    private Queue inComingMessages;    /**     *  Stores all the currently active     *  {@link net.sf.cscc.ClientConnection}s.     */    protected Hashtable connections;    /**     *   Initializes the data structures of this registry.     *     *   @pre true     *   @post connections != null && inComingMessages != null     *         && dataObservable != null && communicationObservable != null     */    public ClientConnectionRegistry() {        connections = new Hashtable();        inComingMessages = new Queue();        dataObservable = new DataEventObservable();        dataDispatcher = new DataDispatcher(dataObservable, inComingMessages);        communicationObservable = new CommunicationEventObservable();    }    /**     *  Searches for a specific {@link net.sf.cscc.ClientConnection}.     *  The search is done by a given unique client identifier which is     *  provided by the server.     *     *   @pre clientId != null     *   @post true     *   @param clientId A client identification object which is used as     *          key to search for the ClientConnection object.     *   @return Returns the associated {@link ClientConnection} object.     */    public synchronized  ClientConnection findConnection(Object clientId) {        return (ClientConnection)connections.get(clientId);    }    /**     *   This method registers a newly instantiated connection associated with     *   a given identifier (key) which is used to identify the connection.     *     *   @pre clientId != null && connection != null     *   @post true     *   @param clientId An identification object for the ClientConnection.     *          This Id is provided by the {@link ConnectionPortListener} object.     *   @param connection The connection that has to be registered.     */    public synchronized void registerConnection(Object clientId, ClientConnection connection) {        connections.put(clientId, connection);    }    /**     *   This method unregisters a {@link net.sf.cscc.ClientConnection}     *   which is identified by the given clientId parameter.     *   @pre clientId != null     *   @post true     *   @param clientId Identifier object which identifies a     *          {@link net.sf.cscc.ClientConnection}     *          object uniquely.     */    public synchronized  void unregisterConnection(Object clientId) {        connections.remove(clientId);    }    /**     *   Returns all the registered client connections of the server.     *     *   @pre true     *   @post return == connections.elements()     *   @return Returns an {@link java.util.Enumeration} object     *           of all register {@link net.sf.cscc.ClientConnection} objects.     */    public Enumeration clientConnections() {        return connections.elements();    }     /**     *   This method returns the oldest DataEvent object received     *   from the communication partner. 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. This happens if, and only if the     *   event was not removed in the meantime by another thread.     *   @pre true     *   @post true     *   @return Returns the oldest DataEvent object in the event queue without     *           removing it from the queue.     */    public  DataEvent getDataEvent() {        return inComingMessages.getDataEvent();    }    /**     *   This method is used to remove manually the oldest event in the     *   incoming data event queue.     *   @pre true     *   @post true     */    public  void  removeDataEvent() {        inComingMessages.removeDataEvent();    }    /**     *   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 dequeueDataEvent() {        return inComingMessages.dequeueDataEvent();    }    /**     *   This method is internally used for enqueuing a new data event     *   in the incoming event queue. It also notifies all the observers     *   of the event queue and, if the data event was directly delivered     *   to all clients, the data event will be dequeued automatically from     *   the event queue.     *   @pre de != null     *   @post true     */    void enqueueIncomingDataEvent(DataEvent de) {        synchronized (inComingMessages) {            inComingMessages.enqueueDataEvent(de);            dataDispatcher.notifyDispatch();        }    }    /**     *   This method stops the data dispatcher mechanism.     *   @pre true     *   @post true     */    void stopDataDispatcher() {        dataDispatcher.stopDispatching();    }    /**     *   This method returns true, if data events are available in the incoming data event     *   queue. This method can be used if a pull model     *   is used, i.e. interested clients (in the sense of using components) of     *   the server will (periodically) check for new 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 inComingMessages.hasDataEvents();    }    /**     *   An observer which wants to be notified as soon as new messages from the     *   communication partner arrives, can register itself at the data event     *   observer list. This causes an automatic notification about the incoming     *   data event (push model).<br>     *   The observer should implement the interface     *   {@link net.sf.cscc.DataEventObserver} or {@link net.sf.cscc.DataEventReceivingObserver},     *   which are subinterfaces of {@link net.sf.cscc.DataEventBaseObserver}. This     *   method can be used when using a push model, i.e. if the interested clients of the server     *   want to be notified automatically, if new data event are in the queue.     *   @pre o != null     *   @post false     *   @param o DataEventBaseObserver which wants to be notified if a new data     *          event from a client is received.     */    public  void addDataObserver(DataEventBaseObserver o) {        dataObservable.addDataObserver(o);    }    /**     *   An observer which wants to be notified when communication events occur,     *   can register itself at the observer list. It is notified     *   automatically about the communication event (push model). The 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     *          communication event occurred.     */    public  void addCommunicationObserver(CommunicationEventObserver o) {        communicationObservable.addCommunicationEventObserver(o);    }    /**     *   An observer which is already registered at the data event 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 removeDataObserver(DataEventBaseObserver o) {        dataObservable.removeDataEventObserver(o);    }    /**     *   An observer which is already registered at the communication event     *   observer list, can be deregisterd with this method. If the given     *   observer is not registered, nothing happens.     *   @pre o != null     *   @post true     *   @param o DataEvetnBaseObserver which wants to be deleted from the     *          observer list.     */    public  void removeCommunicationObserver(CommunicationEventObserver o) {        communicationObservable.removeCommunicationEventObserver(o);    }    /**     *  This method is used component internally for accessing the     *  incoming event queue {@link net.sf.cscc.DataEventObservable} object     *  which notifies interested clients of the server about new     *  data events in the event queue.     *  @pre true     *  @post true     *  @return Returns the {@link net.sf.cscc.DataEventObservable} object     *          of the server.     */    DataEventObservable getDataEventObservable() {        return dataObservable;    }    /**     * Notifies all the communication event observers about a comminication     * event that occurred.     * @pre ce != null     * @post true     * @param ce CommunicationEvent which is propagated to all communication     *        event observers.     *     */    public void notifyCommunicationEventObservers(CommunicationEvent ce) {        communicationObservable.notifyCommunicationEventObservers(ce);    }    /**     * This method returns the incoming message queue. It is used component     * internally.     * @pre true     * @post true     * @return Returns the incoming data event queue of the server.     */    Queue getIncomingQueue() {        return inComingMessages;    }}

⌨️ 快捷键说明

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