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