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