프로젝트

일반

사용자정보

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

markus / DownloadManager / Program.cs @ 907a99b3

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

1

2
using log4net;
3
using System;
4
using System.Diagnostics;
5
using System.Linq;
6

    
7
namespace DownloadManager
8
{
9
    class Program
10
    {
11
        private static ILog logger = LogManager.GetLogger(typeof(Program));
12

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

    
15
        static IIpc.WcfClient wcfClient;
16
        static bool IsDownload;
17
        static bool IsDebug;
18
        static int TotalPages = 0;
19

    
20
        //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
        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
        static void Main(string[] args)
49
        {
50
            try
51
            {
52
                ChangeLogFileName($"{DateTime.Now.ToString("yyMMddssmm")}_{args[0]}");
53

    
54
                logger.Info($"param : {string.Join(",", args)}");
55

    
56
                if (args.Length > 0)
57
                {
58
                    for (int i = 0; i < args.Length; i++)
59
                    {
60
                        ConsoleWrite(args[i]);
61
                    }
62

    
63
                    //if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE)
64
                    //{
65
                    //    IsDebug = bool.Parse(args[1]);
66

    
67
                    //    DeleteFiles();
68
                    //}
69
                    if (args[0] == IIpc.ProcessTypeDefine.DEFINE_MONITOR)
70
                    {
71
                        CheckAutoUpdate.Validation();
72
                   
73
                        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
                    {
121
                        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

    
134
                            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
                                client.DownloadFileAsync(new Uri(System.Web.HttpUtility.UrlDecode(args[3])), args[4]);
153
                            }
154
                        }
155

    
156
                        while (!IsDownload)
157
                        {
158
                            System.Threading.Thread.Sleep(1000);
159
                        }
160

    
161
                    }
162

    
163
                    if (IsDebug)
164
                    {
165
                        Console.ReadLine();
166
                    }
167
                }
168
            }
169
            catch (Exception ex)
170
            {
171
                logger.Error($"param : {string.Join(",", args)}", ex);
172
                System.Diagnostics.Debug.WriteLine(ex);
173
            }
174
        }
175

    
176
        public static void DeleteFiles(string processType, string path)
177
        {
178
            try
179
            {
180
                logger.Info($"DeleteFiles Info {processType} {path}");
181

    
182
                if (processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL)
183
                {
184
                    var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories);
185

    
186
                    for (int i = 0; i < dir.Length; i++)
187
                    {
188
                        System.IO.Directory.Delete(dir[i], true);
189
                    }
190
                }
191
                else
192
                {
193
                    if (System.IO.File.Exists(path))
194
                    {
195
                        System.IO.File.Delete(path);
196
                    }
197
                }
198
            }
199
            catch (Exception ex)
200
            {
201
                logger.Error($"DeleteFiles Error {processType} {path}", ex);
202
                System.Diagnostics.Debug.WriteLine(ex);
203
            }
204
        
205
        }
206

    
207
        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
                {
217
                    result = process.First();
218
                }
219
            }
220
            catch (Exception ex)
221
            {
222
                logger.Error($"FindProcess Error {id}", ex);
223
                System.Diagnostics.Debug.WriteLine(ex);
224
            }
225

    
226
            return result;
227
        }
228

    
229

    
230
        private static void ConsoleWrite(string data)
231
        {
232
            logger.Info(data);
233

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