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.io.network.group.impl.driver;
020
021
022import org.apache.reef.exception.evaluator.NetworkException;
023import org.apache.reef.io.network.Connection;
024import org.apache.reef.io.network.impl.NetworkService;
025import org.apache.reef.io.network.group.impl.GroupCommunicationMessage;
026import org.apache.reef.wake.EventHandler;
027import org.apache.reef.wake.Identifier;
028import org.apache.reef.wake.IdentifierFactory;
029
030import java.util.logging.Logger;
031
032/**
033 * Event handler that receives ctrl msgs and.
034 * dispatched them using network service
035 */
036public class CtrlMsgSender implements EventHandler<GroupCommunicationMessage> {
037
038  private static final Logger LOG = Logger.getLogger(CtrlMsgSender.class.getName());
039  private final IdentifierFactory idFac;
040  private final NetworkService<GroupCommunicationMessage> netService;
041
042  public CtrlMsgSender(final IdentifierFactory idFac, final NetworkService<GroupCommunicationMessage> netService) {
043    this.idFac = idFac;
044    this.netService = netService;
045  }
046
047  @Override
048  public void onNext(final GroupCommunicationMessage srcCtrlMsg) {
049    LOG.entering("CtrlMsgSender", "onNext", srcCtrlMsg);
050    final Identifier id = idFac.getNewInstance(srcCtrlMsg.getDestid());
051    final Connection<GroupCommunicationMessage> link = netService.newConnection(id);
052    try {
053      link.open();
054      link.write(srcCtrlMsg);
055    } catch (final NetworkException e) {
056      throw new RuntimeException("Unable to send ctrl task msg to parent " + id, e);
057    }
058    LOG.exiting("CtrlMsgSender", "onNext", srcCtrlMsg);
059  }
060
061}