List of usage examples for org.apache.commons.net.telnet TelnetClient getInputStream
public InputStream getInputStream()
From source file:org.crsh.telnet.term.AbstractTelnetTestCase.java
@Before public final void setUp() throws Exception { int port = PORTS.getAndIncrement(); ////from w ww. j a v a2s .c o m IOHandler handler = new IOHandler(); // SimplePluginDiscovery discovery = new SimplePluginDiscovery(); discovery.add(new TelnetPlugin()); discovery.add(handler); // ctx = new TestPluginLifeCycle(new TelnetPlugin(), handler); ctx.setProperty(TelnetPlugin.TELNET_PORT, port); // ctx.start(); // TelnetClient client = new TelnetClient(); for (int retry_count = 0; retry_count < CLIENT_CONNECT_RETRY_LIMIT; retry_count++) { try { client.connect("localhost", port); break; } catch (IOException e) { if (retry_count < CLIENT_CONNECT_RETRY_LIMIT) { Thread.sleep(CLIENT_CONNECT_RETRY_SLEEP); } else { throw e; } } } // this.out = client.getOutputStream(); this.in = client.getInputStream(); this.handler = handler; this.client = client; this.running = true; }
From source file:org.mule.transport.telnet.TelnetClientWrapper.java
public TelnetClientWrapper(String host, int port, TelnetMessageDispatcher dispatcher, int waitTime) throws IOException { TelnetClient client = new TelnetClient(); TelnetOptionHandler handler = new EchoOptionHandler(true, true, true, true); try {/*from www . ja v a 2 s . c o m*/ client.addOptionHandler(handler); } catch (InvalidTelnetOptionException e) { dispatcher.exceptionThrown(e); } //TODO enable LINEMODE client.connect(host, port); if (!client.isConnected()) { throw new ConnectException("cannot connect : " + host + ":" + port); } setWaitTime(waitTime); this.responseTimeout = dispatcher.getEndpoint().getResponseTimeout(); this.client = client; this.dispatcher = dispatcher; this.encoding = dispatcher.getEndpoint().getEncoding(); exitStatusCommand = dispatcher.getConnector().getExitStatusCommand(); reader = new BufferedInputStream(client.getInputStream(), 1024); writer = new BufferedOutputStream(client.getOutputStream(), 1024); client.setSoTimeout(responseTimeout + waitTime); client.setReaderThread(true); if (logger.isTraceEnabled()) logger.trace(BeanUtils.describe(this)); }
From source file:org.openhab.binding.ddwrt.internal.DDWRTBinding.java
/** * Receive answer from DD-WRT - careful! This blocks if there is no answer * from DD-WRT/*from w w w .j av a 2 s . c o m*/ * * @param client * the telnet client * @return */ private static String receive(TelnetClient client) { StringBuffer strBuffer; try { strBuffer = new StringBuffer(); byte[] buf = new byte[4096]; int len = 0; Thread.sleep(750L); while ((len = client.getInputStream().read(buf)) != 0) { strBuffer.append(new String(buf, 0, len)); Thread.sleep(750L); if (client.getInputStream().available() == 0) { break; } } return strBuffer.toString(); } catch (Exception e) { logger.warn("Error receiving data", e); } return null; }
From source file:org.openhab.binding.fritzbox.internal.FritzboxBinding.java
/** * Receive answer from FritzBox - careful! This blocks if there is no answer * from FritzBox/*w w w. ja va 2s. co m*/ * * @param client * the telnet client * @return */ private static String receive(TelnetClient client) { StringBuffer strBuffer; try { strBuffer = new StringBuffer(); byte[] buf = new byte[4096]; int len = 0; Thread.sleep(750L); while ((len = client.getInputStream().read(buf)) != 0) { strBuffer.append(new String(buf, 0, len)); Thread.sleep(750L); if (client.getInputStream().available() == 0) break; } return strBuffer.toString(); } catch (Exception e) { logger.warn("Error receiving data", e); } return null; }
From source file:org.openremote.controller.protocol.telnet.TelnetCommand.java
/** * Read from the telnet session until the string we are waiting for is found or the timeout has been reached. The * timeout is 1 second// w w w . ja v a2 s . c o m * * @param s The string to wait on * @param tc The instance of the TelnetClient */ private void waitForString(String s, TelnetClient tc) throws Exception { InputStream is = tc.getInputStream(); StringBuffer sb = new StringBuffer(); Calendar endTime = Calendar.getInstance(); endTime.add(Calendar.SECOND, getTimeOut()); while (sb.toString().indexOf(s) == -1) { while (Calendar.getInstance().before(endTime) && is.available() == 0) { Thread.sleep(250); } if (is.available() == 0) { logger.info("WaitForString read before running into timeout: " + sb.toString()); throw new Exception("waitForString response timed-out waiting for \"" + s + "\""); } sb.append((char) is.read()); } logger.info("WaitForString received: " + sb.toString()); }
From source file:org.openremote.controller.protocol.telnet.TelnetCommand.java
private void readString(String s, TelnetClient tc) throws Exception { InputStream is = tc.getInputStream(); StringBuffer sb = new StringBuffer(); Calendar endTime = Calendar.getInstance(); endTime.add(Calendar.SECOND, getTimeOut()); while (sb.toString().indexOf(s) == -1) { while (Calendar.getInstance().before(endTime) && is.available() == 0) { Thread.sleep(250);/*from ww w . j a va 2 s . c o m*/ } if (is.available() == 0) { if ("null".equals(s)) { break; } logger.info("Read before running into timeout: " + sb.toString()); throw new Exception("Response timed-out waiting for \"" + s + "\""); } sb.append((char) is.read()); } logger.info("received: " + sb.toString()); this.response += sb.toString() + "\n"; }
From source file:org.xerela.net.sim.telnet.TelnetTest.java
/** * /* w w w . j a v a 2 s.co m*/ * @throws Exception */ public void testTelnet() throws Exception { /* Start: * +------------+ * ,-! Send Input ! * ! +------------+ * ! | * ! +------------+ * ! ! Read Input !------. no * ! +------------+ \ * !yes | | * ! +------------+ no +------------+ yes +------+ * `-! IsCorrect? !----! IsTooLong? !-----! FAIL ! * +------------+ +------------+ +------+ * */ IpAddress local = IpAddress.getIpAddress(Util.getLocalHost(), null); TelnetClient client = new TelnetClient(); client.connect(local.getRealAddress()); RecordingLoader recordingLoader = RecordingLoader.getInstance(); Configuration config = ConfigurationService.getInstance() .findConfigurationFile(ConfigurationService.DEFAULT_CONFIG); WorkingConfig wc = config.getDefaultOperationWorkingConfig(); // create the operation manually first so that we can easily get the records RecordingOperation operation = (RecordingOperation) recordingLoader.createOperation(wc, local, local); Interaction[] interactions = operation.getInteractions(); operation.tearDown(); BufferedInputStream in = new BufferedInputStream(client.getInputStream()); PrintStream out = new PrintStream(client.getOutputStream(), true); byte[] bbuf = new byte[2048]; CharSequenceBuffer cbuf = new CharSequenceBuffer(); for (int i = 0; i < interactions.length; i++) { Interaction currInteraction = interactions[i]; String proto = currInteraction.getCliProtocol(); if (!proto.equals("Telnet")) { /* * Because the recording might have other protocols we should stop when we encounter one. * The recording may not behave properly if we continue as we are. * If we got through at least 10 interactions then this test is probably still valid. */ assertTrue( "At least 10 telnet interction should have been handled. Maybe this test should be run with another recording.", i > 10); System.err.println( "Continueing could disrupt the validity of this test. This test will only support Telnet operations."); break; } // The timeout will be four times the expected time or 4 seconds, whichever is longer. Long interactionTime = currInteraction.getEndTime() - currInteraction.getStartTime(); long time = Math.max((long) (interactionTime * wc.getRateMultiplier()) * 4, 4000); long start = System.currentTimeMillis(); CharSequence input = currInteraction.getCliCommand(); CharSequence response = currInteraction.getCliResponse(); if (!input.equals("No input sent") && !input.equals("")) { out.println(input); } cbuf.reset(); while (true) { int len = 0; if (in.available() <= 0) { try { Thread.sleep(250); } catch (InterruptedException e) { e.printStackTrace(); } if (in.available() <= 0) { if (System.currentTimeMillis() - start > time) { // isTooLong fail("Timeout reached waiting for response for interaction '" + currInteraction.getCliCommand() + "'"); } continue; } } len = in.read(bbuf); cbuf.write(bbuf, 0, len); // isCorrect? if (compare(cbuf, response)) { break; } else if (System.currentTimeMillis() - start > time) { // isTooLong fail("Timeout reached waiting for response for interaction '" + currInteraction.getCliCommand() + "'"); } } } }