Example usage for java.net ServerSocket getLocalPort

List of usage examples for java.net ServerSocket getLocalPort

Introduction

In this page you can find the example usage for java.net ServerSocket getLocalPort.

Prototype

public int getLocalPort() 

Source Link

Document

Returns the port number on which this socket is listening.

Usage

From source file:org.apache.jackrabbit.oak.remote.http.handler.RemoteServerIT.java

private int getRandomPort() throws Exception {
    ServerSocket serverSocket = null;

    try {//from  w  ww . j  ava2  s .  co m
        serverSocket = new ServerSocket(0);
        return serverSocket.getLocalPort();
    } finally {
        if (serverSocket != null) {
            serverSocket.close();
        }
    }
}

From source file:uk.ac.horizon.ubihelper.service.PeerManager.java

public PeerManager(Service service) {
    this.service = service;
    // Note: meant to open database on another thread?!
    database = new PeersOpenHelper(service).getWritableDatabase();

    protocol = new MyProtocolManager();
    peerConnectionListener = new OnPeerConnectionListener(protocol);
    remoteChannelTimer = new Timer();

    wifi = (WifiManager) service.getSystemService(Service.WIFI_SERVICE);

    try {//w w w .j  a  v  a  2s . c om
        messageDigest = MessageDigest.getInstance("MD5");
    } catch (Exception e) {
        Log.e(TAG, "Could not get MessageDigest: " + e);
    }
    try {
        serverSocketChannel = ServerSocketChannel.open();
        ServerSocket ss = serverSocketChannel.socket();
        ss.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
        serverPort = ss.getLocalPort();
        serverSocketChannel.configureBlocking(false);
    } catch (IOException e) {
        Log.w(TAG, "Error opening ServerSocketChannel: " + e.getMessage());
    }
    try {
        selector = new PeerConnectionScheduler(serverSocketChannel);
        selector.setListener(selectorListener);
        selector.start();
    } catch (IOException e) {
        Log.w(TAG, "Error starting Selector: " + e.getMessage());
    }
}

From source file:org.apache.geode.internal.net.SSLSocketIntegrationTest.java

@Test
public void configureClientSSLSocketCanTimeOut() throws Exception {
    final Semaphore serverCoordination = new Semaphore(0);

    // configure a non-SSL server socket. We will connect
    // a client SSL socket to it and demonstrate that the
    // handshake times out
    final ServerSocket serverSocket = new ServerSocket();
    serverSocket.bind(new InetSocketAddress(SocketCreator.getLocalHost(), 0));
    Thread serverThread = new Thread() {
        public void run() {
            serverCoordination.release();
            try (Socket clientSocket = serverSocket.accept()) {
                System.out.println("server thread accepted a connection");
                serverCoordination.acquire();
            } catch (Exception e) {
                System.err.println("accept failed");
                e.printStackTrace();//ww  w  .j  a  v a2 s . c om
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                // ignored
            }
            System.out.println("server thread is exiting");
        }
    };
    serverThread.setName("SocketCreatorJUnitTest serverSocket thread");
    serverThread.setDaemon(true);
    serverThread.start();

    serverCoordination.acquire();

    SocketCreator socketCreator = SocketCreatorFactory
            .getSocketCreatorForComponent(SecurableCommunicationChannel.SERVER);

    int serverSocketPort = serverSocket.getLocalPort();
    try {
        Awaitility.await("connect to server socket").atMost(30, TimeUnit.SECONDS).until(() -> {
            try {
                Socket clientSocket = socketCreator.connectForClient(
                        SocketCreator.getLocalHost().getHostAddress(), serverSocketPort, 2000);
                clientSocket.close();
                System.err.println(
                        "client successfully connected to server but should not have been able to do so");
                return false;
            } catch (SocketTimeoutException e) {
                // we need to verify that this timed out in the handshake
                // code
                System.out.println("client connect attempt timed out - checking stack trace");
                StackTraceElement[] trace = e.getStackTrace();
                for (StackTraceElement element : trace) {
                    if (element.getMethodName().equals("configureClientSSLSocket")) {
                        System.out.println("client connect attempt timed out in the appropriate method");
                        return true;
                    }
                }
                // it wasn't in the configuration method so we need to try again
            } catch (IOException e) {
                // server socket may not be in accept() yet, causing a connection-refused
                // exception
            }
            return false;
        });
    } finally {
        serverCoordination.release();
    }
}

From source file:common.DataNode.java

/**
 * This method starts the data node with the specified conf.
 * /*from   ww  w.  j  a v  a2  s . c  om*/
 * @param conf - the configuration
 *  if conf's CONFIG_PROPERTY_SIMULATED property is set
 *  then a simulated storage based data node is created.
 * 
 * @param dataDirs - only for a non-simulated storage data node
 * @throws IOException
 */
