<commit_before>/*
 * Owl Platform Sensor-Aggregator Library for Java
 * Copyright (C) 2012 <NAME> and the Owl Platform
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but 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.
 *  
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package com.owlplatform.sensor;

import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.owlplatform.common.SampleMessage;
import com.owlplatform.sensor.listeners.ConnectionListener;
import com.owlplatform.sensor.protocol.codecs.AggregatorSensorProtocolCodecFactory;
import com.owlplatform.sensor.protocol.messages.HandshakeMessage;

/**
 * A convenience class for interfacing with aggregators as a sensor/hub. Since
 * most sensors are implemented in C/C++, this class is primarily used by
 * Aggregators to forward sensor data to another aggregator.
 * 
 * @author <NAME>
 */
public class SensorAggregatorInterface {

  /**
   * A wrapper to take hide the IOAdapter events from classes using the
   * SensorAggregatorInterface.
   * 
   * @author <NAME>
   */
  private static final class AdapterHandler implements SensorIoAdapter {

    /**
     * The actual object that will respond to events.
     */
    private final SensorAggregatorInterface parent;

    /**
     * Creates a new AdapterHandler with the specified
     * {@code SensorAggregatorInterface} to actually handle events.
     * 
     * @param parent
     *          the real event handler
     */
    public AdapterHandler(SensorAggregatorInterface parent) {
      if (parent == null) {
        throw new IllegalArgumentException(
            "SensorAggregatorInterface cannot be null");
      }
      this.parent = parent;
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) {
      this.parent.exceptionCaught(session, cause);
    }

    @Override
    public void sensorConnected(IoSession session) {
      this.parent.sensorConnected(session);

    }

    @Override
    public void sensorDisconnected(IoSession session) {
      this.parent.sensorDisconnected(session);

    }

    @Override
    public void handshakeMessageReceived(IoSession session,
        HandshakeMessage handshakeMessage) {
      this.parent.handshakeMessageReceived(session, handshakeMessage);
    }

    @Override
    public void handshakeMessageSent(IoSession session,
        HandshakeMessage handshakeMessage) {
      this.parent.handshakeMessageSent(session, handshakeMessage);
    }

    @Override
    public void sensorSampleReceived(IoSession session,
        SampleMessage sampleMessage) {
      this.parent.sensorSampleReceived(session, sampleMessage);
    }

    @Override
    public void sensorSampleSent(IoSession session, SampleMessage sampleMessage) {
      this.parent.sensorSampleSent(session, sampleMessage);
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus idleStatus) {
      this.parent.sessionIdle(session, idleStatus);
    }

  }

  /**
   * Logging facility for this class.
   */
  private static final Logger log = LoggerFactory
      .getLogger(SensorAggregatorInterface.class);

  /**
   * The object that will pass events to this SensorAggregatorInterface. Used to
   * hide interface methods.
   */
  private final AdapterHandler handler = new AdapterHandler(this);

  /**
   * The handshake sent to the aggregator.
   */
  private HandshakeMessage sentHandshake;

  /**
   * The handshake received from the aggregator.
   */
  private HandshakeMessage receivedHandshake;

  /**
   * Indicates whether handshakes have been exchanged/checked and the aggregator
   * can receive sample messages.
   */
  protected boolean canSendSamples = false;

  /**
   * How long to wait between connection attempts to the aggregator, in
   * milliseconds.
   */
  protected long connectionRetryDelay = 10000;

  /**
   * Whether or not to try and stay connected to the aggregator.
   */
  protected boolean stayConnected = false;

  /**
   * Whether or not to disconnect from the aggregator if an exception is thrown.
   */
  protected boolean disconnectOnException = true;

  /**
   * The hostname or IP address of the aggregator.
   */
  private String host;

  /**
   * The port number the aggregator is listening on for sensors.
   */
  private int port = -1;

  /**
   * The session of the connected aggregator, or {@code null} if no connection
   * is established.
   */
  private IoSession session;

  /**
   * SocketConnector used to connect to the aggregator.
   */
  private SocketConnector connector;

  /**
   * IoHandler used by this aggregator interface.
   */
  private SensorIoHandler ioHandler = new SensorIoHandler(this.handler);

  /**
   * List (queue) of listeners for connection events for this aggregator
   * interface.
   */
  private ConcurrentLinkedQueue<ConnectionListener> connectionListeners = new ConcurrentLinkedQueue<ConnectionListener>();

