프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / DownloadManager / Program.cs @ 664ea2e1

이력 | 보기 | 이력해설 | 다운로드 (9.44 KB)

1

2
using log4net;
3
using System;
4
using System.Collections.Generic;
5
using System.Diagnostics;
6
using System.Globalization;
7
using System.Linq;
8
using System.Text;
9
using System.Threading.Tasks;
10

    
11
namespace DownloadManager
12
{
13
    class Program
14
    {
15
        private static ILog logger = LogManager.GetLogger(typeof(Program));
16

    
17
        private static string TEMP_FOLDER = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS");
18

    
19
        static IIpc.WcfClient wcfClient;
20
        static bool IsDownload;
21
        static bool IsDebug;
22
        static int TotalPages = 0;
23

    
24
        //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
        static void Main(string[] args)
42
        {
43
            try
44
            {
45
                logger.Info($"param : {string.Join(",", args)}");
46

    
47
                if (args.Length > 0)
48
                {
49
                    for (int i = 0; i < args.Length; i++)
50
                    {
51
                        ConsoleWrite(args[i]);
52
                    }
53

    
54
                    //if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE)
55
                    //{
56
                    //    IsDebug = bool.Parse(args[1]);
57

    
58
                    //    DeleteFiles();
59
                    //}
60
                    if (args[0] == IIpc.ProcessTypeDefine.DEFINE_MONITOR)
61
                    {
62
                        CheckAutoUpdate.Validation();
63

    
64
                        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
                    {
112
                        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

    
125
                            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
                                client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]);
144
                            }
145
                        }
146

    
147
                        while (!IsDownload)
148
                        {
149
                            System.Threading.Thread.Sleep(1000);
150
                        }
151

    
152
                    }
153

    
154
                    if (IsDebug)
155
                    {
156
                        Console.ReadLine();
157
                    }
158
                }
159
            }
160
            catch (Exception ex)
161
            {
162
                logger.Error($"param : {string.Join(",", args)}", ex);
163
                System.Diagnostics.Debug.WriteLine(ex);
164
            }
165
        }
166

    
167
        public static void DeleteFiles(string processType, string path)
168
        {
169
            try
170
            {
171
                if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL)
172
                {
173
                    var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories);
174

    
175
                    for (int i = 0; i < dir.Length; i++)
176
                    {
177
                        System.IO.Directory.Delete(dir[i], true);
178
                    }
179
                }
180
                else
181
                {
182
                    if (System.IO.File.Exists(path))
183
                    {
184
                        System.IO.File.Delete(path);
185
                    }
186
                }
187
            }
188
            catch (Exception ex)
189
            {
190
                logger.Error($"DeleteFiles Error {processType} {path}", ex);
191
                System.Diagnostics.Debug.WriteLine(ex);
192
            }
193
        
194
        }
195

    
196
        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
                {
206
                    result = process.First();
207
                }
208
            }
209
            catch (Exception ex)
210
            {
211
                logger.Error($"FindProcess Error {id}", ex);
212
                System.Diagnostics.Debug.WriteLine(ex);
213
            }
214

    
215
            return result;
216
        }
217

    
218

    
219
        private static void ConsoleWrite(string data)
220
        {
221
            logger.Info(data);
222

    
223
            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
}
클립보드 이미지 추가 (최대 크기: 500 MB)