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