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

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

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

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