Logo
Distributed Genetic Programming Framework
print print

File org.sfc.net.cs.ServerThread.java

Here you can find all the information about the file org.sfc.net.cs.ServerThread.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-04-11 13:43:49
 * Original Filename: org.sfc.net.ServerThread.java
 * Version          : 1.0.1
 * Last modification: 2006-06-08
 *                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.cs;

import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

import org.sfc.events.ErrorEvent;
import org.sfc.events.IEventListener;
import org.sfc.net.Reconnection;
import org.sfc.parallel.SfcThread;
import org.sfc.parallel.SfcThreadGroup;


/**
 * A server thread acts as a server and accepts connections on a specified
 * port.
 *
 * @author Thomas Weise
 */

public final  class ServerThread  extends SfcThread
  {
/**
 * The server sockt to be used.
 */

                ServerSocket  m_socket  ;
/**
 * The server connection manager.
 */

  private final ServerConnex  m_connex  ; 
/**
 * The owning server component.
 */

          final Server        m_owner   ;
  
/**
 * Create a new server thread, that is, a thread that listens for
 * connections at a server socket.
 * @param p_owner The owning server component.
 * @param p_group The sfc-thread group owning this thread. This can be
 *                <code>null</code>.
 * @param p_events  The event listener to be used internally.
 */

  protected ServerThread  (final Server           p_owner,
                           final SfcThreadGroup   p_group,
                           final IEventListener  p_events)
    {
    super(p_group, p_events);
    this.m_owner  = p_owner;
    this.m_connex = new ServerConnex();
    }
  
/**
 * This method must be overriden to contain the thread's code. It will be
 * executed in a loop as long as it returns <code>true</code> and throws
 * <b>no</b> <code>Throwable</code>.
 * @return  <code>true</code> if this method should be executed again.
 */

  @Override
  protected final boolean do_run  ()
    {
    ServerSocket    l_s;
    IEventListener l_pep;
    Socket          l_a;

    l_s = this.m_connex.connect();
    if(l_s == null) return true;

    try
      {
      l_a = l_s.accept();

      if(l_a != null)
        {
        try
          {
          if(this.is_running())
            {
            if(this.m_owner.process_socket(l_a))
              {
              l_a = null;
              }
            }
          }
        catch(Throwable l_t2)
          {
          l_pep = this.m_owner.do_get_event_propagator();
          if(l_pep != null)
            {
            l_pep.receive(new ErrorEvent((l_a != null)
                    ? l_a.getRemoteSocketAddress() : this.m_owner, l_t2));
            }
          }
        finally
          {
          if(l_a != null)
            {
            this.m_owner.close_socket(l_a);
            }
          }
        }
      }
    catch(SocketTimeoutException l_ste)
      {
      return true;
      }
    catch(Throwable l_t)
      {
      l_pep = this.m_owner.do_get_event_propagator();
      if(l_pep != null)
        {
        l_pep.receive(new ErrorEvent(l_s.getLocalSocketAddress(), l_t));
        }
      this.close_socket();
      }

    return true;
    }

/**
 * Close the server socket.
 */

  private final void  close_socket  ()
    {
    IEventListener l_pep;

    if(this.m_socket != null)
      {
      try
        {
        this.m_socket.close();
        }
      catch(Throwable l_t)
        {
        l_pep = this.m_owner.do_get_event_propagator();
        if(l_pep != null)
          {
          l_pep.receive(new ErrorEvent(
                  "ServerThread close, port " + this.m_owner.m_port, l_t));
          }
        }
      finally
        {
        this.m_socket = null;
        }
      }
    }

/**
 * This method is called when the connection failed.
 * @param p_error         The error raised because of the connection
 *                        failure.
 * @param p_time_to_wait  The time to wait until the next connection retry
 *                        should be performed.
 */

  protected final void  on_error  (final Throwable  p_error,
                                   final long       p_time_to_wait)
    {
    IEventListener l_pep;

    if(p_error != null)
      {
      l_pep = this.m_owner.do_get_event_propagator();
      if(l_pep != null)
        {
        l_pep.receive(new ErrorEvent("ServerThread socket, port " +
                              this.m_owner.m_port, p_error));
        }
      }

    this.safe_sleep(p_time_to_wait);
    }

/**
 * Override this method to perform something when the thread dies.
 */

  @Override
  protected final void  after_termination ()
    {
    this.close_socket();
    }

/**
 * Obtain a string representation of this server part of a peer-to-peer
 * node.
 * @return A string representation of this server part of a peer-to-peer
 *         node.
 */

  @Override
  public  String  toString()
    {
    return "Server Thread of " + this.m_owner.toString();
    }

/**
 * The internal server connection class.
 *
 * @author Thomas Weise
 */

  private final class ServerConnex  extends Reconnection<ServerSocket>
    {

/**
 * 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 ServerSocket  do_connect () throws Throwable
      {
      ServerSocket  l_s;

      l_s = ServerThread.this.m_socket;
      if(l_s != null) return l_s;

      l_s = new ServerSocket(ServerThread.this.m_owner.m_port);
      try
        {
        l_s.setReuseAddress(true);
        }
      catch(Throwable l_tt)
        {
        //
        }
      try
        {
        l_s.setSoTimeout(500);        
        }
      catch(Throwable l_t)
        {
        l_s.close();
        throw l_t;
        }
      return  ServerThread.this.m_socket = l_s;
      }

/**
 * 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)
      {
      ServerThread.this.on_error(p_error, p_time_to_wait);
      }
    }
  }

File Information:

file name:ServerThread.java
package:org.sfc.net.cs
qualified name:org.sfc.net.cs.ServerThread.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/net/cs/ServerThread.java
size:7.755 KB (7942 B)
uploaded: 2015-07-22 04:11:12 GMT+0000
last update: 2006-08-27 10:38:09 GMT+0000
last access: 2018-01-22 00:08:01 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 2015-07-22 04:10:53 GMT+0000 served at 2018-01-22 00:08:01 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo