Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/oxdStream.java, Revision 1.1
1.1 ! takayama 1: /* $OpenXM$ */
! 2: package ORG.openxm.tam;
! 3:
! 4: import java.io.* ;
! 5: import java.net.*;
! 6: import java.util.*;
! 7: import java.lang.*;
! 8:
! 9: public class oxdStream {
! 10: private InputStream is = null;
! 11: private OutputStream os = null;
! 12: private Socket socket = null;
! 13: private static long seed = 0;
! 14: private static Random rrr = new Random(0);
! 15:
! 16: public oxdStream() {
! 17: // dummy
! 18: }
! 19:
! 20: public oxdStream(int port) throws IOException {
! 21: try {
! 22: socket = new Socket("localhost",port);
! 23: }catch(IOException e) {
! 24: System.err.println("The oxd server at localhost:"+port+" does not exist.\n");
! 25: }
! 26: is = new BufferedInputStream(socket.getInputStream());
! 27: os = socket.getOutputStream();
! 28: }
! 29:
! 30: public oxdStream(String host,int port) throws IOException {
! 31: socket = new Socket(host,port);
! 32: is = new BufferedInputStream(socket.getInputStream());
! 33: os = socket.getOutputStream();
! 34: }
! 35:
! 36: public void write(String s) throws IOException {
! 37: os.write(s.getBytes());
! 38: os.flush();
! 39: }
! 40:
! 41: public String getFileName(String s) {
! 42: int i;
! 43: int start = -1;
! 44: int end;
! 45: for (i=0; i<s.length(); i++) {
! 46: if (s.regionMatches(i,"file=\"",0,6)) {
! 47: start = i+6;
! 48: break;
! 49: }
! 50: }
! 51: if (start == -1) return null;
! 52: end = s.length()-1;
! 53: for (i=start; i<s.length(); i++) {
! 54: if (s.regionMatches(i,"\"",0,1)) {
! 55: end = i;
! 56: }
! 57: }
! 58: return (new String(s.getBytes(),start,end-start) );
! 59: }
! 60: public String getTagName(String s) {
! 61: int i;
! 62: int start = -1;
! 63: int end;
! 64: for (i=0; i<s.length(); i++) {
! 65: if (s.regionMatches(i,"<",0,1)) {
! 66: start = i+1;
! 67: break;
! 68: }
! 69: }
! 70: if (start == -1) return null;
! 71: end = s.length()-1;
! 72: for (i=start; i<s.length(); i++) {
! 73: if (s.regionMatches(i,">",0,1)) {
! 74: end = i; break;
! 75: }
! 76: if (s.regionMatches(i,"/>",0,2)) {
! 77: end = i; break;
! 78: }
! 79: if (s.regionMatches(i," ",0,1)) {
! 80: end = i; break;
! 81: }
! 82: }
! 83: return (new String(s.getBytes(),start,end-start) );
! 84: }
! 85:
! 86: public void touch(String fname) throws IOException {
! 87: FileOutputStream fp = new FileOutputStream(fname);
! 88: fp.write(0);
! 89: fp.close();
! 90: }
! 91:
! 92: public int getRandomPortNumber() {
! 93: int fn = (int) Runtime.getRuntime().freeMemory(); // java.lang.Runtime.freeMemory()
! 94: int p;
! 95: if (seed == 0) {
! 96: seed = (long) System.currentTimeMillis() // java.lang.System.Current...
! 97: + (long) fn;
! 98: rrr = new Random(seed);
! 99: }
! 100: for (int i=0; i<fn % 1000; i++) rrr.nextInt();
! 101: p = rrr.nextInt();
! 102: if (p < 0) p = -p;
! 103: return (2000 + (p % 6000));
! 104: }
! 105:
! 106: public String readUntil(char u) throws IOException {
! 107: String s = "";
! 108: char c = 0;
! 109: while (c != u) {
! 110: c = (char) is.read(); // BUG
! 111: s += c;
! 112: }
! 113: return s;
! 114: }
! 115:
! 116: public int startPhase1() throws IOException {
! 117: String s;
! 118: String ans;
! 119: String fname;
! 120: int port;
! 121:
! 122: readUntil('?');
! 123: s = "<login method=\"file\">\n";
! 124: System.err.println(s);
! 125: write(s);
! 126:
! 127: ans = readUntil('?');
! 128: System.err.println(ans);
! 129: if (!getTagName(ans).equals("challenge")) {
! 130: ; // not yet written
! 131: }
! 132: port = getRandomPortNumber();
! 133: fname = getFileName(ans);
! 134: touch(fname);
! 135:
! 136: s = "<done/>\n";
! 137: System.err.println(s);
! 138: write(s);
! 139: ans = readUntil('?');
! 140: return port;
! 141: }
! 142: // Open the port and port2 for listenning.
! 143: public void startPhase2(String oxname,int port) throws IOException {
! 144: String s;
! 145: String ans;
! 146: String fname;
! 147: int port2;
! 148: port2 = port+1;
! 149:
! 150: s="<launch> ox -ox "+oxname+" -reverse -control "+port+" -data "+port2
! 151: + " </launch> \n";
! 152: System.err.println(s);
! 153: write(s);
! 154: }
! 155:
! 156: public static void main0(String[] argv) throws IOException {
! 157: oxdStream oxd = new oxdStream();
! 158: String s = "<challenge file=\"/home/nobuki/.oxd123\"/>";
! 159: System.err.println( oxd.getFileName(s) );
! 160: System.err.println( oxd.getTagName(s) );
! 161: System.err.println( oxd.getRandomPortNumber() );
! 162: System.err.println( oxd.getRandomPortNumber() );
! 163: System.err.println( oxd.getRandomPortNumber() );
! 164: }
! 165: public static void main(String[] argv) throws IOException {
! 166: int port;
! 167: oxdStream oxd = new oxdStream(8089);
! 168: port = oxd.startPhase1();
! 169: // start for listenning.
! 170: oxd.startPhase2("ox_asir",port);
! 171: }
! 172: }
! 173:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>