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

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

1.2       tam         1: /**
1.7     ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/OMproxy.java,v 1.6 1999/11/04 18:38:59 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.1       tam        13:
                     14:   public OMproxy(String host,int ControlPort,int DataPort) throws IOException{
                     15:     ox = new OpenXM(this,host,ControlPort,DataPort);
                     16:   }
                     17:
                     18:   public void run(){
                     19:     OM2OXM P = new OM2OXM();
                     20:
                     21:     try{
                     22:       while(true){
                     23:        synchronized(ox){
                     24:          int ox_tag = ox.receiveOXtag();
                     25:
                     26:          switch(ox_tag){
                     27:          case OpenXM.OX_COMMAND:
                     28:            StackMachine(ox.receiveSM());
                     29:            break;
                     30:
                     31:          case OpenXM.OX_DATA:
                     32:            stack.push(ox.receiveCMO());
1.4       tam        33:            debug("push: "+ stack.peek());
1.1       tam        34:            break;
                     35:          }
                     36:        }
                     37:       }
                     38:     }catch(java.io.IOException e){
                     39:       System.err.println(e.getMessage());
                     40:       e.printStackTrace();
                     41:     }
                     42:
                     43:     System.out.println("breaking...");
                     44:   }
                     45:
                     46:   public void stop(){
                     47:     System.out.println("OMproxy Stoping...");
                     48:     synchronized(ox){
                     49:       //this.stop();
                     50:       while(!stack.empty()){
                     51:        stack.pop();
                     52:       }
                     53:       System.out.println("OMproxy Stopped");
                     54:     }
                     55:   }
                     56:
                     57:   private void SM_popCMO() throws java.io.IOException{
                     58:     if(stack.empty()){
                     59:       ox.send(new CMO_NULL());
                     60:     }else{
                     61:       ox.send(stack.pop());
                     62:     }
                     63:   }
                     64:
                     65:   private void SM_executeFunction() throws java.io.IOException{
                     66:     String function_name;
                     67:     CMO[] argv;
1.3       tam        68:     int argc = 1;
1.1       tam        69:
                     70:     if(!(stack.peek() instanceof CMO_STRING)){
                     71:       stack.push(new CMO_ERROR2());
                     72:       return;
                     73:     }
                     74:     function_name = ((CMO_STRING)stack.pop()).getString();
1.3       tam        75:     //argc = ((CMO_INT32)stack.pop()).intValue();
1.1       tam        76:     argv = new CMO[argc];
                     77:     for(int i=0;i<argc;i++){
                     78:       argv[i] = (CMO)stack.pop();
                     79:     }
                     80:
                     81:     if(argc != 1){
                     82:       stack.push(new CMO_ERROR2());
                     83:       return;
                     84:     }
                     85:
                     86:     if(function_name.equals("OMXML2CMO")){
                     87:       stack.push(OMXML2CMO(argv[0]));
                     88:       return;
                     89:     }else if(function_name.equals("CMO2OMXML")){
                     90:       stack.push(CMO2OMXML(argv[0]));
                     91:       return;
                     92:     }
                     93:
                     94:     stack.push(new CMO_ERROR2());
                     95:     return;
                     96:   }
                     97:
1.7     ! tam        98:   private void SM_mathcap() throws java.io.IOException{
        !            99:     CMO[] mathcap = new CMO[3];
        !           100:
        !           101:     {
        !           102:       CMO[] list = {new CMO_INT32(199911070),
        !           103:                  new CMO_STRING("Ox_system=OMproxy.class"),
        !           104:                  new CMO_STRING("Version=0.199911070"),
        !           105:                  new CMO_STRING("HOSTTYPE=JAVA")};
        !           106:       mathcap[0] = new CMO_LIST(list);
        !           107:     }
        !           108:
        !           109:     {
        !           110:       CMO[] list = {new CMO_INT32(SM.SM_popCMO),
        !           111:                    new CMO_INT32(SM.SM_executeFunction),
        !           112:                    new CMO_INT32(SM.SM_mathcap)};
        !           113:       mathcap[1] = new CMO_LIST(list);
        !           114:     }
        !           115:
        !           116:     {
        !           117:       CMO[] DataFormat = {new CMO_INT32(OpenXM.OX_DATA)};
        !           118:       CMO[] CMOFormat = {new CMO_INT32(CMO.CMO_NULL),
        !           119:                         new CMO_INT32(CMO.CMO_INT32),
        !           120:                         new CMO_INT32(CMO.CMO_STRING),
        !           121:                         new CMO_INT32(CMO.CMO_MONOMIAL32),
        !           122:                         new CMO_INT32(CMO.CMO_ZZ),
        !           123:                         new CMO_INT32(CMO.CMO_QQ),
        !           124:                         new CMO_INT32(CMO.CMO_ZERO),
        !           125:                         new CMO_INT32(CMO.CMO_DMS_GENERIC)};
        !           126:       CMO[] list = {new CMO_LIST(DataFormat),
        !           127:                    new CMO_LIST(CMOFormat)};
        !           128:       mathcap[2] = new CMO_LIST(list);
        !           129:     }
        !           130:
        !           131:     ox.send(new CMO_LIST(mathcap));
        !           132:   }
        !           133:
1.1       tam       134:   private void StackMachine(SM mesg) throws java.io.IOException{
1.4       tam       135:     debug("receive: "+mesg);
                    136:
1.1       tam       137:     switch(mesg.getCode()){
                    138:     case SM.SM_popCMO:
                    139:       SM_popCMO();
                    140:       break;
                    141:
                    142:     case SM.SM_executeFunction:
                    143:       SM_executeFunction();
1.7     ! tam       144:       break;
        !           145:
        !           146:     case SM.SM_mathcap:
        !           147:       SM_mathcap();
1.1       tam       148:       break;
                    149:
                    150:     default:
                    151:       System.out.println("received "+ mesg);
                    152:     }
                    153:   }
                    154:
                    155:   private CMO CMO2OMXML(CMO obj){
                    156:     String str = OM2OXM.CMO2OM(obj);
                    157:
                    158:     return new CMO_STRING(str);
                    159:   }
                    160:
                    161:   private CMO OMXML2CMO(CMO obj){
                    162:     OM2OXM trans = new OM2OXM();
                    163:     //StringBufferInputStream stream;
                    164:     ByteArrayInputStream stream;
                    165:     CMO ret;
                    166:
1.5       tam       167:     debug("OMXML2CMO called: "+obj);
1.6       tam       168:     if(!(obj instanceof CMO_STRING)){
1.1       tam       169:       return new CMO_ERROR2();
                    170:     }
                    171:
                    172:     try{
                    173:       stream = new ByteArrayInputStream(((CMO_STRING)obj).getString().getBytes());
                    174:       ret = trans.parse(stream);
                    175:     }catch(IOException e){
1.6       tam       176:       debug("OMXML2CMO occuered error in trans");
1.1       tam       177:       return new CMO_ERROR2(new CMO_STRING(e.toString()));
                    178:     }
                    179:
1.4       tam       180:     debug("push: "+ret);
1.1       tam       181:     return ret;
1.4       tam       182:   }
                    183:
                    184:   private void debug(String str){
                    185:     if(debug){
                    186:       System.out.println(str);
                    187:     }
1.1       tam       188:   }
                    189:
1.2       tam       190:   private static String usage(){
                    191:     String ret = "";
                    192:
                    193:     ret += "usage\t: java OMproxy [options]\n";
                    194:     ret += "options\t:\n";
                    195:     ret += "\t -h \t show this message\n";
                    196:     ret += "\t -host hostname \t (default localhost)\n";
                    197:     ret += "\t -data port \t (default 1300)\n";
                    198:     ret += "\t -control port \t (default 1200)\n";
                    199:
                    200:     return ret;
                    201:   }
                    202:
1.1       tam       203:   public static void main(String argv[]){
                    204:     String host = "localhost";
                    205:     int DataPort = 1300, ControlPort = 1200;
                    206:
                    207:     for(int i=0;i<argv.length;i++){
                    208:       if(argv[i].equals("-h")){
1.2       tam       209:        System.out.print(usage());
1.1       tam       210:        System.exit(0);
                    211:       }else if(argv[i].equals("-host")){
                    212:        host = argv[++i];
                    213:       }else if(argv[i].equals("-data")){
                    214:        DataPort = Integer.valueOf(argv[++i]).intValue();
                    215:       }else if(argv[i].equals("-control")){
                    216:        ControlPort = Integer.valueOf(argv[++i]).intValue();
                    217:       }else{
1.2       tam       218:        System.err.println("unknown option : "+ argv[i]);
                    219:        System.err.print(usage());
1.1       tam       220:        System.exit(1);
                    221:       }
                    222:     }
1.2       tam       223:
1.1       tam       224:     System.out.println("host(ctrl,data): "+ host
                    225:                       +"("+ ControlPort +","+ DataPort +")");
                    226:
                    227:     try{
                    228:       new OMproxy(host,ControlPort,DataPort);
                    229:       System.out.println("connected.");
                    230:     }catch(IOException e){
                    231:       System.err.println("Error occured: "+ e);
                    232:       System.err.println(e.getLocalizedMessage());
                    233:       System.err.println(e.getMessage());
                    234:     }
                    235:   }
                    236: }

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