void startDataNode(Configuration conf, AbstractList<File> dataDirs, DatanodeProtocol namenode)
        throws IOException {
    // use configured nameserver & interface to get local hostname
    if (conf.get(DFSConfigKeys.DFS_DATANODE_HOST_NAME_KEY) != null) {
        machineName = conf.get(DFSConfigKeys.DFS_DATANODE_HOST_NAME_KEY);
    }
    if (machineName == null) {
        machineName = DNS.getDefaultHost(conf.get("dfs.datanode.dns.interface", "default"),
                conf.get("dfs.datanode.dns.nameserver", "default"));
    }
    this.nameNodeAddr = NameNode.getAddress(conf);

    this.socketTimeout = conf.getInt(DFSConfigKeys.DFS_CLIENT_SOCKET_TIMEOUT_KEY, HdfsConstants.READ_TIMEOUT);
    this.socketWriteTimeout = conf.getInt("dfs.datanode.socket.write.timeout", HdfsConstants.WRITE_TIMEOUT);
    /* Based on results on different platforms, we might need set the default 
     * to false on some of them. */
    this.transferToAllowed = conf.getBoolean("dfs.datanode.transferTo.allowed", true);
    this.writePacketSize = conf.getInt(DFSConfigKeys.DFS_CLIENT_WRITE_PACKET_SIZE_KEY,
            DFSConfigKeys.DFS_CLIENT_WRITE_PACKET_SIZE_DEFAULT);
    InetSocketAddress socAddr = NetUtils.createSocketAddr(conf.get("dfs.datanode.address", "0.0.0.0:50010"));
    int tmpPort = socAddr.getPort();
    storage = new DataStorage();
    // construct registration
    this.dnRegistration = new DatanodeRegistration(machineName + ":" + tmpPort);

    // connect to name node
    this.namenode = namenode;

    // get version and id info from the name-node
    NamespaceInfo nsInfo = handshake();
    StartupOption startOpt = getStartupOption(conf);
    assert startOpt != null : "Startup option must be set.";

    boolean simulatedFSDataset = conf.getBoolean("dfs.datanode.simulateddatastorage", false);
    if (simulatedFSDataset) {
        setNewStorageID(dnRegistration);
        dnRegistration.storageInfo.layoutVersion = FSConstants.LAYOUT_VERSION;
        dnRegistration.storageInfo.namespaceID = nsInfo.namespaceID;
        // it would have been better to pass storage as a parameter to
        // constructor below - need to augment ReflectionUtils used below.
        conf.set(DFSConfigKeys.DFS_DATANODE_STORAGEID_KEY, dnRegistration.getStorageID());
        try {
            //Equivalent of following (can't do because Simulated is in test dir)
            //  this.data = new SimulatedFSDataset(conf);
            this.data = (FSDatasetInterface) ReflectionUtils.newInstance(
                    Class.forName("org.apache.hadoop.hdfs.server.datanode.SimulatedFSDataset"), conf);
        } catch (ClassNotFoundException e) {
            throw new IOException(StringUtils.stringifyException(e));
        }
    } else { // real storage
        // read storage info, lock data dirs and transition fs state if necessary
        storage.recoverTransitionRead(nsInfo, dataDirs, startOpt);
        // adjust
        this.dnRegistration.setStorageInfo(storage);
        // initialize data node internal structure
        this.data = new FSDataset(storage, conf);
    }

    // find free port
    ServerSocket ss = (socketWriteTimeout > 0) ? ServerSocketChannel.open().socket() : new ServerSocket();
    Server.bind(ss, socAddr, 0);
    ss.setReceiveBufferSize(DEFAULT_DATA_SOCKET_SIZE);
    // adjust machine name with the actual port
    tmpPort = ss.getLocalPort();
    selfAddr = new InetSocketAddress(ss.getInetAddress().getHostAddress(), tmpPort);
    this.dnRegistration.setName(machineName + ":" + tmpPort);
    LOG.info("Opened info server at " + tmpPort);

    this.threadGroup = new ThreadGroup("dataXceiverServer");
    this.dataXceiverServer = new Daemon(threadGroup, new DataXceiverServer(ss, conf, this));
    this.threadGroup.setDaemon(true); // auto destroy when empty

    this.blockReportInterval = conf.getLong("dfs.blockreport.intervalMsec", BLOCKREPORT_INTERVAL);
    this.initialBlockReportDelay = conf.getLong("dfs.blockreport.initialDelay", BLOCKREPORT_INITIAL_DELAY)
            * 1000L;
    if (this.initialBlockReportDelay >= blockReportInterval) {
        this.initialBlockReportDelay = 0;
        LOG.info("dfs.blockreport.initialDelay is greater than " + "dfs.blockreport.intervalMsec."
                + " Setting initial delay to 0 msec:");
    }
    this.heartBeatInterval = conf.getLong("dfs.heartbeat.interval", HEARTBEAT_INTERVAL) * 1000L;

    //initialize periodic block scanner
    String reason = null;
    if (conf.getInt("dfs.datanode.scan.period.hours", 0) < 0) {
        reason = "verification is turned off by configuration";
    } else if (!(data instanceof FSDataset)) {
        reason = "verifcation is supported only with FSDataset";
    }
    if (reason == null) {
        blockScanner = new DataBlockScanner(this, (FSDataset) data, conf);
    } else {
        LOG.info("Periodic Block Verification is disabled because " + reason + ".");
    }

    //create a servlet to serve full-file content
    InetSocketAddress infoSocAddr = NetUtils
            .createSocketAddr(conf.get("dfs.datanode.http.address", "0.0.0.0:50075"));
    String infoHost = infoSocAddr.getHostName();
    int tmpInfoPort = infoSocAddr.getPort();
    this.infoServer = new HttpServer("datanode", infoHost, tmpInfoPort, tmpInfoPort == 0, conf);
    if (conf.getBoolean("dfs.https.enable", false)) {
        boolean needClientAuth = conf.getBoolean(DFSConfigKeys.DFS_CLIENT_HTTPS_NEED_AUTH_KEY,
                DFSConfigKeys.DFS_CLIENT_HTTPS_NEED_AUTH_DEFAULT);
        InetSocketAddress secInfoSocAddr = NetUtils
                .createSocketAddr(conf.get("dfs.datanode.https.address", infoHost + ":" + 0));
        Configuration sslConf = new HdfsConfiguration(false);
        sslConf.addResource(conf.get("dfs.https.server.keystore.resource", "ssl-server.xml"));
        this.infoServer.addSslListener(secInfoSocAddr, sslConf, needClientAuth);
    }
    this.infoServer.addInternalServlet(null, "/streamFile/*", StreamFile.class);
    this.infoServer.addInternalServlet(null, "/getFileChecksum/*", FileChecksumServlets.GetServlet.class);
    this.infoServer.setAttribute("datanode.blockScanner", blockScanner);
    this.infoServer.setAttribute("datanode.conf", conf);
    this.infoServer.addServlet(null, "/blockScannerReport", DataBlockScanner.Servlet.class);
    this.infoServer.start();
    // adjust info port
    this.dnRegistration.setInfoPort(this.infoServer.getPort());
    myMetrics = new DataNodeMetrics(conf, dnRegistration.getName());

    // set service-level authorization security policy
    if (conf.getBoolean(ServiceAuthorizationManager.SERVICE_AUTHORIZATION_CONFIG, false)) {
        ServiceAuthorizationManager.refresh(conf, new HDFSPolicyProvider());
    }

    //init ipc server
    InetSocketAddress ipcAddr = NetUtils.createSocketAddr(conf.get("dfs.datanode.ipc.address"));
    ipcServer = RPC.getServer(DataNode.class, this, ipcAddr.getHostName(), ipcAddr.getPort(),
            conf.getInt("dfs.datanode.handler.count", 3), false, conf);
    ipcServer.start();
    dnRegistration.setIpcPort(ipcServer.getListenerAddress().getPort());

    LOG.info("dnRegistration = " + dnRegistration);

    plugins = conf.getInstances("dfs.datanode.plugins", ServicePlugin.class);
    for (ServicePlugin p : plugins) {
        try {
            p.start(this);
            LOG.info("Started plug-in " + p);
        } catch (Throwable t) {
            LOG.warn("ServicePlugin " + p + " could not be started", t);
        }
    }
}

