markus / DownloadManager / Program.cs @ 1063eb9d
이력 | 보기 | 이력해설 | 다운로드 (9.44 KB)
1 | 9d5b4bc2 | taeseongkim | |
---|---|---|---|
2 | 664ea2e1 | taeseongkim | using log4net; |
3 | 9d5b4bc2 | taeseongkim | using System; |
4 | 2007ecaa | taeseongkim | using System.Collections.Generic; |
5 | 9d5b4bc2 | taeseongkim | using System.Diagnostics; |
6 | using System.Globalization; |
||
7 | 2007ecaa | taeseongkim | using System.Linq; |
8 | using System.Text; |
||
9 | using System.Threading.Tasks; |
||
10 | |||
11 | namespace DownloadManager |
||
12 | { |
||
13 | class Program |
||
14 | { |
||
15 | 664ea2e1 | taeseongkim | private static ILog logger = LogManager.GetLogger(typeof(Program)); |
16 | |||
17 | 9d5b4bc2 | taeseongkim | private static string TEMP_FOLDER = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS"); |
18 | |||
19 | 2007ecaa | taeseongkim | static IIpc.WcfClient wcfClient; |
20 | static bool IsDownload; |
||
21 | static bool IsDebug; |
||
22 | static int TotalPages = 0; |
||
23 | |||
24 | 9d5b4bc2 | taeseongkim | //private const string SECTION_DOWNLOAD_FILE = "DOWNLOAD_FILE"; |
25 | |||
26 | //private const string SECTION_DOWNLOAD_DIR = "DOWNLOAD_DIR"; |
||
27 | |||
28 | //private const string KEY_VALUES = "VALUE"; |
||
29 | |||
30 | |||
31 | //private static string DOWNLOAD_LOG_FILE = System.IO.Path.Combine(TEMP_FOLDER, "Download.log"); |
||
32 | |||
33 | //private static ConfigParser DOWNLOAD_LOG = new ConfigParser(DOWNLOAD_LOG_FILE, |
||
34 | // new ConfigParserSettings |
||
35 | // { |
||
36 | // MultiLineValues = MultiLineValues.Simple | MultiLineValues.AllowValuelessKeys | MultiLineValues.QuoteDelimitedValues, |
||
37 | // Culture = new CultureInfo("en-US") |
||
38 | // }); |
||
39 | |||
40 | |||
41 | 2007ecaa | taeseongkim | static void Main(string[] args) |
42 | { |
||
43 | 9d5b4bc2 | taeseongkim | try |
44 | 2007ecaa | taeseongkim | { |
45 | 664ea2e1 | taeseongkim | logger.Info($"param : {string.Join(",", args)}"); |
46 | 2007ecaa | taeseongkim | |
47 | 9d5b4bc2 | taeseongkim | if (args.Length > 0) |
48 | 2007ecaa | taeseongkim | { |
49 | 9d5b4bc2 | taeseongkim | for (int i = 0; i < args.Length; i++) |
50 | { |
||
51 | ConsoleWrite(args[i]); |
||
52 | } |
||
53 | 2007ecaa | taeseongkim | |
54 | 9d5b4bc2 | taeseongkim | //if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE) |
55 | //{ |
||
56 | // IsDebug = bool.Parse(args[1]); |
||
57 | 2007ecaa | taeseongkim | |
58 | 9d5b4bc2 | taeseongkim | // DeleteFiles(); |
59 | //} |
||
60 | if (args[0] == IIpc.ProcessTypeDefine.DEFINE_MONITOR) |
||
61 | { |
||
62 | 664ea2e1 | taeseongkim | CheckAutoUpdate.Validation(); |
63 | |||
64 | 9d5b4bc2 | taeseongkim | IsDebug = bool.Parse(args[1]); |
65 | int processId = int.Parse(args[2]); /// kcom process id |
||
66 | int thumbnamilPID = int.Parse(args[3]); |
||
67 | int filePID = int.Parse(args[4]); |
||
68 | |||
69 | string folder1 = args[5]; // folder |
||
70 | string folder2 = args[6]; // folder |
||
71 | string file = args[7]; // file |
||
72 | |||
73 | try |
||
74 | { |
||
75 | Process process = FindProcess(processId); |
||
76 | |||
77 | while (process != null) |
||
78 | { |
||
79 | process = FindProcess(processId); |
||
80 | |||
81 | ConsoleWrite("Process Check"); |
||
82 | |||
83 | System.Threading.Thread.Sleep(10000); |
||
84 | } |
||
85 | |||
86 | var p1 = FindProcess(thumbnamilPID); |
||
87 | |||
88 | if (p1 != null) |
||
89 | { |
||
90 | p1.Kill(); |
||
91 | } |
||
92 | |||
93 | |||
94 | var p2 = FindProcess(filePID); |
||
95 | |||
96 | if (p2 != null) |
||
97 | { |
||
98 | p2.Kill(); |
||
99 | } |
||
100 | } |
||
101 | catch (Exception ex) |
||
102 | { |
||
103 | ConsoleWrite(ex.ToString()); |
||
104 | } |
||
105 | |||
106 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder1); |
||
107 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder2); |
||
108 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_FILE, file); |
||
109 | } |
||
110 | else |
||
111 | 2007ecaa | taeseongkim | { |
112 | 9d5b4bc2 | taeseongkim | wcfClient = new IIpc.WcfClient(args[1]); |
113 | IsDebug = bool.Parse(args[2]); |
||
114 | |||
115 | if (args[0] == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
||
116 | { |
||
117 | string BaseUri = args[3]; |
||
118 | string localStoragePath = args[4]; |
||
119 | string fileExt = args[5]; |
||
120 | int totalPages = int.Parse(args[6]); |
||
121 | int takeCount = int.Parse(args[6]); |
||
122 | |||
123 | TotalPages = totalPages; |
||
124 | 2007ecaa | taeseongkim | |
125 | 9d5b4bc2 | taeseongkim | KCOM.PageManager.PageStorage pageStorage = new KCOM.PageManager.PageStorage(BaseUri, localStoragePath, fileExt, totalPages, takeCount); |
126 | pageStorage.PageLoadCompleted += PageStorage_PageLoadCompleted; |
||
127 | pageStorage.DownloadWork(1, totalPages); |
||
128 | } |
||
129 | else if (args[0] == IIpc.ProcessTypeDefine.DEFINE_FILE) |
||
130 | { |
||
131 | using (System.Net.WebClient client = new System.Net.WebClient()) |
||
132 | { |
||
133 | client.DownloadFileCompleted += Client_DownloadFileCompleted; |
||
134 | client.DownloadProgressChanged += Client_DownloadProgressChanged; |
||
135 | client.UseDefaultCredentials = true; |
||
136 | System.Net.IWebProxy webProxy = client.Proxy; |
||
137 | |||
138 | if (webProxy != null) |
||
139 | { |
||
140 | webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
||
141 | } |
||
142 | |||
143 | 664ea2e1 | taeseongkim | client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]); |
144 | 9d5b4bc2 | taeseongkim | } |
145 | } |
||
146 | |||
147 | while (!IsDownload) |
||
148 | 2007ecaa | taeseongkim | { |
149 | 9d5b4bc2 | taeseongkim | System.Threading.Thread.Sleep(1000); |
150 | 2007ecaa | taeseongkim | } |
151 | |||
152 | 9d5b4bc2 | taeseongkim | } |
153 | |||
154 | if (IsDebug) |
||
155 | { |
||
156 | Console.ReadLine(); |
||
157 | 2007ecaa | taeseongkim | } |
158 | } |
||
159 | 9d5b4bc2 | taeseongkim | } |
160 | catch (Exception ex) |
||
161 | { |
||
162 | 664ea2e1 | taeseongkim | logger.Error($"param : {string.Join(",", args)}", ex); |
163 | 9d5b4bc2 | taeseongkim | System.Diagnostics.Debug.WriteLine(ex); |
164 | } |
||
165 | } |
||
166 | |||
167 | public static void DeleteFiles(string processType, string path) |
||
168 | { |
||
169 | 664ea2e1 | taeseongkim | try |
170 | 9d5b4bc2 | taeseongkim | { |
171 | 664ea2e1 | taeseongkim | if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
172 | { |
||
173 | var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories); |
||
174 | 2007ecaa | taeseongkim | |
175 | 664ea2e1 | taeseongkim | for (int i = 0; i < dir.Length; i++) |
176 | { |
||
177 | System.IO.Directory.Delete(dir[i], true); |
||
178 | } |
||
179 | } |
||
180 | else |
||
181 | 9d5b4bc2 | taeseongkim | { |
182 | 664ea2e1 | taeseongkim | if (System.IO.File.Exists(path)) |
183 | { |
||
184 | System.IO.File.Delete(path); |
||
185 | } |
||
186 | 9d5b4bc2 | taeseongkim | } |
187 | } |
||
188 | 664ea2e1 | taeseongkim | catch (Exception ex) |
189 | 9d5b4bc2 | taeseongkim | { |
190 | 664ea2e1 | taeseongkim | logger.Error($"DeleteFiles Error {processType} {path}", ex); |
191 | System.Diagnostics.Debug.WriteLine(ex); |
||
192 | 9d5b4bc2 | taeseongkim | } |
193 | 664ea2e1 | taeseongkim | |
194 | 9d5b4bc2 | taeseongkim | } |
195 | 2007ecaa | taeseongkim | |
196 | 9d5b4bc2 | taeseongkim | private static Process FindProcess(int id) |
197 | { |
||
198 | Process result = null; |
||
199 | |||
200 | try |
||
201 | { |
||
202 | var process = Process.GetProcesses().Where(x=>x.Id == id); |
||
203 | |||
204 | if(process.Count()> 0) |
||
205 | 2007ecaa | taeseongkim | { |
206 | 9d5b4bc2 | taeseongkim | result = process.First(); |
207 | 2007ecaa | taeseongkim | } |
208 | } |
||
209 | 664ea2e1 | taeseongkim | catch (Exception ex) |
210 | 9d5b4bc2 | taeseongkim | { |
211 | 664ea2e1 | taeseongkim | logger.Error($"FindProcess Error {id}", ex); |
212 | System.Diagnostics.Debug.WriteLine(ex); |
||
213 | 9d5b4bc2 | taeseongkim | } |
214 | |||
215 | return result; |
||
216 | 2007ecaa | taeseongkim | } |
217 | |||
218 | 9d5b4bc2 | taeseongkim | |
219 | 2007ecaa | taeseongkim | private static void ConsoleWrite(string data) |
220 | { |
||
221 | 664ea2e1 | taeseongkim | logger.Info(data); |
222 | |||
223 | 2007ecaa | taeseongkim | if (IsDebug) |
224 | { |
||
225 | Console.WriteLine(data); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | |||
230 | private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) |
||
231 | { |
||
232 | try |
||
233 | { |
||
234 | wcfClient.SendFileDownloadReceived(100, true); |
||
235 | |||
236 | ConsoleWrite($"Download file Completed"); |
||
237 | IsDownload = true; |
||
238 | } |
||
239 | catch (Exception ex) |
||
240 | { |
||
241 | ConsoleWrite(ex.ToString()); |
||
242 | } |
||
243 | } |
||
244 | |||
245 | private static void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) |
||
246 | { |
||
247 | try |
||
248 | { |
||
249 | wcfClient.SendFileDownloadReceived(e.ProgressPercentage, false); |
||
250 | |||
251 | ConsoleWrite($"Download file {e.ProgressPercentage}"); |
||
252 | } |
||
253 | catch (Exception ex) |
||
254 | { |
||
255 | ConsoleWrite(ex.ToString()); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | private static void PageStorage_PageLoadCompleted(object sender, KCOM.PageManager.PageLoadCompletedEventArgs e) |
||
260 | { |
||
261 | try |
||
262 | { |
||
263 | wcfClient.SendThumbnailReceived(e.PageItem.PageNo, e.PageItem.LocalFilePath); |
||
264 | |||
265 | ConsoleWrite($"{e.PageItem.PageNo} {e.PageItem.LocalUri}"); |
||
266 | |||
267 | if (e.PageItem.PageNo == TotalPages) |
||
268 | { |
||
269 | IsDownload = true; |
||
270 | } |
||
271 | } |
||
272 | catch (Exception ex) |
||
273 | { |
||
274 | ConsoleWrite(ex.ToString()); |
||
275 | } |
||
276 | |||
277 | } |
||
278 | } |
||
279 | } |