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.task;
020
021import org.apache.reef.io.network.group.api.task.CommGroupNetworkHandler;
022import org.apache.reef.io.network.group.impl.GroupCommunicationMessage;
023import org.apache.reef.io.network.group.impl.utils.Utils;
024import org.apache.reef.io.network.proto.ReefNetworkGroupCommProtos;
025import org.apache.reef.tang.annotations.Name;
026import org.apache.reef.wake.EventHandler;
027
028import javax.inject.Inject;
029import java.util.Arrays;
030import java.util.Map;
031import java.util.concurrent.BlockingQueue;
032import java.util.concurrent.ConcurrentHashMap;
033import java.util.concurrent.LinkedBlockingQueue;
034import java.util.logging.Logger;
035
036public class CommGroupNetworkHandlerImpl implements
037    CommGroupNetworkHandler {
038
039  private static final Logger LOG = Logger.getLogger(CommGroupNetworkHandlerImpl.class.getName());
040
041  private final Map<Class<? extends Name<String>>, EventHandler<GroupCommunicationMessage>> operHandlers =
042      new ConcurrentHashMap<>();
043  private final Map<Class<? extends Name<String>>, BlockingQueue<GroupCommunicationMessage>> topologyNotifications =
044      new ConcurrentHashMap<>();
045
046  @Inject
047  public CommGroupNetworkHandlerImpl() {
048  }
049
050  @Override
051  public void register(final Class<? extends Name<String>> operName,
052                       final EventHandler<GroupCommunicationMessage> operHandler) {
053    LOG.entering("CommGroupNetworkHandlerImpl", "register", new Object[]{Utils.simpleName(operName), operHandler});
054    operHandlers.put(operName, operHandler);
055    LOG.exiting("CommGroupNetworkHandlerImpl", "register",
056        Arrays.toString(new Object[]{Utils.simpleName(operName), operHandler}));
057  }
058
059  @Override
060  public void addTopologyElement(final Class<? extends Name<String>> operName) {
061    LOG.entering("CommGroupNetworkHandlerImpl", "addTopologyElement", Utils.simpleName(operName));
062    LOG.finest("Creating LBQ for " + operName);
063    topologyNotifications.put(operName, new LinkedBlockingQueue<GroupCommunicationMessage>());
064    LOG.exiting("CommGroupNetworkHandlerImpl", "addTopologyElement", Utils.simpleName(operName));
065  }
066
067  @Override
068  public void onNext(final GroupCommunicationMessage msg) {
069    LOG.entering("CommGroupNetworkHandlerImpl", "onNext", msg);
070    final Class<? extends Name<String>> operName = Utils.getClass(msg.getOperatorname());
071    if (msg.getType() == ReefNetworkGroupCommProtos.GroupCommMessage.Type.TopologyUpdated ||
072        msg.getType() == ReefNetworkGroupCommProtos.GroupCommMessage.Type.TopologyChanges) {
073      topologyNotifications.get(operName).add(msg);
074    } else {
075      operHandlers.get(operName).onNext(msg);
076    }
077    LOG.exiting("CommGroupNetworkHandlerImpl", "onNext", msg);
078  }
079
080  @Override
081  public byte[] waitForTopologyChanges(final Class<? extends Name<String>> operName) {
082    LOG.entering("CommGroupNetworkHandlerImpl", "waitForTopologyChanges", Utils.simpleName(operName));
083    try {
084      final byte[] retVal = Utils.getData(topologyNotifications.get(operName).take());
085      LOG.exiting("CommGroupNetworkHandlerImpl", "waitForTopologyChanges", retVal);
086      return retVal;
087    } catch (final InterruptedException e) {
088      throw new RuntimeException("InterruptedException while waiting for topology update of "
089          + operName.getSimpleName(), e);
090    }
091  }
092
093  @Override
094  public GroupCommunicationMessage waitForTopologyUpdate(final Class<? extends Name<String>> operName) {
095    LOG.entering("CommGroupNetworkHandlerImpl", "waitForTopologyUpdate", Utils.simpleName(operName));
096    try {
097      final GroupCommunicationMessage retVal = topologyNotifications.get(operName).take();
098      LOG.exiting("CommGroupNetworkHandlerImpl", "waitForTopologyUpdate", retVal);
099      return retVal;
100    } catch (final InterruptedException e) {
101      throw new RuntimeException("InterruptedException while waiting for topology update of "
102          + operName.getSimpleName(), e);
103    }
104  }
105
106}