markus / DownloadManager / Program.cs @ 9f56088c
이력 | 보기 | 이력해설 | 다운로드 (10.1 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 | 664ea2e1 | taeseongkim | CheckAutoUpdate.Validation(); |
72 | 75f6ff19 | taeseongkim | |
73 | 9d5b4bc2 | taeseongkim | IsDebug = bool.Parse(args[1]); |
74 | int processId = int.Parse(args[2]); /// kcom process id |
||
75 | int thumbnamilPID = int.Parse(args[3]); |
||
76 | int filePID = int.Parse(args[4]); |
||
77 | |||
78 | string folder1 = args[5]; // folder |
||
79 | string folder2 = args[6]; // folder |
||
80 | string file = args[7]; // file |
||
81 | |||
82 | try |
||
83 | { |
||
84 | Process process = FindProcess(processId); |
||
85 | |||
86 | while (process != null) |
||
87 | { |
||
88 | process = FindProcess(processId); |
||
89 | |||
90 | ConsoleWrite("Process Check"); |
||
91 | |||
92 | System.Threading.Thread.Sleep(10000); |
||
93 | } |
||
94 | |||
95 | var p1 = FindProcess(thumbnamilPID); |
||
96 | |||
97 | if (p1 != null) |
||
98 | { |
||
99 | p1.Kill(); |
||
100 | } |
||
101 | |||
102 | |||
103 | var p2 = FindProcess(filePID); |
||
104 | |||
105 | if (p2 != null) |
||
106 | { |
||
107 | p2.Kill(); |
||
108 | } |
||
109 | } |
||
110 | catch (Exception ex) |
||
111 | { |
||
112 | ConsoleWrite(ex.ToString()); |
||
113 | } |
||
114 | |||
115 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder1); |
||
116 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder2); |
||
117 | DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_FILE, file); |
||
118 | } |
||
119 | else |
||
120 | 2007ecaa | taeseongkim | { |
121 | 9d5b4bc2 | taeseongkim | wcfClient = new IIpc.WcfClient(args[1]); |
122 | IsDebug = bool.Parse(args[2]); |
||
123 | |||
124 | if (args[0] == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
||
125 | { |
||
126 | string BaseUri = args[3]; |
||
127 | string localStoragePath = args[4]; |
||
128 | string fileExt = args[5]; |
||
129 | int totalPages = int.Parse(args[6]); |
||
130 | int takeCount = int.Parse(args[6]); |
||
131 | |||
132 | TotalPages = totalPages; |
||
133 | 2007ecaa | taeseongkim | |
134 | 9d5b4bc2 | taeseongkim | KCOM.PageManager.PageStorage pageStorage = new KCOM.PageManager.PageStorage(BaseUri, localStoragePath, fileExt, totalPages, takeCount); |
135 | pageStorage.PageLoadCompleted += PageStorage_PageLoadCompleted; |
||
136 | pageStorage.DownloadWork(1, totalPages); |
||
137 | } |
||
138 | else if (args[0] == IIpc.ProcessTypeDefine.DEFINE_FILE) |
||
139 | { |
||
140 | using (System.Net.WebClient client = new System.Net.WebClient()) |
||
141 | { |
||
142 | client.DownloadFileCompleted += Client_DownloadFileCompleted; |
||
143 | client.DownloadProgressChanged += Client_DownloadProgressChanged; |
||
144 | client.UseDefaultCredentials = true; |
||
145 | System.Net.IWebProxy webProxy = client.Proxy; |
||
146 | |||
147 | if (webProxy != null) |
||
148 | { |
||
149 | webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials; |
||
150 | } |
||
151 | |||
152 | 664ea2e1 | taeseongkim | client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]); |
153 | 9d5b4bc2 | taeseongkim | } |
154 | } |
||
155 | |||
156 | while (!IsDownload) |
||
157 | 2007ecaa | taeseongkim | { |
158 | 9d5b4bc2 | taeseongkim | System.Threading.Thread.Sleep(1000); |
159 | 2007ecaa | taeseongkim | } |
160 | |||
161 | 9d5b4bc2 | taeseongkim | } |
162 | |||
163 | if (IsDebug) |
||
164 | { |
||
165 | Console.ReadLine(); |
||
166 | 2007ecaa | taeseongkim | } |
167 | } |
||
168 | 9d5b4bc2 | taeseongkim | } |
169 | catch (Exception ex) |
||
170 | { |
||
171 | 664ea2e1 | taeseongkim | logger.Error($"param : {string.Join(",", args)}", ex); |
172 | 9d5b4bc2 | taeseongkim | System.Diagnostics.Debug.WriteLine(ex); |
173 | } |
||
174 | } |
||
175 | |||
176 | public static void DeleteFiles(string processType, string path) |
||
177 | { |
||
178 | 664ea2e1 | taeseongkim | try |
179 | 9d5b4bc2 | taeseongkim | { |
180 | 75f6ff19 | taeseongkim | logger.Info($"DeleteFiles Info {processType} {path}"); |
181 | |||
182 | 664ea2e1 | taeseongkim | if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL) |
183 | { |
||
184 | var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories); |
||
185 | 2007ecaa | taeseongkim | |
186 | 664ea2e1 | taeseongkim | for (int i = 0; i < dir.Length; i++) |
187 | { |
||
188 | System.IO.Directory.Delete(dir[i], true); |
||
189 | } |
||
190 | } |
||
191 | else |
||
192 | 9d5b4bc2 | taeseongkim | { |
193 | 664ea2e1 | taeseongkim | if (System.IO.File.Exists(path)) |
194 | { |
||
195 | System.IO.File.Delete(path); |
||
196 | } |
||
197 | 9d5b4bc2 | taeseongkim | } |
198 | } |
||
199 | 664ea2e1 | taeseongkim | catch (Exception ex) |
200 | 9d5b4bc2 | taeseongkim | { |
201 | 664ea2e1 | taeseongkim | logger.Error($"DeleteFiles Error {processType} {path}", ex); |
202 | System.Diagnostics.Debug.WriteLine(ex); |
||
203 | 9d5b4bc2 | taeseongkim | } |
204 | 664ea2e1 | taeseongkim | |
205 | 9d5b4bc2 | taeseongkim | } |
206 | 2007ecaa | taeseongkim | |
207 | 9d5b4bc2 | taeseongkim | private static Process FindProcess(int id) |
208 | { |
||
209 | Process result = null; |
||
210 | |||
211 | try |
||
212 | { |
||
213 | var process = Process.GetProcesses().Where(x=>x.Id == id); |
||
214 | |||
215 | if(process.Count()> 0) |
||
216 | 2007ecaa | taeseongkim | { |
217 | 9d5b4bc2 | taeseongkim | result = process.First(); |
218 | 2007ecaa | taeseongkim | } |
219 | } |
||
220 | 664ea2e1 | taeseongkim | catch (Exception ex) |
221 | 9d5b4bc2 | taeseongkim | { |
222 | 664ea2e1 | taeseongkim | logger.Error($"FindProcess Error {id}", ex); |
223 | System.Diagnostics.Debug.WriteLine(ex); |
||
224 | 9d5b4bc2 | taeseongkim | } |
225 | |||
226 | return result; |
||
227 | 2007ecaa | taeseongkim | } |
228 | |||
229 | 9d5b4bc2 | taeseongkim | |
230 | 2007ecaa | taeseongkim | private static void ConsoleWrite(string data) |
231 | { |
||
232 | 664ea2e1 | taeseongkim | logger.Info(data); |
233 | |||
234 | 2007ecaa | taeseongkim | if (IsDebug) |
235 | { |
||
236 | Console.WriteLine(data); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | |||
241 | private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) |
||
242 | { |
||
243 | try |
||
244 | { |
||
245 | wcfClient.SendFileDownloadReceived(100, true); |
||
246 | |||
247 | ConsoleWrite($"Download file Completed"); |
||
248 | IsDownload = true; |
||
249 | } |
||
250 | catch (Exception ex) |
||
251 | { |
||
252 | ConsoleWrite(ex.ToString()); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | private static void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) |
||
257 | { |
||
258 | try |
||
259 | { |
||
260 | wcfClient.SendFileDownloadReceived(e.ProgressPercentage, false); |
||
261 | |||
262 | ConsoleWrite($"Download file {e.ProgressPercentage}"); |
||
263 | } |
||
264 | catch (Exception ex) |
||
265 | { |
||
266 | ConsoleWrite(ex.ToString()); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | private static void PageStorage_PageLoadCompleted(object sender, KCOM.PageManager.PageLoadCompletedEventArgs e) |
||
271 | { |
||
272 | try |
||
273 | { |
||
274 | wcfClient.SendThumbnailReceived(e.PageItem.PageNo, e.PageItem.LocalFilePath); |
||
275 | |||
276 | ConsoleWrite($"{e.PageItem.PageNo} {e.PageItem.LocalUri}"); |
||
277 | |||
278 | if (e.PageItem.PageNo == TotalPages) |
||
279 | { |
||
280 | IsDownload = true; |
||
281 | } |
||
282 | } |
||
283 | catch (Exception ex) |
||
284 | { |
||
285 | ConsoleWrite(ex.ToString()); |
||
286 | } |
||
287 | |||
288 | } |
||
289 | } |
||
290 | } |