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

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

1.2       tam         1: /**
1.3     ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/OMproxy.java,v 1.2 1999/11/02 13:09:19 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();
                     12:
                     13:   public OMproxy(String host,int ControlPort,int DataPort) throws IOException{
                     14:     ox = new OpenXM(this,host,ControlPort,DataPort);
                     15:   }
                     16:
                     17:   public void run(){
                     18:     OM2OXM P = new OM2OXM();
                     19:
                     20:     try{
                     21:       while(true){
                     22:        synchronized(ox){
                     23:          int ox_tag = ox.receiveOXtag();
                     24:
                     25:          switch(ox_tag){
                     26:          case OpenXM.OX_COMMAND:
                     27:            StackMachine(ox.receiveSM());
                     28:            break;
                     29:
                     30:          case OpenXM.OX_DATA:
                     31:            stack.push(ox.receiveCMO());
                     32:            System.out.println("push: "+ stack.peek());
                     33:            break;
                     34:          }
                     35:        }
                     36:       }
                     37:     }catch(java.io.IOException e){
                     38:       System.err.println(e.getMessage());
                     39:       e.printStackTrace();
                     40:     }
                     41:
                     42:     System.out.println("breaking...");
                     43:   }
                     44:
                     45:   public void stop(){
                     46:     System.out.println("OMproxy Stoping...");
                     47:     synchronized(ox){
                     48:       //this.stop();
                     49:       while(!stack.empty()){
                     50:        stack.pop();
                     51:       }
                     52:       System.out.println("OMproxy Stopped");
                     53:     }
                     54:   }
                     55:
                     56:   private void SM_popCMO() throws java.io.IOException{
                     57:     if(stack.empty()){
                     58:       ox.send(new CMO_NULL());
                     59:     }else{
                     60:       ox.send(stack.pop());
                     61:     }
                     62:   }
                     63:
                     64:   private void SM_executeFunction() throws java.io.IOException{
                     65:     String function_name;
                     66:     CMO[] argv;
1.3     ! tam        67:     int argc = 1;
1.1       tam        68:
                     69:     if(!(stack.peek() instanceof CMO_STRING)){
                     70:       stack.push(new CMO_ERROR2());
                     71:       return;
                     72:     }
                     73:     function_name = ((CMO_STRING)stack.pop()).getString();
1.3     ! tam        74:     //argc = ((CMO_INT32)stack.pop()).intValue();
1.1       tam        75:     argv = new CMO[argc];
                     76:     for(int i=0;i<argc;i++){
                     77:       argv[i] = (CMO)stack.pop();
                     78:     }
                     79:
                     80:     if(argc != 1){
                     81:       stack.push(new CMO_ERROR2());
                     82:       return;
                     83:     }
                     84:
                     85:     if(function_name.equals("OMXML2CMO")){
                     86:       stack.push(OMXML2CMO(argv[0]));
                     87:       return;
                     88:     }else if(function_name.equals("CMO2OMXML")){
                     89:       stack.push(CMO2OMXML(argv[0]));
                     90:       return;
                     91:     }
                     92:
                     93:     stack.push(new CMO_ERROR2());
                     94:     return;
                     95:   }
                     96:
                     97:   private void StackMachine(SM mesg) throws java.io.IOException{
                     98:     switch(mesg.getCode()){
                     99:     case SM.SM_popCMO:
                    100:       SM_popCMO();
                    101:       break;
                    102:
                    103:     case SM.SM_executeFunction:
                    104:       SM_executeFunction();
                    105:       break;
                    106:
                    107:     default:
                    108:       System.out.println("received "+ mesg);
                    109:     }
                    110:   }
                    111:
                    112:   private CMO CMO2OMXML(CMO obj){
                    113:     String str = OM2OXM.CMO2OM(obj);
                    114:
                    115:     return new CMO_STRING(str);
                    116:   }
                    117:
                    118:   private CMO OMXML2CMO(CMO obj){
                    119:     OM2OXM trans = new OM2OXM();
                    120:     //StringBufferInputStream stream;
                    121:     ByteArrayInputStream stream;
                    122:     CMO ret;
                    123:
                    124:     if(obj instanceof CMO_STRING){
                    125:       return new CMO_ERROR2();
                    126:     }
                    127:
                    128:     try{
                    129:       stream = new ByteArrayInputStream(((CMO_STRING)obj).getString().getBytes());
                    130:       ret = trans.parse(stream);
                    131:     }catch(IOException e){
                    132:       return new CMO_ERROR2(new CMO_STRING(e.toString()));
                    133:     }
                    134:
                    135:     return ret;
                    136:   }
                    137:
1.2       tam       138:   private static String usage(){
                    139:     String ret = "";
                    140:
                    141:     ret += "usage\t: java OMproxy [options]\n";
                    142:     ret += "options\t:\n";
                    143:     ret += "\t -h \t show this message\n";
                    144:     ret += "\t -host hostname \t (default localhost)\n";
                    145:     ret += "\t -data port \t (default 1300)\n";
                    146:     ret += "\t -control port \t (default 1200)\n";
                    147:
                    148:     return ret;
                    149:   }
                    150:
1.1       tam       151:   public static void main(String argv[]){
                    152:     String host = "localhost";
                    153:     int DataPort = 1300, ControlPort = 1200;
                    154:
                    155:     for(int i=0;i<argv.length;i++){
                    156:       if(argv[i].equals("-h")){
1.2       tam       157:        System.out.print(usage());
1.1       tam       158:        System.exit(0);
                    159:       }else if(argv[i].equals("-host")){
                    160:        host = argv[++i];
                    161:       }else if(argv[i].equals("-data")){
                    162:        DataPort = Integer.valueOf(argv[++i]).intValue();
                    163:       }else if(argv[i].equals("-control")){
                    164:        ControlPort = Integer.valueOf(argv[++i]).intValue();
                    165:       }else{
1.2       tam       166:        System.err.println("unknown option : "+ argv[i]);
                    167:        System.err.print(usage());
1.1       tam       168:        System.exit(1);
                    169:       }
                    170:     }
1.2       tam       171:
1.1       tam       172:     System.out.println("host(ctrl,data): "+ host
                    173:                       +"("+ ControlPort +","+ DataPort +")");
                    174:
                    175:     try{
                    176:       new OMproxy(host,ControlPort,DataPort);
                    177:       System.out.println("connected.");
                    178:     }catch(IOException e){
                    179:       System.err.println("Error occured: "+ e);
                    180:       System.err.println(e.getLocalizedMessage());
                    181:       System.err.println(e.getMessage());
                    182:     }
                    183:   }
                    184: }

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