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 org.apache.reef.wake.remote.transport.Link; 022 023import java.net.SocketAddress; 024 025 026/** 027 * Event sent from a remote node. 028 */ 029public class TransportEvent { 030 private final byte[] data; 031 private final SocketAddress localAddr; 032 private final SocketAddress remoteAddr; 033 private final Link<byte[]> link; 034 035 /** 036 * Constructs an object event. 037 * 038 * @param data the data 039 * @param localAddr the local socket address 040 * @param remoteAddr the remote socket address 041 */ 042 public TransportEvent(final byte[] data, final SocketAddress localAddr, final SocketAddress remoteAddr) { 043 this.data = data; 044 this.localAddr = localAddr; 045 this.remoteAddr = remoteAddr; 046 link = null; 047 } 048 049 /** 050 * Constructs the transport even object using link to. 051 * initialize local and remote address if link not null 052 * 053 * @param data 054 * @param link 055 */ 056 public TransportEvent(final byte[] data, final Link<byte[]> link) { 057 this.data = data; 058 this.link = link; 059 if (this.link != null) { 060 localAddr = link.getLocalAddress(); 061 remoteAddr = link.getRemoteAddress(); 062 } else { 063 localAddr = null; 064 remoteAddr = null; 065 } 066 } 067 068 /** 069 * Gets the data. 070 * 071 * @return data 072 */ 073 public byte[] getData() { 074 return data; 075 } 076 077 /** 078 * Returns the link associated with the event. 079 * which can be used to write back to the client 080 * without creating a new link 081 * 082 * @return an existing link associated with the event 083 */ 084 public Link<byte[]> getLink() { 085 return link; 086 } 087 088 /** 089 * Gets the local socket address. 090 * 091 * @return the local socket address 092 */ 093 public SocketAddress getLocalAddress() { 094 return localAddr; 095 } 096 097 /** 098 * Gets the remote socket address. 099 * 100 * @return the remote socket address 101 */ 102 public SocketAddress getRemoteAddress() { 103 return remoteAddr; 104 } 105 106}