1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.client.api; 20 21 import org.eclipse.jetty.client.HttpClient; 22 import org.eclipse.jetty.util.FuturePromise; 23 import org.eclipse.jetty.util.Promise; 24 25 /** 26 * {@link Destination} represents the triple made of the {@link #getScheme}, the {@link #getHost} 27 * and the {@link #getPort}. 28 * <p /> 29 * {@link Destination} holds a pool of {@link Connection}s, but allows to create unpooled 30 * connections if the application wants full control over connection management via {@link #newConnection(Promise)}. 31 * <p /> 32 * {@link Destination}s may be obtained via {@link HttpClient#getDestination(String, String, int)} 33 */ 34 public interface Destination 35 { 36 /** 37 * @return the scheme of this destination, such as "http" or "https" 38 */ 39 String getScheme(); 40 41 /** 42 * @return the host of this destination, such as "127.0.0.1" or "google.com" 43 */ 44 String getHost(); 45 46 /** 47 * @return the port of this destination such as 80 or 443 48 */ 49 int getPort(); 50 51 /** 52 * Creates asynchronously a new, unpooled, {@link Connection} that will be returned 53 * at a later time through the given {@link Promise}. 54 * <p /> 55 * Use {@link FuturePromise} to wait for the connection: 56 * <pre> 57 * Destination destination = ...; 58 * FuturePromise<Connection> futureConnection = new FuturePromise<>(); 59 * destination.newConnection(futureConnection); 60 * Connection connection = futureConnection.get(5, TimeUnit.SECONDS); 61 * </pre> 62 * 63 * @param promise the promise of a new, unpooled, {@link Connection} 64 */ 65 void newConnection(Promise<Connection> promise); 66 67 public static class Address 68 { 69 private final String host; 70 private final int port; 71 72 public Address(String host, int port) 73 { 74 this.host = host; 75 this.port = port; 76 } 77 78 public String getHost() 79 { 80 return host; 81 } 82 83 public int getPort() 84 { 85 return port; 86 } 87 88 @Override 89 public boolean equals(Object obj) 90 { 91 if (this == obj) return true; 92 if (obj == null || getClass() != obj.getClass()) return false; 93 Address that = (Address)obj; 94 return host.equals(that.host) && port == that.port; 95 } 96 97 @Override 98 public int hashCode() 99 { 100 int result = host.hashCode(); 101 result = 31 * result + port; 102 return result; 103 } 104 } 105 }