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

File: [local] / OpenXM / src / OpenMath / ORG / openxm / tam / OpenXMstream.java (download)

Revision 1.4, Wed Oct 23 08:40:17 2002 UTC (21 years, 8 months ago) by takayama
Branch: MAIN
CVS Tags: R_1_3_1-2, RELEASE_1_3_1_13b, RELEASE_1_2_3_12, RELEASE_1_2_3, RELEASE_1_2_2_KNOPPIX_b, RELEASE_1_2_2_KNOPPIX, RELEASE_1_2_2, KNOPPIX_2006, HEAD, DEB_REL_1_2_3-9
Changes since 1.3: +15 -1 lines

OpenXM(host,ctrlPort,streamPort,ox_server,oxd_port,pass)
starts "ox_server" by calling oxd super server at the port "oxd_port".
(Implementation has not been finished.)

/**
 * $OpenXM: OpenXM/src/OpenMath/ORG/openxm/tam/OpenXMstream.java,v 1.4 2002/10/23 08:40:17 takayama Exp $
 */
package ORG.openxm.tam;

import java.io.*;
import java.net.*;

/**
 * OpenXM $B%a%C%;!<%8$NAw<u?.$r9T$J$&%/%i%9(B.
 * $B%3%s%H%m!<%k(B, $B%G!<%?$=$l$>$l$K0l$D$:$DI,MW$H$J$k(B.
 * $B$3$N%/%i%9$G$O(B, $B@\B3;~$K%P%$%H%*!<%@$N8r49$r9T$J$$(B,
 * 32bit $B@0?t$NAw<u?.;~$K%P%$%H%*!<%@$NJQ49$r9T$J$&(B.
 * $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@$N$_<BAu$5$l$F$$$k(B.
 */
public class OpenXMstream{
  private int serial = 0;
  //private Socket socket = null;
  private InputStream  is = null;
  private OutputStream os = null;
  private int order = OX_BYTE_NETWORK_BYTE_ORDER;
  private CMO_MATHCAP mathcap = null;
  private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  private ServerSocket ssocket = null;

  /**
   * $B%M%C%H%o!<%/%P%$%H%*!<%@$rI=$9(B.
   * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
   */
  final public static int OX_BYTE_NETWORK_BYTE_ORDER = 0;

  /**
   * $B%j%H%k%$%s%G%#%"%s$rI=$9(B.
   * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
   */
  final public static int OX_BYTE_LITTLE_INDIAN      = 1;

  /**
   * $B%S%C%0%$%s%G%#%"%s$rI=$9(B.
   * $B%P%$%H%*!<%@$N7hDj;~$KMQ$$$k(B.
   */
  final public static int OX_BYTE_BIG_INDIAN         = 0xff;

  /**
   * InputStream is $B$H(B OutputStream os $B$rMQ$$$F(B OpenXM $BDL?.$r9T$J$&(B.
   */
  public OpenXMstream(InputStream is,OutputStream os) throws IOException{
    this.is = is;
    this.os = os;
  }

  /**
   * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
   * $B@\B3$r9T$J$&(B. $B8=:_$O8_49@-$N$?$a$K;D$7$F$"$k(B.
   */
  public OpenXMstream(String host,int Port) throws IOException{
    this(host,Port,false);
  }

  /**
   * $B%^%7%sL>(B host $B$N%]!<%HHV9f(B Port $B$X(B TCP $B@\B3$r9T$J$$(B,
   * $B@\B3$r9T$J$&(B. reverse $B$,(B true $B$N$H$-!"(B TCP $B%=%1%C%H$r(B
   * $B:n@.$7(B, $B@\B3$rBT$D(B. $B8=:_$O8_49@-$N$?$a$K;D$7$F$"$k(B.
   */
  public OpenXMstream(String host,int Port,boolean reverse) throws IOException{
    Socket socket;

    // create socket
    if(!reverse){
      socket = new Socket(host,Port);
    }else{
      socket = new ServerSocket(Port,50,InetAddress.getByName(host)).accept();
    }

    //is =new DebugInputStream(new BufferedInputStream(socket.getInputStream()));
    is= new BufferedInputStream(socket.getInputStream());
    os = socket.getOutputStream();
  }

