Exemplo n.º 1
0
 /* starts the socket server */
 private void startServer(boolean isDaemon, int pause) {
   // try to connect, if no connection then start new server
   try {
     System.err.println("testing connection");
     getClient().process("test");
   } catch (Exception ioe) {
     System.err.println("no connection, so starting server");
     // no connection, so start server
     server =
         new ServerThread(
             getModuleName() + "-server",
             getCodeCall() + " " + getPort() + " " + getParams(),
             isDaemon);
     server.start();
     try {
       // pause to allow loading data for some modules
       Thread.sleep(pause);
     } catch (Exception e) {
       System.err.println(e);
     }
     // keep a reference to the process provided through the thread to be
     // able to destroy it when the module gets destroyed.  Otherwise,
     // the process continues to run in the background even when the
     // module has been destroyed.
     p = server.getProcess();
     return;
   }
 }
Exemplo n.º 2
0
 /**
  * processes the input String and produces an output String.
  *
  * @param input the String to input to the underlying process
  * @throws java.lang.Exception exceptions from Runtime and Process that this class uses
  * @return the output from the underlying process
  */
 public String process(String input) throws Exception {
   String returnValue = new String();
   StringBuffer buffer = new StringBuffer();
   if (socketServer == true) {
     returnValue = client.process(input);
     ServerThread current = getServer();
     if (current != null) {
       if (!current.isDaemon()) {
         current.getProcess().destroy();
       }
       current.interrupt();
     }
   } else { // not a socket server
     Runtime rt = Runtime.getRuntime();
     String output = "";
     String errors = "";
     try {
       p = rt.exec(getCodeCall() + " " + getParams());
       PrintStream processInputStream = new PrintStream(p.getOutputStream(), true, "utf-8");
       processInputStream.println(input + "\n\u0003"); // put in garbage to kill Bikel's loop
       StreamConsumer errorConsumer = new StreamConsumer(p.getErrorStream(), "error", 1);
       StreamConsumer outputConsumer = new StreamConsumer(p.getInputStream(), "output", 10);
       outputConsumer.start();
       errorConsumer.start();
       int countloops = 0;
       int time = 0;
       String message = "";
       while (!outputConsumer.isComplete()) {
         // wait
         Thread.sleep(100);
         countloops++;
         time += 100; // one tenth of a second
         if (time > waittime) { // just wait 5 minutes
           message = "exceeded waittime of " + waittime + " milliseconds";
           break;
         }
       }
       errors = errorConsumer.getOutput();
       output = outputConsumer.getOutput();
       if (!message.equals("")) {
         errors = message;
       }
     } catch (IOException ioe) {
       System.err.println("Module error: " + getModuleName());
       ioe.printStackTrace();
       System.exit(0);
     }
     p.destroy();
     if (errors.equals("")) {
       returnValue = output;
     } else {
       returnValue = errors;
     }
   }
   return returnValue;
 }