From source file:slash.navigation.mapview.browser.BrowserMapView.java

private ServerSocket createCallbackListenerServerSocket() {
    try {/*from  w  w w  .j av a  2s.co  m*/
        ServerSocket serverSocket = new ServerSocket(0, 0,
                InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
        serverSocket.setSoTimeout(1000);
        int port = serverSocket.getLocalPort();
        log.info("Map listens on port " + port + " for callbacks");
        setCallbackListenerPort(port);
        return serverSocket;
    } catch (IOException e) {
        log.severe("Cannot open callback listener socket: " + e);
        return null;
    }
}

From source file:org.hyperic.hq.bizapp.agent.client.AgentClient.java

private int cmdStart(boolean force) throws AgentInvokeException {
    ServerSocket startupSock;
    ProviderInfo providerInfo;/*ww w . j  a v a  2  s. c om*/
    Properties bootProps;

    // Try to ping the agent one time to see if the agent is already up
    try {
        this.cmdPing(1);
        SYSTEM_OUT.println("Agent already running");
        return -1;
    } catch (AgentConnectionException exc) {
        // Normal operation 
    } catch (AgentRemoteException exc) {
        // Very nearly a normal operation
    }

    bootProps = this.config.getBootProperties();

    try {
        int iSleepTime = getStartupTimeout(bootProps);

        startupSock = new ServerSocket(0);
        startupSock.setSoTimeout(iSleepTime);
    } catch (IOException e) {
        AgentInvokeException ex = new AgentInvokeException(
                "Unable to setup a socket to listen for Agent startup: " + e);
        ex.initCause(e);
        throw ex;
    }

    SYSTEM_OUT.println("- Invoking agent");

    try {
        this.config.setNotifyUpPort(startupSock.getLocalPort());
    } catch (AgentConfigException e) {
        throw new AgentInvokeException("Invalid notify up port: " + startupSock.getLocalPort());
    }

    RunnableAgent runnableAgent = new AgentDaemon.RunnableAgent(this.config);
    agentDaemonThread = new Thread(runnableAgent);
    agentDaemonThread.setName("AgentDaemonMain");
    AgentUpgradeManager.setAgentDaemonThread(agentDaemonThread);
    AgentUpgradeManager.setAgent(runnableAgent);
    agentDaemonThread.setDaemon(true);
    agentDaemonThread.start();
    SYSTEM_OUT.println("- Agent thread running");

    /* Now comes the painful task of figuring out if the agent started correctly. */
    SYSTEM_OUT.println("- Verifying if agent is running...");
    this.verifyAgentRunning(startupSock);
    SYSTEM_OUT.println("- Agent is running");

    // Ask the agent if they have a server setup
    try {
        providerInfo = this.camCommands.getProviderInfo();
    } catch (Exception exc) {
        // This should rarely (never) occur, since we just ensured things
        // were operational.
        throw new AgentInvokeException("Unexpected connection exception: " + "agent is still running");
    }

    SYSTEM_OUT.println("Agent successfully started");

    // Only force a setup if we are not running the agent in Java Service Wrapper mode
    if (providerInfo == null && !WrapperManager.isControlledByNativeWrapper()) {
        SYSTEM_OUT.println();
        return FORCE_SETUP;
    } else {
        redirectOutputs(bootProps); //win32
        return 0;
    }

}

From source file:org.apache.hadoop.hdfs.server.datanode.DataNode.java

/**
 * This method starts the data node with the specified conf.
 * //from w  w  w.ja v a2 s .co  m
 * @param conf - the configuration
 *  if conf's CONFIG_PROPERTY_SIMULATED property is set
 *  then a simulated storage based data node is created.
 * 
 * @param dataDirs - only for a non-simulated storage data node
 * @throws IOException
 * @throws MalformedObjectNameException 
 * @throws MBeanRegistrationException 
 * @throws InstanceAlreadyExistsException 
 */
void startDataNode(Configuration conf, AbstractList<File> dataDirs, SecureResources resources)
        throws IOException {
    if (UserGroupInformation.isSecurityEnabled() && resources == null)
        throw new RuntimeException("Cannot start secure cluster without " + "privileged resources.");

    this.secureResources = resources;
    // use configured nameserver & interface to get local hostname
    if (conf.get("slave.host.name") != null) {
        machineName = conf.get("slave.host.name");
    }
    if (machineName == null) {
        machineName = DNS.getDefaultHost(conf.get("dfs.datanode.dns.interface", "default"),
                conf.get("dfs.datanode.dns.nameserver", "default"));
    }
    InetSocketAddress nameNodeAddr = NameNode.getServiceAddress(conf, true);

    this.socketTimeout = conf.getInt("dfs.socket.timeout", HdfsConstants.READ_TIMEOUT);
    this.socketWriteTimeout = conf.getInt("dfs.datanode.socket.write.timeout", HdfsConstants.WRITE_TIMEOUT);
    /* Based on results on different platforms, we might need set the default 
     * to false on some of them. */
    this.transferToAllowed = conf.getBoolean("dfs.datanode.transferTo.allowed", true);
    this.writePacketSize = conf.getInt("dfs.write.packet.size", 64 * 1024);

    InetSocketAddress socAddr = DataNode.getStreamingAddr(conf);
    int tmpPort = socAddr.getPort();
    storage = new DataStorage();
    // construct registration
    this.dnRegistration = new DatanodeRegistration(machineName + ":" + tmpPort);

    // connect to name node
    this.namenode = (DatanodeProtocol) RPC.waitForProxy(DatanodeProtocol.class, DatanodeProtocol.versionID,
            nameNodeAddr, conf);
    // get version and id info from the name-node
    NamespaceInfo nsInfo = handshake();
    StartupOption startOpt = getStartupOption(conf);
    assert startOpt != null : "Startup option must be set.";

    boolean simulatedFSDataset = conf.getBoolean("dfs.datanode.simulateddatastorage", false);
    if (simulatedFSDataset) {
        setNewStorageID(dnRegistration);
        dnRegistration.storageInfo.layoutVersion = FSConstants.LAYOUT_VERSION;
        dnRegistration.storageInfo.namespaceID = nsInfo.namespaceID;
        // it would have been better to pass storage as a parameter to
        // constructor below - need to augment ReflectionUtils used below.
        conf.set("StorageId", dnRegistration.getStorageID());
        try {
            //Equivalent of following (can't do because Simulated is in test dir)
            //  this.data = new SimulatedFSDataset(conf);
            this.data = (FSDatasetInterface) ReflectionUtils.newInstance(
                    Class.forName("org.apache.hadoop.hdfs.server.datanode.SimulatedFSDataset"), conf);
        } catch (ClassNotFoundException e) {
            throw new IOException(StringUtils.stringifyException(e));
        }
    } else { // real storage
        // read storage info, lock data dirs and transition fs state if necessary
        storage.recoverTransitionRead(nsInfo, dataDirs, startOpt);
        // adjust
        this.dnRegistration.setStorageInfo(storage);
        // initialize data node internal structure
        this.data = new FSDataset(storage, conf);
    }

    // register datanode MXBean
    this.registerMXBean(conf); // register the MXBean for DataNode

    // Allow configuration to delay block reports to find bugs
    artificialBlockReceivedDelay = conf.getInt("dfs.datanode.artificialBlockReceivedDelay", 0);

    // find free port or use privileged port provide
    ServerSocket ss;
    if (secureResources == null) {
        ss = (socketWriteTimeout > 0) ? ServerSocketChannel.open().socket() : new ServerSocket();
        Server.bind(ss, socAddr, 0);
    } else {
        ss = resources.getStreamingSocket();
    }
    ss.setReceiveBufferSize(DEFAULT_DATA_SOCKET_SIZE);
    // adjust machine name with the actual port
    tmpPort = ss.getLocalPort();
    selfAddr = new InetSocketAddress(ss.getInetAddress().getHostAddress(), tmpPort);
    this.dnRegistration.setName(machineName + ":" + tmpPort);
    LOG.info("Opened info server at " + tmpPort);

    this.threadGroup = new ThreadGroup("dataXceiverServer");
    this.dataXceiverServer = new Daemon(threadGroup, new DataXceiverServer(ss, conf, this));
    this.threadGroup.setDaemon(true); // auto destroy when empty

    this.blockReportInterval = conf.getLong("dfs.blockreport.intervalMsec", BLOCKREPORT_INTERVAL);
    this.initialBlockReportDelay = conf.getLong("dfs.blockreport.initialDelay", BLOCKREPORT_INITIAL_DELAY)
            * 1000L;
    if (this.initialBlockReportDelay >= blockReportInterval) {
        this.initialBlockReportDelay = 0;
        LOG.info("dfs.blockreport.initialDelay is greater than " + "dfs.blockreport.intervalMsec."
                + " Setting initial delay to 0 msec:");
    }
    this.heartBeatInterval = conf.getLong("dfs.heartbeat.interval", HEARTBEAT_INTERVAL) * 1000L;
    DataNode.nameNodeAddr = nameNodeAddr;

    //initialize periodic block scanner
    String reason = null;
    if (conf.getInt("dfs.datanode.scan.period.hours", 0) < 0) {
        reason = "verification is turned off by configuration";
    } else if (!(data instanceof FSDataset)) {
        reason = "verifcation is supported only with FSDataset";
    }
    if (reason == null) {
        blockScanner = new DataBlockScanner(this, (FSDataset) data, conf);
    } else {
        LOG.info("Periodic Block Verification is disabled because " + reason + ".");
    }

    //create a servlet to serve full-file content
    InetSocketAddress infoSocAddr = DataNode.getInfoAddr(conf);
    String infoHost = infoSocAddr.getHostName();
    int tmpInfoPort = infoSocAddr.getPort();
    this.infoServer = (secureResources == null)
            ? new HttpServer("datanode", infoHost, tmpInfoPort, tmpInfoPort == 0, conf,
                    SecurityUtil.getAdminAcls(conf, DFSConfigKeys.DFS_ADMIN))
            : new HttpServer("datanode", infoHost, tmpInfoPort, tmpInfoPort == 0, conf,
                    SecurityUtil.getAdminAcls(conf, DFSConfigKeys.DFS_ADMIN), secureResources.getListener());
    if (conf.getBoolean("dfs.https.enable", false)) {
        boolean needClientAuth = conf.getBoolean("dfs.https.need.client.auth", false);
        InetSocketAddress secInfoSocAddr = NetUtils
                .createSocketAddr(conf.get("dfs.datanode.https.address", infoHost + ":" + 0));
        Configuration sslConf = new Configuration(false);
        sslConf.addResource(conf.get("dfs.https.server.keystore.resource", "ssl-server.xml"));
        this.infoServer.addSslListener(secInfoSocAddr, sslConf, needClientAuth);
    }
    this.infoServer.addInternalServlet(null, "/streamFile/*", StreamFile.class);
    this.infoServer.addInternalServlet(null, "/getFileChecksum/*", FileChecksumServlets.GetServlet.class);

    this.infoServer.setAttribute("datanode", this);
    this.infoServer.setAttribute("datanode.blockScanner", blockScanner);
    this.infoServer.setAttribute(JspHelper.CURRENT_CONF, conf);
    this.infoServer.addServlet(null, "/blockScannerReport", DataBlockScanner.Servlet.class);

    if (WebHdfsFileSystem.isEnabled(conf, LOG)) {
        infoServer.addJerseyResourcePackage(
                DatanodeWebHdfsMethods.class.getPackage().getName() + ";" + Param.class.getPackage().getName(),
                WebHdfsFileSystem.PATH_PREFIX + "/*");
    }
    this.infoServer.start();
    // adjust info port
    this.dnRegistration.setInfoPort(this.infoServer.getPort());
    myMetrics = DataNodeInstrumentation.create(conf, dnRegistration.getStorageID());

    // set service-level authorization security policy
    if (conf.getBoolean(ServiceAuthorizationManager.SERVICE_AUTHORIZATION_CONFIG, false)) {
        ServiceAuthorizationManager.refresh(conf, new HDFSPolicyProvider());
    }

    // BlockTokenSecretManager is created here, but it shouldn't be
    // used until it is initialized in register().
    this.blockTokenSecretManager = new BlockTokenSecretManager(false, 0, 0);
    //init ipc server
    InetSocketAddress ipcAddr = NetUtils.createSocketAddr(conf.get("dfs.datanode.ipc.address"));
    ipcServer = RPC.getServer(this, ipcAddr.getHostName(), ipcAddr.getPort(),
            conf.getInt("dfs.datanode.handler.count", 3), false, conf, blockTokenSecretManager);
    dnRegistration.setIpcPort(ipcServer.getListenerAddress().getPort());

    LOG.info("dnRegistration = " + dnRegistration);
}

From source file:org.eredlab.g4.ccl.net.ftp.FTPClient.java

/**
 * Establishes a data connection with the FTP server, returning
 * a Socket for the connection if successful.  If a restart
 * offset has been set with {@link #setRestartOffset(long)},
 * a REST command is issued to the server with the offset as
 * an argument before establishing the data connection.  Active
 * mode connections also cause a local PORT command to be issued.
 * <p>/*w  ww. ja  va2  s .  co m*/
 * @param command  The text representation of the FTP command to send.
 * @param arg The arguments to the FTP command.  If this parameter is
 *             set to null, then the command is sent with no argument.
 * @return A Socket corresponding to the established data connection.
 *         Null is returned if an FTP protocol error is reported at
 *         any point during the establishment and initialization of
 *         the connection.
 * @exception IOException  If an I/O error occurs while either sending a
 *      command to the server or receiving a reply from the server.
 */
protected Socket _openDataConnection_(int command, String arg) throws IOException {
    Socket socket;

    if (__dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE
            && __dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE)
        return null;

    if (__dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE) {
        ServerSocket server;
        server = _socketFactory_.createServerSocket(0, 1, getLocalAddress());

        if (!FTPReply.isPositiveCompletion(port(getLocalAddress(), server.getLocalPort()))) {
            server.close();
            return null;
        }

        if ((__restartOffset > 0) && !restart(__restartOffset)) {
            server.close();
            return null;
        }

        if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
            server.close();
            return null;
        }

        // For now, let's just use the data timeout value for waiting for
        // the data connection.  It may be desirable to let this be a
        // separately configurable value.  In any case, we really want
        // to allow preventing the accept from blocking indefinitely.
        if (__dataTimeout >= 0)
            server.setSoTimeout(__dataTimeout);
        socket = server.accept();
        server.close();
    } else { // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE

        if (pasv() != FTPReply.ENTERING_PASSIVE_MODE)
            return null;

        __parsePassiveModeReply((String) _replyLines.elementAt(0));

        socket = _socketFactory_.createSocket(__passiveHost, __passivePort);
        if ((__restartOffset > 0) && !restart(__restartOffset)) {
            socket.close();
            return null;
        }

        if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
            socket.close();
            return null;
        }
    }

    if (__remoteVerificationEnabled && !verifyRemote(socket)) {
        InetAddress host1, host2;

        host1 = socket.getInetAddress();
        host2 = getRemoteAddress();

        socket.close();

        throw new IOException("Host attempting data connection " + host1.getHostAddress()
                + " is not same as server " + host2.getHostAddress());
    }

    if (__dataTimeout >= 0)
        socket.setSoTimeout(__dataTimeout);

    return socket;
}

