Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/OpenXM.java, Revision 1.9
1.1 tam 1: /**
1.9 ! takayama 2: * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXM.java,v 1.8 2001/11/04 09:58:14 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.9 ! takayama 87: public OpenXM(String host,int CtrlPort,int StreamPort,String ox_server,int oxd_port,String pass)
! 88: throws IOException{
! 89: // pass may be a null string.
! 90: control = new OpenXMstream(host,CtrlPort,pass);
! 91: stream = new OpenXMstream(host,StreamPort,pass);
! 92: System.err.println("Listening...");
! 93: System.err.println("Launch ox server with the reverse option, e.g., ox -ox ox_asir -reverse");
! 94: // Launch ox_server by the oxd daemon (oxd_port).
! 95: // BUG: It has not yet been implemented.
! 96: control.OpenXMstreamAccept();
! 97: System.err.println("Accepted the control port.");
! 98: stream.OpenXMstreamAccept();
! 99: System.err.println("Accepted the data port.");
! 100:
! 101:
! 102: control.sendByteOrder();
! 103: stream.sendByteOrder();
! 104: }
1.8 takayama 105: /*&ja
1.9 ! takayama 106: * サーバの計算中断を行なう. 現在は未実装.
1.8 takayama 107: */
1.1 tam 108: /**
1.8 takayama 109: * Resetting the engine process. It has not yet been implemented.
1.1 tam 110: */
111: public synchronized void resetConnection(){
112: debug("control: stopping computer process...");
113: debug("control: sending SYNC BALL.");
114: }
115:
1.8 takayama 116: /*&ja
1.3 tam 117: * OpenXM メッセージをデータストリームに送信する.
118: * このメソッドはメッセージのボディの部分だけでよい.
119: * ヘッダ部分は自動で付加される.
1.2 tam 120: */
1.8 takayama 121: /**
122: * Send an OpenXM message object.
123: * @param object a message. For example, oxm.send(new CMO_STRING("Hello"))
124: * sends a string "Hello" to the OpenXM server oxm in the CMO_STRING data
125: * encoding.
126: */
1.1 tam 127: public void send(OXbody object) throws IOException,MathcapViolation{
128: stream.send(object);
129: }
130:
1.8 takayama 131: /*&ja
132: * データストリームから OpenXM メッセージを受け取る.
133: */
1.1 tam 134: /**
1.8 takayama 135: * Receive an OpenXM message.
1.1 tam 136: */
137: public OXmessage receive() throws IOException{
138: return stream.receive();
139: }
140:
1.8 takayama 141: /*&ja
1.3 tam 142: * データストリームの MathCap を mathcap に設定する.
143: * 以後, 送信するオブジェクトは mathcap に合っているかどうか
144: * チェックが入る. 実際にチェックが入るかどうかは
145: * OXbody クラスの派生クラスの実装による.
146: * mathcap に反したオブジェクトを送ろうとした時には,
147: * 以後 MathcapViolation が発生することが期待される.
1.8 takayama 148: */
149: /**
150: * Set the mathcap.
151: * If one tries to send an object which is prohibited to send by the mathcap,
152: * the mathcapViolation exception is thrown.
1.1 tam 153: */
154: public void setMathCap(CMO_MATHCAP mathcap){
155: stream.setMathCap(mathcap);
156: }
157:
158: private final void debug(String str){
159: if(debug){
160: System.err.println(str);
161: }
162: }
163:
164: public static void main(String[] argv){
165: String hostname = "localhost";
166: int ControlPort = 1200, DataPort = 1300;
167: Runnable process = null;
168: Thread thread;
169: OpenXM ox;
170:
171: for(int i=0;i<argv.length;i++){
172: if(argv[i].equals("-h")){
1.5 ohara 173: System.err.println("");
1.1 tam 174: System.exit(0);
175: }else if(argv[i].equals("-host")){
176: hostname = argv[++i];
177: }else if(argv[i].equals("-data")){
178: DataPort = Integer.valueOf(argv[++i]).intValue();
179: }else if(argv[i].equals("-control")){
180: ControlPort = Integer.valueOf(argv[++i]).intValue();
181: }else{
182: System.err.println("unknown option :"+ argv[i]);
183: System.exit(1);
184: }
185: }
186:
187: try{
188: ox = new OpenXM(hostname,ControlPort,DataPort);
189:
190: thread = new Thread(process);
191: thread.start();
192: }catch(UnknownHostException e){
193: System.err.println("host unknown.");
194: System.err.println(e.getMessage());
195: return;
196: }catch(IOException e){
197: System.err.println("connection failed.");
198: System.err.println("IOException occuer !!");
199: System.err.println(e.getMessage());
200: return;
201: }
202:
203: try{
204: //サーバ側へ文字列を送信します。
205: ox.send(new SM(SM.SM_mathcap));
206: ox.send(new SM(SM.SM_popString));
207:
208: //ox.send(new CMO_STRING("print(\"Hello world!!\");\n"));
209: //ox.send(new SM(SM.SM_executeStringByLocalParser));
210:
211: //ox.send(new CMO_STRING("def sub(B,A){return B-A;}"));
212: //ox.send(new SM(SM.SM_executeStringByLocalParser));
213: //ox.send(new SM(SM.SM_popString));
214:
215: //ox.send(new CMO_STRING("diff((x+2*y)^2,x);\0 1+2;"));
216: //ox.send(new SM(SM.SM_executeStringByLocalParser));
217: //ox.send(new SM(SM.SM_popString));
218:
219: //ox.send(new CMO_ZZ("1"));
220: ox.send(new CMO_ZZ("-2"));
221: //ox.send(new CMO_INT32(2));
222: //ox.send(new CMO_STRING("sub"));
223: //ox.send(new SM(SM.SM_executeFunction));
224: ox.send(new SM(SM.SM_popCMO));
225:
226: { int[] array = {1,2};
227: ox.send(new CMO_MONOMIAL32(array,new CMO_ZZ("-2")));
228: }
229: ox.send(new SM(SM.SM_popCMO));
230:
231: ox.send(new SM(SM.SM_popString));
232: //ox.send(new SM(SM.SM_popString));
233:
234: ox.send(new SM(SM.SM_control_kill));
235:
236: //os.flush();
237:
238: //サーバ側から送信された文字列を受信します。
239: while(true){
240: ox.receive();
241: }
242:
243: }catch(Exception e){
244: e.printStackTrace();
245: }
246:
1.5 ohara 247: System.err.println("breaking...");
1.1 tam 248:
249: try{
250: // close connection
251: ox.stream.close();
252: ox.control.close();
253: }catch(IOException e){
254: e.printStackTrace();
255: }
256: }
257: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>