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

📄 reconnect.java

📁 这是一个用java和xml编写的流媒体服务器管理软件
💻 JAVA
字号:
/* * ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. 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. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY 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. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc.  For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * *//** * Title:        Reconnect.java<p> * Description:  see below<p> * Copyright:    Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.<p> * Company:      Vovida Networks<p> * @author * @version 1.0 */package vocal.comm;import java.net.Socket;import java.net.InetAddress;import java.io.OutputStreamWriter;import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;/** * A simple class which implements the runnable interface and attempts to * connect to a given host and port for a number of retries. <p> * This is intended to be run in its own thread once a connection to a server * has been lost in order to attempt to reconnect. Once the recconnect has * succeeded or has failed (exceeded number of retries), the appropriate * callback is called. */public class Reconnect implements Runnable{  private Transaction respondent;  private String hostName;  private int portNumber;  private static final int MAX_RECONNECT_ATTEMPTS = 30;  private static final int RECONNECT_DELAY = 3000;  private static final int SOCKET_TIMEOUT = 2000;  private int currentAttempt = 0;  private Socket socket;  private BufferedReader fromSocket;  private OutputStreamWriter toSocket;  private boolean reconnected = false;  /**   * @param resp a reference to a class which is to be notified of the result   * of the connect attempts.   * @param host a string giving the host name attempt to connect to   * @param port the port number to attempt to connect to   */  public Reconnect(Transaction resp, String host, int port)  {    respondent = resp;    hostName = host;    portNumber = port;  }  public void run()  {    while ((currentAttempt < MAX_RECONNECT_ATTEMPTS) && (!reconnected))    {      try      {        // make sure reconnect attempts should continue;        if (!respondent.okToReconnect())        {          return;        }        System.out.println("Reconnect attempt " + currentAttempt + " "                           + Thread.currentThread().toString());        socket = new Socket(InetAddress.getByName(hostName), portNumber);        // disable blocking io by setting a timeout on the        // socketInputStream.read operation        socket.setSoTimeout(SOCKET_TIMEOUT);        fromSocket =          new BufferedReader(new InputStreamReader(socket.getInputStream()));        toSocket = new OutputStreamWriter(socket.getOutputStream());        reconnected = true;        System.out.println("Reconnect successful. " + Thread.currentThread().toString());        respondent.reconnected();      }      catch (IOException ex)      {        try        {          Thread.sleep(RECONNECT_DELAY);          currentAttempt++;        }        catch (InterruptedException e)        {          // can't throw and exception from run          System.out.println("ERROR: -------- "                             + "Reconnect sleep was interrupted.");        }      }         // end catch ioException    }           // end while not reconnected    if (!reconnected)    {      respondent.reconnectFailed();    }  }     // end method run  /**   * Returns the socket on which the connection was established. <p>   * Only call this method after the reconnect was successful   * (the reconnected() method was called on the callback object).   */  public Socket getSocket()  {    return socket;  }  /**   * Returns the input stream for the socket on which the connection was   * established. <p>   * Only call this method after the reconnect was successfull   * (the reconnected() method was called on the callback object).   */  public BufferedReader getInputStream()  {    return fromSocket;  }  /**   * Returns the output stream for the socket on which the connection was   * established. <p>   * Only call this method after the reconnect was successfull   * (the reconnected() method was called on the callback object).   */  public OutputStreamWriter getOutputStream()  {    return toSocket;  }}       // end class reconnect

⌨️ 快捷键说明

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