Example usage for java.net InetSocketAddress getPort

List of usage examples for java.net InetSocketAddress getPort

Introduction

In this page you can find the example usage for java.net InetSocketAddress getPort.

Prototype

public final int getPort() 

Source Link

Document

Gets the port number.

Usage

From source file:org.apache.hama.bsp.message.TestHamaAsyncMessageManager.java

private static void messagingInternal(HamaConfiguration conf) throws Exception {
    conf.set(MessageManagerFactory.MESSAGE_MANAGER_CLASS,
            "org.apache.hama.bsp.message.HamaAsyncMessageManagerImpl");
    MessageManager<IntWritable> messageManager = MessageManagerFactory.getMessageManager(conf);

    assertTrue(messageManager instanceof HamaAsyncMessageManagerImpl);

    InetSocketAddress peer = new InetSocketAddress(BSPNetUtils.getCanonicalHostname(),
            BSPNetUtils.getFreePort() + (increment++));
    conf.set(Constants.PEER_HOST, Constants.DEFAULT_PEER_HOST);
    conf.setInt(Constants.PEER_PORT, Constants.DEFAULT_PEER_PORT);

    BSPPeer<?, ?, ?, ?, IntWritable> dummyPeer = new BSPPeerImpl<NullWritable, NullWritable, NullWritable, NullWritable, IntWritable>(
            conf, FileSystem.get(conf), new Counters());
    TaskAttemptID id = new TaskAttemptID("1", 1, 1, 1);
    messageManager.init(id, dummyPeer, conf, peer);
    peer = messageManager.getListenerAddress();
    String peerName = peer.getHostName() + ":" + peer.getPort();
    System.out.println("Peer is " + peerName);
    messageManager.send(peerName, new IntWritable(1337));

    Iterator<Entry<InetSocketAddress, BSPMessageBundle<IntWritable>>> messageIterator = messageManager
            .getOutgoingBundles();/*from   w  ww .j  ava 2s  . c  o m*/

    Entry<InetSocketAddress, BSPMessageBundle<IntWritable>> entry = messageIterator.next();

    assertEquals(entry.getKey(), peer);

    assertTrue(entry.getValue().size() == 1);

    BSPMessageBundle<IntWritable> bundle = new BSPMessageBundle<IntWritable>();
    Iterator<IntWritable> it = entry.getValue().iterator();
    while (it.hasNext()) {
        bundle.addMessage(it.next());
    }

    messageManager.transfer(peer, bundle);

    messageManager.clearOutgoingMessages();

    assertTrue(messageManager.getNumCurrentMessages() == 1);
    IntWritable currentMessage = messageManager.getCurrentMessage();

    assertEquals(currentMessage.get(), 1337);
    messageManager.close();
}

From source file:org.apache.hadoop.mapred.ProxyJobTracker.java

