001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.node.connection;
020
021import org.jppf.comm.discovery.*;
022import org.jppf.utils.*;
023import org.jppf.utils.configuration.JPPFProperties;
024import org.slf4j.*;
025
026/**
027 * This implementation of {@link DriverConnectionStrategy} is the JPPF default
028 * and produces DriverConnectionInfo instances based solely on the JPPF configuration.
029 * @author Laurent Cohen
030 * @since 4.1
031 */
032public class JPPFDefaultConnectionStrategy implements DriverConnectionStrategy {
033  /**
034   * Logger for this class.
035   */
036  private static Logger log = LoggerFactory.getLogger(JPPFDefaultConnectionStrategy.class);
037
038  @Override
039  public DriverConnectionInfo nextConnectionInfo(final DriverConnectionInfo currentInfo, final ConnectionContext context) {
040    return JPPFConfiguration.get(JPPFProperties.DISCOVERY_ENABLED) ? discoverDriver() : connectionFromManualConfiguration();
041  }
042
043  /**
044   * Automatically discover the server connection information using a datagram multicast.
045   * Upon receiving the connection information, the JPPF configuration is modified to take into
046   * account the discovered information. If no information could be received, the node relies on
047   * the static information in the configuration file.
048   * @return the discovered connection information.
049   */
050  private static DriverConnectionInfo discoverDriver() {
051    final TypedProperties config = JPPFConfiguration.getProperties();
052    final JPPFMulticastReceiver receiver = new JPPFMulticastReceiver(new IPFilter(config));
053    final JPPFConnectionInformation info = receiver.receive();
054    receiver.setStopped(true);
055    if (info == null) {
056      if (log.isDebugEnabled()) log.debug("Could not auto-discover the driver connection information");
057      return connectionFromManualConfiguration();
058    }
059    if (log.isDebugEnabled()) log.debug("Discovered driver: " + info);
060    final boolean ssl = config.get(JPPFProperties.SSL_ENABLED);
061    final boolean recovery = config.get(JPPFProperties.RECOVERY_ENABLED);
062    return JPPFDriverConnectionInfo.fromJPPFConnectionInformation(info, ssl, recovery);
063  }
064
065  /**
066   * Determine the connection information specified manually in the configuration.
067   * @return the configured connection information.
068   */
069  private static DriverConnectionInfo connectionFromManualConfiguration() {
070    final TypedProperties config = JPPFConfiguration.getProperties();
071    final boolean ssl = config.get(JPPFProperties.SSL_ENABLED);
072    final String host = config.get(JPPFProperties.SERVER_HOST);
073    final int port = config.get(ssl ? JPPFProperties.SERVER_SSL_PORT_NODE : JPPFProperties.SERVER_PORT);
074    return new JPPFDriverConnectionInfo(ssl, host, port, config.get(JPPFProperties.RECOVERY_ENABLED));
075  }
076}