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