CS構造ソフトウェアの自動更新実現(三)


http://www.blogjava.net/rochoc/archive/2009/01/09/250740.html
 
AUpdSrvProc.javaサービススレッド、キーコードの一つ:
 
/** *//********************************************************************
   *                     :rochoc<p>
   *                      :com.rochoc.autoupdate<p>
   *                     :AUpdSrvProc.java<p>
   *                     :kfzx-luoc<p>
   *                     :2008-12-22<p>
   *     ( )      :<p>
   *            
   *                   :
  *                     :
  *                     :
 ********************************************************************/
 package com.rochoc.autoupdate;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.StringReader;
 import java.net.Socket;
 import java.util.HashMap;
 
 /** *//**
  * @author kfzx-luoc
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
 public class AUpdSrvProc extends Thread
 {
     private Socket socket = null;
     private InputStream socketIn;
     private OutputStream socketOut;    
     private Config config = Config.getInstance();//      
     private ClientVerParser cvPaser = null;
     private HashMap cFiles = new HashMap();
     
     byte bFlag [] = new byte[1];//   
     byte bCmd[] = new byte[8];//  
     
     public AUpdSrvProc(Socket socket)
     {
         this.socket = socket;
     }    
    /** *//**
      *           ,     
      */
     public void run()
     {
         try
         {
             config.refresh();//        
             socketIn = socket.getInputStream();
             socketOut = socket.getOutputStream();
             byte datahead [] = new byte[5];//    ,          ,      
             byte buffer[] = new byte[AUPD.BUFFER_SIZE];//           
             byte data[] = new byte[AUPD.DATA_SIZE];//             
             while(true)
             {
                 //     
                 int len = socketIn.read(bFlag,0,1);
                 if(len!=1)
                 {
                     Config.print(socket.getInetAddress()+":       ");
                     socketOut.write(Config.getCmd(AUPD.BYE));//  
                     break;
                 }
                 if(bFlag[0]==AUPD.CMD_DATA_SECT)//   
                 {
                     len = socketIn.read(bCmd,0,8);
                     if(len!=8)
                     {
                         Config.print(socket.getInetAddress()+":      ,CMD="+bCmd);
                         socketOut.write(Config.getCmd(AUPD.BYE));//  
                         break;
                     }
                     if(Config.parseCmd(bCmd).equals(AUPD.READY_TO_UPDATE))//           
                     {
                         Config.print(socket.getInetAddress()+":              ");
                         int ret = sendUpdateFile();
                         switch(ret)
                         {
                             case 0:
                                 socketOut.write(Config.getCmd(AUPD.UPDATED_FAILURE));//  
                             break;
                             case 1:
                                 socketOut.write(Config.getCmd(AUPD.UPDATED_SUCCESSFUL));//  
                             break;
                             default:
                                 socketOut.write(Config.getCmd(AUPD.NOTNEED_UPDATED));//    
                             break;
                         }
                     }else if(Config.parseCmd(bCmd).equals(AUPD.BYE))//    
                     {
                         socketOut.write(Config.getCmd(AUPD.BYE));//  
                         break;
                     }
                 }else if(bFlag[0]==AUPD.MARK_DATA_SECT || bFlag[0]==AUPD.MARK_DATA_END)//    
                {
                    if(Config.parseCmd(bCmd).equals(AUPD.SEND_CLIENT_VERSION))//          
                   {
                        receiveClientVer(bFlag[0]);
                   }else
                    {
                        Config.print("       ,"+new String(bCmd));
                        socketOut.write(Config.getCmd(AUPD.BYE));//  
                        break;
                    }
                }else
                {
                    Config.print(socket.getInetAddress()+":      ,"+bFlag[0]);
                    socketOut.write(Config.getCmd(AUPD.BYE));//  
                    break;
                }
            }//END while(ture)
            //    
            socketIn.close();
            socketOut.close();
            socket.close();
        } catch (IOException e)
        {
            Config.print("           ,"+socket.getInetAddress()+","+e);
            e.printStackTrace();
        }        
    }
    /** *//**
    *     :sendUpdateFile<p>
     *     :<p>
     *     :<p>
     *   :int<p>
     *   :kfzx-luoc
     *   :2008-12-23
     * @return 0.     1.     2.    
     */
    private int sendUpdateFile()
    {
        try
        {
            //                ,     ,     
            if (config.getVerstion().equals(cvPaser.getVerstion()))
            {           
                Config.print(socket.getInetAddress()+":    ,    ");                
                return 2;
            }
            //      
            UpdFile srvFiles [] = config.getFiles();
            boolean isSuccess = true;
            for(int i=0;i<srvFiles.length;i++)
            {
                UpdFile cf = (UpdFile)cFiles.get(srvFiles[i].getName());
                //                    
                if(cf==null || !cf.getVersion().equals(srvFiles[i].getVersion()))
                {
                    if(!sendFile(srvFiles[i]))
                    {
                        isSuccess = false;
                    }                    
                }
            }//END for
            //        ,        
            if(isSuccess)
            {
                UpdFile verFile = new UpdFile("autoupdate.xml");
                verFile.setPath("." + File.separator + "config");
                verFile.setType(0);
               verFile.setVersion(config.getVerstion());
                if(!sendFile(verFile))
                {
                    Config.print(socket.getInetAddress()+":        ");
                    return 0;
                }
                //      
                UpdFile infFile = new UpdFile("history.htm");
                infFile.setPath("." + File.separator + "config");
                infFile.setType(0);
                infFile.setVersion(config.getVerstion());
                if(!sendFile(infFile))
                {
                    Config.print(socket.getInetAddress()+":        ");
                }
                return 1;
            }else
            {
               return 0;
            }
        }catch(Exception e)
        {
            Config.print("          ,"+e);
            e.printStackTrace();
            return 0;
        }
    }
    //0.   1.  
    private boolean sendFileAbsPath(String path)
    {
        try
        {
            byte buffer[] = new byte[AUPD.BUFFER_SIZE];
            int len = 0;
            //      
            buffer[0] = AUPD.MARK_DATA_SECT;
            Config.copyArray(buffer, Config.getLen(path.getBytes().length), 1, 0, 4);//4   
            //     
            for (int i = 0; i < path.getBytes().length; i++)
                buffer[i + 5] = path.getBytes()[i];
            socketOut.write(buffer, 0, path.getBytes().length + 5);//      1   +4   
            //         ,       
            buffer[0] = AUPD.MARK_DATA_END;
            socketOut.write(buffer, 0, 1);
            socketOut.flush();
            //                     
            len = socketIn.read(bFlag,0,1);
            if(len!=1)
            {
                Config.print(socket.getInetAddress()+":       ");
                socketOut.write(Config.getCmd(AUPD.BYE));//  
                return false;
            }
            //    
           len = socketIn.read(bCmd,0,8);
            if(len!=8)
            {
                Config.print(socket.getInetAddress()+":      ,CMD="+bCmd);
                socketOut.write(Config.getCmd(AUPD.BYE));//  
                return false;
            }
            if(Config.parseCmd(bCmd).equals(AUPD.RECEIVED_FILE_ABSOULT))//  
            {
                Config.print(socket.getInetAddress()+":        ,"+path);
                return true;
            }else if(Config.parseCmd(bCmd).equals(AUPD.BYE))//  
            {
                Config.print(socket.getInetAddress()+":        ,"+path);
                return false;
            }else//  
            {
                return false;
            }            
        }catch(Exception e)
        {
            Config.print(socket.getInetAddress()+":        ,"+path);
            e.printStackTrace();
            return false;
        }
    }
    //false.   true.  
    private boolean sendFile(UpdFile file)
    {
       try
       {
           File f = new File(Config.formatPath(file.getPath())+file.getName());
           if(!f.exists()||!f.isFile())
           {
               Config.print(file+",   ,    ");
               return false;
           }
           Config.print(socket.getInetAddress()+":      >>"+file);
           socketOut.write(Config.getCmd(AUPD.SEND_FILE_ABSOULT));//       
           String fileAbsPath = Config.formatPath(file.getPath())+file.getName();
           if(!sendFileAbsPath(fileAbsPath))
          {
               return false;
           }
           socketOut.write(Config.getCmd(AUPD.START_TRANSMIT));//    
           FileInputStream fin = new FileInputStream(f);
           //       
           byte[] data = new byte[AUPD.DATA_SIZE];
           //        
           byte[] buffer = new byte[AUPD.BUFFER_SIZE];
           int len = -1;
           while ((len=fin.read(data)) != -1)
            {
                //       
                buffer[0] = AUPD.MARK_DATA_SECT;
                Config.copyArray(buffer,Config.getLen(len),1,0,4);//    
                //      
                for (int i=0; i<len; i++)
                    buffer[i+5] = data[i];
                socketOut.write(buffer,0,len+5);
            }
           //          ,       
            buffer[0] = AUPD.MARK_DATA_END;            
          socketOut.write(buffer,0,1);
            socketOut.flush();
            fin.close();
            //         
            len = socketIn.read(bFlag,0,1);
            if(len!=1)
            {
                Config.print(socket.getInetAddress()+":       ");
                socketOut.write(Config.getCmd(AUPD.BYE));//  
                return false;
            }
            //    
            len = socketIn.read(bCmd,0,8);
            if(len!=8)
            {
                Config.print(socket.getInetAddress()+":      ,CMD="+new String(bCmd));
                socketOut.write(Config.getCmd(AUPD.BYE));//  
               return false;
           }
            if(Config.parseCmd(bCmd).equals(AUPD.TERMINATE_TRANSMIT))//  
            {
                Config.print(socket.getInetAddress()+":    '"+file+"'  ");
                return true;
            }else if(Config.parseCmd(bCmd).equals(AUPD.BYE))//  
            {
                Config.print(socket.getInetAddress()+":      ,"+file);
                return false;
            }else//  
            {
                Config.print(socket.getInetAddress()+":      ,"+file+","+new String(bCmd));
                return false;
           } 
       }catch(Exception e)
       {
           Config.print("    '"+file+"'  ,"+e);
           e.printStackTrace();
           return false;
       }
    }
    private void receiveClientVer(byte flag)//                
    {
        try
        {
            //       
            byte flagb[] = new byte[1];//  
            byte lenb [] = new byte[4];//  
            //       
            StringBuffer strBuf = new StringBuffer();//      
            int len = -1;
           boolean isFirst = true;
           boolean isOk = false;        
            flagb[0] = flag;
            while(true)
            {
                //   
                if(isFirst)
                {
                    isFirst = false;        
                }else
                {
                    len = socketIn.read(flagb,0,1);//     
                    if(len != 1)
                    {
                        Config.print(socket.getInetAddress() + ":         ");
                        break;
                    }
                }
                //      
                if(flagb[0]==AUPD.MARK_DATA_SECT)
               {
                    len = socketIn.read(lenb, 0, 4);
                    if (len != 4)
                    {
                        Config.print(socket.getInetAddress() + ":        ");
                        break;
                    }
                }
                if (flagb[0] == AUPD.MARK_DATA_SECT)//    
                {
                   int cLen = Integer.parseInt(new String(lenb, 0, 4));//      
                    byte data[] = new byte[cLen];
                    len = socketIn.read(data, 0, cLen);
                   int totLen = len;
                    while (totLen < cLen)//        
                    {
                        strBuf.append(new String(data, 0, len));
                       len = socketIn.read(data, 0, cLen - totLen);
                        totLen = totLen + len;
                    }
                    strBuf.append(new String(data, 0, len));
                }else if(flagb[0]==AUPD.MARK_DATA_END)//    
                {
                    isOk = true;
                    break;
                }else
                {
                    Config.print(socket.getInetAddress()+":       ,"+new String(flagb,0,1)+"<<");
                    break;
                }
            }//END while(true)
            if(isOk)//  
            {
                socketOut.write(Config.getCmd(AUPD.RECEIVED_CLIENT_VERSION));//    
                Config.print("     " + socket.getInetAddress() + "       ");
                cvPaser = new ClientVerParser(new StringReader(strBuf
                        .toString()));
                UpdFile files[] = cvPaser.getFiles();
                for (int i = 0; i < files.length; i++)
                {
                    cFiles.put(files[i].getName(), files[i]);
                }
            }else//  
            {
                socketOut.write(Config.getCmd(AUPD.BYE));//  
            }
        }catch(Exception e)
        {
            Config.print("     "+socket.getInetAddress()+"          ,"+e);
        }
    }    
}
 
