List of usage examples for java.net ServerSocket getLocalPort
public int getLocalPort()
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testExitNoRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/* w w w . jav a2 s . c om*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 0); SocketSourceThread runner = new SocketSourceThread(source); runner.start(); channel = server.accept(); channel.close(); try { runner.waitUntilDone(); } catch (Exception e) { assertTrue(e.getCause() instanceof EOFException); } } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:VASSAL.launch.TilingHandler.java
protected void runSlicer(List<String> multi, final int tcount, int maxheap) throws CancellationException, IOException { final InetAddress lo = InetAddress.getByName(null); final ServerSocket ssock = new ServerSocket(0, 0, lo); final int port = ssock.getLocalPort(); final List<String> args = new ArrayList<String>(); args.addAll(Arrays.asList(new String[] { Info.javaBinPath, "-classpath", System.getProperty("java.class.path"), "-Xmx" + maxheap + "M", "-DVASSAL.id=" + pid, "-Duser.home=" + System.getProperty("user.home"), "-DVASSAL.port=" + port, "VASSAL.tools.image.tilecache.ZipFileImageTiler", aname, cdir.getAbsolutePath(), String.valueOf(tdim.width), String.valueOf(tdim.height) })); // get the progress dialog final ProgressDialog pd = ProgressDialog.createOnEDT(ModuleManagerWindow.getInstance(), "Processing Image Tiles", " "); // set up the process final InputStreamPump outP = new InputOutputStreamPump(null, System.out); final InputStreamPump errP = new InputOutputStreamPump(null, System.err); final ProcessWrapper proc = new ProcessLauncher().launch(null, outP, errP, args.toArray(new String[args.size()])); // write the image paths to child's stdin, one per line PrintWriter stdin = null;/*w w w. j a v a 2 s .c o m*/ try { stdin = new PrintWriter(proc.stdin); for (String m : multi) { stdin.println(m); } } finally { IOUtils.closeQuietly(stdin); } Socket csock = null; DataInputStream in = null; try { csock = ssock.accept(); csock.shutdownOutput(); in = new DataInputStream(csock.getInputStream()); final Progressor progressor = new Progressor(0, tcount) { @Override protected void run(Pair<Integer, Integer> prog) { pd.setProgress((100 * prog.second) / max); } }; // setup the cancel button in the progress dialog EDT.execute(new Runnable() { public void run() { pd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { pd.setVisible(false); proc.future.cancel(true); } }); } }); boolean done = false; byte type; while (!done) { type = in.readByte(); switch (type) { case STARTING_IMAGE: final String ipath = in.readUTF(); EDT.execute(new Runnable() { public void run() { pd.setLabel("Tiling " + ipath); if (!pd.isVisible()) pd.setVisible(true); } }); break; case TILE_WRITTEN: progressor.increment(); if (progressor.get() >= tcount) { pd.setVisible(false); } break; case TILING_FINISHED: done = true; break; default: throw new IllegalStateException("bad type: " + type); } } in.close(); csock.close(); ssock.close(); } catch (IOException e) { } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(csock); IOUtils.closeQuietly(ssock); } // wait for the tiling process to end try { final int retval = proc.future.get(); if (retval != 0) { throw new IOException("return value == " + retval); } } catch (ExecutionException e) { // should never happen throw new IllegalStateException(e); } catch (InterruptedException e) { // should never happen throw new IllegalStateException(e); } }
From source file:uk.co.unclealex.persistence.hbase.testing.HBaseTestContainer.java
/** * Configure ports either statically or dynamically. This method is called * recursively (and hence why an {@link Iterator} is an argument) to allow the * compiler to correctly check that all port resources are closed. * /*from www . jav a 2 s .c o m*/ * @param portIterator * An {@link Iterator} for the remaining ports to configure. * @throws IOException * Signals that an I/O exception has occurred. */ protected void configurePorts(Iterator<Port> portIterator) throws IOException { if (portIterator.hasNext()) { Port port = portIterator.next(); Integer portNumber = getFixedPorts().get(port); if (portNumber == null) { portNumber = 0; } ServerSocket serverSocket = new ServerSocket(portNumber); try { int localPort = serverSocket.getLocalPort(); LOG.info("Using port " + localPort + " for property " + port.getPropertyName()); getConfiguration().setInt(port.getPropertyName(), localPort); getPorts().put(port, localPort); configurePorts(portIterator); } finally { serverSocket.close(); } } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceSimpleOutput() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/* w ww . ja v a2s .c o m*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 0); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check"); runner.start(); channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("test1\n"); writer.write("check\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.interrupt(); runner.waitUntilDone(); channel.close(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.apache.flink.runtime.taskmanager.TaskManagerStartupTest.java
/** * Tests that the TaskManager fails synchronously when the actor system port * is in use.// w w w . j ava 2 s .c om * * @throws Throwable */ @Test(expected = BindException.class) public void testStartupWhenTaskmanagerActorPortIsUsed() throws BindException { ServerSocket blocker = null; try { final String localHostName = "localhost"; final InetAddress localBindAddress = InetAddress.getByName(NetUtils.getWildcardIPAddress()); // block some port blocker = new ServerSocket(0, 50, localBindAddress); final int port = blocker.getLocalPort(); TaskManager.runTaskManager(localHostName, ResourceID.generate(), port, new Configuration(), TaskManager.class); fail("This should fail with an IOException"); } catch (IOException e) { // expected. validate the error message List<Throwable> causes = StartupUtils.getExceptionCauses(e, new ArrayList<Throwable>()); for (Throwable cause : causes) { if (cause instanceof BindException) { throw (BindException) cause; } } fail("This should fail with an exception caused by BindException"); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } finally { if (blocker != null) { try { blocker.close(); } catch (IOException e) { // no need to log here } } } }
From source file:org.apache.flink.runtime.taskmanager.TaskManagerStartupTest.java
/** * Tests that the task manager start-up fails if the network stack cannot be initialized. * @throws Exception//from w w w .j a v a 2s . c o m */ @Test(expected = IOException.class) public void testStartupWhenNetworkStackFailsToInitialize() throws Exception { ServerSocket blocker = null; try { blocker = new ServerSocket(0, 50, InetAddress.getByName("localhost")); final Configuration cfg = new Configuration(); cfg.setString(ConfigConstants.TASK_MANAGER_HOSTNAME_KEY, "localhost"); cfg.setInteger(ConfigConstants.TASK_MANAGER_DATA_PORT_KEY, blocker.getLocalPort()); cfg.setInteger(ConfigConstants.TASK_MANAGER_MEMORY_SIZE_KEY, 1); TaskManager.startTaskManagerComponentsAndActor(cfg, ResourceID.generate(), null, "localhost", Option.<String>empty(), Option.<LeaderRetrievalService>empty(), false, TaskManager.class); } finally { if (blocker != null) { try { blocker.close(); } catch (IOException e) { // ignore, best effort } } } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceOutputWithRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/*ww w .j a va 2 s .c om*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check"); runner.start(); // first connection: nothing channel = server.accept(); channel.close(); // second connection: first string channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("test1\n"); writer.close(); channel.close(); // third connection: nothing channel = server.accept(); channel.close(); // forth connection: second string channel = server.accept(); writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("check\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.waitUntilDone(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceOutputInfiniteRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/*from ww w . j a v a 2 s . c o m*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", -1, 100); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check"); runner.start(); // first connection: nothing channel = server.accept(); channel.close(); // second connection: first string channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("test1\n"); writer.close(); channel.close(); // third connection: nothing channel = server.accept(); channel.close(); // forth connection: second string channel = server.accept(); writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("check\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.waitUntilDone(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java
@Test public void testSocketSourceOutputAcrossRetries() throws Exception { ServerSocket server = new ServerSocket(0); Socket channel = null;/*w ww . j ava2 s. com*/ try { SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n", 10, 100); SocketSourceThread runner = new SocketSourceThread(source, "test1", "check1", "check2"); runner.start(); // first connection: nothing channel = server.accept(); channel.close(); // second connection: first string channel = server.accept(); OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("te"); writer.close(); channel.close(); // third connection: nothing channel = server.accept(); channel.close(); // forth connection: second string channel = server.accept(); writer = new OutputStreamWriter(channel.getOutputStream()); writer.write("st1\n"); writer.write("check1\n"); writer.write("check2\n"); writer.flush(); runner.waitForNumElements(2); runner.cancel(); runner.waitUntilDone(); } finally { if (channel != null) { IOUtils.closeQuietly(channel); } IOUtils.closeQuietly(server); } }
From source file:org.apache.http.localserver.LocalTestServer.java
/** * Obtains the port this server is servicing. * * @return the service port/*from w w w . ja va 2s . c om*/ */ public int getServicePort() { ServerSocket ssock = servicedSocket; // avoid synchronization if (ssock == null) throw new IllegalStateException("not running"); return ssock.getLocalPort(); }