org.montsuqi.client
Class Connection

java.lang.Object
  extended by org.montsuqi.client.Connection
Direct Known Subclasses:
Protocol

 class Connection
extends java.lang.Object

A class that represents client/server connection.

This class implements methods for sending/receiving basic data types such as booleans, integers and strings.


Field Summary
protected  java.io.DataInput in
           
protected  java.io.DataOutput out
           
 
Constructor Summary
Connection(java.net.Socket s, java.lang.String encoding, boolean networkByteOrder)
          Constructs a Connection instance.
 
Method Summary
(package private)  void close()
           
 int getLastDataType()
          Returns the last(latest) data type received.
 byte[] receiveBinaryData()
           
(package private)  boolean receiveBoolean()
           
 boolean receiveBooleanData()
           
(package private)  int receiveChar()
           
(package private)  int receiveDataType()
          Receives a data type.
 int receiveDataTypeWithCheck(int expected)
          Receives a data type.
(package private)  java.math.BigDecimal receiveFixed()
           
 java.math.BigDecimal receiveFixedData()
           
(package private)  double receiveFloat()
           
 int receiveInt()
           
 int receiveIntData()
           
(package private)  int receiveLength()
          Receives length of 4-byte long.
(package private)  int receiveLong()
          Receives an integer of 4-byte long.
(package private)  byte receivePacketClass()
          Receives a packet class.
 java.lang.String receiveString()
          Receives a string
(package private)  java.lang.String receiveStringBody(int size)
          Receives string data part of given size.
 java.lang.String receiveStringData()
           
(package private)  void sendBoolean(boolean data)
           
 void sendBooleanData(int type, boolean value)
           
(package private)  void sendChar(byte data)
           
 void sendDataType(int c)
          Sends a data type.
(package private)  void sendFixed(java.math.BigDecimal xval)
           
 void sendFixedData(int type, java.math.BigDecimal xval)
           
(package private)  void sendFloat(double data)
           
 void sendInt(int data)
           
 void sendIntegerData(int type, int value)
           
(package private)  void sendLength(int size)
          Sends length of 4-byte long.
(package private)  void sendLong(int data)
           
 void sendPacketClass(int c)
          Sends a packet class.
 void sendString(java.lang.String s)
          Sends a string.
 void sendStringData(int type, java.lang.String str)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected java.io.DataInput in

out

protected java.io.DataOutput out
Constructor Detail

Connection

Connection(java.net.Socket s,
           java.lang.String encoding,
           boolean networkByteOrder)
     throws java.io.IOException

Constructs a Connection instance.

Input/Output streams are initialized correctly by networkByteOrder.

Parameters:
s - the socket to communicate.
encoding - the encoding to use.
networkByteOrder - true if networkByteOrder(Big Endian) is used. false otherwise(Litle Endian).
Throws:
java.io.IOException
Method Detail

sendPacketClass

public void sendPacketClass(int c)
                     throws java.io.IOException

Sends a packet class.

Parameters:
c - packet class. should use constants defined in PacketClass.
Throws:
java.io.IOException - on I/O error.

receivePacketClass

byte receivePacketClass()
                  throws java.io.IOException

Receives a packet class.

Returns:
a byte representing packet class.
Throws:
java.io.IOException - on I/O error.

sendDataType

public void sendDataType(int c)
                  throws java.io.IOException

Sends a data type.

Parameters:
c - data type. should use constants defined in Type.
Throws:
java.io.IOException - on I/O error.

receiveDataType

int receiveDataType()
              throws java.io.IOException

Receives a data type.

Returns:
a byte representing data type.
Throws:
java.io.IOException - on I/O error.

receiveDataTypeWithCheck

public int receiveDataTypeWithCheck(int expected)
                             throws java.io.IOException

Receives a data type. If the type does not match to expected type, an IOException is thrown.

Parameters:
expected - data type that should have been sent from the server.
Returns:
the type received.
Throws:
java.io.IOException - on I/O error or type mismatch.

getLastDataType

public int getLastDataType()

Returns the last(latest) data type received.

Returns:
the latest result of recevieDataType().

sendLength

void sendLength(int size)
          throws java.io.IOException

Sends length of 4-byte long.

Parameters:
size - the length to send.
Throws:
java.io.IOException - on I/O error.

receiveLength

int receiveLength()
            throws java.io.IOException

Receives length of 4-byte long.

Returns:
the length received.
Throws:
java.io.IOException - on I/O error.

sendString

public void sendString(java.lang.String s)
                throws java.io.IOException

Sends a string.

If the client environment uses MS932 characters, incompatible characters are converted to compatible ones.

Parameters:
s - the string to send.
Throws:
java.io.IOException - on I/O error.

receiveStringBody

java.lang.String receiveStringBody(int size)
                             throws java.io.IOException

Receives string data part of given size.

Parameters:
size - the size of the data to be received.
Returns:
a String instance.
Throws:
java.io.IOException - on I/O error.

receiveString

public java.lang.String receiveString()
                               throws java.io.IOException

Receives a string

At first, the length of the string is received. Then the string's data of that length are received and built into a String.

Returns:
a String instance.
Throws:
java.io.IOException - on I/O error.

receiveLong

int receiveLong()
          throws java.io.IOException

Receives an integer of 4-byte long.

Returns:
the integer received.
Throws:
java.io.IOException - on I/O error.

sendLong

void sendLong(int data)
        throws java.io.IOException
Throws:
java.io.IOException

receiveInt

public int receiveInt()
               throws java.io.IOException
Throws:
java.io.IOException

sendInt

public void sendInt(int data)
             throws java.io.IOException
Throws:
java.io.IOException

receiveChar

int receiveChar()
          throws java.io.IOException
Throws:
java.io.IOException

sendChar

void sendChar(byte data)
        throws java.io.IOException
Throws:
java.io.IOException

receiveFloat

double receiveFloat()
              throws java.io.IOException
Throws:
java.io.IOException

sendFloat

void sendFloat(double data)
         throws java.io.IOException
Throws:
java.io.IOException

receiveBoolean

boolean receiveBoolean()
                 throws java.io.IOException
Throws:
java.io.IOException

sendBoolean

void sendBoolean(boolean data)
           throws java.io.IOException
Throws:
java.io.IOException

sendFixed

void sendFixed(java.math.BigDecimal xval)
         throws java.io.IOException
Throws:
java.io.IOException

receiveFixed

java.math.BigDecimal receiveFixed()
                            throws java.io.IOException
Throws:
java.io.IOException

receiveFixedData

public java.math.BigDecimal receiveFixedData()
                                      throws java.io.IOException
Throws:
java.io.IOException

sendFixedData

public void sendFixedData(int type,
                          java.math.BigDecimal xval)
                   throws java.io.IOException
Throws:
java.io.IOException

sendStringData

public void sendStringData(int type,
                           java.lang.String str)
                    throws java.io.IOException
Throws:
java.io.IOException

receiveStringData

public java.lang.String receiveStringData()
                                   throws java.io.IOException
Throws:
java.io.IOException

sendIntegerData

public void sendIntegerData(int type,
                            int value)
                     throws java.io.IOException
Throws:
java.io.IOException

receiveIntData

public int receiveIntData()
                   throws java.io.IOException
Throws:
java.io.IOException

sendBooleanData

public void sendBooleanData(int type,
                            boolean value)
                     throws java.io.IOException
Throws:
java.io.IOException

receiveBooleanData

public boolean receiveBooleanData()
                           throws java.io.IOException
Throws:
java.io.IOException

receiveBinaryData

public byte[] receiveBinaryData()
                         throws java.io.IOException
Throws:
java.io.IOException

close

void close()
     throws java.io.IOException
Throws:
java.io.IOException