private static SessionInfo getRunningSessionInfo(String sessionHandle) throws Exception {
    // Connect to cluster manager thrift service
    String target = CoronaConf.getClusterManagerAddress(conf);
    LOG.info("Connecting to Cluster Manager at " + target);
    InetSocketAddress address = NetUtils.createSocketAddr(target);
    TTransport transport = new TFramedTransport(new TSocket(address.getHostName(), address.getPort()));
    TProtocol protocol = new TBinaryProtocol(transport);
    ClusterManagerService.Client client = new ClusterManagerService.Client(protocol);
    transport.open();/*from   w w  w. j  a v  a2s  .  c o  m*/
    LOG.info("Requesting running session info for handle: " + sessionHandle);
    SessionInfo info = client.getSessionInfo(sessionHandle);
    transport.close();
    return info;
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Compose a "host:port" string from the address.
 *//*w ww.  j  a va2s .  c om*/
public static String getHostPortString(InetSocketAddress addr) {
    return addr.getHostName() + ":" + addr.getPort();
}

From source file:org.apache.hadoop.hbase.mob.mapreduce.SweepJob.java

static ServerName getCurrentServerName(Configuration conf) throws IOException {
    String hostname = conf.get("hbase.regionserver.ipc.address",
            Strings.domainNamePointerToHostName(
                    DNS.getDefaultHost(conf.get("hbase.regionserver.dns.interface", "default"),
                            conf.get("hbase.regionserver.dns.nameserver", "default"))));
    int port = conf.getInt(HConstants.REGIONSERVER_PORT, HConstants.DEFAULT_REGIONSERVER_PORT);
    // Creation of a HSA will force a resolve.
    InetSocketAddress initialIsa = new InetSocketAddress(hostname, port);
    if (initialIsa.getAddress() == null) {
        throw new IllegalArgumentException("Failed resolve of " + initialIsa);
    }/*from w  ww .j a va 2 s .co  m*/
    return ServerName.valueOf(initialIsa.getHostName(), initialIsa.getPort(),
            EnvironmentEdgeManager.currentTime());
}

From source file:au.com.jwatmuff.genericp2p.rmi.RMIPeerManager.java

@SuppressWarnings("unchecked")
private static <T> T getService(InetSocketAddress address, String serviceName, Class<T> serviceClass)
        throws NoSuchServiceException {
    String url = "rmi://" + address.getHostName() + ":" + address.getPort() + "/" + serviceName;
    try {//ww  w  .  java 2  s  .co m
        /* maybe should use address.getAddress().getHostAddress() */
        RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
        factory.setServiceInterface(serviceClass);
        factory.setServiceUrl(url);
        factory.afterPropertiesSet();
        return (T) factory.getObject();
    } catch (RemoteLookupFailureException rlfe) {
        throw new NoSuchServiceException("Unable to resolve service " + serviceName + " at url: " + url);
    } catch (Exception e) {
        throw new RuntimeException("getService failed", e);
    }
}

From source file:org.apache.hadoop.security.SecurityUtil.java

/**
 * Construct the service key for a token
 * @param addr InetSocketAddress of remote connection with a token
 * @return "ip:port" or "host:port" depending on the value of
 *          hadoop.security.token.service.use_ip
 *//* w  w  w  . jav a2  s  . c  om*/
public static Text buildTokenService(InetSocketAddress addr) {
    String host = null;
    if (useIpForTokenService) {
        if (addr.isUnresolved()) { // host has no ip address
            throw new IllegalArgumentException(new UnknownHostException(addr.getHostName()));
        }
        host = addr.getAddress().getHostAddress();
    } else {
        host = addr.getHostName().toLowerCase();
    }
    return new Text(host + ":" + addr.getPort());
}

From source file:fr.cls.atoll.motu.processor.wps.TestServiceMetadata.java

public static void DetectProxy() throws Exception {
    System.setProperty("proxyHost", "proxy.cls.fr"); // adresse IP
    System.setProperty("proxyPort", "8080");
    System.setProperty("socksProxyHost", "proxy.cls.fr");
    // System.setProperty("http.proxyHost", "http-proxy.ece.fr");
    // System.setProperty("http.proxyPort", "3128");
    // System.setProperty("java.net.useSystemProxies", "true");
    // List<Proxy> proxyList = ProxySelector.getDefault().select(new URI("http://schemas.opengis.net"));
    List<Proxy> proxyList = ProxySelector.getDefault().select(new URI("http://opendap.aviso.oceanobs.com"));

    for (Proxy proxy : proxyList) {
        System.out.println("Proxy type : " + proxy.type());
        InetSocketAddress addr = (InetSocketAddress) proxy.address();
        if (addr == null) {
            System.out.println("DIRECT CONXN");
        } else {//from  w  w  w .j a v a  2 s  . c  o  m
            System.out.println("Proxy hostname : " + addr.getHostName() + ":" + addr.getPort());
        }
    }
}

From source file:edu.umass.cs.utils.Util.java

private static String sockAddrToEncodedString(InetSocketAddress isa) throws UnsupportedEncodingException {
    byte[] address = isa.getAddress().getAddress();
    byte[] buf = new byte[address.length + 2];
    for (int i = 0; i < address.length; i++)
        buf[i] = address[i];/*from  w w  w . j a v  a  2s  .c  o m*/
    buf[address.length] = (byte) (isa.getPort() >> 8);
    buf[address.length + 1] = (byte) (isa.getPort() & 255);
    return new String(buf, CHARSET);

}

From source file:gov.nih.nci.nbia.StandaloneDMDispatcher.java

static String getProxyInfo() {
    StringBuffer sb = new StringBuffer("Get proxy info: ");
    try {//from  w  w  w. j  a  v  a 2  s.  c o  m
        System.setProperty("java.net.useSystemProxies", "true");
        List<Proxy> l = ProxySelector.getDefault().select(new URI("https://www.google.com/"));

        for (Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
            Proxy proxy = iter.next();
            sb.append("proxy type : " + proxy.type() + "\n");
            InetSocketAddress addr = (InetSocketAddress) proxy.address();

            if (addr == null) {
                sb.append("No Proxy\n");
            } else {
                sb.append("proxy hostname : " + addr.getHostName() + "\n");
                sb.append("proxy port : " + addr.getPort() + "\n");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        sb.append(e.getMessage());
    }
    return sb.toString();
}

From source file:com.buaa.cfs.utils.NetUtils.java

/**
 * Returns an InetSocketAddress that a client can use to connect to the given listening address.
 *
 * @param addr of a listener/*from   w  w w . j a v  a 2s .c  o m*/
 *
 * @return socket address that a client can use to connect to the server.
 */
public static InetSocketAddress getConnectAddress(InetSocketAddress addr) {
    if (!addr.isUnresolved() && addr.getAddress().isAnyLocalAddress()) {
        try {
            addr = new InetSocketAddress(InetAddress.getLocalHost(), addr.getPort());
        } catch (UnknownHostException uhe) {
            // shouldn't get here unless the host doesn't have a loopback iface
            addr = createSocketAddrForHost("127.0.0.1", addr.getPort());
        }
    }
    return addr;
}