Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.apache.commons.net.io.Util.copyStream usage to parse a live telnet session

 
Piter Smith
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Apache WeatherTelnet example uses org.apache.commons.net.io.Util.copyStream to read and concurrently. The threading is done in examples.util.IOUtil, which I would like to change the behaviour of.

Currently, IOUtil sends a copy of the stream to System.out, which works fine for a telnet session.

However, I would like to add a new thread, which "intercepts" or reads the stream *live*.

I can't think of an approach which allows the processing of a stream while the stream is still open and while bytes are being read.

links:


http://svn.apache.org/repos/asf/commons/proper/net/trunk/src/main/java/examples/util/IOUtil.java

http://svn.apache.org/repos/asf/commons/proper/net/tags/NET_1_0_0/src/java/examples/weatherTelnet.java

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/io/Util.html#copyStream%28java.io.InputStream,%20java.io.OutputStream%29
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, just off the top of my head what I would try would be to call System.setOut to redirect System.out to some other PrintStream of your choosing. I'm not clear on what you want to do with this data, so I can't really suggest what kind of PrintStream you'd want to use.
 
Piter Smith
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, just off the top of my head what I would try would be to call System.setOut to redirect System.out to some other PrintStream of your choosing. I'm not clear on what you want to do with this data, so I can't really suggest what kind of PrintStream you'd want to use.


That sounds like the right approach. It's for a a MUD client:

http://en.wikipedia.org/wiki/Mud_client

For now, if I get it into a String, that would be sufficient. This sounds like a good idea, I was thinking along those lines.

Can I send System.setOut to a String, then send the String to the screen? There has to be some sort of output to the screen.
 
Luan Cestari
Ranch Hand
Posts: 163
C++ Redhat Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you could use http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/TeeOutputStream.html , creating it using both the Output to the file and for the standard output, then just setting this object into System.out (using a new PrintStream). Another option is to create your own OutputStream with your own business requirement.

Regards,
Luan Cestari
 
Piter Smith
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luan Cestari wrote:I think you could use http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/TeeOutputStream.html , creating it using both the Output to the file and for the standard output, then just setting this object into System.out (using a new PrintStream). Another option is to create your own OutputStream with your own business requirement.

Regards,
Luan Cestari


where were you last week!? Good link, thank you
 
Luan Cestari
Ranch Hand
Posts: 163
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piter Smith wrote:
Luan Cestari wrote:I think you could use http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/output/TeeOutputStream.html , creating it using both the Output to the file and for the standard output, then just setting this object into System.out (using a new PrintStream). Another option is to create your own OutputStream with your own business requirement.

Regards,
Luan Cestari


where were you last week!? Good link, thank you


Welcome I feel very happy to hear that =) So it worked fine, right? If you got any problem, just post here
 
Piter Smith
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on regex now, but will definitely go back and change that. Right now it's threaded, and it might need threads, but might run fine without:

https://github.com/THUFIR/TelnetConsole


so, I'm assuming, for now that it works. The API documentation is pretty clear.
 
Piter Smith
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luan Cestari wrote:

Welcome I feel very happy to hear that =) So it worked fine, right? If you got any problem, just post here




Sadly, no I don't see a provision in that Apache API for anything like the functionality of the tee command. On the plus, the real tee command could fit the bill and remove 2/3 of, and most of the complexity to, the project!

If I "telnet mymud.com 123 | tee processThis.txt" then I can, with relative ease, parse the end of that file?

When I run tail -f processThis.txt it *seems* to write output concurrently, as the man page says it will, or close enough! If you see a provision in the Apache API to actually *get* the split output, please let me know, but I see nothing along those lines. Apache will copy the stream, but there's no mechanism to get the copied stream(??).


-Piter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic