Network : adding a file utilty for managing to download the tmp file from device 34/21634/1
authorHyunjong,park <phjwithyou.park@samsung.com>
Mon, 26 May 2014 00:11:02 +0000 (09:11 +0900)
committerHyunjong,park <phjwithyou.park@samsung.com>
Mon, 26 May 2014 00:17:54 +0000 (09:17 +0900)
Signed-off-by: Hyunjong,park <phjwithyou.park@samsung.com>
Change-Id: Ieeecca3d2221870d24a63b11565aab2142eb11e8

org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerConstants.java
org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/utils/DownloadContentfileManager.java [new file with mode: 0644]

index d918e88..5e0f54a 100644 (file)
@@ -288,6 +288,12 @@ public class AnalyzerConstants {
 
        public final static String DAEMONLOG_PREFIX = "da_daemon_log_";//$NON-NLS-1$
        public final static int DAEMONLOG_COUNT = 3;
+       
+       public final static String DOWNLOAD_FILE_FOLDER = "download_file";//$NON-NLS-1$
+       public final static String TMP_FOLDER = "tmp";//$NON-NLS-1$
+       public static final String CHECK_DOWNLOAD_FILE_STRING = "SkWek2n3_File:";//$NON-NLS-1$
+       
+       
 
        /*** version 3.0 */
        public final static int MSG_KEEP_ALIVE = 0x0001;
diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/utils/DownloadContentfileManager.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/utils/DownloadContentfileManager.java
new file mode 100644 (file)
index 0000000..90fae67
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+ *  Dynamic Analyzer
+ *
+ * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * Hyunjong Park <phjwithyou.park@samsung.com>
+ * Juyoung Kim <j0.kim@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ * Contributors:
+ * - S-Core Co., Ltd
+ * 
+ */
+
+package org.tizen.dynamicanalyzer.utils;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.tizen.dynamicanalyzer.common.AnalyzerConstants;
+import org.tizen.dynamicanalyzer.common.AnalyzerManager;
+import org.tizen.dynamicanalyzer.communicator.CommunicatorUtils;
+import org.tizen.dynamicanalyzer.constant.CommonConstants;
+import org.tizen.dynamicanalyzer.util.CommonUtil;
+import org.tizen.dynamicanalyzer.util.DALogger;
+import org.tizen.sdblib.service.SyncResult;
+
+public class DownloadContentfileManager {
+
+       private final static int SPLIT_PATH_INDEX = 1;
+
+       /**
+        * Download file of contents that payload of network or shader source of
+        * OpenGL from device.
+        * 
+        * @param contents
+        *            It will check that string like
+        *            "SkWek2n3_File:/tmp/<file_name>" is included.
+        * @return new file path of download file
+        */
+       public static String getDowonloadFilePath(String contents) {
+               String returnString = contents;
+               if (contents.contains(AnalyzerConstants.CHECK_DOWNLOAD_FILE_STRING)) {
+                       String[] strFile = contents
+                                       .split(AnalyzerConstants.CHECK_DOWNLOAD_FILE_STRING);
+                       if (strFile.length < SPLIT_PATH_INDEX) {
+                               return returnString;
+                       }
+                       String deviceFilePath = strFile[SPLIT_PATH_INDEX];
+
+                       String[] fileNameSplit = deviceFilePath.split(File.separator
+                                       + AnalyzerConstants.TMP_FOLDER + File.separator);
+                       if (fileNameSplit.length < SPLIT_PATH_INDEX) {
+                               return returnString;
+                       }
+                       String downloadFolderPath = File.separator
+                                       + AnalyzerConstants.DOWNLOAD_FILE_FOLDER;
+
+                       makeFolder(AnalyzerManager.getProject().getSavePath()
+                                       + downloadFolderPath);
+
+                       String saveFilePath = downloadFolderPath + File.separator
+                                       + fileNameSplit[SPLIT_PATH_INDEX];
+
+                       if (pullFile(deviceFilePath, AnalyzerManager.getProject()
+                                       .getSavePath() + saveFilePath)) {
+                               returnString = saveFilePath;
+                       }
+               }
+               return returnString;
+       }
+
+       /**
+        * copy contents file from device to local save folder. if the copy of the
+        * file is successful, delete the file on the device.
+        * 
+        * @param from
+        *            device file path
+        * @param to
+        *            local file path
+        * @return
+        */
+       public static boolean pullFile(String from, String to) {
+               SyncResult res = CommunicatorUtils.pull(from, to);
+               if (null != res && res.isOk()) {
+                       DALogger.getInstance().debug(from + "file copying success!!");//$NON-NLS-1$ 
+               } else {
+                       DALogger.getInstance().debug("Failed to get " + from); //$NON-NLS-1$ 
+                       return false;
+               }
+               CommunicatorUtils.removeCommand(from);
+               return true;
+       }
+
+       /**
+        * get contents of file
+        * 
+        * @param contents
+        *            get file path from database. It will check that string like
+        *            "/download_file/<file_name>" is included.
+        * @return contents
+        */
+       public static String getFileContents(String contents) {
+               String returnString = contents;
+               if (contents.contains(AnalyzerConstants.DOWNLOAD_FILE_FOLDER
+                               + File.separator)) {
+                       StringBuffer contextsBuffer = new StringBuffer();
+
+                       String filePath = AnalyzerManager.getProject().getSavePath()
+                                       + contents;
+                       File contentsFile = new File(filePath);
+                       if (contentsFile.isFile()) {
+                               BufferedReader in = null;
+                               String content;
+                               try {
+                                       in = new BufferedReader(new FileReader(contentsFile));
+                                       while (null != (content = in.readLine())) {
+                                               contextsBuffer.append(content).append(
+                                                               CommonConstants.NEW_LINE);
+                                       }
+                               } catch (FileNotFoundException e) {
+                                       e.printStackTrace();
+                               } catch (IOException e) {
+                                       e.printStackTrace();
+                               } finally {
+                                       CommonUtil.tryClose(in);
+                               }
+                       }
+                       returnString = contextsBuffer.toString();
+               }
+               return returnString;
+       }
+
+       /**
+        * make new folder when not exist folder
+        * 
+        * @param folderPath
+        * 
+        */
+       private static void makeFolder(String folderPath) {
+               File toDir = new File(folderPath);
+               if (!toDir.exists() || !toDir.isDirectory()) {
+                       toDir.mkdirs();
+               }
+       }
+
+}