From source file:uk.ac.soton.itinnovation.sad.service.services.EccIntegrationService.java

public boolean start() {

    logger.debug("Initialising ECC Integration service");
    createdProcesses = new ArrayList<>();

    boolean result = true;

    // If ECC intagration enabled
    if (!configurationService.getConfiguration().isEccEnabled()) {
        logger.debug("Nothing to do, ECC integration disabled");

    } else {/*from  ww  w  .j a  va2 s  .  co  m*/

        // Discover plugins
        JSONObject pluginInfo;
        String pathToPlugins = pluginsService.getAbsolutePluginsPath();
        JSONObject currentSadConfig = JSONObject.fromObject(configurationService.getConfiguration());
        String configurationFilePath;
        try {
            File tmp = File.createTempFile("sadconfiguration", ".json");
            tmp.deleteOnExit();

            File coordinatorPath = new File(configurationService.getConfiguration().getCoordinatorPath());

            currentSadConfig.put("coordinatorPathAbs", coordinatorPath.getAbsolutePath());

            FileWriter fileWriter = new FileWriter(tmp);
            fileWriter.write(currentSadConfig.toString());
            fileWriter.close();
            configurationFilePath = tmp.getAbsolutePath();
            logger.debug("Stored current configuration for plugins\n" + currentSadConfig.toString(2) + "\nin: "
                    + configurationFilePath);
        } catch (IOException e) {
            logger.error("Failed to create temporary configuration file", e);
            return false;
        }
        ServerSocket s;

        Thread tempThread;
        int pluginCounter = 0;
        for (String pluginName : pluginsService.getPluginNames()) {

            pluginCounter++;

            logger.debug("Processing plugin " + pluginCounter + ": " + pluginName);
            pluginInfo = pluginsService.getPluginByName(pluginName);

            if (pluginInfo.isEmpty()) {
                logger.error("Plugin not found: " + pluginName);
                continue;
            }

            if (!pluginInfo.containsKey("paths")) {
                logger.error(
                        "Plugin configuration must contain \'paths\'. Misconfigured plugin: " + pluginName);
                continue;
            }

            if (!pluginInfo.containsKey("pluginFolder")) {
                logger.error(
                        "Plugin configuration must be loaded with pluginsService that fills in the \'pluginFolder\' field. Misconfigured plugin: "
                                + pluginName);
                continue;
            }

            JSONObject pluginPaths = pluginInfo.getJSONObject("paths");

            if (!pluginPaths.containsKey("jar")) {
                logger.error(
                        "Plugin configuration must contain \'paths/jar\'. Misconfigured plugin: " + pluginName);
                continue;
            }
            if (!pluginPaths.containsKey("dependenciesFolder")) {
                logger.error(
                        "Plugin configuration must contain \'paths/dependenciesFolder\'. Misconfigured plugin: "
                                + pluginName);
                continue;
            }
            if (!pluginInfo.containsKey("pluginFolder")) {
                logger.error("Plugin configuration must contain \'pluginFolder\'. Misconfigured plugin: "
                        + pluginName);
                continue;
            }

            //                portNumber++;
            String jarPath = pluginPaths.getString("jar");
            String dependenciesFolderPath = pluginPaths.getString("dependenciesFolder");
            String pluginFolder = pluginInfo.getString("pluginFolder");
            String port = null;
            try {
                s = new ServerSocket(0);
                port = Integer.toString(s.getLocalPort());
                s.close();
            } catch (IOException ex) {
                logger.error("Failed to assign a new free port", ex);
                throw new RuntimeException(ex);
            }

            String uuid = configurationService.getConfiguration().getEcc().getClientsUuuidSeed()
                    + Integer.toHexString(pluginCounter);
            logger.debug("Using seeded UUID: " + uuid);

            logger.debug("Plugin jar path from configuration: " + jarPath + ", dependenciesFolderPath: "
                    + dependenciesFolderPath);
            logger.debug("Plugin folder configuration: " + pathToPlugins);
            logger.debug("Socket port: " + port);
            logger.debug("ECC client UUID: " + uuid);

            String[] command = new String[] { "java", "-cp",
                    pathToPlugins + fileSeparator + pluginFolder + fileSeparator + dependenciesFolderPath
                            + fileSeparator + "*",
                    "-jar", pathToPlugins + fileSeparator + pluginFolder + fileSeparator + jarPath, "ecc",
                    configurationFilePath, port, uuid };

            StringBuilder commandSb = new StringBuilder();
            for (String c : command) {
                commandSb.append(c);
                commandSb.append(" ");
            }
            String commandAsString = commandSb.toString();

            logger.debug("Launching ECC part of '" + pluginName + "' plugin with command: " + commandAsString
                    + " on port: " + port);

            pluginNamesAndPorts.put(pluginName, port);

            try {
                Process p = Runtime.getRuntime().exec(command);
                tempThread = new Thread(new ProcessWatcher(p));
                tempThread.start();
                createdProcesses.add(p);

            } catch (Throwable ex) {
                logger.error("Failed to start ECC part of '" + pluginName + "' plugin", ex);
                result = false;
            }
        }

    }

    return result;
}

