summaryrefslogtreecommitdiff
path: root/src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java')
-rw-r--r--src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java254
1 files changed, 125 insertions, 129 deletions
diff --git a/src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java b/src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java
index c8441ac..cafa0b8 100644
--- a/src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java
+++ b/src/YalpOutputs/YalpVlcTelnetOutput/TelnetInterface.java
@@ -1,5 +1,4 @@
-/***********************************************************************
- *
+/*
* Copyright (c) 2006 Manuel Traut and Volker Dahnke
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
@@ -7,8 +6,7 @@
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Manuel Traut and Volker Dahnke
- *
- ************************************************************************/
+ */
package YalpOutputs.YalpVlcTelnetOutput;
@@ -17,131 +15,129 @@ import java.io.*;
import org.apache.commons.net.telnet.*;
-/*******************************************************************************
-*
-* Class TelnetInterface
-*
-* <em>handels telnet connection</em>
-*
-* @author Volker Dahnke / Manuel Traut
-*
-* @version 0.1 20-11-2005<br>
-*
-* @see VlcStreamer
-*
-******************************************************************************/
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+/*
+ * Class TelnetInterface
+ *
+ * <em>handels telnet connection</em>
+ *
+ * @author Volker Dahnke / Manuel Traut
+ *
+ * @version 0.1 20-11-2005<br>
+ *
+ * @see VlcStreamer
+ */
+
public class TelnetInterface {
- private PrintStream out;
- private InputStream in;
- private TelnetClient tc = new TelnetClient();
-
- /**
- * creates socket, logs on
- *
- * @param host
- * hostname of telnetserver
- * @param port
- * destination port
- * @param pass
- * password
- *
- */
-
- public TelnetInterface(String host, int port, String pass){
- try{
- this.tc.connect(host,port);
- this.out = new PrintStream(this.tc.getOutputStream());
- this.in = this.tc.getInputStream();
- } catch(IOException e){
- System.out.println("server.TelnetInterface.java: Telnet connection "+host+":"+port+" failed");
- return;
- }
-
- readUntil( "Password:" );
- write(pass);
- // Advance to a prompt
- readUntil("> ");
- }
-
- /**
- * write to TelnetInterface
- * @param value
- * String to write to Interface
- */
- public void write( String value ) {
- try {
- out.println( value );
- out.flush();
- System.out.println( value );
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- }
-
- /**
- * reads output of telnet client
- * @param pattern
- * for stop reading
- * @return String
- */
- public String readUntil( String pattern ) {
- try {
- char lastChar = pattern.charAt( pattern.length() - 1 );
- StringBuffer sb = new StringBuffer();
- char ch = ( char )in.read();
- while( true ) {
- System.out.print( ch );
- sb.append( ch );
- if( ch == lastChar ) {
- if( sb.toString().endsWith( pattern ) ) {
- return sb.toString();
- }
- }
- ch = ( char )in.read();
- }
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * executes command
- * @param cmd
- * any telnet command
- * @return String
- * telnet output caused by the command
- * null if failed
- */
- public String exec(String cmd){
-
- try {
- write(cmd);
- return readUntil( "> " );
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
- return null;
- }
-
- /**
- * close telnet connection
- *
- * @return boolean
- * true: connection closed
- * false: connection close failed
- */
-
- public boolean close(){
- try {
- this.tc.disconnect();
- } catch(IOException e){
- return false;
- }
- return true;
- }
+ private PrintStream out;
+ private InputStream in;
+ private TelnetClient tc = new TelnetClient();
+
+ private String log4jFile = "log4j_output_plugin.conf";
+
+ private static Logger logger =
+ Logger.getLogger("Yalp.OutputPlugins.VlcTelnetOutput.TelnetInterface");
+
+ public TelnetInterface(String host, int port, String pass) {
+
+ PropertyConfigurator.configureAndWatch(log4jFile);
+ logger.debug("TelnetInterface("+host+" "+port+" "+pass+")");
+
+ try {
+ tc.connect(host, port);
+ out = new PrintStream(this.tc.getOutputStream());
+ in = this.tc.getInputStream();
+ } catch(IOException e) {
+ System.out.println("server.TelnetInterface.java: Telnet connection "+host+":"+port+" failed");
+ return;
+ }
+
+ readUntil( "Password:" );
+ write(pass);
+ // Advance to a prompt
+ readUntil("> ");
+ }
+
+/*
+ * write to TelnetInterface
+ * @param value
+ * String to write to Interface
+ */
+ public void write( String value ) {
+ logger.debug("write("+value+")");
+ try {
+ out.println( value );
+ out.flush();
+ } catch( Exception e ) {
+ e.printStackTrace();
+ }
+ }
+
+/*
+ * reads output of telnet client
+ * @param pattern
+ * for stop reading
+ * @return String
+ */
+ public String readUntil( String pattern ) {
+ logger.debug("readUntil("+pattern+")");
+ try {
+ char lastChar = pattern.charAt( pattern.length() - 1 );
+ StringBuffer sb = new StringBuffer();
+ char ch = ( char )in.read();
+ while( true ) {
+ System.out.print( ch );
+ sb.append( ch );
+ if( ch == lastChar ) {
+ if( sb.toString().endsWith( pattern ) ) {
+ return sb.toString();
+ }
+ }
+ ch = ( char )in.read();
+ }
+ } catch( Exception e ) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+/*
+ * executes command
+ * @param cmd
+ * any telnet command
+ * @return String
+ * telnet output caused by the command
+ * null if failed
+ */
+ public String exec(String cmd){
+ logger.debug("exec("+cmd+")");
+ try {
+ write(cmd);
+ return readUntil( "> " );
+ } catch( Exception e ) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+/*
+ * close telnet connection
+ *
+ * @return boolean
+ * true: connection closed
+ * false: connection close failed
+ */
+ public boolean close(){
+ logger.debug("close()");
+ try {
+ tc.disconnect();
+ } catch(IOException e) {
+ return false;
+ }
+ return true;
+ }
+
}