Example 4: Remote control of GUESS.

InfoInfo
Search:    

Example 4: Remote control of GUESS

When loaded the following toolbar will allow you to telnet, or open a socket to, the machine running GUESS. Every command typed in the remote end will be executed in the GUESS client. The script is available as dockexample4, but is included below (to execute load the script and then type guessnetwork()):

import java
import javax.swing
import com

# this is our toolbar
class dockexample4(com.hp.hpl.guess.ui.DockableAdapter):

       myLabel = javax.swing.JLabel("Waiting for connection")

       def __init__(self):
              # add our toolbar
              self.add(self.myLabel)
              ui.dock(self)

       def getTitle(self):
              return("dockexample4")

       def update(self,val):
              # eval or execute the command as appropriate
              self.myLabel.setText(val);
              try:
                     eval(val)
              except (SyntaxError,ValueError,NameError):
                     try:
                           exec(val)
                     except (SyntaxError,ValueError,NameError):
                           self.myLabel.setText("command error "+val)
              v.repaint()

# extend the java thread object
class guessnetwork(java.lang.Thread):

       # keep a reference to our toolbar
       screeninterface = None;

       def __init__(self):
              self.screeninterface = dockexample4()
              # start the thread
              self.start()

       def run(self):
              mySocket = java.net.ServerSocket(2222) # run on port 2222
                                clientSocket = mySocket.accept() # accept a connection
              inst = java.io.BufferedReader(java.io.InputStreamReader(clientSocket.inputStream))
              val = inst.readLine() # read the line, and execute:
              while (val != None):
                     self.screeninterface.update(val)
                     val = inst.readLine()
This is a Wiki Spot wiki. Wiki Spot is a non-profit organization that helps communities collaborate via wikis.