프로젝트

일반

사용자정보

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

markus / DownloadManager / Program.cs @ 9d5b4bc2

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

1

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

    
10
namespace DownloadManager
11
{
12
    class Program
13
    {
14
        private static string TEMP_FOLDER = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MARKUS");
15

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

    
21
        //private const string SECTION_DOWNLOAD_FILE = "DOWNLOAD_FILE";
22

    
23
        //private const string SECTION_DOWNLOAD_DIR = "DOWNLOAD_DIR";
24

    
25
        //private const string KEY_VALUES = "VALUE";
26

    
27

    
28
        //private static string DOWNLOAD_LOG_FILE = System.IO.Path.Combine(TEMP_FOLDER, "Download.log");
29

    
30
        //private static ConfigParser DOWNLOAD_LOG = new ConfigParser(DOWNLOAD_LOG_FILE,
31
        //                                            new ConfigParserSettings
32
        //                                            {
33
        //                                                MultiLineValues = MultiLineValues.Simple | MultiLineValues.AllowValuelessKeys | MultiLineValues.QuoteDelimitedValues,
34
        //                                                Culture = new CultureInfo("en-US")
35
        //                                            });
36

    
37
        
38
        static void Main(string[] args)
39
        {
40
            try
41
            {
42

    
43
                if (args.Length > 0)
44
                {
45
                    for (int i = 0; i < args.Length; i++)
46
                    {
47
                        ConsoleWrite(args[i]);
48
                    }
49

    
50
                    //if (args[0] == IIpc.ProcessTypeDefine.DEFINE_DELETE)
51
                    //{
52
                    //    IsDebug = bool.Parse(args[1]);
53

    
54
                    //    DeleteFiles();
55
                    //}
56
                    if (args[0] == IIpc.ProcessTypeDefine.DEFINE_MONITOR)
57
                    {
58
                        IsDebug = bool.Parse(args[1]);
59
                        int processId = int.Parse(args[2]); /// kcom process id
60
                        int thumbnamilPID = int.Parse(args[3]);
61
                        int filePID = int.Parse(args[4]);
62

    
63
                        string folder1 = args[5];              // folder
64
                        string folder2 = args[6];              // folder
65
                        string file = args[7];              // file
66

    
67
                        try
68
                        {
69
                            Process process = FindProcess(processId);
70

    
71
                            while (process != null)
72
                            {
73
                                process = FindProcess(processId);
74

    
75
                                ConsoleWrite("Process Check");
76

    
77
                                System.Threading.Thread.Sleep(10000);
78
                            }
79

    
80
                            var p1 = FindProcess(thumbnamilPID);
81

    
82
                            if (p1 != null)
83
                            {
84
                                p1.Kill();
85
                            }
86

    
87

    
88
                            var p2 = FindProcess(filePID);
89

    
90
                            if (p2 != null)
91
                            {
92
                                p2.Kill();
93
                            }
94
                        }
95
                        catch (Exception ex)
96
                        {
97
                            ConsoleWrite(ex.ToString());
98
                        }
99

    
100
                        DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder1);
101
                        DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL, folder2);
102
                        DeleteFiles(IIpc.ProcessTypeDefine.DEFINE_FILE, file);
103
                    }
104
                    else
105
                    {
106
                        wcfClient = new IIpc.WcfClient(args[1]);
107
                        IsDebug = bool.Parse(args[2]);
108

    
109
                        if (args[0] == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL)
110
                        {
111
                            string BaseUri = args[3];
112
                            string localStoragePath = args[4];
113
                            string fileExt = args[5];
114
                            int totalPages = int.Parse(args[6]);
115
                            int takeCount = int.Parse(args[6]);
116

    
117
                            TotalPages = totalPages;
118

    
119
                            KCOM.PageManager.PageStorage pageStorage = new KCOM.PageManager.PageStorage(BaseUri, localStoragePath, fileExt, totalPages, takeCount);
120
                            pageStorage.PageLoadCompleted += PageStorage_PageLoadCompleted;
121
                            pageStorage.DownloadWork(1, totalPages);
122
                        }
123
                        else if (args[0] == IIpc.ProcessTypeDefine.DEFINE_FILE)
124
                        {
125
                            using (System.Net.WebClient client = new System.Net.WebClient())
126
                            {
127
                                client.DownloadFileCompleted += Client_DownloadFileCompleted;
128
                                client.DownloadProgressChanged += Client_DownloadProgressChanged;
129
                                client.UseDefaultCredentials = true;
130
                                System.Net.IWebProxy webProxy = client.Proxy;
131

    
132
                                if (webProxy != null)
133
                                {
134
                                    webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
135
                                }
136

    
137
                                client.DownloadFileAsync(new Uri(args[3]), args[4]);
138
                            }
139
                        }
140

    
141
                        while (!IsDownload)
142
                        {
143
                            System.Threading.Thread.Sleep(1000);
144
                        }
145

    
146
                    }
147

    
148
                    if (IsDebug)
149
                    {
150
                        Console.ReadLine();
151
                    }
152
                }
153
            }
154
            catch (Exception ex)
155
            {
156

    
157
                System.Diagnostics.Debug.WriteLine(ex);
158
            }
159
        }
