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