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

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

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

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