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

📄 clientconnectionmanager.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;/** *  This is one of the two main classes ({@link net.sf.cscc.ServerConnectionManager} *  and {@link net.sf.cscc.ClientConnectionManager}) of the CSCC library *  which helps to connect and to exchange the so-called data events between a *  remote server and a client connected over a particular TCP/IP port. *  A {@link net.sf.cscc.DataEvent} is an event which is the carrier for *  particular data which is sent from the client to server or from server to *  the client, respectively.<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  either be got *  by a pull (the interessted clients have to go and look for the incoming data) *  or a push (the interessted clients will be notified over a observer 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-16 SM Null pointer bug removed (when reading port *                      number with closed connection) *                      2003-05-20 TR Comments corrected *                      2006-10-25 SM Comments revised. *	@version            $Date: 2007/07/01 17:04:06 $, $Author: reode_orm $, $Revision: 1.1 $ */public class ClientConnectionManager {    /**     * An observable object is a helper for notifying all observing     * clients when a  DataEvent occurs.     */    private DataEventObservable dataObservable;    /**     * An observable object is a helper for notifying all communication     * event observers when a communication event occurs.     *     */    private CommunicationEventObservable communicationObservable;    /**     *  The port number used to connect to the server.     */    private int portNumber;    /**     * This is a reference to a helper class which is responsible for     * sending all the enqueued out-going events and/or to receive all     * the enqueued incoming messages, respectively.     *     */    private ServerConnection serverConnection;    /**     *   The constructor initializes this object. It prepares the     *   ClientConnectionManager object. A conection is not opened     *   when initializeing the connection manager, this has to be done     *   by  calling the method {@link #open(String serverName)}.     *   The The client connection manager constructor take a port number     *   as argument which denotes on which port the connection is opened to     *   the server.     *     *   @pre portNumber >= 0     *   @post dataObservable != null && communicationObservable != null     *   @param portNumber Number of the TCP/IP port where the     *          connection should be established.     */    public ClientConnectionManager(int portNumber) {        // store the portnumber        this.portNumber = portNumber;        // observable object notifies observers as soon as something change        dataObservable = new DataEventObservable();        // observable object for the communication events.        communicationObservable = new CommunicationEventObservable();    }    /**     *   This method opens a new connection to a specified server.     *   The destination server is either specified by the ip address     *   or by its internet DNS name.     *     *   @pre serverName != null     *   @post serverConnection != null     *   @param serverName Name of the server to which the connection     *   should be opened.     */    public boolean open(String serverName) {        //create a server connection object        serverConnection = new ServerConnection(portNumber, dataObservable, communicationObservable);        // open the connection to a specific server        return serverConnection.open(serverName);    }    /**     *   Closes the connection to the server.     *     *   @pre true     *   @post !(isConnectionOpen())     */    public  void close() {        if (serverConnection != null) {            serverConnection.close();            serverConnection = null;        }    }    /**     *   This method returns true if the connection to the server is currently     *   open.     *     *   @pre true     *   @post true     *   @return Returns true, if the server connection is currently open.     */    public boolean isConnectionOpen() {        return (serverConnection != null) && (serverConnection.isOpen());    }    /**     *   This method sends a {@link net.sf.cscc.DataEvent}     *   object from the client to the server. This happens asynchronous,     *   i.e. the method returns immediately.     *   @pre de != null     *   @post true     *   @param de DataEvent object which should be sent to the server.     */    public  void sendDataEvent(DataEvent de) {        serverConnection.enqueueDataEvent(de);    }    /**     *   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 a second time results in returning     *   the same event. This happens 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 serverConnection.getDataEvent();    }   /**     *   This method is used to remove manually the oldest event in the     *   incoming event queue.     *     *   @pre true     *   @post true     */    public  void  removeDataEvent() {        serverConnection.removeDataEvent();    }    /**     *   This method returns the oldest DataEvent object in the incoming event     *   queue. It removes the element after returning it from     *   the event queue.     *     *   @pre true     *   @post true     *   @return Returns the oldest DataEvent object and removes it from     *           the event queue.     */    public  DataEvent receiveDataEvent() {        return  serverConnection.dequeueDataEvent();    }    /**     *   This method returns true, if any DataEvent objects are in the incoming     *   data event queue.     *     *   @pre true     *   @post true     *   @return Returns true, if there are events in the event queue.     */    public boolean hasDataEvents() {        return serverConnection.hasDataEvents();    }    /**     *   This method unregisters an observer from the data event observer list.     *   The observer list is used for the push model, i.e. the model, where     *   observing components are notified about new DataEvent objects in the     *   queue.     *     *   @pre   o != null     *   @post  true     *   @param o DataEventBaseObserver which should be removed from the observer     *          list.     */    public  void removeObserver(DataEventBaseObserver o) {        dataObservable.removeDataEventObserver(o);    }    /**     *   An communication event 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) {        communicationObservable.removeCommunicationEventObserver(o);    }    /**     *   This method adds an observer to the data event observer list. The     *   observer list is used for the push model, i.e. the model, which     *   notifies the observing components about new DataEvent objects     *   in the queue.     *   @pre o != null     *   @post true     *   @param o DataEventBaseObserver object which should be added to the     *          observer list.     */    public  void addObserver(DataEventBaseObserver o) {        dataObservable.addDataObserver(o);    }    /**     *   An observer which wants to be notified about communication events     *   can register itself at the communication observer list by this method.     *   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 false     *   @param o CommunicationEventObserver which wants to be notified if a new data     *          event from a client is received.     */    public  void addObserver(CommunicationEventObserver o) {        communicationObservable.addCommunicationEventObserver(o);    }    /**     *   Sets the port number for a connection. The newly set port number will     *   be used as soon a new connection to the server is established.     *     *   @pre portNumber >= 0     *   @post this.portNubmer == portNumber     *   @param portNumber The TCP/IP port number which is used     *          to connect the next time to the server.     */    public void setPortNumber(int portNumber) {        this.portNumber = portNumber;        if (serverConnection != null) {            serverConnection.setPortNumber(portNumber);        }    }    /**     *   This method returns the currently set port number. This port number     *   is used to connect to the server.     *     *   @pre true     *   @post return == this.portNumber     *   @return Returns the currently set TCP/IP port number which is used to     *           connect to the server     */    public int getPortNumber() {        return portNumber;    }    /**     *   This method closes the connection to the server.     *     *   @pre true     *   @post true     *   @deprecated Don't use this method anymore,     *               use {@link #close()} instead!     */    public void closeConnection() {        serverConnection.close();    }}

⌨️ 快捷键说明

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