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.wake.remote.impl;
020
021import com.google.protobuf.ByteString;
022import org.apache.reef.wake.remote.Encoder;
023import org.apache.reef.wake.remote.exception.RemoteRuntimeException;
024import org.apache.reef.wake.remote.proto.WakeRemoteProtos.WakeMessagePBuf;
025
026/**
027 * Remote event encoder using the WakeMessage protocol buffer.
028 *
029 * @param <T> type
030 */
031public class RemoteEventEncoder<T> implements Encoder<RemoteEvent<T>> {
032
033  private final Encoder<T> encoder;
034
035  /**
036   * Constructs a remote event encoder.
037   *
038   * @param encoder the encoder of the event
039   */
040  public RemoteEventEncoder(final Encoder<T> encoder) {
041    this.encoder = encoder;
042  }
043
044  /**
045   * Encodes the remote event object to bytes.
046   *
047   * @param obj the remote event
048   * @return bytes
049   * @throws RemoteRuntimeException
050   */
051  @Override
052  public byte[] encode(final RemoteEvent<T> obj) {
053    if (obj.getEvent() == null) {
054      throw new RemoteRuntimeException("Event is null");
055    }
056
057    final WakeMessagePBuf.Builder builder = WakeMessagePBuf.newBuilder();
058    builder.setSeq(obj.getSeq());
059    builder.setData(ByteString.copyFrom(encoder.encode(obj.getEvent())));
060
061    return builder.build().toByteArray();
062  }
063
064}