프로젝트

일반

사용자정보

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

markus / DownloadManager / Program.cs @ a1e2ba68

이력 | 보기 | 이력해설 | 다운로드 (10.2 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
                        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
                        string baseUri = args[8];              // uri
81

    
82
                        CheckAutoUpdate.Validation(baseUri);
83

    
84
                        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
                    {
123
                        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

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

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

    
163
                    }
164

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

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

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

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

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

    
228
            return result;
229
        }
230

    
231

    
232
        private static void ConsoleWrite(string data)
233
        {
234
            logger.Info(data);
235

    
236
            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
}