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

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

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

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