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

1.1       tam         1: /**
1.2     ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXM.java,v 1.1 2000/09/07 10:41:34 tam Exp $
1.1       tam         3:  */
                      4: package ORG.openxm.tam;
                      5:
                      6: import java.io.*;
                      7: import java.net.*;
                      8:
                      9:
                     10: /**
1.2     ! tam        11:  * OpenXM サーバとの接続を行なう抽象クラス.
1.1       tam        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:    * ボディの部分だけでよい. ヘッダの部分は自動で付加される.
1.2     ! tam        44:    */
1.1       tam        45:   public void send(OXbody object) throws IOException,MathcapViolation{
                     46:     stream.send(object);
                     47:   }
                     48:
                     49:   /**
                     50:    * OpenXM メッセージを受け取る.
                     51:    */
                     52:   public OXmessage receive() throws IOException{
                     53:     return stream.receive();
                     54:   }
                     55:
                     56:   /**
                     57:    * MathCap を mathcap に設定する.
                     58:    * 以後, mathcap に反した CMO を送ると
                     59:    * MathcapViolation が発生する.
                     60:    */
                     61:   public void setMathCap(CMO_MATHCAP mathcap){
                     62:     stream.setMathCap(mathcap);
                     63:   }
                     64:
                     65:   private final void debug(String str){
                     66:     if(debug){
                     67:       System.err.println(str);
                     68:     }
                     69:   }
                     70:
                     71:   public static void main(String[] argv){
                     72:     String hostname = "localhost";
                     73:     int ControlPort = 1200, DataPort = 1300;
                     74:     Runnable process = null;
                     75:     Thread thread;
                     76:     OpenXM ox;
                     77:
                     78:     for(int i=0;i<argv.length;i++){
                     79:       if(argv[i].equals("-h")){
                     80:         System.out.println("");
                     81:         System.exit(0);
                     82:       }else if(argv[i].equals("-host")){
                     83:         hostname = argv[++i];
                     84:       }else if(argv[i].equals("-data")){
                     85:         DataPort = Integer.valueOf(argv[++i]).intValue();
                     86:       }else if(argv[i].equals("-control")){
                     87:         ControlPort = Integer.valueOf(argv[++i]).intValue();
                     88:       }else{
                     89:         System.err.println("unknown option :"+ argv[i]);
                     90:         System.exit(1);
                     91:       }
                     92:     }
                     93:
                     94:     try{
                     95:       ox = new OpenXM(hostname,ControlPort,DataPort);
                     96:
                     97:       thread = new Thread(process);
                     98:       thread.start();
                     99:     }catch(UnknownHostException e){
                    100:       System.err.println("host unknown.");
                    101:       System.err.println(e.getMessage());
                    102:       return;
                    103:     }catch(IOException e){
                    104:       System.err.println("connection failed.");
                    105:       System.err.println("IOException occuer !!");
                    106:       System.err.println(e.getMessage());
                    107:       return;
                    108:     }
                    109:
                    110:     try{
                    111:       //サーバ側へ文字列を送信します。
                    112:       ox.send(new SM(SM.SM_mathcap));
                    113:       ox.send(new SM(SM.SM_popString));
                    114:
                    115:       //ox.send(new CMO_STRING("print(\"Hello world!!\");\n"));
                    116:       //ox.send(new SM(SM.SM_executeStringByLocalParser));
                    117:
                    118:       //ox.send(new CMO_STRING("def sub(B,A){return B-A;}"));
                    119:       //ox.send(new SM(SM.SM_executeStringByLocalParser));
                    120:       //ox.send(new SM(SM.SM_popString));
                    121:
                    122:       //ox.send(new CMO_STRING("diff((x+2*y)^2,x);\0 1+2;"));
                    123:       //ox.send(new SM(SM.SM_executeStringByLocalParser));
                    124:       //ox.send(new SM(SM.SM_popString));
                    125:
                    126:       //ox.send(new CMO_ZZ("1"));
                    127:       ox.send(new CMO_ZZ("-2"));
                    128:       //ox.send(new CMO_INT32(2));
                    129:       //ox.send(new CMO_STRING("sub"));
                    130:       //ox.send(new SM(SM.SM_executeFunction));
                    131:       ox.send(new SM(SM.SM_popCMO));
                    132:
                    133:       { int[] array = {1,2};
                    134:       ox.send(new CMO_MONOMIAL32(array,new CMO_ZZ("-2")));
                    135:       }
                    136:       ox.send(new SM(SM.SM_popCMO));
                    137:
                    138:       ox.send(new SM(SM.SM_popString));
                    139:       //ox.send(new SM(SM.SM_popString));
                    140:
                    141:       ox.send(new SM(SM.SM_control_kill));
                    142:
                    143:       //os.flush();
                    144:
                    145:       //サーバ側から送信された文字列を受信します。
                    146:       while(true){
                    147:        ox.receive();
                    148:       }
                    149:
                    150:     }catch(Exception e){
                    151:       e.printStackTrace();
                    152:     }
                    153:
                    154:     System.out.println("breaking...");
                    155:
                    156:     try{
                    157:       // close connection
                    158:       ox.stream.close();
                    159:       ox.control.close();
                    160:     }catch(IOException e){
                    161:       e.printStackTrace();
                    162:     }
                    163:   }
                    164: }

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