Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/OpenXM.java, Revision 1.8
1.1 tam 1: /**
1.8 ! takayama 2: * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXM.java,v 1.7 2001/11/01 08:54:50 takayama Exp $
1.1 tam 3: */
4: package ORG.openxm.tam;
5:
6: import java.io.*;
7: import java.net.*;
8:
9:
1.7 takayama 10: /*&ja
11: OpenXM サーバとの接続を行なうクラス.
12: クライアント側が使用する.
13: 接続するサーバ毎に一つの OpenXM クラスが必要.
14: */
1.1 tam 15: /**
1.8 ! takayama 16: * OpenXM is a class to connect to OpenXM servers,
! 17: * which are compliant to OpenXM RFC 100.
1.7 takayama 18: * There is one-to-one correspondence between the instances
19: * of the class OpenXM and the OpenXM servers.
1.1 tam 20: */
21: public class OpenXM{
1.4 tam 22: private OpenXMstream control = null, stream = null;
1.1 tam 23: final protected boolean debug = false;
24:
1.7 takayama 25: /*&ja
1.3 tam 26: * OpenXM サーバとの接続を TCP/IP ソケットを用いて行なう.
27: * マシン名 host のポート番号 CtrlPort にコントロールを,
28: * ポート番号 StreamPort にデータ用の接続を行なう.
1.7 takayama 29: */
30: /**
31: * Connect to an OpenXM server via TCP/IP socket.
1.8 ! takayama 32: * @param host a machine name of the OpenXM server.
! 33: * @param CtrlPort the control port number of the OpenXM server.
! 34: * @param StreamPort the data port number of the OpenXM server.
1.7 takayama 35: * As to details on the notion of control port and data port, see
36: * Design and Implementation of OpenXM client server model and
37: * common mathematical object format (OpenXM-RFC 100,
38: * proposed standard)
1.8 ! takayama 39: * @see <a href="http://www.openxm.org">OpenXM</a>
1.3 tam 40: */
1.1 tam 41: public OpenXM(String host,int CtrlPort,int StreamPort) throws IOException{
1.6 tam 42: control = new OpenXMstream(host,CtrlPort);
43:
44: try{
45: Thread.sleep(100); // We need a few wait for starting up server.
46: }catch(InterruptedException e){
47: System.err.println(e.getMessage());
48: }
49:
50: stream = new OpenXMstream(host,StreamPort);
51:
52: control.sendByteOrder();
53: stream.sendByteOrder();
54: }
55:
1.8 ! takayama 56: /*&ja
1.6 tam 57: * コマンド command を立ち上げ、
58: * OpenXM サーバとの接続を TCP/IP ソケットを用いて行なう.
59: * マシン名 host のポート番号 CtrlPort にコントロールを,
60: * ポート番号 StreamPort にデータ用の接続を行なう.
61: */
1.8 ! takayama 62: /**
! 63: * First, execute a command, which is usually an OpenXM server,
! 64: * and next try to connect to the OpenXM server via TCP/IP.
! 65: * @param command a command.
! 66: * @param host a machine name of the OpenXM server.
! 67: * @param CtrlPort the control port number of the OpenXM server.
! 68: * @param StreamPort the data port number of the OpenXM server.
! 69: */
1.6 tam 70: public OpenXM(String command,String host,int CtrlPort,int StreamPort)
71: throws IOException{
72: Runtime.getRuntime().exec(command);
1.4 tam 73: control = new OpenXMstream(host,CtrlPort);
1.1 tam 74:
75: try{
76: Thread.sleep(100); // We need a few wait for starting up server.
77: }catch(InterruptedException e){
78: System.err.println(e.getMessage());
79: }
80:
1.4 tam 81: stream = new OpenXMstream(host,StreamPort);
1.1 tam 82:
83: control.sendByteOrder();
84: stream.sendByteOrder();
85: }
86:
1.8 ! takayama 87: /*&ja
! 88: * 接続の初期化を行なう. 現在は未実装.
! 89: */
1.1 tam 90: /**
1.8 ! takayama 91: * Resetting the engine process. It has not yet been implemented.
1.1 tam 92: */
93: public synchronized void resetConnection(){
94: debug("control: stopping computer process...");
95: debug("control: sending SYNC BALL.");
96: }
97:
1.8 ! takayama 98: /*&ja
1.3 tam 99: * OpenXM メッセージをデータストリームに送信する.
100: * このメソッドはメッセージのボディの部分だけでよい.
101: * ヘッダ部分は自動で付加される.
1.2 tam 102: */
1.8 ! takayama 103: /**
! 104: * Send an OpenXM message object.
! 105: * @param object a message. For example, oxm.send(new CMO_STRING("Hello"))
! 106: * sends a string "Hello" to the OpenXM server oxm in the CMO_STRING data
! 107: * encoding.
! 108: */
1.1 tam 109: public void send(OXbody object) throws IOException,MathcapViolation{
110: stream.send(object);
111: }
112:
1.8 ! takayama 113: /*&ja
! 114: * データストリームから OpenXM メッセージを受け取る.
! 115: */
1.1 tam 116: /**
1.8 ! takayama 117: * Receive an OpenXM message.
1.1 tam 118: */
119: public OXmessage receive() throws IOException{
120: return stream.receive();
121: }
122:
1.8 ! takayama 123: /*&ja
1.3 tam 124: * データストリームの MathCap を mathcap に設定する.
125: * 以後, 送信するオブジェクトは mathcap に合っているかどうか
126: * チェックが入る. 実際にチェックが入るかどうかは
127: * OXbody クラスの派生クラスの実装による.
128: * mathcap に反したオブジェクトを送ろうとした時には,
129: * 以後 MathcapViolation が発生することが期待される.
1.8 ! takayama 130: */
! 131: /**
! 132: * Set the mathcap.
! 133: * If one tries to send an object which is prohibited to send by the mathcap,
! 134: * the mathcapViolation exception is thrown.
1.1 tam 135: */
136: public void setMathCap(CMO_MATHCAP mathcap){
137: stream.setMathCap(mathcap);
138: }
139:
140: private final void debug(String str){
141: if(debug){
142: System.err.println(str);
143: }
144: }
145:
146: public static void main(String[] argv){
147: String hostname = "localhost";
148: int ControlPort = 1200, DataPort = 1300;
149: Runnable process = null;
150: Thread thread;
151: OpenXM ox;
152:
153: for(int i=0;i<argv.length;i++){
154: if(argv[i].equals("-h")){
1.5 ohara 155: System.err.println("");
1.1 tam 156: System.exit(0);
157: }else if(argv[i].equals("-host")){
158: hostname = argv[++i];
159: }else if(argv[i].equals("-data")){
160: DataPort = Integer.valueOf(argv[++i]).intValue();
161: }else if(argv[i].equals("-control")){
162: ControlPort = Integer.valueOf(argv[++i]).intValue();
163: }else{
164: System.err.println("unknown option :"+ argv[i]);
165: System.exit(1);
166: }
167: }
168:
169: try{
170: ox = new OpenXM(hostname,ControlPort,DataPort);
171:
172: thread = new Thread(process);
173: thread.start();
174: }catch(UnknownHostException e){
175: System.err.println("host unknown.");
176: System.err.println(e.getMessage());
177: return;
178: }catch(IOException e){
179: System.err.println("connection failed.");
180: System.err.println("IOException occuer !!");
181: System.err.println(e.getMessage());
182: return;
183: }
184:
185: try{
186: //サーバ側へ文字列を送信します。
187: ox.send(new SM(SM.SM_mathcap));
188: ox.send(new SM(SM.SM_popString));
189:
190: //ox.send(new CMO_STRING("print(\"Hello world!!\");\n"));
191: //ox.send(new SM(SM.SM_executeStringByLocalParser));
192:
193: //ox.send(new CMO_STRING("def sub(B,A){return B-A;}"));
194: //ox.send(new SM(SM.SM_executeStringByLocalParser));
195: //ox.send(new SM(SM.SM_popString));
196:
197: //ox.send(new CMO_STRING("diff((x+2*y)^2,x);\0 1+2;"));
198: //ox.send(new SM(SM.SM_executeStringByLocalParser));
199: //ox.send(new SM(SM.SM_popString));
200:
201: //ox.send(new CMO_ZZ("1"));
202: ox.send(new CMO_ZZ("-2"));
203: //ox.send(new CMO_INT32(2));
204: //ox.send(new CMO_STRING("sub"));
205: //ox.send(new SM(SM.SM_executeFunction));
206: ox.send(new SM(SM.SM_popCMO));
207:
208: { int[] array = {1,2};
209: ox.send(new CMO_MONOMIAL32(array,new CMO_ZZ("-2")));
210: }
211: ox.send(new SM(SM.SM_popCMO));
212:
213: ox.send(new SM(SM.SM_popString));
214: //ox.send(new SM(SM.SM_popString));
215:
216: ox.send(new SM(SM.SM_control_kill));
217:
218: //os.flush();
219:
220: //サーバ側から送信された文字列を受信します。
221: while(true){
222: ox.receive();
223: }
224:
225: }catch(Exception e){
226: e.printStackTrace();
227: }
228:
1.5 ohara 229: System.err.println("breaking...");
1.1 tam 230:
231: try{
232: // close connection
233: ox.stream.close();
234: ox.control.close();
235: }catch(IOException e){
236: e.printStackTrace();
237: }
238: }
239: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>