[Title] Changed "FileTransfer" to use object method
authordonghee yang <donghee.yang@samsung.com>
Tue, 9 Oct 2012 09:37:05 +0000 (18:37 +0900)
committerdonghee yang <donghee.yang@samsung.com>
Tue, 9 Oct 2012 09:37:05 +0000 (18:37 +0900)
src/build_server/BuildComm.rb
src/common/fileTransfer.rb

index 96b490fe1309c9c196aed4e5d28b29a8566f7f5f..d8a02916c35f48badcf2eb86bd6ef174b0a61451 100644 (file)
@@ -163,8 +163,9 @@ class BuildCommServer
 
                                        # upload to ftp server
                                        ftp_filepath = nil                                      
+                                       @trans = FileTransfer.new(ip, port, username, passwd, @log)
                                        for attempt in ATTEMPTS
-                           ftp_filepath = FileTransfer.putfile(ip, port, username, passwd, src_file, @log)
+                           ftp_filepath = @trans.putfile( src_file )
                                                if !ftp_filepath.nil? then break;
                                                else @log.info "Server is the #{attempt} upload attempt fails" end
                                        end                                             
@@ -177,7 +178,7 @@ class BuildCommServer
                     req.puts "UPLOADED,#{ftp_filepath}"
                 elsif cmd == "SUCC" then
                     @log.info "Client downloaded file successfully"
-                    FileTransfer.cleandir(ip, port, username, passwd, ftp_filepath, @log)
+                    @trans.cleandir( ftp_filepath )
                     @log.info "Cleaned temporary dir on FTP server: #{ftp_filepath}"
                     break 
                                elsif cmd == "ERROR" then                                       
@@ -256,8 +257,9 @@ class BuildCommServer
 
                                        # download from ftp server
                                        dst_filepath = nil
+                                       @trans = FileTransfer.new(ip, port, username, passwd, @log)
                                        for attempt in ATTEMPTS
-                           dst_filepath = FileTransfer.getfile(ip, port, username, passwd, filepath, dst_file, @log)
+                           dst_filepath = @trans.getfile( filepath, dst_file )
                                                if not dst_filepath.nil? then break
                                                else 
                                                        @log.warn "Server is the #{attempt} download attempt fails" 
@@ -549,9 +551,11 @@ class BuildCommClient
                     @log.info "Server already has cached file"
                 elsif line.strip == "NOT_CACHED" then
                        @log.info "Server doest not have cached file"
+
                                        ftp_filepath = nil
+                                       @trans = FileTransfer.new(ip, port, username, passwd, @log)
                                        for attempt in ATTEMPTS
-                       ftp_filepath = FileTransfer.putfile(ip, port, username, passwd, src_file, @log)
+                       ftp_filepath = @trans.putfile( src_file )
                                                if !ftp_filepath.nil? then break;
                                                else @log.info "Client is the #{attempt} upload attempt fails" end
                                        end                                             
@@ -562,7 +566,7 @@ class BuildCommClient
                     send "UPLOADED,#{ip},#{port},#{ftp_filepath},#{username},#{passwd}"
                 elsif line.strip == "SUCC" then 
                     @log.info "Server downloaded file sucessfully"
-                    FileTransfer.cleandir(ip, port, username, passwd, ftp_filepath, @log)
+                    @trans.cleandir( ftp_filepath )
                     @log.info "Client cleaned temporary dir on ftp server: #{ftp_filepath}"
                 elsif line.strip == "ERROR" then
                     @log.error "Server failed to download the file. Please check server log"
@@ -618,8 +622,9 @@ class BuildCommClient
                     ftp_filepath = tok[1].strip
                                        @log.info "Server uploaded file sucessfully"                                    
                                        dst_filepath = nil
+                                       @trans = FileTransfer.new(ip, port, username, passwd, @log)
                                        for attempt in ATTEMPTS
-                           dst_filepath = FileTransfer.getfile(ip, port, username, passwd, ftp_filepath, dst_file, @log)
+                           dst_filepath = @trans.getfile( ftp_filepath, dst_file )
                                                if not dst_filepath.nil? then break
                                                else 
                                                        @log.warn "Client is the #{attempt} download attempt fails" 
index cda6a4b0ac7d29e6bf48d3233e7fcabd45409bf3..09461ccae545aa2cd947cd974b5bacb0925d90e7 100644 (file)
@@ -1,46 +1,59 @@
 
 require 'socket'
+require 'log'
 
 class FileTransfer
+       def initialize(ip, port, username, passwd, logger)
+               @ip = ip
+               @port = port
+               @username = username
+               @passwd = passwd
+               if not logger.nil? then
+                       @log = logger
+               else
+                       @log = DummyLog.new
+               end
+       end
+
 
-    def FileTransfer.putfile(ip, port, username, passwd, bpath, logger)
+    def putfile( bpath )
         filename = File.basename(bpath)
         uniqdir = Utils.create_uniq_name
         ftp_filepath = File.join(uniqdir, filename)
 
         begin
             ftp = Net::FTP.new
-            if port.nil? or port == "" then
-                ftp.connect(ip)
+            if @port.nil? or @port == "" then
+                ftp.connect(@ip)
             else
-                ftp.connect(ip, port)                                 
+                ftp.connect(@ip, @port)                                 
             end
-                       logger.info "[FTP log] Connected FTP server (#{ip}:#{port})"
-            ftp.login(username, passwd)
+                       @log.info "[FTP log] Connected FTP server (#{@ip}:#{@port})"
+            ftp.login(@username, @passwd)
             ftp.binary = true
                        ftp.passive = true                      
             ftp.mkdir(uniqdir)
             ftp.chdir(uniqdir)
             ftp.put(bpath)
-                       logger.info "[FTP log] Put a file"
-                       logger.info "[FTP log]   from \"#{bpath}\" to \"#{ftp_filepath}\""                      
+                       @log.info "[FTP log] Put a file"
+                       @log.info "[FTP log]   from \"#{bpath}\" to \"#{ftp_filepath}\""                        
                        files = ftp.list(filename)
                        if files.empty? then 
-                               logger.error "[FTP log] Failed to upload file (#{filename} does not exist)"
+                               @log.error "[FTP log] Failed to upload file (#{filename} does not exist)"
                                return nil      
                        end
             ftp.quit
-                       logger.info "[FTP log] Disconnected FTP server"
+                       @log.info "[FTP log] Disconnected FTP server"
         rescue => e
-                       logger.error "[FTP log] Exception"
-                       logger.error e.message
-                       logger.error e.backtrace.inspect
+                       @log.error "[FTP log] Exception"
+                       @log.error e.message
+                       @log.error e.backtrace.inspect
                        return nil                      
         end
                return ftp_filepath
     end
 
-    def FileTransfer.getfile(ip, port, username, passwd, bpath, target, logger)
+    def getfile( bpath, target )
         dirname = File.dirname(bpath)
         filename = File.basename(bpath)
 
@@ -53,46 +66,46 @@ class FileTransfer
 
         begin
             ftp = Net::FTP.new
-            if port.nil? or port == "" then
-                ftp.connect(ip)
+            if @port.nil? or @port == "" then
+                ftp.connect(@ip)
             else                
-                ftp.connect(ip, port)
+                ftp.connect(@ip, @port)
             end                
-                       logger.info "[FTP log] Connected FTP server (#{ip}:#{port})"
-            ftp.login(username, passwd)
+                       @log.info "[FTP log] Connected FTP server (#{@ip}:#{@port})"
+            ftp.login(@username, @passwd)
             ftp.binary = true
                        ftp.passive = true
             ftp.chdir(dirname)
             ftp.get(filename, dst_file)
-                       logger.info "[FTP log] Get a file"
-                       logger.info "[FTP log]   from \"#{bpath}\" to \"#{dst_file}\""
+                       @log.info "[FTP log] Get a file"
+                       @log.info "[FTP log]   from \"#{bpath}\" to \"#{dst_file}\""
             ftp.quit
-                       logger.info "[FTP log] Disconnected FTP server"
+                       @log.info "[FTP log] Disconnected FTP server"
         rescue => e
-                       logger.error "[FTP log] Exception"
-                       logger.error e.message
-                       logger.error e.backtrace.inspect
+                       @log.error "[FTP log] Exception"
+                       @log.error e.message
+                       @log.error e.backtrace.inspect
                        return nil                      
                end
                if not File.exist? dst_file then
-                       logger.error "[FTP log] Failed to download file (#{dst_file} does not exist)"
+                       @log.error "[FTP log] Failed to download file (#{dst_file} does not exist)"
                        return nil
                end                             
         return bpath
     end
 
-    def FileTransfer.cleandir(ip, port, username, passwd, path, logger)
+    def cleandir(path)
         dirname = File.dirname(path)
 
         begin
             ftp = Net::FTP.new
-            if port.nil? or port == "" then
-                ftp.connect(ip)
+            if @port.nil? or @port == "" then
+                ftp.connect(@ip)
             else
-                ftp.connect(ip, port)                    
+                ftp.connect(@ip, @port)                    
             end                    
-                       logger.info "[FTP log] Connected FTP server (#{ip}:#{port})"
-            ftp.login(username, passwd)
+                       @log.info "[FTP log] Connected FTP server (#{@ip}:#{@port})"
+            ftp.login(@username, @passwd)
             old_dir = ftp.pwd
             ftp.chdir(dirname)
             list = ftp.ls
@@ -103,13 +116,13 @@ class FileTransfer
             end
             ftp.chdir(old_dir)
             ftp.rmdir(dirname)
-                       logger.info "[FTP log] Clean dir (#{dirname})"                  
+                       @log.info "[FTP log] Clean dir (#{dirname})"                    
             ftp.quit
-                       logger.info "[FTP log] Disconnected FTP server"
+                       @log.info "[FTP log] Disconnected FTP server"
         rescue => e
-                       logger.error "[FTP log] Exception"
-                       logger.error e.message
-                       logger.error e.backtrace.inspect
+                       @log.error "[FTP log] Exception"
+                       @log.error e.message
+                       @log.error e.backtrace.inspect
                        return nil                      
         end