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 * Event sent from a remote node. 027 */ 028public class TransportEvent { 029 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 @Override 069 public String toString() { 070 return String.format( 071 "TransportEvent: {local: %s remote: %s size: %d bytes}", 072 this.localAddr, this.remoteAddr, this.data.length); 073 } 074 075 /** 076 * Gets the data. 077 * 078 * @return data 079 */ 080 public byte[] getData() { 081 return data; 082 } 083 084 /** 085 * Returns the link associated with the event. 086 * which can be used to write back to the client 087 * without creating a new link 088 * 089 * @return an existing link associated with the event 090 */ 091 public Link<byte[]> getLink() { 092 return link; 093 } 094 095 /** 096 * Gets the local socket address. 097 * 098 * @return the local socket address 099 */ 100 public SocketAddress getLocalAddress() { 101 return localAddr; 102 } 103 104 /** 105 * Gets the remote socket address. 106 * 107 * @return the remote socket address 108 */ 109 public SocketAddress getRemoteAddress() { 110 return remoteAddr; 111 } 112}