160

    
161
        public static void DeleteFiles(string processType, string path)
162
        {
163
            if(processType == IIpc.ProcessTypeDefine.DEFINE_THUMBNAIL)
164
            {
165
                var dir = System.IO.Directory.GetDirectories(path, "*.*", System.IO.SearchOption.AllDirectories);
166

    
167
                for (int i = 0; i < dir.Length; i++)
168
                {
169
                    System.IO.Directory.Delete(dir[i],true);
170
                }
171
            }
172
            else
173
            {
174
                if (System.IO.File.Exists(path))
175
                {
176
                    System.IO.File.Delete(path);
177
                }
178
            }
179
        }
180

    
181
        private static Process FindProcess(int id)
182
        {
183
            Process result = null;
184

    
185
            try
186
            {
187
                var process = Process.GetProcesses().Where(x=>x.Id == id);
188
                
189
                if(process.Count()> 0)
190
                {
191
                    result = process.First();
192
                }
193
            }
194
            catch (Exception)
195
            {
196
            }
197

    
198
            return result;
199
        }
200

    
201

    
202
        private static void ConsoleWrite(string data)
203
        {
204
            if (IsDebug)
205
            {
206
                Console.WriteLine(data);
207
            }
208
        }
209

    
210

    
211
        private static void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
212
        {
213
            try
214
            {
215
                wcfClient.SendFileDownloadReceived(100, true);
216

    
217
                ConsoleWrite($"Download file Completed");
218
                IsDownload = true;
219
            }
220
            catch (Exception ex)
221
            {
222
                ConsoleWrite(ex.ToString());
223
            }
224
        }
225

    
226
        private static void Client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
227
        {
228
            try
229
            {
230
                wcfClient.SendFileDownloadReceived(e.ProgressPercentage, false);
231

    
232
                ConsoleWrite($"Download file {e.ProgressPercentage}");
233
            }
234
            catch (Exception ex)
235
            {
236
                ConsoleWrite(ex.ToString());
237
            }
238
        }
239

    
240
        private static void PageStorage_PageLoadCompleted(object sender, KCOM.PageManager.PageLoadCompletedEventArgs e)
241
        {
242
            try
243
            {
244
                wcfClient.SendThumbnailReceived(e.PageItem.PageNo, e.PageItem.LocalFilePath);
245

    
246
                ConsoleWrite($"{e.PageItem.PageNo} {e.PageItem.LocalUri}");
247

    
248
                if (e.PageItem.PageNo == TotalPages)
249
                {
250
                    IsDownload = true;
251
                }
252
            }
253
            catch (Exception ex)
254
            {
255
                ConsoleWrite(ex.ToString());
256
            }
257
            
258
        }
259
    }
260
}
클립보드 이미지 추가 (최대 크기: 500 MB)