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