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