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

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

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

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