This project has retired. For details please refer to its Attic page.
Source code
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.utils;
020
021import org.apache.reef.io.network.Message;
022import org.apache.reef.io.network.group.impl.GroupCommunicationMessage;
023import org.apache.reef.io.network.proto.ReefNetworkGroupCommProtos;
024import org.apache.reef.tang.annotations.Name;
025
026import java.util.Iterator;
027
028/**
029 * Utility class for group communications.
030 */
031public final class Utils {
032
033  public static final byte[] EMPTY_BYTE_ARR = new byte[0];
034
035  public static GroupCommunicationMessage bldVersionedGCM(final Class<? extends Name<String>> groupName,
036                                                          final Class<? extends Name<String>> operName,
037                                                          final ReefNetworkGroupCommProtos.GroupCommMessage.Type
038                                                              msgType,
039                                                          final String from, final int srcVersion,
040                                                          final String to, final int dstVersion, final byte[]... data) {
041
042    return new GroupCommunicationMessage(groupName.getName(), operName.getName(), msgType, from, srcVersion, to,
043        dstVersion, data);
044  }
045
046  public static Class<? extends Name<String>> getClass(final String className) {
047    try {
048      return (Class<? extends Name<String>>) Class.forName(className);
049    } catch (final ClassNotFoundException e) {
050      throw new RuntimeException("Unable to find class " + className, e);
051    }
052  }
053
054  public static String simpleName(final Class<?> className) {
055    if (className != null) {
056      return className.getSimpleName();
057    } else {
058      return "NULL";
059    }
060  }
061
062  public static byte[] getData(final GroupCommunicationMessage gcm) {
063    return (gcm.getMsgsCount() == 1) ? gcm.getData()[0] : null;
064  }
065
066  /**
067   * Extract a group communication message object from a message.
068   * @param msg
069   * @return
070   */
071  public static GroupCommunicationMessage getGCM(final Message<GroupCommunicationMessage> msg) {
072    final Iterator<GroupCommunicationMessage> gcmIterator = msg.getData().iterator();
073    if (gcmIterator.hasNext()) {
074      final GroupCommunicationMessage gcm = gcmIterator.next();
075      if (gcmIterator.hasNext()) {
076        throw new RuntimeException("Expecting exactly one GCM object inside Message but found more");
077      }
078      return gcm;
079    } else {
080      throw new RuntimeException("Expecting exactly one GCM object inside Message but found none");
081    }
082  }
083
084  /**
085   * Empty private constructor to prohibit instantiation of utility class.
086   */
087  private Utils() {
088  }
089}