From source file:org.apache.hadoop.dfs.DataNode.java

/**
 * This method starts the data node with the specified conf.
 * /*from w  w w  .  j a  va  2  s.c o m*/
 * @param conf - the configuration
 *  if conf's CONFIG_PROPERTY_SIMULATED property is set
 *  then a simulated storage based data node is created.
 * 
 * @param dataDirs - only for a non-simulated storage data node
 * @throws IOException
 */
void startDataNode(Configuration conf, AbstractList<File> dataDirs) throws IOException {
    // use configured nameserver & interface to get local hostname
    if (conf.get("slave.host.name") != null) {
        machineName = conf.get("slave.host.name");
    }
    if (machineName == null) {
        machineName = DNS.getDefaultHost(conf.get("dfs.datanode.dns.interface", "default"),
                conf.get("dfs.datanode.dns.nameserver", "default"));
    }
    InetSocketAddress nameNodeAddr = NameNode.getAddress(conf);

    this.estimateBlockSize = conf.getLong("dfs.block.size", DEFAULT_BLOCK_SIZE);
    this.socketTimeout = conf.getInt("dfs.socket.timeout", FSConstants.READ_TIMEOUT);
    this.socketWriteTimeout = conf.getInt("dfs.datanode.socket.write.timeout", FSConstants.WRITE_TIMEOUT);
    /* Based on results on different platforms, we might need set the default 
     * to false on some of them. */
    this.transferToAllowed = conf.getBoolean("dfs.datanode.transferTo.allowed", true);
    this.writePacketSize = conf.getInt("dfs.write.packet.size", 64 * 1024);
    String address = NetUtils.getServerAddress(conf, "dfs.datanode.bindAddress", "dfs.datanode.port",
            "dfs.datanode.address");
    InetSocketAddress socAddr = NetUtils.createSocketAddr(address);
    int tmpPort = socAddr.getPort();
    storage = new DataStorage();
    // construct registration
    this.dnRegistration = new DatanodeRegistration(machineName + ":" + tmpPort);

    // connect to name node
    this.namenode = (DatanodeProtocol) RPC.waitForProxy(DatanodeProtocol.class, DatanodeProtocol.versionID,
            nameNodeAddr, conf);
    // get version and id info from the name-node
    NamespaceInfo nsInfo = handshake();
    StartupOption startOpt = getStartupOption(conf);
    assert startOpt != null : "Startup option must be set.";

    boolean simulatedFSDataset = conf.getBoolean("dfs.datanode.simulateddatastorage", false);
    if (simulatedFSDataset) {
        setNewStorageID(dnRegistration);
        dnRegistration.storageInfo.layoutVersion = FSConstants.LAYOUT_VERSION;
        dnRegistration.storageInfo.namespaceID = nsInfo.namespaceID;
        // it would have been better to pass storage as a parameter to
        // constructor below - need to augment ReflectionUtils used below.
        conf.set("StorageId", dnRegistration.getStorageID());
        try {
            //Equivalent of following (can't do because Simulated is in test dir)
            //  this.data = new SimulatedFSDataset(conf);
            this.data = (FSDatasetInterface) ReflectionUtils
                    .newInstance(Class.forName("org.apache.hadoop.dfs.SimulatedFSDataset"), conf);
        } catch (ClassNotFoundException e) {
            throw new IOException(StringUtils.stringifyException(e));
        }
    } else { // real storage
        // read storage info, lock data dirs and transition fs state if necessary
        storage.recoverTransitionRead(nsInfo, dataDirs, startOpt);
        // adjust
        this.dnRegistration.setStorageInfo(storage);
        // initialize data node internal structure
        this.data = new FSDataset(storage, conf);
    }

    // find free port
    ServerSocket ss = (socketWriteTimeout > 0) ? ServerSocketChannel.open().socket() : new ServerSocket();
    Server.bind(ss, socAddr, 0);
    ss.setReceiveBufferSize(DEFAULT_DATA_SOCKET_SIZE);
    ss.setSoTimeout(conf.getInt("dfs.dataXceiver.timeoutInMS", 30000)); //30s
    // adjust machine name with the actual port
    tmpPort = ss.getLocalPort();
    selfAddr = new InetSocketAddress(ss.getInetAddress().getHostAddress(), tmpPort);
    this.dnRegistration.setName(machineName + ":" + tmpPort);
    LOG.info("Opened info server at " + tmpPort);

    this.maxXceiverCount = conf.getInt("dfs.datanode.max.xcievers", MAX_XCEIVER_COUNT);
    this.threadGroup = new ThreadGroup("dataXceiveServer");
    this.dataXceiveServer = new Daemon(threadGroup, new DataXceiveServer(ss));
    this.threadGroup.setDaemon(true); // auto destroy when empty

    this.blockReportInterval = conf.getLong("dfs.blockreport.intervalMsec", BLOCKREPORT_INTERVAL);
    this.initialBlockReportDelay = conf.getLong("dfs.blockreport.initialDelay", BLOCKREPORT_INITIAL_DELAY)
            * 1000L;
    if (this.initialBlockReportDelay >= blockReportInterval) {
        this.initialBlockReportDelay = 0;
        LOG.info("dfs.blockreport.initialDelay is greater than " + "dfs.blockreport.intervalMsec."
                + " Setting initial delay to 0 msec:");
    }
    this.heartBeatInterval = conf.getLong("dfs.heartbeat.interval", HEARTBEAT_INTERVAL) * 1000L;
    DataNode.nameNodeAddr = nameNodeAddr;

    this.balancingThrottler = new BlockBalanceThrottler(
            conf.getLong("dfs.balance.bandwidthPerSec", 1024L * 1024));

    //initialize periodic block scanner
    String reason = null;
    if (conf.getInt("dfs.datanode.scan.period.hours", 0) < 0) {
        reason = "verification is turned off by configuration";
    } else if (!(data instanceof FSDataset)) {
        reason = "verifcation is supported only with FSDataset";
    }
    if (reason == null) {
        blockScanner = new DataBlockScanner(this, (FSDataset) data, conf);
    } else {
        LOG.info("Periodic Block Verification is disabled because " + reason + ".");
    }

    //create a servlet to serve full-file content
    String infoAddr = NetUtils.getServerAddress(conf, "dfs.datanode.info.bindAddress", "dfs.datanode.info.port",
            "dfs.datanode.http.address");
    InetSocketAddress infoSocAddr = NetUtils.createSocketAddr(infoAddr);
    String infoHost = infoSocAddr.getHostName();
    int tmpInfoPort = infoSocAddr.getPort();
    this.infoServer = new StatusHttpServer("datanode", infoHost, tmpInfoPort, tmpInfoPort == 0);
    InetSocketAddress secInfoSocAddr = NetUtils
            .createSocketAddr(conf.get("dfs.datanode.https.address", infoHost + ":" + 0));
    Configuration sslConf = new Configuration(conf);
    sslConf.addResource(conf.get("https.keystore.info.rsrc", "sslinfo.xml"));
    String keyloc = sslConf.get("https.keystore.location");
    if (null != keyloc) {
        this.infoServer.addSslListener(secInfoSocAddr, keyloc, sslConf.get("https.keystore.password", ""),
                sslConf.get("https.keystore.keypassword", ""));
    }
    this.infoServer.addServlet(null, "/streamFile/*", StreamFile.class);
    this.infoServer.setAttribute("datanode.blockScanner", blockScanner);
    this.infoServer.addServlet(null, "/blockScannerReport", DataBlockScanner.Servlet.class);
    this.infoServer.start();
    // adjust info port
    this.dnRegistration.setInfoPort(this.infoServer.getPort());
    myMetrics = new DataNodeMetrics(conf, dnRegistration.getStorageID());

    //init ipc server
    InetSocketAddress ipcAddr = NetUtils.createSocketAddr(conf.get("dfs.datanode.ipc.address"));
    ipcServer = RPC.getServer(this, ipcAddr.getHostName(), ipcAddr.getPort(),
            conf.getInt("dfs.datanode.handler.count", 3), false, conf);
    ipcServer.start();
    dnRegistration.setIpcPort(ipcServer.getListenerAddress().getPort());

    LOG.info("dnRegistration = " + dnRegistration);
}