  /**
   * Number of outstanding (buffered) sample messages to be sent on the
   * Aggregator's session.
   */<commit_msg>Fixing default port for aggregator.<commit_after>/*
 * Owl Platform Sensor-Aggregator Library for Java
 * Copyright (C) 2012 <NAME> and the Owl Platform
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but 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.
 *  
 * You should have received a copy of the GNU Lesser General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package com.owlplatform.sensor;

import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.SocketConnector;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.owlplatform.common.SampleMessage;
import com.owlplatform.sensor.listeners.ConnectionListener;
import com.owlplatform.sensor.protocol.codecs.AggregatorSensorProtocolCodecFactory;
import com.owlplatform.sensor.protocol.messages.HandshakeMessage;

/**
 * A convenience class for interfacing with aggregators as a sensor/hub. Since
 * most sensors are implemented in C/C++, this class is primarily used by
 * Aggregators to forward sensor data to another aggregator.
 * 
 * @author <NAME>
 */
public class SensorAggregatorInterface {

  /**
   * A wrapper to take hide the IOAdapter events from classes using the
   * SensorAggregatorInterface.
   * 
   * @author <NAME>
   */
  private static final class AdapterHandler implements SensorIoAdapter {

    /**
     * The actual object that will respond to events.
     */
    private final SensorAggregatorInterface parent;

    /**
     * Creates a new AdapterHandler with the specified
     * {@code SensorAggregatorInterface} to actually handle events.
     * 
     * @param parent
     *          the real event handler
     */
    public AdapterHandler(SensorAggregatorInterface parent) {
      if (parent == null) {
        throw new IllegalArgumentException(
            "SensorAggregatorInterface cannot be null");
      }
      this.parent = parent;
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) {
      this.parent.exceptionCaught(session, cause);
    }

    @Override
    public void sensorConnected(IoSession session) {
      this.parent.sensorConnected(session);

    }

    @Override
    public void sensorDisconnected(IoSession session) {
      this.parent.sensorDisconnected(session);

    }

    @Override
    public void handshakeMessageReceived(IoSession session,
        HandshakeMessage handshakeMessage) {
      this.parent.handshakeMessageReceived(session, handshakeMessage);
    }

    @Override
    public void handshakeMessageSent(IoSession session,
        HandshakeMessage handshakeMessage) {
      this.parent.handshakeMessageSent(session, handshakeMessage);
    }

    @Override
    public void sensorSampleReceived(IoSession session,
        SampleMessage sampleMessage) {
      this.parent.sensorSampleReceived(session, sampleMessage);
    }

    @Override
    public void sensorSampleSent(IoSession session, SampleMessage sampleMessage) {
      this.parent.sensorSampleSent(session, sampleMessage);
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus idleStatus) {
      this.parent.sessionIdle(session, idleStatus);
    }

  }

  /**
   * Logging facility for this class.
   */
  private static final Logger log = LoggerFactory
      .getLogger(SensorAggregatorInterface.class);

  /**
   * The object that will pass events to this SensorAggregatorInterface. Used to
   * hide interface methods.
   */
  private final AdapterHandler handler = new AdapterHandler(this);

  /**
   * The handshake sent to the aggregator.
   */
  private HandshakeMessage sentHandshake;

  /**
   * The handshake received from the aggregator.
   */
  private HandshakeMessage receivedHandshake;

  /**
   * Indicates whether handshakes have been exchanged/checked and the aggregator
   * can receive sample messages.
   */
  protected boolean canSendSamples = false;

  /**
   * How long to wait between connection attempts to the aggregator, in
   * milliseconds.
   */
  protected long connectionRetryDelay = 10000;

  /**
   * Whether or not to try and stay connected to the aggregator.
   */
  protected boolean stayConnected = false;

  /**
   * Whether or not to disconnect from the aggregator if an exception is thrown.
   */
  protected boolean disconnectOnException = true;

  /**
   * The hostname or IP address of the aggregator.
   */
  private String host;

  /**
   * The port number the aggregator is listening on for sensors.
   */
  private int port = 7007;

  /**
   * The session of the connected aggregator, or {@code null} if no connection
   * is established.
   */
  private IoSession session;

  /**
   * SocketConnector used to connect to the aggregator.
   */
  private SocketConnector connector;

  /**
   * IoHandler used by this aggregator interface.
   */
  private SensorIoHandler ioHandler = new SensorIoHandler(this.handler);

  /**
   * List (queue) of listeners for connection events for this aggregator
   * interface.
   */
  private ConcurrentLinkedQueue<ConnectionListener> connectionListeners = new ConcurrentLinkedQueue<ConnectionListener>();

  /**
   * Number of outstanding (buffered) sample messages to be sent on the
   * Aggregator's session.
   */
{'fraction_non_alphanumeric': 0.07599478147423352, 'fraction_numerical': 0.004321591650358773, 'mean_word_length': 3.4976164283094975, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3228503', 'n_tokens_mistral': 3296, 'n_tokens_neox': 3202, 'n_words': 1090}