[BACK]Return to oxdStream.java CVS log [TXT][DIR] Up to [local] / OpenXM / src / OpenMath / ORG / openxm / tam

File: [local] / OpenXM / src / OpenMath / ORG / openxm / tam / oxdStream.java (download)

Revision 1.2, Mon Oct 28 00:38:32 2002 UTC (21 years, 8 months ago) by takayama
Branch: MAIN
CVS Tags: R_1_3_1-2, RELEASE_1_3_1_13b, RELEASE_1_2_3_12, RELEASE_1_2_3, RELEASE_1_2_2_KNOPPIX_b, RELEASE_1_2_2_KNOPPIX, RELEASE_1_2_2, KNOPPIX_2006, HEAD, DEB_REL_1_2_3-9
Changes since 1.1: +10 -2 lines

Made the following change for the oxd starting protocol:
  1. ox itself has to finish.  ( ox -finish )
  2. oxd waits the termination of ox and gets the result code.
  3. if the result code is 0, then oxd returns <suceeded/>, else
     oxd returns <failed code=xxyy/>
  4. The client of oxd receives the result (<suceeded/> or <failed/>) and
     sends <login/> and close the session.

/* $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/oxdStream.java,v 1.2 2002/10/28 00:38:32 takayama Exp $ */
package ORG.openxm.tam;

import java.io.* ;
import java.net.*;
import java.util.*;
import java.lang.*;

public class oxdStream {
  private InputStream is = null;
  private OutputStream os = null;
  private Socket socket = null;
  private static long seed = 0;
  private static Random rrr = new Random(0);
 
  public oxdStream() {
	// dummy
  }
	
  public oxdStream(int port) throws IOException {
	  try {
		  socket = new Socket("localhost",port);
	  }catch(IOException e) {
		  System.err.println("The oxd server at localhost:"+port+" does not exist.\n");
	  }
	is = new BufferedInputStream(socket.getInputStream());
	os = socket.getOutputStream();
  }

  public oxdStream(String host,int port) throws IOException {
    socket = new Socket(host,port);
	is = new BufferedInputStream(socket.getInputStream());
	os = socket.getOutputStream();
  }

  public void write(String s) throws IOException {
	os.write(s.getBytes());
    os.flush();
  }

  public String getFileName(String s) {
	int i;
	int start = -1;
	int end;
	for (i=0; i<s.length(); i++) {
	  if (s.regionMatches(i,"file=\"",0,6)) {
		start = i+6;
		break;
	  }
	}
	if (start == -1) return null;
	end = s.length()-1;
	for (i=start; i<s.length(); i++) {
	  if (s.regionMatches(i,"\"",0,1)) {
		end = i;
	  }
	}
	return (new String(s.getBytes(),start,end-start) );
  }
  public String getTagName(String s) {
	int i;
	int start = -1;
	int end;
	for (i=0; i<s.length(); i++) {
	  if (s.regionMatches(i,"<",0,1)) {
		start = i+1;
		break;
	  }
	}
	if (start == -1) return null;
	end = s.length()-1;
	for (i=start; i<s.length(); i++) {
	  if (s.regionMatches(i,">",0,1)) {
		  end = i; break;
	  }
	  if (s.regionMatches(i,"/>",0,2)) {
		  end = i; break;
	  }
	  if (s.regionMatches(i," ",0,1)) {
		  end = i; break;
	  }
	}
	return (new String(s.getBytes(),start,end-start) );
  }

	public void touch(String fname) throws IOException {
		FileOutputStream fp = new FileOutputStream(fname);
		fp.write(0);
		fp.close();
	}
	
	public int getRandomPortNumber() {
		int fn = (int) Runtime.getRuntime().freeMemory(); // java.lang.Runtime.freeMemory()
		int p;
		if (seed == 0) {
			seed = (long) System.currentTimeMillis() // java.lang.System.Current...
				+ (long) fn;
			rrr = new Random(seed);
		}
		for (int i=0; i<fn % 1000; i++) rrr.nextInt();
		p = rrr.nextInt();
		if (p < 0) p = -p;
		return (2000 + (p % 6000));
	}
	
  public String readUntil(char u) throws IOException {
		String s = "";
		char c = 0;
		while (c != u) {
			c = (char) is.read();  // BUG
			s += c;
		}
		return s;
	}

	public int startPhase1() throws IOException {
		String s;
		String ans;
		String fname;
		int port;
		
		readUntil('?');
		s = "<login method=\"file\">\n";
		System.err.println(s);
		write(s);

		ans = readUntil('?');
		System.err.println(ans);
		if (!getTagName(ans).equals("challenge")) {
			; // not yet written
		}
		port = getRandomPortNumber();
		fname = getFileName(ans);
		touch(fname);

		s = "<done/>\n";
		System.err.println(s);
		write(s);
		ans = readUntil('?');
		return port;
	}
    // Open the port and port2 for listenning.
	public void startPhase2(String oxname,int port) throws IOException {
		String s;
		String ans;
		String fname;
		int port2;
		port2 = port+1;
		
        s="<launch> ox -ox "+oxname+" -reverse -control "+port+" -data "+port2 
		  +	" -finish </launch> \n";
		System.err.println(s);
		write(s);
		ans = readUntil('?');
		System.err.println(ans);
		write("<login/>\n");
		if (getTagName(ans).equals("suceeded")) {
		  
		}else{
		  throw new IOException("OX server could not be started.");
		}
	}

  public static void main0(String[] argv) throws IOException {
 	 oxdStream oxd = new oxdStream();
     String s = "<challenge file=\"/home/nobuki/.oxd123\"/>";
     System.err.println( oxd.getFileName(s) );
     System.err.println( oxd.getTagName(s) );
     System.err.println( oxd.getRandomPortNumber() );
     System.err.println( oxd.getRandomPortNumber() );
     System.err.println( oxd.getRandomPortNumber() );
  }
  public static void main(String[] argv) throws IOException {
	  int port;
 	 oxdStream oxd = new oxdStream(8089);
	 port = oxd.startPhase1();
     // start for listenning.
	 oxd.startPhase2("ox_asir",port);
  }
}