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

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

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

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