[BACK]Return to OpenXMstream.java CVS log [TXT][DIR] Up to [local] / OpenXM / src / OpenMath / ORG / openxm / tam

Annotation of OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java, Revision 1.4

1.1       tam         1: /**
1.4     ! takayama    2:  * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java,v 1.3 2001/03/02 11:13:57 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;
1.3       tam        18:   //private Socket socket = null;
1.1       tam        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();
1.4     ! takayama   24:   private ServerSocket ssocket = null;
1.1       tam        25:
                     26:   /**
                     27:    * $B%M%C%H%o!<%/%P%$%H%*!<%@$rI=$9(B.
                     28:    * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
                     29:    */
                     30:   final public static int OX_BYTE_NETWORK_BYTE_ORDER = 0;
                     31:
                     32:   /**
                     33:    * $B%j%H%k%$%s%G%#%"%s$rI=$9(B.
                     34:    * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
                     35:    */
                     36:   final public static int OX_BYTE_LITTLE_INDIAN      = 1;
                     37:
                     38:   /**
                     39:    * $B%S%C%0%$%s%G%#%"%s$rI=$9(B.
                     40:    * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
                     41:    */
                     42:   final public static int OX_BYTE_BIG_INDIAN         = 0xff;
                     43:
                     44:   /**
1.3       tam        45:    * InputStream is $B$H(B OutputStream os $B$rMQ$$$F(B OpenXM $BDL?.$r9T$J$&(B.
                     46:    */
                     47:   public OpenXMstream(InputStream is,OutputStream os) throws IOException{
                     48:     this.is = is;
                     49:     this.os = os;
                     50:   }
                     51:
                     52:   /**
1.1       tam        53:    * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
1.3       tam        54:    * $B@\B3$r9T$J$&(B. $B8=:_$O8_49@-$N$?$a$K;D$7$F$"$k(B.
1.1       tam        55:    */
                     56:   public OpenXMstream(String host,int Port) throws IOException{
                     57:     this(host,Port,false);
                     58:   }
                     59:
                     60:   /**
                     61:    * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
                     62:    * $B@\B3$r9T$J$&(B. reverse $B$,(B true $B$N$H$-!"(B TCP $B%=%1%C%H$r(B
1.3       tam        63:    * $B:n@.$7(B, $B@\B3$rBT$D(B. $B8=:_$O8_49@-$N$?$a$K;D$7$F$"$k(B.
1.1       tam        64:    */
                     65:   public OpenXMstream(String host,int Port,boolean reverse) throws IOException{
1.3       tam        66:     Socket socket;
                     67:
1.1       tam        68:     // create socket
                     69:     if(!reverse){
                     70:       socket = new Socket(host,Port);
                     71:     }else{
                     72:       socket = new ServerSocket(Port,50,InetAddress.getByName(host)).accept();
                     73:     }
                     74:
1.4     ! takayama   75:     //is =new DebugInputStream(new BufferedInputStream(socket.getInputStream()));
        !            76:     is= new BufferedInputStream(socket.getInputStream());
        !            77:     os = socket.getOutputStream();
        !            78:   }
        !            79:
        !            80:   public OpenXMstream(String host,int Port,String pass) throws IOException{
        !            81:     // create socket
        !            82:     ssocket = new ServerSocket(Port,1,InetAddress.getByName(host));
        !            83:        // pass has not been implemented.
        !            84:   }
        !            85:
        !            86:   public void OpenXMstreamAccept() throws IOException{
        !            87:        Socket socket = ssocket.accept();
1.1       tam        88:     //is =new DebugInputStream(new BufferedInputStream(socket.getInputStream()));
1.3       tam        89:     is= new BufferedInputStream(socket.getInputStream());
1.1       tam        90:     os = socket.getOutputStream();
                     91:   }
                     92:
                     93:   /**
                     94:    * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
                     95:    */
