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

Annotation of OpenXM/src/OpenMath/OXplot.java, Revision 1.2

1.1       tam         1: /**
1.2     ! tam         2:  * $OpenXM: OpenXM/src/OpenMath/OXplot.java,v 1.1 2000/07/03 05:57:43 tam Exp $
1.1       tam         3:  */
                      4:
                      5: import JP.ac.kobe_u.math.tam.OpenXM.*;
                      6: import java.util.Stack;
1.2     ! tam         7: import java.util.Vector;
        !             8: import java.awt.*;
1.1       tam         9:
                     10: public class OXplot extends OpenXMserver{
                     11:   private Stack stack = new Stack();
                     12:   private Vector plotframe = new Vector();
                     13:   protected boolean debug = true;
                     14:   final int version = 200007010;
                     15:
                     16:   public OXplot(String hostname,int ControlPort,int DataPort){
                     17:     super(hostname,ControlPort,DataPort);
                     18:   }
                     19:
                     20:   public void computeProcess(OpenXMconnection stream){
                     21:     debug("OXplot started.");
1.2     ! tam        22:     stack = new Stack();
        !            23:     plotframe = new Vector();
1.1       tam        24:     try{
                     25:       while(true){
                     26:        try{
                     27:          OXmessage message = stream.receive();
                     28:          int ox_tag = message.getTag();
                     29:
                     30:          switch(ox_tag){
                     31:          case OXmessage.OX_COMMAND:
                     32:            StackMachine((SM)message.getBody(),stream);
                     33:            break;
                     34:
                     35:          case OXmessage.OX_DATA:
                     36:            stack.push(message.getBody());
                     37:            debug("push: "+ stack.peek());
                     38:            break;
                     39:          }
                     40:        }catch(RuntimeException e){
                     41:          System.err.println(e.getMessage());
                     42:          e.printStackTrace();
                     43:          debug("error occured. stack was cleared.");
                     44:          stack = new Stack();
                     45:        }
                     46:       }
                     47:     }catch(java.io.IOException e){
                     48:       System.err.println(e.getMessage());
                     49:       e.printStackTrace();
                     50:       System.err.println("error occured, and recovering processes seems to be impossible.");
                     51:     }catch(Exception e){
                     52:       System.err.println(e.getMessage());
                     53:       e.printStackTrace();
                     54:       System.err.println("error occured, and recovering processes seems to be impossible.");
                     55:     }finally{
                     56:       System.out.println("breaking...");
                     57:     }
                     58:   }
                     59:
                     60:   class plotframe extends java.awt.Frame{
1.2     ! tam        61:     Canvas canvas;
1.1       tam        62:     int pixels[][];
                     63:
                     64:     plotframe(int width,int height){
                     65:       super("plotframe");
1.2     ! tam        66:       add("Center", new Panel().add(canvas = new Canvas()));
        !            67:       canvas.setSize(width,height);
1.1       tam        68:       setResizable(false);
                     69:
                     70:       pixels = new int[width][];
                     71:       for(int i=0;i<pixels.length;i++){
                     72:        pixels[i] = new int[height];
                     73:       }
1.2     ! tam        74:
        !            75:       pack();
        !            76:       show();
        !            77:     }
        !            78:
        !            79:     public void paint(Graphics gr){
        !            80:       Graphics g = canvas.getGraphics();
        !            81:
        !            82:       for(int x=0;x<pixels.length;x++){
        !            83:        for(int y=0;y<pixels[x].length;y++){
        !            84:          if(pixels[x][y] == 1){
        !            85:            g.setColor(Color.black);
        !            86:          }else{
        !            87:            g.setColor(Color.white);
        !            88:          }
        !            89:          g.fillRect(x,y,1,1);
        !            90:        }
        !            91:       }
        !            92:     }
        !            93:
        !            94:     public void pset(int x,int y){
        !            95:       pixels[x][y] = 1;
1.1       tam        96:     }
                     97:   }
                     98:
                     99:   private void SM_popCMO(OpenXMconnection stream) throws java.io.IOException{
                    100:     try{
                    101:       if(stack.empty()){
                    102:        stream.send(new CMO_NULL());
                    103:       }else{
                    104:        debug("sending CMO: "+ stack.peek());
                    105:        stream.send((CMO)stack.pop());
                    106:        debug("test");
                    107:       }
                    108:     }catch(MathcapViolation e){
                    109:       try{
                    110:        stream.send(new CMO_ERROR2(new CMO_STRING("MathcapViolation: "+
                    111:                                              e.getMessage())));
                    112:       }catch(MathcapViolation tmp){}
                    113:     }
                    114:   }
                    115:
                    116:   private void SM_executeFunction() throws java.io.IOException{
                    117:     String function_name;
                    118:     CMO[] argv;
                    119:     int argc;
                    120:
                    121:     if(!(stack.peek() instanceof CMO_STRING)){
                    122:       stack.push(new CMO_ERROR2(new CMO_NULL()));
                    123:       return;
                    124:     }
                    125:     function_name = ((CMO_STRING)stack.pop()).getString();
                    126:     argc = ((CMO_INT32)stack.pop()).intValue();
                    127:     argv = new CMO[argc];
                    128:     for(int i=0;i<argc;i++){
                    129:       argv[i] = (CMO)stack.pop();
                    130:     }
                    131:
                    132:     if(function_name.equals("CREATE") && argc==2){
                    133:       stack.push(CREATE(argv));
                    134:     }else if(function_name.equals("CMO2OMXML") && argc==1){
                    135:       //stack.push(CMO2OMXML(argv[0]));
                    136:     }else{
                    137:       stack.push(new CMO_ERROR2(new CMO_NULL()));
                    138:     }
                    139:
                    140:     return;
                    141:   }
                    142:
                    143:   private void SM_mathcap(OpenXMconnection stream) throws java.io.IOException{
                    144:     CMO[] mathcap = new CMO[3];
                    145:
                    146:     {
                    147:       CMO[] list = {new CMO_INT32(version),
                    148:                  new CMO_STRING("Ox_system=OMproxy.class"),
                    149:                  new CMO_STRING("Version=0."+ version),
                    150:                  new CMO_STRING("HOSTTYPE=JAVA")};
                    151:       mathcap[0] = new CMO_LIST(list);
                    152:     }
                    153:
                    154:     {
                    155:       CMO[] list = {new CMO_INT32(SM.SM_popCMO),
                    156:                    new CMO_INT32(SM.SM_executeFunction),
                    157:                    new CMO_INT32(SM.SM_mathcap),
                    158:                    new CMO_INT32(SM.SM_setMathCap),
                    159:                    new CMO_INT32(SM.SM_control_kill),
                    160:                    new CMO_INT32(SM.SM_control_reset_connection)};
                    161:       mathcap[1] = new CMO_LIST(list);
                    162:     }
                    163:
                    164:     {
                    165:       CMO[] CMOFormat = {new CMO_INT32(CMO.NULL),
                    166:                         new CMO_INT32(CMO.INT32),
                    167:                         new CMO_INT32(CMO.STRING),
                    168:                         new CMO_INT32(CMO.LIST),
                    169:                         new CMO_INT32(CMO.MONOMIAL32),
                    170:                         new CMO_INT32(CMO.ZZ),
                    171:                         new CMO_INT32(CMO.QQ),
                    172:                         new CMO_INT32(CMO.ZERO),
                    173:                         new CMO_INT32(CMO.BIGFLOAT),
                    174:                         new CMO_INT32(CMO.INDETERMINATE),
                    175:                         new CMO_INT32(CMO.TREE)};
                    176:       CMO[] DataFormat1 = {new CMO_INT32(OXmessage.OX_DATA),
                    177:                           new CMO_LIST(CMOFormat)};
                    178:       CMO[] list = {new CMO_LIST(DataFormat1)};
                    179:
                    180:       mathcap[2] = new CMO_LIST(list);
                    181:     }
                    182:
                    183:     stack = new Stack();
                    184:     stack.push(new CMO_MATHCAP(new CMO_LIST(mathcap)));
                    185:     debug("push: "+ stack.peek());
                    186:   }
                    187:
                    188:   private void SM_setMathCap(OpenXMconnection stream)
                    189:        throws java.io.IOException{
                    190:     Object mathcap = stack.pop();
                    191:
                    192:     if(mathcap instanceof CMO_MATHCAP){
                    193:       stack.push(new CMO_ERROR2(new CMO_NULL()));
                    194:     }
                    195:     stream.setMathCap((CMO_MATHCAP)mathcap);
                    196:   }
                    197:
                    198:   private void StackMachine(SM mesg,OpenXMconnection stream)
                    199:        throws java.io.IOException{
                    200:     debug("receive: "+mesg);
                    201:
                    202:     switch(mesg.getCode()){
                    203:     case SM.SM_popCMO:
                    204:       SM_popCMO(stream);
                    205:       break;
                    206:
                    207:     case SM.SM_executeFunction:
                    208:       SM_executeFunction();
                    209:       break;
                    210:
                    211:     case SM.SM_mathcap:
                    212:       SM_mathcap(stream);
                    213:       break;
                    214:
                    215:     case SM.SM_setMathCap:
                    216:       SM_setMathCap(stream);
                    217:       break;
                    218:
                    219:     default:
                    220:       System.out.println("received "+ mesg);
                    221:     }
                    222:   }
                    223:
1.2     ! tam       224:   private CMO CREATE(CMO[] argv){
        !           225:     plotframe tmp = new plotframe(((CMO_INT32)argv[0]).intValue()
        !           226:                                  ,((CMO_INT32)argv[1]).intValue());
1.1       tam       227:     int i;
                    228:
                    229:     for(i=0;i<plotframe.size();i++){
                    230:       if(plotframe.elementAt(i) == null){
                    231:        plotframe.setElementAt(tmp,i);
1.2     ! tam       232:        return new CMO_INT32(i);
1.1       tam       233:       }
                    234:     }
                    235:
                    236:     plotframe.addElement(tmp);
1.2     ! tam       237:
        !           238:     return new CMO_INT32(plotframe.size());
1.1       tam       239:   }
                    240:
                    241:   private void debug(String str){
                    242:     if(debug){
                    243:       System.err.println(str);
                    244:     }
                    245:   }
                    246:
                    247:   private static String usage(){
                    248:     String ret = "";
                    249:
                    250:     ret += "usage\t: java OMproxy [options]\n";
                    251:     ret += "options\t:\n";
                    252:     ret += "\t -h \t show this message\n";
                    253:     ret += "\t -host hostname \t (default localhost)\n";
                    254:     ret += "\t -data port \t (default 1300)\n";
                    255:     ret += "\t -control port \t (default 1200)\n";
                    256:     ret += "\t -insecure \t this version ignore this option\n";
                    257:     ret += "\t -debug \t display debug messages\n";
                    258:
                    259:     return ret;
                    260:   }
                    261:
                    262:   public static void main(String[] argv){
                    263:     String hostname = "localhost";
                    264:     int ControlPort = 1200, DataPort = 1300;
                    265:     OpenXMserver ox;
                    266:
                    267:     for(int i=0;i<argv.length;i++){
                    268:       if(argv[i].equals("-h")){
                    269:         System.out.println("");
                    270:         System.exit(0);
                    271:       }else if(argv[i].equals("-host")){
                    272:         hostname = argv[++i];
                    273:       }else if(argv[i].equals("-data")){
                    274:         DataPort = Integer.valueOf(argv[++i]).intValue();
                    275:       }else if(argv[i].equals("-control")){
                    276:         ControlPort = Integer.valueOf(argv[++i]).intValue();
                    277:       }else{
                    278:         System.err.println("unknown option :"+ argv[i]);
                    279:         System.exit(1);
                    280:       }
                    281:     }
                    282:
                    283:     //ox = new OpenXMserver(hostname,ControlPort,DataPort);
1.2     ! tam       284:     ox = new OXplot(hostname,ControlPort,DataPort);
1.1       tam       285:     /*
                    286:     try{
                    287:     }catch(java.net.UnknownHostException e){
                    288:       System.err.println("host unknown.");
                    289:       System.err.println(e.getMessage());
                    290:       return;
                    291:     }catch(IOException e){
                    292:       System.err.println("connection failed.");
                    293:       System.err.println("IOException occuer !!");
                    294:       System.err.println(e.getMessage());
                    295:       return;
                    296:     }
                    297:     */
                    298:
                    299:     ox.start();
                    300:
                    301:     System.out.println("breaking...");
                    302:   }
                    303: }

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