[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.4

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

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