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.wake.remote.impl; 020 021import com.google.protobuf.InvalidProtocolBufferException; 022import org.apache.reef.wake.remote.Decoder; 023import org.apache.reef.wake.remote.exception.RemoteRuntimeException; 024import org.apache.reef.wake.remote.proto.WakeRemoteProtos.WakeMessagePBuf; 025 026/** 027 * Remote event decoder using the WakeMessage protocol buffer. 028 * 029 * @param <T> type 030 */ 031public class RemoteEventDecoder<T> implements Decoder<RemoteEvent<T>> { 032 033 private final Decoder<T> decoder; 034 035 /** 036 * Constructs a remote event decoder. 037 * 038 * @param decoder the decoder of the event 039 */ 040 public RemoteEventDecoder(final Decoder<T> decoder) { 041 this.decoder = decoder; 042 } 043 044 /** 045 * Decodes a remote event from the byte array data. 046 * 047 * @param data the byte array data 048 * @return a remote event object 049 * @throws RemoteRuntimeException 050 */ 051 @Override 052 public RemoteEvent<T> decode(final byte[] data) { 053 final WakeMessagePBuf pbuf; 054 try { 055 pbuf = WakeMessagePBuf.parseFrom(data); 056 return new RemoteEvent<T>(null, null, pbuf.getSeq(), decoder.decode(pbuf.getData().toByteArray())); 057 } catch (final InvalidProtocolBufferException e) { 058 throw new RemoteRuntimeException(e); 059 } 060 } 061 062}