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 021import org.apache.reef.io.network.group.impl.GroupCommunicationMessage; 022import org.apache.reef.io.network.group.impl.utils.BroadcastingEventHandler; 023import org.apache.reef.io.network.group.impl.utils.Utils; 024import org.apache.reef.tang.annotations.Name; 025import org.apache.reef.wake.AbstractEStage; 026import org.apache.reef.wake.EventHandler; 027import org.apache.reef.wake.impl.SingleThreadStage; 028import org.apache.reef.wake.impl.SyncStage; 029 030import java.util.HashMap; 031import java.util.Map; 032import java.util.logging.Logger; 033 034/** 035 * The network handler for the group communcation service on the driver side. 036 */ 037public class GroupCommMessageHandler implements EventHandler<GroupCommunicationMessage> { 038 039 private static final Logger LOG = Logger.getLogger(GroupCommMessageHandler.class.getName()); 040 041 private final Map<Class<? extends Name<String>>, AbstractEStage<GroupCommunicationMessage>> 042 commGroupMessageStages = new HashMap<>(); 043 044 /** 045 * @deprecated in 0.14. 046 */ 047 @Deprecated 048 public void addHandler(final Class<? extends Name<String>> groupName, 049 final BroadcastingEventHandler<GroupCommunicationMessage> handler) { 050 LOG.entering("GroupCommMessageHandler", "addHandler", new Object[]{Utils.simpleName(groupName), handler}); 051 commGroupMessageStages.put(groupName, new SyncStage<>(groupName.getName(), handler)); 052 LOG.exiting("GroupCommMessageHandler", "addHandler", Utils.simpleName(groupName)); 053 } 054 055 public void addHandler(final Class<? extends Name<String>> groupName, 056 final SingleThreadStage<GroupCommunicationMessage> stage) { 057 LOG.entering("GroupCommMessageHandler", "addHandler", new Object[]{Utils.simpleName(groupName), stage}); 058 commGroupMessageStages.put(groupName, stage); 059 LOG.exiting("GroupCommMessageHandler", "addHandler", Utils.simpleName(groupName)); 060 } 061 062 @Override 063 public void onNext(final GroupCommunicationMessage msg) { 064 LOG.entering("GroupCommMessageHandler", "onNext", msg); 065 final Class<? extends Name<String>> groupName = Utils.getClass(msg.getGroupname()); 066 commGroupMessageStages.get(groupName).onNext(msg); 067 LOG.exiting("GroupCommMessageHandler", "onNext", msg); 068 } 069}