001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.runtime.common.driver.context;
020
021import org.apache.reef.annotations.audience.DriverSide;
022import org.apache.reef.annotations.audience.Private;
023import org.apache.reef.proto.EvaluatorRuntimeProtocol;
024import org.apache.reef.runtime.common.driver.evaluator.EvaluatorControlHandler;
025import org.apache.reef.runtime.common.driver.evaluator.EvaluatorManager;
026import org.apache.reef.tang.annotations.Parameter;
027
028import javax.inject.Inject;
029import java.util.logging.Level;
030import java.util.logging.Logger;
031
032/**
033 * Handles context control messages.
034 */
035@DriverSide
036@Private
037public class ContextControlHandler {
038  private static final Logger LOG = Logger.getLogger(ContextControlHandler.class.getName());
039  private final EvaluatorControlHandler evaluatorControlHandler;
040  private final String evaluatorId;
041
042  /**
043   * @param evaluatorControlHandler used to send the actual evaluator control messages.
044   * @param evaluatorId             the ID of the evaluator this ContextControlHandler communicates with.
045   */
046  @Inject
047  ContextControlHandler(final EvaluatorControlHandler evaluatorControlHandler,
048                        @Parameter(EvaluatorManager.EvaluatorIdentifier.class) final String evaluatorId) {
049    this.evaluatorControlHandler = evaluatorControlHandler;
050    this.evaluatorId = evaluatorId;
051    LOG.log(Level.FINE, "Instantiated 'ContextControlHandler'");
052  }
053
054  public synchronized void send(final EvaluatorRuntimeProtocol.ContextControlProto contextControlProto) {
055    final EvaluatorRuntimeProtocol.EvaluatorControlProto evaluatorControlProto =
056        EvaluatorRuntimeProtocol.EvaluatorControlProto.newBuilder()
057            .setTimestamp(System.currentTimeMillis())
058            .setIdentifier(evaluatorId)
059            .setContextControl(contextControlProto).build();
060    this.evaluatorControlHandler.send(evaluatorControlProto);
061  }
062}