[BACK]Return to OMproxy.java CVS log [TXT][DIR] Up to [local] / OpenXM / src / OpenMath

Annotation of OpenXM/src/OpenMath/OMproxy.java, Revision 1.37

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>