1.3       tam        96:   public int exchangeByteOrder(int order) throws IOException{
1.1       tam        97:     // send byte order
                     98:     os.write(OX_BYTE_NETWORK_BYTE_ORDER);
                     99:     os.flush();
                    100:
                    101:     // receive byte order
1.2       ohara     102:     //System.err.println("get: "+is.read());
1.1       tam       103:     is.read();
                    104:
1.3       tam       105:     this.order = OX_BYTE_NETWORK_BYTE_ORDER;
1.1       tam       106:
1.3       tam       107:     return this.order;
                    108:   }
                    109:
                    110:   /**
                    111:    * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
                    112:    * $B?d>)$5$l$J$$(B.
                    113:    */
                    114:   public int sendByteOrder() throws IOException{
                    115:     return exchangeByteOrder(OX_BYTE_NETWORK_BYTE_ORDER);
1.1       tam       116:   }
                    117:
                    118:   /**
                    119:    * $B@\B3$rJD$8$k(B.
                    120:    */
                    121:   public void close() throws IOException{
                    122:     is.close();
                    123:     os.close();
                    124:   }
                    125:
                    126:   /**
                    127:    * 1 $B%P%$%H$NAw?.$r9T$J$&(B.
                    128:    */
                    129:   public void writeByte(int b) throws IOException{
                    130:     buffer.write(b);
                    131:   }
                    132:
                    133:   /**
                    134:    * 32 bit integer $B$NAw?.$r9T$J$&(B.
                    135:    * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
                    136:    */
                    137:   public void writeInt(int i) throws IOException{
                    138:     new DataOutputStream(buffer).writeInt(i);
                    139:   }
                    140:
                    141:   /**
                    142:    * 1 $B%P%$%H$N<u?.$r9T$J$&(B.
                    143:    */
                    144:   public byte readByte() throws IOException{
                    145:     return (byte)is.read();
                    146:   }
                    147:
                    148:   /**
                    149:    * 32 bit integer $B$N<u?.$r9T$J$&(B.
                    150:    * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
                    151:    */
                    152:   public int readInt() throws IOException{
                    153:     return new DataInputStream(is).readInt();
                    154:   }
                    155:
                    156:   /**
                    157:    * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
                    158:    */
                    159:   public void write(OXmessage message) throws IOException,MathcapViolation{
                    160:     buffer.reset();
                    161:     message.write(this);
                    162:     buffer.writeTo(os);
                    163:     os.flush();
                    164:   }
                    165:
                    166:   /**
                    167:    * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
                    168:    * $B%\%G%#$N$_$G$h$/(B, $B%X%C%@$O<+F0$GIU2C$5$l$k(B.
                    169:    */
                    170:   public void send(OXbody object) throws IOException,MathcapViolation{
                    171:     this.write(new OXmessage(serial++,object));
                    172:   }
                    173:
                    174:   /**
                    175:    * OXtag $B$KAjEv$9$k(B mathcap $B$rF@$k(B.
                    176:    * mathcap $B$,@_Dj$5$l$F$$$J$1$l$P(B, null $B$,JV$5$l$k(B.
                    177:    */
                    178:   public CMO[] getMathcap(int OXtag) throws IOException,MathcapViolation{
                    179:     if(mathcap != null){
                    180:       CMO[] list =((CMO_LIST)mathcap.getList().getElements()[2]).getElements();
                    181:
                    182:       for(int i=0;i<list.length;i++){
                    183:        CMO[] datacap = ((CMO_LIST)list[i]).getElements();
                    184:
                    185:        if(((CMO_INT32)datacap[0]).intValue() == OXtag){
                    186:          return datacap;
                    187:        }
                    188:       }
                    189:     }
                    190:
                    191:     return null;
                    192:   }
                    193:
                    194:   /**
                    195:    * OX_SYNC_BALL $B$rAw?.$9$k(B. $B8=:_$OL$<BAu(B.
                    196:    */
                    197:   public void sendOX_SYNC_BALL() throws IOException,MathcapViolation{
                    198:     this.send(null);
                    199:   }
                    200:
                    201:   /**
                    202:    * OpenXM $B%a%C%;!<%8$r<u?.$9$k(B.
                    203:    */
                    204:   public OXmessage receive() throws IOException{
                    205:     return new OXmessage(this);
                    206:   }
                    207:
                    208:   /**
                    209:    * mathcap $B$r@_Dj$9$k(B.
                    210:    * $B0J8e(B, $BAw?.$9$k:]$K(B mathcap $B$KE,9g$7$F$$$k$+$I$&$+%A%'%C%/$,9T$J$o$l$k(B.
                    211:    */
                    212:   public void setMathCap(CMO_MATHCAP mathcap){
                    213:     this.mathcap = mathcap;
                    214:   }
                    215: }

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