Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package bulk.reserving.helpers;
  5.  
  6. import java.io.*;
  7. import java.net.*;
  8.  
  9. import bulk.sql.Tm_IOHelper;
  10.  
  11. /** Frame socket class to easy read/write frames
  12.  * @author Vladimir Sadovnikov
  13.  *
  14.  */
  15. public class Tm_FrameSocket
  16. {
  17.     private static final int    RESET_COUNTER = 0x20; // Flush stream after 20 frames
  18.    
  19.     private Socket              m_Socket = null;
  20.     private ObjectInputStream   m_OIS    = null;
  21.     private ObjectOutputStream  m_OOS    = null;
  22.     private int                 m_ResetCounter = RESET_COUNTER;
  23.    
  24.     /** Create frame socket
  25.      * @param sock
  26.      * @throws IOException
  27.      */
  28.     public Tm_FrameSocket(Socket sock) throws IOException
  29.     {
  30.         // Remember socket
  31.         m_Socket = sock;
  32.        
  33.         // Get object streams
  34.         try
  35.         {
  36.             m_OOS = new ObjectOutputStream(/*new BufferedOutputStream(*/sock.getOutputStream()/*)*/);
  37.             m_OIS = new ObjectInputStream(/*new BufferedInputStream(*/sock.getInputStream()/*)*/);
  38.         }
  39.         catch (IOException ex)
  40.         {
  41.             close();
  42.             throw ex;
  43.         }
  44.     }
  45.    
  46.     /** Read frame to serve
  47.      * @return read frame from socket
  48.      * @throws IOException
  49.      */
  50.     public Tm_Frame readFrame() throws IOException
  51.     {
  52.         return readFrame(null);
  53.     }
  54.    
  55.     /** Read frame
  56.      * @param frame socket frame required to read
  57.      * @return socket frame required to read or null
  58.      * @throws IOException
  59.      */
  60.     public Tm_Frame readFrame(En_FrameType frame) throws IOException
  61.     {
  62.         while (true)
  63.         {
  64.             try
  65.             {
  66.                 Object obj = m_OIS.readObject();
  67.                
  68.                 if (obj instanceof Tm_Frame)
  69.                 {
  70.                     Tm_Frame frm = (Tm_Frame)obj;
  71.                    
  72.                     // First check if all frames are accepted
  73.                     if (frame==null)
  74.                         return frm;
  75.                    
  76.                     // Then check if it is a needed frame
  77.                     if (frm.getType() == frame)
  78.                         return frm;
  79.                 }
  80.                
  81.                 return null;
  82.             }
  83.             catch (ClassNotFoundException ex)
  84.             {
  85.                 /* Nothing */
  86.             }
  87.         }
  88.     }
  89.    
  90.     /**
  91.      * @param type
  92.      * @throws IOException
  93.      */
  94.     public void writeFrame(En_FrameType type) throws IOException
  95.     {
  96.         writeFrame(new Tm_Frame(type));    
  97.     }
  98.  
  99.     /** Write frame to output and read answer
  100.      * @param frm frame answer
  101.      * @param answer needed frame type or null
  102.      * @return read frame
  103.      * @throws IOException
  104.      */
  105.     public Tm_Frame poll(Tm_Frame frm, En_FrameType answer) throws IOException
  106.     {
  107.         writeFrame(frm);
  108.         return readFrame(answer);
  109.     }
  110.    
  111.     /** Write frame and read answer without checking frame type
  112.      * @param frm frame to write
  113.      * @return answer frame
  114.      * @throws IOException
  115.      */
  116.     public Tm_Frame poll(Tm_Frame frm) throws IOException
  117.     {
  118.         writeFrame(frm);
  119.         return readFrame();
  120.     }
  121.    
  122.     /** Make a poll and get answer
  123.      * @param frm frame polling type
  124.      * @param answer needed frame type or null
  125.      * @return read frame
  126.      * @throws IOException
  127.      */
  128.     public Tm_Frame poll(En_FrameType frm, En_FrameType answer) throws IOException
  129.     {
  130.         writeFrame(frm);
  131.         return readFrame(answer);
  132.     }
  133.    
  134.     /** Make a poll and get answer without checking frame type
  135.      * @param frm frame polling type
  136.      * @return read frame
  137.      * @throws IOException
  138.      */
  139.     public Tm_Frame poll(En_FrameType frm) throws IOException
  140.     {
  141.         writeFrame(frm);
  142.         return readFrame();
  143.     }
  144.    
  145.    
  146.     /** Write frame to output
  147.      * @param frm frame
  148.      * @throws IOException
  149.      */
  150.     public void writeFrame(Tm_Frame frm) throws IOException
  151.     {
  152.         m_OOS.writeObject(frm);
  153.         m_OOS.flush();
  154.        
  155.         if ((--m_ResetCounter) <= 0) // Achtung!!! There was a problem that no reset() was called
  156.         {
  157.             m_OOS.reset();
  158.             m_ResetCounter = RESET_COUNTER;
  159.         }
  160.     }
  161.    
  162.     /** Safely close socket
  163.      *
  164.      */
  165.     public void close()
  166.     {
  167.         // Close output stream
  168.         try
  169.         {
  170.             if (m_OOS!=null)
  171.                 m_OOS.close();
  172.         }
  173.         catch (IOException ex)
  174.         {
  175.             Tm_IOHelper.safeCloseOutputStream(m_OOS);
  176.         }
  177.        
  178.         // Close input stream
  179.         try
  180.         {
  181.             if (m_OIS!=null)
  182.                 m_OIS.close();
  183.         }
  184.         catch (IOException ex)
  185.         {
  186.             Tm_IOHelper.safeCloseInputStream(m_OIS);
  187.         }
  188.        
  189.         safeCloseSocket(m_Socket);
  190.     }
  191.    
  192.     /** Safe close socket
  193.      * @param sock socket to close
  194.      */
  195.     private static void safeCloseSocket(Socket sock)
  196.     {
  197.         try
  198.         {
  199.             if (sock!=null)
  200.                 sock.close();
  201.         }
  202.         catch (IOException ex)
  203.         {
  204.             /* nothing */
  205.         }
  206.        
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement