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.util;
020
021import com.google.protobuf.ByteString;
022import org.apache.commons.lang.StringUtils;
023import org.apache.reef.io.network.proto.ReefNetworkGroupCommProtos;
024import org.apache.reef.wake.ComparableIdentifier;
025import org.apache.reef.wake.Identifier;
026import org.apache.reef.wake.IdentifierFactory;
027
028import java.net.Inet4Address;
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.Comparator;
032import java.util.List;
033
034/**
035 * Utility class for REEF IO network module.
036 */
037public final class Utils {
038
039  private static final String DELIMITER = "-";
040
041  /**
042   * Parse a string of multiple IDs.
043   *
044   * @param ids A string containing multiple IDs
045   * @param factory An Identifier factory
046   * @param <T> A type
047   * @return A list of identifier
048   */
049  public static <T extends Identifier> List<T> parseList(final String ids, final IdentifierFactory factory) {
050    final List<T> result = new ArrayList<>();
051    for (final String token : ids.split(DELIMITER)) {
052      result.add((T) factory.getNewInstance(token.trim()));
053    }
054    return result;
055  }
056
057  /**
058   * @deprecated in 0.14. Please use parseList instead.
059   */
060  @Deprecated
061  public static List<ComparableIdentifier> parseListCmp(
062      final String ids, final IdentifierFactory factory) {
063    final List<ComparableIdentifier> result = new ArrayList<>();
064    for (final String token : ids.split(DELIMITER)) {
065      result.add((ComparableIdentifier) factory.getNewInstance(token.trim()));
066    }
067    return result;
068  }
069
070  public static String listToString(final List<ComparableIdentifier> ids) {
071    return StringUtils.join(ids, DELIMITER);
072  }
073
074  public static List<Integer> createUniformCounts(final int elemSize, final int childSize) {
075    final int remainder = elemSize % childSize;
076    final int quotient = elemSize / childSize;
077    final ArrayList<Integer> result = new ArrayList<>(childSize);
078    result.addAll(Collections.nCopies(remainder, quotient + 1));
079    result.addAll(Collections.nCopies(childSize - remainder, quotient));
080    return Collections.unmodifiableList(result);
081  }
082
083  private static class AddressComparator implements Comparator<Inet4Address> {
084    @Override
085    public int compare(final Inet4Address aa, final Inet4Address ba) {
086      final byte[] a = aa.getAddress();
087      final byte[] b = ba.getAddress();
088      // local subnet comes after all else.
089      if (a[0] == 127 && b[0] != 127) {
090        return 1;
091      }
092      if (a[0] != 127 && b[0] == 127) {
093        return -1;
094      }
095      for (int i = 0; i < 4; i++) {
096        if (a[i] < b[i]) {
097          return -1;
098        }
099        if (a[i] > b[i]) {
100          return 1;
101        }
102      }
103      return 0;
104    }
105  }
106
107  public static ReefNetworkGroupCommProtos.GroupCommMessage bldGCM(
108      final ReefNetworkGroupCommProtos.GroupCommMessage.Type msgType,
109      final Identifier from, final Identifier to, final byte[]... elements) {
110
111    final ReefNetworkGroupCommProtos.GroupCommMessage.Builder gcmBuilder =
112        ReefNetworkGroupCommProtos.GroupCommMessage.newBuilder()
113            .setType(msgType)
114            .setSrcid(from.toString())
115            .setDestid(to.toString());
116
117    final ReefNetworkGroupCommProtos.GroupMessageBody.Builder bodyBuilder =
118        ReefNetworkGroupCommProtos.GroupMessageBody.newBuilder();
119
120    for (final byte[] element : elements) {
121      bodyBuilder.setData(ByteString.copyFrom(element));
122      gcmBuilder.addMsgs(bodyBuilder.build());
123    }
124
125    return gcmBuilder.build();
126  }
127
128  /**
129   * Empty private constructor to prohibit instantiation of utility class.
130   */
131  private Utils() {
132  }
133}