Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java, Revision 1.2
1.1 tam 1: /**
1.2 ! ohara 2: * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java,v 1.1 2000/09/08 07:29:45 tam Exp $
1.1 tam 3: */
4: package ORG.openxm.tam;
5:
6: import java.io.*;
7: import java.net.*;
8:
9: /**
10: * OpenXM $B%a%C%;!<%8$NAw<u?.$r9T$J$&%/%i%9(B.
11: * $B%3%s%H%m!<%k(B, $B%G!<%?$=$l$>$l$K0l$D$:$DI,MW$H$J$k(B.
12: * $B$3$N%/%i%9$G$O(B, $B@\B3;~$K%P%$%H%*!<%@$N8r49$r9T$J$$(B,
13: * 32bit $B@0?t$NAw<u?.;~$K%P%$%H%*!<%@$NJQ49$r9T$J$&(B.
14: * $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@$N$_<BAu$5$l$F$$$k(B.
15: */
16: public class OpenXMstream{
17: private int serial = 0;
18: private Socket socket = null;
19: private InputStream is = null;
20: private OutputStream os = null;
21: private int order = OX_BYTE_NETWORK_BYTE_ORDER;
22: private CMO_MATHCAP mathcap = null;
23: private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
24:
25: /**
26: * $B%M%C%H%o!<%/%P%$%H%*!<%@$rI=$9(B.
27: * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
28: */
29: final public static int OX_BYTE_NETWORK_BYTE_ORDER = 0;
30:
31: /**
32: * $B%j%H%k%$%s%G%#%"%s$rI=$9(B.
33: * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
34: */
35: final public static int OX_BYTE_LITTLE_INDIAN = 1;
36:
37: /**
38: * $B%S%C%0%$%s%G%#%"%s$rI=$9(B.
39: * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
40: */
41: final public static int OX_BYTE_BIG_INDIAN = 0xff;
42:
43: /**
44: * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
45: * $B@\B3$r9T$J$&(B.
46: */
47: public OpenXMstream(String host,int Port) throws IOException{
48: this(host,Port,false);
49: }
50:
51: /**
52: * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
53: * $B@\B3$r9T$J$&(B. reverse $B$,(B true $B$N$H$-!"(B TCP $B%=%1%C%H$r(B
54: * $B:n@.$7(B, $B@\B3$rBT$D(B.
55: */
56: public OpenXMstream(String host,int Port,boolean reverse) throws IOException{
57: // create socket
58: if(!reverse){
59: socket = new Socket(host,Port);
60: }else{
61: socket = new ServerSocket(Port,50,InetAddress.getByName(host)).accept();
62: }
63:
64: //is =new DebugInputStream(new BufferedInputStream(socket.getInputStream()));
65: is = new BufferedInputStream(socket.getInputStream());
66: os = socket.getOutputStream();
67: }
68:
69: /**
70: * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
71: */
72: public int sendByteOrder() throws IOException{
73: // send byte order
74: os.write(OX_BYTE_NETWORK_BYTE_ORDER);
75: os.flush();
76:
77: // receive byte order
1.2 ! ohara 78: //System.err.println("get: "+is.read());
1.1 tam 79: is.read();
80:
81: order = OX_BYTE_NETWORK_BYTE_ORDER;
82:
83: return order;
84: }
85:
86: /**
87: * $B@\B3$rJD$8$k(B.
88: */
89: public void close() throws IOException{
90: is.close();
91: os.close();
92: socket.close();
93: }
94:
95: /**
96: * 1 $B%P%$%H$NAw?.$r9T$J$&(B.
97: */
98: public void writeByte(int b) throws IOException{
99: buffer.write(b);
100: }
101:
102: /**
103: * 32 bit integer $B$NAw?.$r9T$J$&(B.
104: * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
105: */
106: public void writeInt(int i) throws IOException{
107: new DataOutputStream(buffer).writeInt(i);
108: }
109:
110: /**
111: * 1 $B%P%$%H$N<u?.$r9T$J$&(B.
112: */
113: public byte readByte() throws IOException{
114: return (byte)is.read();
115: }
116:
117: /**
118: * 32 bit integer $B$N<u?.$r9T$J$&(B.
119: * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
120: */
121: public int readInt() throws IOException{
122: return new DataInputStream(is).readInt();
123: }
124:
125: /**
126: * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
127: */
128: public void write(OXmessage message) throws IOException,MathcapViolation{
129: buffer.reset();
130: message.write(this);
131: buffer.writeTo(os);
132: os.flush();
133: }
134:
135: /**
136: * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
137: * $B%\%G%#$N$_$G$h$/(B, $B%X%C%@$O<+F0$GIU2C$5$l$k(B.
138: */
139: public void send(OXbody object) throws IOException,MathcapViolation{
140: this.write(new OXmessage(serial++,object));
141: }
142:
143: /**
144: * OXtag $B$KAjEv$9$k(B mathcap $B$rF@$k(B.
145: * mathcap $B$,@_Dj$5$l$F$$$J$1$l$P(B, null $B$,JV$5$l$k(B.
146: */
147: public CMO[] getMathcap(int OXtag) throws IOException,MathcapViolation{
148: if(mathcap != null){
149: CMO[] list =((CMO_LIST)mathcap.getList().getElements()[2]).getElements();
150:
151: for(int i=0;i<list.length;i++){
152: CMO[] datacap = ((CMO_LIST)list[i]).getElements();
153:
154: if(((CMO_INT32)datacap[0]).intValue() == OXtag){
155: return datacap;
156: }
157: }
158: }
159:
160: return null;
161: }
162:
163: /**
164: * OX_SYNC_BALL $B$rAw?.$9$k(B. $B8=:_$OL$<BAu(B.
165: */
166: public void sendOX_SYNC_BALL() throws IOException,MathcapViolation{
167: this.send(null);
168: }
169:
170: /**
171: * OpenXM $B%a%C%;!<%8$r<u?.$9$k(B.
172: */
173: public OXmessage receive() throws IOException{
174: return new OXmessage(this);
175: }
176:
177: /**
178: * mathcap $B$r@_Dj$9$k(B.
179: * $B0J8e(B, $BAw?.$9$k:]$K(B mathcap $B$KE,9g$7$F$$$k$+$I$&$+%A%'%C%/$,9T$J$o$l$k(B.
180: */
181: public void setMathCap(CMO_MATHCAP mathcap){
182: this.mathcap = mathcap;
183: }
184: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>