jdk1.4 構建 java多線程,併發設計框架 使用列子(六)

java 多線程,,Socket,網絡,流,併發,框架,已經講完。

 一個客戶端實現的代碼:
  1. import java.io.*;
  2. import java.nio.ByteBuffer;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import com.willfar.commons.net.ConcurrentQueue;
  6. import com.willfar.commons.net.IProcessClientData;
  7. import com.willfar.shantou.engine.protocol.ScadaIfcValidate;
  8. /**
  9.  * 處理客戶端的服務請求,ClientStream
  10.  * ByteArrayOutputStream
  11.  * @author guishuanglin 2008-11-3
  12.  * 
  13.  */
  14. public class ProcessClientDataImpl implements
  15.         IProcessClientData {
  16.     private Log logger = LogFactory.getLog(ProcessClientDataImpl.class);
  17.     //ByteArrayOutputStream
  18.     private ConcurrentQueue dataQueue;
  19.     private ByteArrayOutputStream streamData;
  20.     private ByteArrayOutputStream tempStream;
  21.     private ByteBuffer buf;
  22.     private int requestCount;
  23.     private String netAddress;
  24.     private ScadaIfcValidate sv;
  25.     public void run() {
  26.         try {
  27.             String s = "";
  28.             boolean bool = true;
  29.             sv = new ScadaIfcValidate();
  30.             tempStream = new ByteArrayOutputStream();
  31.             while (bool) {
  32.                 streamData = (ByteArrayOutputStream)dataQueue.deQueue();
  33.                 if (streamData != null) {
  34.                     int ll = streamData.size();
  35.                     if (ll == 3) {
  36.                         s = streamData.toString();
  37.                         if ("end".equals(s)) {
  38.                             logger.info("[" + requestCount + "],客戶端發送數據結束.");
  39.                             bool = false;
  40.                             continue;
  41.                         }
  42.                     }
  43.                     //數據太短,暫時保存
  44.                     streamData.writeTo(tempStream);
  45.                     ll = tempStream.size();
  46.                     if (ll < 3){
  47.                         continue;
  48.                     }
  49.                     byte[] b = tempStream.toByteArray();
  50.                     //驗證數據是否完整,正確.
  51.                     boolean bol = sv.isValid(b);
  52.                     if (bol) {
  53.                         logger.info("[" + requestCount + "],收到客戶端[" 
  54.                                 + netAddress+ "]數據,長度: " + tempStream.size() 
  55.                                 + " 字節,Fn內容:["+ sv.getFn() + "]");
  56.                         new Thread(new ProcessAcceptData(b)).start();
  57.                     }else{
  58.                         //數據長度不夠,等下一包數據
  59.                         if(sv.getRemaining()>0){
  60.                             logger.info("數據總長: "+sv.getAllLength()+" 字節,收到: "
  61.                                     + sv.getCurLength()+" 字節,等下一包數據...");
  62.                             continue;
  63.                         }else{
  64.                             logger.info("[" + requestCount + "],收到客戶端[" + netAddress
  65.                                     + "]數據,長度: " + tempStream.size() + " 字節,內容:[數據不完整]");
  66.                             //無用數據清除
  67.                             this.getStringByByte(tempStream);
  68.                             tempStream.reset();
  69.                             tempStream.close();
  70.                             
  71.                         }
  72.                     }               
  73.                     try {
  74.                         streamData.reset();
  75.                         streamData.close();
  76.                     } catch (IOException e) {
  77.                         e.printStackTrace();
  78.                     }
  79.                 }
  80.             }
  81.         } catch (IOException e1) {
  82.             e1.printStackTrace();
  83.         } finally {
  84.             try {
  85.                 dataQueue.clear();
  86.                 dataQueue = null;
  87.                 tempStream.reset();
  88.                 tempStream.close();
  89.                 tempStream= null;
  90.                 streamData.reset();
  91.                 streamData.close();
  92.                 streamData=null;
  93.                 logger = null;
  94.                 sv =null;
  95.             } catch (IOException e) {
  96.                 e.printStackTrace();
  97.             }
  98.         }
  99.     }
  100.     //ConcurrentQueue<ByteArrayOutputStream>
  101.     public void ProcessStreamData(ConcurrentQueue dataQueue, int requestCount, String netAddress) {
  102.         this.dataQueue = dataQueue;
  103.         this.requestCount = requestCount;
  104.         this.netAddress = netAddress;
  105.     }
  106.     
  107.     public void getStringByByte(ByteArrayOutputStream tempStream){
  108.         //輸出內容
  109.         if(tempStream!=null){
  110.             StringBuffer bf = new StringBuffer();
  111.             byte[] b = tempStream.toByteArray();
  112.             for(int i=0;i<b.length;i++){
  113.                 bf.append(b[i]);
  114.             }
  115.             logger.info(bf.toString());
  116.             bf = null;
  117.             b  = null;
  118.         }
  119.     }
  120. }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章