Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.search.control.protocol.NetworkCommunicationPeer.java

Here you can find all the information about the file org.dgpf.search.control.protocol.NetworkCommunicationPeer.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Mirko Dietrich
 * 
 * E-Mail           : mirkodietrich@hrz.uni-kassel.de
 * Creation Date    : Jul 4, 2006 5:43:37 PM
 * Original Filename: org.dgpf.search.control.protocol.ICommunicationPeer.java
 * Version          : 1.0
 * Last modification: Jul 4, 2006
 * Last modified by : Mirko Dietrich
 * 
 * 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.dgpf.search.control.protocol;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.net.Socket;

import org.dgpf.search.api.events.SearchUpdateEvent;
import org.sfc.events.IEventListener;
import org.sfc.events.IEventSource;
import org.sfc.events.SfcEvent;

/**
 * This class is a implementation of the interface
 * <code>ICommunicationPeer</code> and provides the possibility to
 * communicate over an TCP network channel. The communication is event
 * driven. Additionally the communication is buffered.
 * 
 * @author Mirko Dietrich
 */

public class NetworkCommunicationPeer implements ICommunicationPeer
  {

  /**
   * Default DGPF port.
   */

  public static final int              DEFAULT_PORT            = 6785;

  /**
   * Network <code>Socket</code>.
   */

  private Socket                       m_s;

  /**
   * The <code>IEventSource</code> that feeds this class with messages.
   */

  private IEventSource                 m_es;

  /**
   * The <code>IEventListener</code> that receives messages from this
   * class.
   */

  private IEventListener               m_el;

  /**
   * The output stream.
   */

  private ObjectOutputStream           m_oos;

  /**
   * Thread recieving <code>Events</code> and saving them to the
   * <code>ProtCircularBuffer</code>.
   */

  private ReceivingThread              m_rect;

  /**
   * Thread reading from the <code>ProtCircularBuffer</code> and
   * delivering the <code>Events</code> to the Listener.
   */

  private ReadingThread                m_readt;

  /**
   * The buffer where recieved <code>Events</code> are stored.
   */

  private ProtCircularBuffer<SfcEvent> m_cb;

  /**
   * Set to <code>true<code> if network session was initiated gracefully.
   */

  private boolean                      m_connectionEstablished = false;

  /**
   * Create a new <code>ICommunicationPeer</code>.
   * 
   * @param p_es
   *          The <code>IEventSource</code> that feeds this class with
   *          messages.
   * @param p_el
   *          The <code>IEventListener</code> that receives events from
   *          this class.
   * @param p_s
   *          The network <code>Socket</code>.
   * @param p_bs
   *          is the amount of <code>SfcEvents</code> that can be
   *          buffered.
   */

  public NetworkCommunicationPeer(final IEventSource p_es,
      final IEventListener p_el, final Socket p_s, final int p_bs)
    {
    this.m_es = p_es;
    this.m_el = p_el;
    this.m_s = p_s;
    this.m_cb = new ProtCircularBuffer<SfcEvent>(SfcEvent.class, p_bs);
    try
      {
      this.m_oos = new ObjectOutputStream(p_s.getOutputStream());
      ObjectInputStream l_ois = new ObjectInputStream(this.m_s
          .getInputStream());
      this.m_readt = new ReadingThread(thisthis.m_cb);
      this.m_readt.start();
      this.m_rect = new ReceivingThread(this, l_ois, this.m_cb);
      this.m_rect.start();

      // Try to initiate network session.
      sendCmd(Commands.INIT);
      synchronized(this)
        {
        try
          {
          this.wait();
          }
        catch(InterruptedException ie)
          {
          this.m_rect.interrupt();
          this.m_rect.close();
          return;
          }
        }
      }
    catch(StreamCorruptedException sce)
      {
      this.m_rect.interrupt();
      return;
      }
    catch(IOException e)
      {
      e.printStackTrace();
      }
    if(this.m_es != null)
      this.m_es.add_listener(this);
    this.m_connectionEstablished = true;
    }

  /**
   * Sends a command.
   * 
   * @param cmd
   *          The command to send.
   * @throws IOException
   *           if command could not be sent.
   * @see org.dgpf.search.control.protocol.Commands
   */

  void sendCmd(short cmd) throws IOException
    {
    this.m_oos.writeShort(cmd);
    this.m_oos.flush();
    }

  /**
   * Send an <code>Object</code> over a network channel between two
   * <code>CommunicationPeers</code>.
   * 
   * @param p_obj
   *          the <code>Object</code> to transmit.
   * @throws IOException
   *           if sending of <code>Object</code> failed.
   */

  public void sendObject(Object p_obj) throws IOException
    {
    this.sendCmd(Commands.SEND_OBJECT);
    this.m_oos.writeUnshared(p_obj);
    this.m_oos.flush();
    }

  /**
   * Receives events and sends them to the other
   * <code>CommunicationPeers</code>.
   * 
   * @param p_event
   *          The event to send.
   */

  public void receive(SfcEvent p_event)
    {
    if(p_event instanceof SearchUpdateEvent)
      {
      try
        {
        sendObject(p_event);
        }
      catch(IOException e)
        {
        this.m_es.remove_listener(this);
        }
      }
    else
      if(p_event instanceof RunningStatusEvent)
        {

        try
          {
          sendObject(p_event);
          }
        catch(IOException e)
          {
          this.m_es.remove_listener(this);
          }
        }
    }

  /**
   * Determine string representation of remote peer.
   * 
   * @return string representation of remote peer.
   */

  public String getRemotePeerName()
    {
    return this.m_s.getRemoteSocketAddress().toString();
    }

  /**
   * Returns the network connection's current status.
   * 
   * @return connection's current status. <code>true</code> if connection
   *         is established, otherwise <code>false</code>.
   * @see #m_connectionEstablished
   */

  public boolean isConnectionEstablished()
    {
    return this.m_connectionEstablished;
    }

  /**
   * Getter for <code>el</code>.
   * 
   * @return Returns <code>IEventListener</code>.
   */

  public final IEventListener getEl()
    {
    return this.m_el;
    }

  /**
   * Closes connection.
   */

  public void close()
    {
    /* Stop ReceivingThread */
    Thread l_moribund = this.m_rect;
    this.m_rect = null;
    if(l_moribund != null)
      l_moribund.interrupt();
    try
      {
      this.m_oos.close();
      this.m_s.close();
      }
    catch(IOException e)
      {
      e.printStackTrace();
      }
    }
  }

File Information:

file name:NetworkCommunicationPeer.java
package:org.dgpf.search.control.protocol
qualified name:org.dgpf.search.control.protocol.NetworkCommunicationPeer.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/search/control/protocol/NetworkCommunicationPeer.java
size:7.097 KB (7268 B)
uploaded: 2015-07-22 04:11:00 GMT+0000
last update: 2006-09-24 23:47:04 GMT+0000
last access: 2017-11-19 03:03:22 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 2017-11-19 03:03:22 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo