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

Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/OpenXM.java, Revision 1.1

1.1     ! tam         1: /**
        !             2:  * $OpenXM$
        !             3:  */
        !             4: package ORG.openxm.tam;
        !             5:
        !             6: import java.io.*;
        !             7: import java.net.*;
        !             8:
        !             9:
        !            10: /**
        !            11:  * OpenXM サーバとの接続を行なう抽象クラス
        !            12:  * クライアント側が使用する.
        !            13:  */
        !            14: public class OpenXM{
        !            15:   private OpenXMconnection control = null, stream = null;
        !            16:   final protected boolean debug = false;
        !            17:
        !            18:   public OpenXM(String host,int CtrlPort,int StreamPort) throws IOException{
        !            19:     control = new OpenXMconnection(host,CtrlPort);
        !            20:
        !            21:     try{
        !            22:       Thread.sleep(100); // We need a few wait for starting up server.
        !            23:     }catch(InterruptedException e){
        !            24:       System.err.println(e.getMessage());
        !            25:     }
        !            26:
        !            27:     stream = new OpenXMconnection(host,StreamPort);
        !            28:
        !            29:     control.sendByteOrder();
        !            30:     stream.sendByteOrder();
        !            31:   }
        !            32:
        !            33:   /**
        !            34:    * 接続の reset を行なう. 現在は未実装.
        !            35:    */
        !            36:   public synchronized void resetConnection(){
        !            37:     debug("control: stopping computer process...");
        !            38:     debug("control: sending SYNC BALL.");
        !            39:   }
        !            40:
        !            41:   /**
        !            42:    * OpenXM メッセージを送信する. ただし、このメソッドは
        !            43:    * ボディの部分だけでよい. ヘッダの部分は自動で付加される.
        !            44:   public void send(OXbody object) throws IOException,MathcapViolation{
        !            45:     stream.send(object);
        !            46:   }
        !            47:
        !            48:   /**
        !            49:    * OpenXM メッセージを受け取る.
        !            50:    */
        !            51:   public OXmessage receive() throws IOException{
        !            52:     return stream.receive();
        !            53:   }
        !            54:
        !            55:   /**
        !            56:    * MathCap を mathcap に設定する.
        !            57:    * 以後, mathcap に反した CMO を送ると
        !            58:    * MathcapViolation が発生する.
        !            59:    */
        !            60:   public void setMathCap(CMO_MATHCAP mathcap){
        !            61:     stream.setMathCap(mathcap);
        !            62:   }
        !            63:
        !            64:   private final void debug(String str){
        !            65:     if(debug){
        !            66:       System.err.println(str);
        !            67:     }
        !            68:   }
        !            69:
        !            70:   public static void main(String[] argv){
        !            71:     String hostname = "localhost";
        !            72:     int ControlPort = 1200, DataPort = 1300;
        !            73:     Runnable process = null;
        !            74:     Thread thread;
        !            75:     OpenXM ox;
        !            76:
        !            77:     for(int i=0;i<argv.length;i++){
        !            78:       if(argv[i].equals("-h")){
        !            79:         System.out.println("");
        !            80:         System.exit(0);
        !            81:       }else if(argv[i].equals("-host")){
        !            82:         hostname = argv[++i];
        !            83:       }else if(argv[i].equals("-data")){
        !            84:         DataPort = Integer.valueOf(argv[++i]).intValue();
        !            85:       }else if(argv[i].equals("-control")){
        !            86:         ControlPort = Integer.valueOf(argv[++i]).intValue();
        !            87:       }else{
        !            88:         System.err.println("unknown option :"+ argv[i]);
        !            89:         System.exit(1);
        !            90:       }
        !            91:     }
        !            92:
        !            93:     try{
        !            94:       ox = new OpenXM(hostname,ControlPort,DataPort);
        !            95:
        !            96:       thread = new Thread(process);
        !            97:       thread.start();
        !            98:     }catch(UnknownHostException e){
        !            99:       System.err.println("host unknown.");
        !           100:       System.err.println(e.getMessage());
        !           101:       return;
        !           102:     }catch(IOException e){
        !           103:       System.err.println("connection failed.");
        !           104:       System.err.println("IOException occuer !!");
        !           105:       System.err.println(e.getMessage());
        !           106:       return;
        !           107:     }
        !           108:
        !           109:     try{
        !           110:       //サーバ側へ文字列を送信します。
        !           111:       ox.send(new SM(SM.SM_mathcap));
        !           112:       ox.send(new SM(SM.SM_popString));
        !           113:
        !           114:       //ox.send(new CMO_STRING("print(\"Hello world!!\");\n"));
        !           115:       //ox.send(new SM(SM.SM_executeStringByLocalParser));
        !           116:
        !           117:       //ox.send(new CMO_STRING("def sub(B,A){return B-A;}"));
        !           118:       //ox.send(new SM(SM.SM_executeStringByLocalParser));
        !           119:       //ox.send(new SM(SM.SM_popString));
        !           120:
        !           121:       //ox.send(new CMO_STRING("diff((x+2*y)^2,x);\0 1+2;"));
        !           122:       //ox.send(new SM(SM.SM_executeStringByLocalParser));
        !           123:       //ox.send(new SM(SM.SM_popString));
        !           124:
        !           125:       //ox.send(new CMO_ZZ("1"));
        !           126:       ox.send(new CMO_ZZ("-2"));
        !           127:       //ox.send(new CMO_INT32(2));
        !           128:       //ox.send(new CMO_STRING("sub"));
        !           129:       //ox.send(new SM(SM.SM_executeFunction));
        !           130:       ox.send(new SM(SM.SM_popCMO));
        !           131:
        !           132:       { int[] array = {1,2};
        !           133:       ox.send(new CMO_MONOMIAL32(array,new CMO_ZZ("-2")));
        !           134:       }
        !           135:       ox.send(new SM(SM.SM_popCMO));
        !           136:
        !           137:       ox.send(new SM(SM.SM_popString));
        !           138:       //ox.send(new SM(SM.SM_popString));
        !           139:
        !           140:       ox.send(new SM(SM.SM_control_kill));
        !           141:
        !           142:       //os.flush();
        !           143:
        !           144:       //サーバ側から送信された文字列を受信します。
        !           145:       while(true){
        !           146:        ox.receive();
        !           147:       }
        !           148:
        !           149:     }catch(Exception e){
        !           150:       e.printStackTrace();
        !           151:     }
        !           152:
        !           153:     System.out.println("breaking...");
        !           154:
        !           155:     try{
        !           156:       // close connection
        !           157:       ox.stream.close();
        !           158:       ox.control.close();
        !           159:     }catch(IOException e){
        !           160:       e.printStackTrace();
        !           161:     }
        !           162:   }
        !           163: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>