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

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

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

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