  public OpenXMstream(String host,int Port,String pass) throws IOException{
    // create socket
    ssocket = new ServerSocket(Port,1,InetAddress.getByName(host));
	// pass has not been implemented.
  }

  public void OpenXMstreamAccept() throws IOException{
	Socket socket = ssocket.accept();
    //is =new DebugInputStream(new BufferedInputStream(socket.getInputStream()));
    is= new BufferedInputStream(socket.getInputStream());
    os = socket.getOutputStream();
  }

  /**
   * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
   */
  public int exchangeByteOrder(int order) throws IOException{
    // send byte order
    os.write(OX_BYTE_NETWORK_BYTE_ORDER);
    os.flush();

    // receive byte order
    //System.err.println("get: "+is.read());
    is.read();

    this.order = OX_BYTE_NETWORK_BYTE_ORDER;

    return this.order;
  }

  /**
   * $B%P%$%H%*!<%@$N7hDj$r9T$J$&(B. $B8=:_$O%M%C%H%o!<%/%P%$%H%*!<%@!<$N$_(B.
   * $B?d>)$5$l$J$$(B.
   */
  public int sendByteOrder() throws IOException{
    return exchangeByteOrder(OX_BYTE_NETWORK_BYTE_ORDER);
  }

  /**
   * $B@\B3$rJD$8$k(B.
   */
  public void close() throws IOException{
    is.close();
    os.close();
  }

  /**
   * 1 $B%P%$%H$NAw?.$r9T$J$&(B.
   */
  public void writeByte(int b) throws IOException{
    buffer.write(b);
  }

  /**
   * 32 bit integer $B$NAw?.$r9T$J$&(B.
   * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
   */
  public void writeInt(int i) throws IOException{
    new DataOutputStream(buffer).writeInt(i);
  }

  /**
   * 1 $B%P%$%H$N<u?.$r9T$J$&(B.
   */
  public byte readByte() throws IOException{
    return (byte)is.read();
  }

  /**
   * 32 bit integer $B$N<u?.$r9T$J$&(B.
   * $B$3$N%a%=%C%I$O(B, $B%P%$%H%*!<%@$NJQ49$r<+F0$G9T$J$&(B.
   */
  public int readInt() throws IOException{
    return new DataInputStream(is).readInt();
  }

  /**
   * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
   */
  public void write(OXmessage message) throws IOException,MathcapViolation{
    buffer.reset();
    message.write(this);
    buffer.writeTo(os);
    os.flush();
  }

  /**
   * OpenXM $B%a%C%;!<%8$NAw?.$r9T$J$&(B.
   * $B%\%G%#$N$_$G$h$/(B, $B%X%C%@$O<+F0$GIU2C$5$l$k(B.
   */
  public void send(OXbody object) throws IOException,MathcapViolation{
    this.write(new OXmessage(serial++,object));
  }

  /**
   * OXtag $B$KAjEv$9$k(B mathcap $B$rF@$k(B.
   * mathcap $B$,@_Dj$5$l$F$$$J$1$l$P(B, null $B$,JV$5$l$k(B.
   */
  public CMO[] getMathcap(int OXtag) throws IOException,MathcapViolation{
    if(mathcap != null){
      CMO[] list =((CMO_LIST)mathcap.getList().getElements()[2]).getElements();

      for(int i=0;i<list.length;i++){
	CMO[] datacap = ((CMO_LIST)list[i]).getElements();

	if(((CMO_INT32)datacap[0]).intValue() == OXtag){
	  return datacap;
	}
      }
    }

    return null;
  }

  /**
   * OX_SYNC_BALL $B$rAw?.$9$k(B. $B8=:_$OL$<BAu(B.
   */
  public void sendOX_SYNC_BALL() throws IOException,MathcapViolation{
    this.send(null);
  }

  /**
   * OpenXM $B%a%C%;!<%8$r<u?.$9$k(B.
   */
  public OXmessage receive() throws IOException{
    return new OXmessage(this);
  }

  /**
   * mathcap $B$r@_Dj$9$k(B.
   * $B0J8e(B, $BAw?.$9$k:]$K(B mathcap $B$KE,9g$7$F$$$k$+$I$&$+%A%'%C%/$,9T$J$o$l$k(B.
   */
  public void setMathCap(CMO_MATHCAP mathcap){
    this.mathcap = mathcap;
  }
}