Logo
Distributed Genetic Programming Framework
print print

File org.sfc.net.p2p.External.java

Here you can find all the information about the file org.sfc.net.p2p.External.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Thomas Weise
 *
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2006-02-22 06:58:14
 * Original Filename: org.sfc.net.p2p.External.java
 * Version          : 3.0.0
 * Last modification: 2006-04-10
 *                by: Thomas Weise
 *
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */


package org.sfc.net.p2p;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.sfc.events.ErrorEvent;
import org.sfc.events.EventPropagator;
import org.sfc.net.Reconnection;

/**
 * This class represents an external peer that can be contacted. It holds
 * the socket address of the server part of the peer and can contact
 * that server whenever needed. If it fails, it is enqueued into the
 * pending externals thread's queue.
 *
 * @author Thomas Weise
 */

final class External  extends Reconnection<Socket>
  {
/**
 * The socket address representing the peer.
 */

          final InetSocketAddress   m_address ;
/**
 * The owning peer to peer component.
 */

  private final PeerToPeer          m_owner   ;
/**
 * The next time this connection might be put into the active list.
 */

                long                m_next_time ;
/**
 * The pending list's concatenation information.
 */

                External            m_next ;


/**
 * Create a representation for an external peer.
 * @param p_address The address representing the peer.
 * @param p_owner   The owning peer node.
 */

  External  (final InetSocketAddress p_address,
             final PeerToPeer        p_owner)
    {
    super();
    this.m_address = p_address;
    this.m_owner   = p_owner;
    }


/**
 * This method must be implemented by sub-classes. It should establish the
 * connection and may fail with an arbitrary error.
 * @return  The connection-representing object if successful.
 * @throws  Throwable An arbitrary error if the connection could not be
 *                    established successfully.
 */

  @Override
  protected final Socket  do_connect () throws Throwable
    {
    Socket        l_s, l_d;
    OutputStream  l_os;
    int           l_p;

    l_s = new Socket();
    l_s.connect(this.m_address);

    try
      {
      l_os = l_s.getOutputStream();
      l_os.write(PeerToPeer.CMD_SOCKET);
      l_p = this.m_owner.get_port();
      l_os.write( (l_p >>> 8) & 0xff );
      l_os.write( (l_p >>> 0) & 0xff );
      l_os.flush();

      if(l_s.getInputStream().read() == 1)
        {
        l_d = l_s;
        l_s = null;
        return l_d;
        }

      }
    finally
      {
      if(l_s != null) l_s.close();
      }

    return null;
    }

/**
 * This method is called when the connection failed.
 * @param p_error         The error raised because of the connection
 *                        failure.
 * @param p_should_retry  <code>true</code> if and only if we should try to
 *                        reconnect, <code>false</code> if we have already
 *                        tried for a long time and now should abort this
 *                        useless attempt.
 * @param p_time_to_wait  The time to wait until the next connection retry
 *                        should be performed.
 */

  @Override
  protected final     void  on_error  (final Throwable  p_error,
                                       final boolean    p_should_retry,
                                       final long       p_time_to_wait)
    {
    EventPropagator l_pep;

    if(p_error != null)
      {
      l_pep = this.m_owner.do_get_events();
      if(l_pep != null)
        {
        l_pep.receive(new ErrorEvent(this.m_address, p_error));
        }
      }

    if(this.m_owner.remove_external(this))
      {
      if(p_should_retry)
        {
        this.m_next_time = (System.currentTimeMillis() + p_time_to_wait);
        this.m_owner.m_pending.enqueue(this);
        }
      else
        {
        this.m_owner.m_repair++;
        }
      }
    }

/**
 * Send the shutdown message.
 */

  final void  shutdown  ()
    {
    Socket l_s;
    try
      {
      l_s = new Socket();
      l_s.connect(this.m_address);
      l_s.getOutputStream().write(PeerToPeer.CMD_SHUTDOWN);
      l_s.close();
      }
    catch(Throwable l_t)
      {
      //
      }
    }
  }

File Information:

file name:External.java
package:org.sfc.net.p2p
qualified name:org.sfc.net.p2p.External.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/net/p2p/External.java
size:5.027 KB (5148 B)
uploaded: 2018-01-07 12:03:36 GMT+0000
last update: 2006-08-27 10:40:01 GMT+0000
last access: 2018-04-21 09:53:03 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2018-01-07 12:03:34 GMT+0000 served at 2018-04-21 09:53:03 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo