Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package bulk.reserving.helpers;
- import java.io.*;
- import java.net.*;
- import bulk.sql.Tm_IOHelper;
- /** Frame socket class to easy read/write frames
- * @author Vladimir Sadovnikov
- *
- */
- public class Tm_FrameSocket
- {
- private static final int RESET_COUNTER = 0x20; // Flush stream after 20 frames
- private Socket m_Socket = null;
- private ObjectInputStream m_OIS = null;
- private ObjectOutputStream m_OOS = null;
- private int m_ResetCounter = RESET_COUNTER;
- /** Create frame socket
- * @param sock
- * @throws IOException
- */
- public Tm_FrameSocket(Socket sock) throws IOException
- {
- // Remember socket
- m_Socket = sock;
- // Get object streams
- try
- {
- m_OOS = new ObjectOutputStream(/*new BufferedOutputStream(*/sock.getOutputStream()/*)*/);
- m_OIS = new ObjectInputStream(/*new BufferedInputStream(*/sock.getInputStream()/*)*/);
- }
- catch (IOException ex)
- {
- close();
- throw ex;
- }
- }
- /** Read frame to serve
- * @return read frame from socket
- * @throws IOException
- */
- public Tm_Frame readFrame() throws IOException
- {
- return readFrame(null);
- }
- /** Read frame
- * @param frame socket frame required to read
- * @return socket frame required to read or null
- * @throws IOException
- */
- public Tm_Frame readFrame(En_FrameType frame) throws IOException
- {
- while (true)
- {
- try
- {
- Object obj = m_OIS.readObject();
- if (obj instanceof Tm_Frame)
- {
- Tm_Frame frm = (Tm_Frame)obj;
- // First check if all frames are accepted
- if (frame==null)
- return frm;
- // Then check if it is a needed frame
- if (frm.getType() == frame)
- return frm;
- }
- return null;
- }
- catch (ClassNotFoundException ex)
- {
- /* Nothing */
- }
- }
- }
- /**
- * @param type
- * @throws IOException
- */
- public void writeFrame(En_FrameType type) throws IOException
- {
- writeFrame(new Tm_Frame(type));
- }
- /** Write frame to output and read answer
- * @param frm frame answer
- * @param answer needed frame type or null
- * @return read frame
- * @throws IOException
- */
- public Tm_Frame poll(Tm_Frame frm, En_FrameType answer) throws IOException
- {
- writeFrame(frm);
- return readFrame(answer);
- }
- /** Write frame and read answer without checking frame type
- * @param frm frame to write
- * @return answer frame
- * @throws IOException
- */
- public Tm_Frame poll(Tm_Frame frm) throws IOException
- {
- writeFrame(frm);
- return readFrame();
- }
- /** Make a poll and get answer
- * @param frm frame polling type
- * @param answer needed frame type or null
- * @return read frame
- * @throws IOException
- */
- public Tm_Frame poll(En_FrameType frm, En_FrameType answer) throws IOException
- {
- writeFrame(frm);
- return readFrame(answer);
- }
- /** Make a poll and get answer without checking frame type
- * @param frm frame polling type
- * @return read frame
- * @throws IOException
- */
- public Tm_Frame poll(En_FrameType frm) throws IOException
- {
- writeFrame(frm);
- return readFrame();
- }
- /** Write frame to output
- * @param frm frame
- * @throws IOException
- */
- public void writeFrame(Tm_Frame frm) throws IOException
- {
- m_OOS.writeObject(frm);
- m_OOS.flush();
- if ((--m_ResetCounter) <= 0) // Achtung!!! There was a problem that no reset() was called
- {
- m_OOS.reset();
- m_ResetCounter = RESET_COUNTER;
- }
- }
- /** Safely close socket
- *
- */
- public void close()
- {
- // Close output stream
- try
- {
- if (m_OOS!=null)
- m_OOS.close();
- }
- catch (IOException ex)
- {
- Tm_IOHelper.safeCloseOutputStream(m_OOS);
- }
- // Close input stream
- try
- {
- if (m_OIS!=null)
- m_OIS.close();
- }
- catch (IOException ex)
- {
- Tm_IOHelper.safeCloseInputStream(m_OIS);
- }
- safeCloseSocket(m_Socket);
- }
- /** Safe close socket
- * @param sock socket to close
- */
- private static void safeCloseSocket(Socket sock)
- {
- try
- {
- if (sock!=null)
- sock.close();
- }
- catch (IOException ex)
- {
- /* nothing */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement