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

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

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

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