AutoUpdateClient.javaクライアントアップグレード処理プロセス、キーコードの2:
 
/** *//********************************************************************
   *                     :rochoc<p>
   *                      :com.rochoc.autoupdate<p>
   *                     :AutoUpdateClient.java<p>
   *                     :kfzx-luoc<p>
   *                     :2008-12-23<p>
   *     ( )      :<p>
   *          
   *                   :
  *                     :
  *                     :
 ********************************************************************/
 package com.rochoc.autoupdate;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.Socket;
 
 /** *//**
  * @author kfzx-luoc
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
 public class AutoUpdateClient
 {
     private Socket socket = null;
     private OutputStream socketOut;
     private InputStream socketIn;
     private Config config = Config.getInstance();//      
     private String currFileAbs = "";//          
     public AutoUpdateClient()
     {
         try
         {
             socket = new Socket(config.getServerIp(),Integer.parseInt(config.getServerPort()));
             socket.setSoTimeout(30000);//30 
         }catch(Exception e)
         {
             Config.print("                 ,"+e);
             e.printStackTrace();
         }
     }
     public void update()
     {
         if(socket == null)
         {
             Config.print("        ,    ");
             return;
         }
         try
         {
             socketOut = socket.getOutputStream();
             socketIn = socket.getInputStream();      
             
             //      
             byte flag [] = new byte[1];
             byte cmd[] = new byte[8];//  
             byte datahead [] = new byte[5];//    ,         ,      
             byte buffer[] = new byte[AUPD.BUFFER_SIZE];//           
             byte data[] = new byte[AUPD.DATA_SIZE];//                     
             //                            
             socketOut.write(Config.getCmd(AUPD.SEND_CLIENT_VERSION));//          
             sendClientVer();//      
             while(true)
             {
                 //    
                 int len = socketIn.read(flag,0,1);
                 if(len!=1)
                 {
                     Config.print("       ");
                     socketOut.write(Config.getCmd(AUPD.BYE));//  
                    break;
                 }
                 if(flag[0]==AUPD.CMD_DATA_SECT)//   
                 {
                     len = socketIn.read(cmd,0,8);
                     if(len!=8)
                     {
                         Config.print("      ");
                         socketOut.write(Config.getCmd(AUPD.BYE));//  
                         break;
                     }
                     if(Config.parseCmd(cmd).equals(AUPD.RECEIVED_CLIENT_VERSION))//      
                     {
                         Config.print("           ");
                         socketOut.write(Config.getCmd(AUPD.READY_TO_UPDATE));
                         continue;
                     }else if(Config.parseCmd(cmd).equals(AUPD.SEND_FILE_ABSOULT))//       
                     {
                         Config.print("         ");                      
                     }else if(Config.parseCmd(cmd).equals(AUPD.UPDATED_FAILURE))//    
                     {
                         Config.print("      ");
                         socketOut.write(Config.getCmd(AUPD.BYE));//  
                         break;
                    }else if(Config.parseCmd(cmd).equals(AUPD.UPDATED_SUCCESSFUL))//    
                    {
                        Config.print("      ");
                        socketOut.write(Config.getCmd(AUPD.BYE));//  
                        //      
                        openFile(".\\config\\history.htm");
                        break;
                    }else if(Config.parseCmd(cmd).equals(AUPD.NOTNEED_UPDATED))//    
                    {
                        Config.print("       ,    ");
                        socketOut.write(Config.getCmd(AUPD.BYE));//  
                       break;
                    }else if(Config.parseCmd(cmd).equals(AUPD.BYE))//    
                    {
                       socketOut.write(Config.getCmd(AUPD.BYE));//  
                        break;
                    }
                }else if(flag[0]==AUPD.MARK_DATA_SECT || flag[0]==AUPD.MARK_DATA_END)//    
                {
                    if(Config.parseCmd(cmd).equals(AUPD.SEND_FILE_ABSOULT))//       
                     {
                        currFileAbs = receiveFileAbsPath(flag[0]);
                        if(currFileAbs!=null && !currFileAbs.equals(""))//  
                        {
                            socketOut.write(Config.getCmd(AUPD.RECEIVED_FILE_ABSOULT));
                            Config.print("       ‘"+currFileAbs+"’  ");
                        }else
                        {
                            Config.print("         ");
                            socketOut.write(Config.getCmd(AUPD.BYE));//  
                            break;
                        }
                   }else if(Config.parseCmd(cmd).equals(AUPD.START_TRANSMIT))//    
                    {
                        if(receiveFile(flag[0]))
                        {
                            socketOut.write(Config.getCmd(AUPD.TERMINATE_TRANSMIT));
                        }else
                        {
                            socketOut.write(Config.getCmd(AUPD.BYE));
                        }
                    }else
                    {
                        Config.print("       ,"+new String(cmd));
                       socketOut.write(Config.getCmd(AUPD.BYE));//  
                       break;
                    }
                }else
                {
                    Config.print("      ,"+flag[0]);
                    socketOut.write(Config.getCmd(AUPD.BYE));//  
                    break;
                }
            }//END while(true)
            
           //       
            socketOut.close();
            socketIn.close();
            socket.close();
            Config.print("        ");
        }catch(Exception e)
        {
           Config.print("      ,"+e);
            e.printStackTrace();
        }
    }
    private void openFile(String file)
    {
        try
       {
            Runtime.getRuntime().exec("cmd /c "+file);
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    private String receiveFileAbsPath(byte flag)
    {
        String absPath = "";
        //       
        try
       {
            //       
            byte flagb[] = new byte[1];//  
            byte lenb [] = new byte[4];//  
            //       
            StringBuffer strBuf = new StringBuffer();//      
           int len = -1;
            boolean isFirst = true;
            boolean isOk = false;        
            flagb[0] = flag;
            while(true)
            {
                //   
               if(isFirst)
                {
                    isFirst = false;        
                }else
                {
                    len = socketIn.read(flagb,0,1);//     
                    if(len != 1)
                    {
                        Config.print(socket.getInetAddress() + ":         ");
                        break;
                    }
                }
               //      
                if(flagb[0]==AUPD.MARK_DATA_SECT)
                {
                    len = socketIn.read(lenb, 0, 4);
                    if (len != 4)
                    {
                        Config.print(socket.getInetAddress() + ":        ");
                   break;
                    }
                }
                if (flagb[0] == AUPD.MARK_DATA_SECT)//    
                {
                    int cLen = Integer.parseInt(new String(lenb, 0, 4));//      
                    byte data[] = new byte[cLen];
                    len = socketIn.read(data, 0, cLen);
                    System.out.println("len:"+len+"cLen="+cLen+">>"+new String(data,0,len));
                    int totLen = len;
                   while (totLen < cLen)//        
                    {
                        strBuf.append(new String(data, 0, len));
                        len = socketIn.read(data, 0, cLen - totLen);
                        totLen = totLen + len;
                        System.out.println("len:"+len+"cLen="+cLen);
                    }
                    strBuf.append(new String(data, 0, len));
                }else if(flagb[0]==AUPD.MARK_DATA_END)//    
              {
                    isOk = true;
                    break;
                }else
                {
                    Config.print(socket.getInetAddress()+":       ,"+new String(flagb,0,1)+"<<");
                    break;
                }
            }//END while(true)
            if(isOk)//  
           {            
                absPath = strBuf.toString();
          }else//  
            {
                socketOut.write(Config.getCmd(AUPD.BYE));//  
            }
      }catch(Exception e)
        {
            Config.print("           ,"+e);
        }
        return absPath;
   }
    private boolean receiveFile(byte flag)
   {
       try
        {
           if(currFileAbs==null||currFileAbs.equals(""))
            {
                Config.print("          ,    ");
               return false;
            }
            File file;
           //         
            //    
            int idx = currFileAbs.lastIndexOf(File.separator);
           String path = currFileAbs.substring(0,idx);
            file = new File(path);
            if(!file.isDirectory() || !file.exists())
            {
                Config.print("     :"+path);
                file.mkdir();
            }
           file = new File(currFileAbs);
            FileOutputStream fout = new FileOutputStream(file);
            //       
            byte flagb[] = new byte[1];//  
            byte lenb [] = new byte[4];//  
            int len = -1;
            boolean isFirst = true;
            boolean isOk = false;
            flagb[0] = flag;
            //         
            while (true)
            {
                //   
                if(isFirst)
                {
                    isFirst = false;        
                }else
              {
                    len = socketIn.read(flagb,0,1);//     
                    if(len != 1)
                    {
                        Config.print(socket.getInetAddress() + ":         ");
                       break;
                    }
              }
                //      
                if(flagb[0]==AUPD.MARK_DATA_SECT)
                {
                    len = socketIn.read(lenb, 0, 4);
                    if (len != 4)
                    {
                        Config.print(socket.getInetAddress() + ":        ");
                        break;
                    }
                }
                if (flagb[0] == AUPD.MARK_DATA_SECT)//    
               {
                    int cLen = Integer.parseInt(new String(lenb, 0, 4));//      
                   byte data[] = new byte[cLen];
                    len = socketIn.read(data, 0, cLen);
                    int totLen = len;
                   while (totLen < cLen)//        
                    {
                       fout.write(data,0,len);
                        len = socketIn.read(data, 0, cLen - totLen);
                        totLen = totLen + len;
                  }
                    fout.write(data,0,len);
                }else if(flagb[0]==AUPD.MARK_DATA_END)//    
                {
                    isOk = true;
                    break;
                }else
                {
                    Config.print(socket.getInetAddress()+":       ,"+new String(flagb,0,1)+"<<");
                    break;
                }
            }//END while
            fout.flush();
           fout.close();
            if(isOk)
            {
                Config.print("      :"+file.getAbsolutePath());
                return true;
            }else
            {
                Config.print("    :"+file.getAbsolutePath()+"  ");                
                return false;
            }
        }catch(Exception e)
        {
            Config.print("      '"+currFileAbs+"'  ,"+e);
            e.printStackTrace();
            return false;
        }
    }
    //         
    private void sendClientVer()
    {
        try
      {
            File verFile = new File(Config.cfgFile);
            if(!verFile.isFile() || !verFile.exists())
            {
                Config.print("         ");
                return;
            }
            //    
            FileInputStream fis = new FileInputStream(verFile);
           byte buffer[] = new byte[AUPD.BUFFER_SIZE];
           byte data[] = new byte[AUPD.DATA_SIZE];
           int len = 0;
            while((len=fis.read(data))!=-1)
            {
               //      
                buffer[0] = AUPD.MARK_DATA_SECT;
               Config.copyArray(buffer,Config.getLen(len),1,0,4);//4   
                //     
                for (int i=0; i<len; i++)
                    buffer[i+5] = data[i];//      1   +4   
                socketOut.write(buffer,0,len+5);//    
            }//END while
            //         ,       
            buffer[0] = AUPD.MARK_DATA_END;
            socketOut.write(buffer,0,1);
            socketOut.flush();
            fis.close();
            Config.print("        ");
      }catch(Exception e)
        {
            Config.print("            ,"+e);
            e.printStackTrace();
       }
    }    
    //     
    public static void main(String args[])
    {
        AutoUpdateClient client = new AutoUpdateClient();
        client.update();
    }
}