Transcript Integrating Command Line Programs with Java Clients Will Fisher August 11
Integrating Command Line Programs with Java Clients
Will Fisher
PPPL Summer Intern Science Education Program
August 11
th
, 2005
Motivation
What did we want out of a command line system?
Access the suite of command line utilities from a standalone application.
Preserve the functionality and performance.
Minimize changes to the programs.
Editing the programs was not an option.
Must capture their output and feed them input.
Add graphical user interface to the command line.
Java is a good language to use for the client application given its portability.
Ultimate goal: Java client command line program.
Issues with Standard I/O
Buffering
Causes problems for reading and writing data as needed.
Java API does not work well, directly, with terminal applications because of buffering.
Java communication, apparently, has no such issues as client/servlet communication is handled fully and completely by the Java system.
PTY Implementation
Pseudo-terminals
Special interprocess communication channel that acts like a terminal. One end is called the
master
, the other end is called the
slave
. Data written to the master is received by the slave as if it were the result of a user typing at an ordinary terminal.
Data written to the slave is sent to the master as if it were written on an ordinary terminal.
Our use of them
Used for communication with the command line program.
Fakes a terminal device that reads from and writes to the command line program.
Set terminal attributes in order to kill the buffering and the echoing.
CLTerm Technology Solution
Forking the program
Use the forkpty() command to fork CLTerm into two processes.
The two processes are connected via a master-slave pseudo-terminal.
The child process replaces its own program image with the image of the command line program by calling the subroutine execl(program).
The new program thinks it’s running normally but is connected to a pseudo-terminal.
The parent monitors the output and feeds input to the child.
Java client CLTerm Command Line Program
Java Client 1 Java Client 2 Bob’s Computer HTTP Servlet
System Architecture
Java Client 3 Alice’s Computer User Computers Java Applet Frank’s Computer The Network Chasm clterm clterm clterm clterm Transpgrid — PPPL Cluster genxpl rplot trxpl rplot
The Servlet
Servlet complements Java applet / application
Applet has limited “sandbox” on user’s computer Servlet runs on cluster – http connection to client Run programs Access user files Enforce security Added 2 methods: Runcl – start a new clterm session Inputcl – send a line of user input to clterm
CLTerm.c
How it works
One instance of CLTerm is launched for each client.
Each instance connects to one command line program.
It forks a new child process which in turn launches the program in its place.
They talk via a pseudo-terminal and CLTerm forwards the output to the servlet and feeds the data it gets from the servlet into the child via the pseudo-terminal.
CLTerm checks each iteration of its I/O loop, that the child is still “alive.” If not then it sends an exit byte down the line.
CLPanel.java
How it works
CLPanel inherits from JPanel and can be implemented in either a JFrame or a JInternalFrame like in ElVis.
Has all the communication and GUI code.
Has an I/O loop that first reads the user’s input and then looks for the program’s response, sent via the servlet.
If the user closes the CLPanel window, it will send an exit byte (ASCII code 25) down the line so that everything else is informed.
CLTerm In Action
Conclusion
End result and advantages
Successfully integrated a solution for Java clients to talk to command line programs.
Minimal changes to the existing infrastructure.
CLTerm is appropriate for larger applications given its encapsulated nature.
CLPanel lets Java developers incorporate this panel into their own applications.
CLPanel is a class which can be instantiated multiple times in the same application: multiple windows/connections in the same app.
Can also run as an Applet in a browser.
Opportunity to expand PPL Fusion Grid Services.