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