Annotation of OpenXM/src/OpenMath/OMproxy.java, Revision 1.30
1.2 tam 1: /**
1.30 ! tam 2: * $OpenXM: OpenXM/src/OpenMath/OMproxy.java,v 1.29 2000/01/21 07:33:18 tam Exp $
1.2 tam 3: */
4:
1.1 tam 5: import JP.ac.kobe_u.math.tam.OpenXM.*;
6: import java.util.Stack;
7: import java.io.*;
8:
9: class OMproxy implements Runnable{
10: private OpenXM ox;
11: private Stack stack = new Stack();
1.22 tam 12: protected boolean debug = false;
1.20 tam 13: final int version = 200001190;
1.1 tam 14:
15: public OMproxy(String host,int ControlPort,int DataPort) throws IOException{
16: ox = new OpenXM(this,host,ControlPort,DataPort);
17: }
18:
19: public void run(){
20: OM2OXM P = new OM2OXM();
21:
1.9 tam 22: debug("OMproxy started.");
1.29 tam 23: try{
24: while(true){
25: try{
26: int ox_tag = ox.receiveOXtag();
27:
28: switch(ox_tag){
29: case OpenXM.OX_COMMAND:
30: StackMachine(ox.receiveSM());
31: break;
32:
33: case OpenXM.OX_DATA:
34: stack.push(ox.receiveCMO());
35: debug("push: "+ stack.peek());
36: break;
37: }
38: }catch(RuntimeException e){
39: System.err.println(e.getMessage());
40: e.printStackTrace();
41: debug("error occured. stack was cleared.");
1.1 tam 42: }
43: }
1.29 tam 44: }catch(IOException e){
45: System.err.println(e.getMessage());
46: e.printStackTrace();
47: System.err.println("error occured, and recovering processes seems to be impossible.");
48: }finally{
49: System.out.println("breaking...");
1.1 tam 50: }
51: }
52:
1.9 tam 53: /*
1.1 tam 54: public void stop(){
55: System.out.println("OMproxy Stoping...");
56: synchronized(ox){
57: //this.stop();
58: while(!stack.empty()){
59: stack.pop();
60: }
61: System.out.println("OMproxy Stopped");
62: }
63: }
1.9 tam 64: */
1.1 tam 65:
66: private void SM_popCMO() throws java.io.IOException{
1.24 tam 67: try{
68: if(stack.empty()){
69: ox.send(new CMO_NULL());
70: }else{
71: debug("sending CMO: "+ stack.peek());
72: ox.send(stack.pop());
73: }
74: }catch(MathcapViolation e){
1.27 tam 75: try{
1.30 ! tam 76: ox.send(new cmoError2(new CMO_STRING("MathcapViolation: "+
1.28 tam 77: e.getMessage())));
1.27 tam 78: }catch(MathcapViolation tmp){}
1.1 tam 79: }
80: }
81:
82: private void SM_executeFunction() throws java.io.IOException{
83: String function_name;
84: CMO[] argv;
1.26 tam 85: int argc;
1.1 tam 86:
87: if(!(stack.peek() instanceof CMO_STRING)){
1.30 ! tam 88: stack.push(new cmoError2(new cmoNull()));
1.1 tam 89: return;
90: }
91: function_name = ((CMO_STRING)stack.pop()).getString();
1.26 tam 92: argc = ((CMO_INT32)stack.pop()).intValue();
1.1 tam 93: argv = new CMO[argc];
94: for(int i=0;i<argc;i++){
95: argv[i] = (CMO)stack.pop();
96: }
97:
98: if(argc != 1){
1.30 ! tam 99: stack.push(new cmoError2(new cmoNull()));
1.1 tam 100: return;
101: }
102:
103: if(function_name.equals("OMXML2CMO")){
104: stack.push(OMXML2CMO(argv[0]));
105: return;
106: }else if(function_name.equals("CMO2OMXML")){
107: stack.push(CMO2OMXML(argv[0]));
108: return;
109: }
110:
1.30 ! tam 111: stack.push(new cmoError2(new cmoNull()));
1.1 tam 112: return;
113: }
114:
1.7 tam 115: private void SM_mathcap() throws java.io.IOException{
116: CMO[] mathcap = new CMO[3];
117:
118: {
1.9 tam 119: CMO[] list = {new CMO_INT32(version),
1.7 tam 120: new CMO_STRING("Ox_system=OMproxy.class"),
1.9 tam 121: new CMO_STRING("Version=0."+ version),
1.7 tam 122: new CMO_STRING("HOSTTYPE=JAVA")};
123: mathcap[0] = new CMO_LIST(list);
124: }
125:
126: {
127: CMO[] list = {new CMO_INT32(SM.SM_popCMO),
128: new CMO_INT32(SM.SM_executeFunction),
1.10 tam 129: new CMO_INT32(SM.SM_mathcap),
1.18 tam 130: new CMO_INT32(SM.SM_setMathCap),
1.10 tam 131: new CMO_INT32(SM.SM_control_kill),
132: new CMO_INT32(SM.SM_control_reset_connection)};
1.7 tam 133: mathcap[1] = new CMO_LIST(list);
134: }
135:
136: {
137: CMO[] CMOFormat = {new CMO_INT32(CMO.CMO_NULL),
138: new CMO_INT32(CMO.CMO_INT32),
139: new CMO_INT32(CMO.CMO_STRING),
1.9 tam 140: new CMO_INT32(CMO.CMO_LIST),
1.7 tam 141: new CMO_INT32(CMO.CMO_MONOMIAL32),
142: new CMO_INT32(CMO.CMO_ZZ),
143: new CMO_INT32(CMO.CMO_QQ),
144: new CMO_INT32(CMO.CMO_ZERO),
1.12 tam 145: new CMO_INT32(CMO.CMO_DMS_GENERIC),
1.14 tam 146: new CMO_INT32(CMO.CMO_RECURSIVE_POLYNOMIAL),
1.9 tam 147: new CMO_INT32(CMO.CMO_DISTRIBUTED_POLYNOMIAL),
1.14 tam 148: new CMO_INT32(CMO.CMO_POLYNOMIAL_IN_ONE_VARIABLE),
1.12 tam 149: new CMO_INT32(CMO.CMO_BIGFLOAT),
150: new CMO_INT32(CMO.CMO_INDETERMINATE),
151: new CMO_INT32(CMO.CMO_TREE)};
1.15 tam 152: CMO[] DataFormat1 = {new CMO_INT32(OpenXM.OX_DATA),
153: new CMO_LIST(CMOFormat)};
154: CMO[] list = {new CMO_LIST(DataFormat1)};
155:
1.7 tam 156: mathcap[2] = new CMO_LIST(list);
157: }
158:
1.8 tam 159: stack.push(new CMO_MATHCAP(new CMO_LIST(mathcap)));
160: debug("push: "+ stack.peek());
1.7 tam 161: }
162:
1.25 tam 163: private void SM_setMathCap() throws java.io.IOException{
164: Object mathcap = stack.pop();
165:
166: if(mathcap instanceof CMO_MATHCAP){
1.30 ! tam 167: stack.push(new cmoError2(new cmoNull()));
1.25 tam 168: }
169: ox.setMathCap((CMO_MATHCAP)mathcap);
170: }
171:
1.1 tam 172: private void StackMachine(SM mesg) throws java.io.IOException{
1.4 tam 173: debug("receive: "+mesg);
174:
1.1 tam 175: switch(mesg.getCode()){
176: case SM.SM_popCMO:
177: SM_popCMO();
178: break;
179:
180: case SM.SM_executeFunction:
181: SM_executeFunction();
1.7 tam 182: break;
183:
184: case SM.SM_mathcap:
185: SM_mathcap();
1.25 tam 186: break;
187:
188: case SM.SM_setMathCap:
189: SM_setMathCap();
1.1 tam 190: break;
191:
192: default:
193: System.out.println("received "+ mesg);
194: }
195: }
196:
197: private CMO CMO2OMXML(CMO obj){
1.11 tam 198: String str;
199:
200: try{
201: str = OM2OXM.CMO2OM(obj);
202: }catch(NumberFormatException e){
203: debug("CMO2OMXML occuered error in trans");
1.30 ! tam 204: return new cmoError2(new CMO_STRING(e.toString()));
1.11 tam 205: }
1.1 tam 206:
207: return new CMO_STRING(str);
208: }
209:
210: private CMO OMXML2CMO(CMO obj){
211: OM2OXM trans = new OM2OXM();
212: //StringBufferInputStream stream;
213: ByteArrayInputStream stream;
214: CMO ret;
215:
1.5 tam 216: debug("OMXML2CMO called: "+obj);
1.6 tam 217: if(!(obj instanceof CMO_STRING)){
1.30 ! tam 218: return new cmoError2(new CMO_STRING("It's not CMO_STRING."));
1.1 tam 219: }
220:
221: try{
222: stream = new ByteArrayInputStream(((CMO_STRING)obj).getString().getBytes());
223: ret = trans.parse(stream);
224: }catch(IOException e){
1.11 tam 225: debug("OMXML2CMO occuered error in trans");
1.30 ! tam 226: return new cmoError2(new CMO_STRING(e.toString()));
1.11 tam 227: }catch(NumberFormatException e){
1.6 tam 228: debug("OMXML2CMO occuered error in trans");
1.30 ! tam 229: return new cmoError2(new CMO_STRING(e.toString()));
1.1 tam 230: }
231:
1.4 tam 232: debug("push: "+ret);
1.1 tam 233: return ret;
1.4 tam 234: }
235:
236: private void debug(String str){
237: if(debug){
238: System.out.println(str);
239: }
1.1 tam 240: }
241:
1.2 tam 242: private static String usage(){
243: String ret = "";
244:
245: ret += "usage\t: java OMproxy [options]\n";
246: ret += "options\t:\n";
247: ret += "\t -h \t show this message\n";
248: ret += "\t -host hostname \t (default localhost)\n";
249: ret += "\t -data port \t (default 1300)\n";
250: ret += "\t -control port \t (default 1200)\n";
1.8 tam 251: ret += "\t -insecure \t this version ignore this option\n";
1.22 tam 252: ret += "\t -debug \t display debug messages\n";
1.2 tam 253:
254: return ret;
255: }
256:
1.1 tam 257: public static void main(String argv[]){
258: String host = "localhost";
259: int DataPort = 1300, ControlPort = 1200;
1.22 tam 260: boolean debug = false;
1.1 tam 261:
262: for(int i=0;i<argv.length;i++){
263: if(argv[i].equals("-h")){
1.2 tam 264: System.out.print(usage());
1.1 tam 265: System.exit(0);
266: }else if(argv[i].equals("-host")){
267: host = argv[++i];
268: }else if(argv[i].equals("-data")){
269: DataPort = Integer.valueOf(argv[++i]).intValue();
270: }else if(argv[i].equals("-control")){
271: ControlPort = Integer.valueOf(argv[++i]).intValue();
1.8 tam 272: }else if(argv[i].equals("-insecure")){
1.22 tam 273: }else if(argv[i].equals("-debug")){
274: debug = true;
1.1 tam 275: }else{
1.2 tam 276: System.err.println("unknown option : "+ argv[i]);
277: System.err.print(usage());
1.1 tam 278: System.exit(1);
279: }
280: }
1.2 tam 281:
1.1 tam 282: System.out.println("host(ctrl,data): "+ host
283: +"("+ ControlPort +","+ DataPort +")");
284:
285: try{
1.22 tam 286: new OMproxy(host,ControlPort,DataPort).debug = debug;
1.1 tam 287: System.out.println("connected.");
288: }catch(IOException e){
289: System.err.println("Error occured: "+ e);
290: System.err.println(e.getLocalizedMessage());
291: System.err.println(e.getMessage());
292: }
293: }
294: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>