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.naming.serialization; 020 021import org.apache.reef.io.naming.NameAssignment; 022import org.apache.reef.io.network.naming.NameAssignmentTuple; 023import org.apache.reef.io.network.naming.avro.AvroNamingAssignment; 024import org.apache.reef.io.network.naming.avro.AvroNamingLookupResponse; 025import org.apache.reef.wake.IdentifierFactory; 026import org.apache.reef.wake.remote.Codec; 027 028import javax.inject.Inject; 029import java.net.InetSocketAddress; 030import java.util.ArrayList; 031import java.util.List; 032 033/** 034 * Naming lookup response codec. 035 */ 036public final class NamingLookupResponseCodec implements Codec<NamingLookupResponse> { 037 038 private final IdentifierFactory factory; 039 040 /** 041 * Constructs a naming lookup response codec. 042 * 043 * @param factory the identifier factory 044 */ 045 @Inject 046 public NamingLookupResponseCodec(final IdentifierFactory factory) { 047 this.factory = factory; 048 } 049 050 /** 051 * Encodes name assignments to bytes. 052 * 053 * @param obj the naming lookup response 054 * @return a byte array 055 */ 056 @Override 057 public byte[] encode(final NamingLookupResponse obj) { 058 final List<AvroNamingAssignment> assignments = new ArrayList<>(obj.getNameAssignments().size()); 059 for (final NameAssignment nameAssignment : obj.getNameAssignments()) { 060 assignments.add(AvroNamingAssignment.newBuilder() 061 .setId(nameAssignment.getIdentifier().toString()) 062 .setHost(nameAssignment.getAddress().getHostName()) 063 .setPort(nameAssignment.getAddress().getPort()) 064 .build()); 065 } 066 return AvroUtils.toBytes( 067 AvroNamingLookupResponse.newBuilder().setTuples(assignments).build(), AvroNamingLookupResponse.class 068 ); 069 } 070 071 /** 072 * Decodes bytes to an iterable of name assignments. 073 * 074 * @param buf the byte array 075 * @return a naming lookup response 076 * @throws org.apache.reef.io.network.naming.exception.NamingRuntimeException 077 */ 078 @Override 079 public NamingLookupResponse decode(final byte[] buf) { 080 final AvroNamingLookupResponse avroResponse = AvroUtils.fromBytes(buf, AvroNamingLookupResponse.class); 081 final List<NameAssignment> nas = new ArrayList<>(avroResponse.getTuples().size()); 082 for (final AvroNamingAssignment tuple : avroResponse.getTuples()) { 083 nas.add( 084 new NameAssignmentTuple( 085 factory.getNewInstance(tuple.getId().toString()), 086 new InetSocketAddress(tuple.getHost().toString(), tuple.getPort()) 087 ) 088 ); 089 } 090 return new NamingLookupResponse(nas); 091 } 092 093}