-
Notifications
You must be signed in to change notification settings - Fork 4
/
AqiManage.cs
633 lines (553 loc) · 17.4 KB
/
AqiManage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
using System;
using System.Linq;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using AQI;
using AQI.Interface;
using AQISet.Interface;
using AQISet.Collection;
using AQISet.Saver;
using AQISet.Runner;
using AQISet.Cfg;
using Helper.Setting;
using System.Threading;
namespace AQISet
{
/// <summary>
/// 管理者
/// </summary>
public sealed class AqiManage : IThrowMessage
{
#region 枚举
enum RunMode
{
NONE, SELF, JOINT
}
#endregion
#region 事件
//定义delegate
public delegate void ManageEventHandler(RunMessage m);
//用event 关键字声明事件对象
public event ManageEventHandler ManageEvent;
//传递事件
private void TransferEvent(RunMessage runmsg)
{
if (ManageEvent != null)
{
runmsg.PutMessageSender(this);
ManageEvent(runmsg);
}
}
//抛出事件
public bool ThrowEvent(RunMessage eRunMessage)
{
if (ManageEvent != null)
{
ManageEvent(eRunMessage);
return true;
}
else
{
return false;
}
}
public void ThrowEvent(RunMessage.RunType eType, string eMsg)
{
if (ManageEvent != null)
ManageEvent(new RunMessage(eType, eMsg, this));
}
#endregion
#region 字段
private static string Tag = "AQIManage";
private IAqiSave aqiSaver; //存储者
private AqiRunner aqiRunner; //运行者
private AqiNoter aqiNoter; //记录者
private AqiRetryer aqiRetryer; //重试者
private List<Assembly> allDLLs; //所有程序集
private Dictionary<string, IAqiWeb> allAqiWebs; //所有数据源
private Dictionary<string, ISrcUrl> allSrcUrls; //所有数据接口
private Dictionary<string, AqiRunner> runnerlist;
private ReaderWriterLockSlim thisLock; //读写锁
#endregion
#region 属性
public string Name
{
get
{
return Tag;
}
}
public IAqiSave AqiSave
{
get
{
return aqiSaver;
}
}
public AqiNoter AqiNote
{
get
{
return aqiNoter;
}
}
public AqiRetryer AqiRetry
{
get
{
return aqiRetryer;
}
}
public AqiRunner AqiRun
{
get
{
return aqiRunner;
}
}
public InstanceSettingHelper AqiSetting
{
get
{
return Setting;
}
}
/// <summary>
/// 设置
/// </summary>
public static InstanceSettingHelper Setting
{
get
{
return InstanceSettingHelper.Instance;
}
}
/// <summary>
/// 根据名称索引运行者
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public AqiRunner this[string name]
{
get
{
AqiRunner r = null;
thisLock.EnterReadLock();
{
RunMode rm = (RunMode)Enum.Parse(typeof(RunMode), AqiManage.Setting["RunMode"]);
switch (rm)
{
case RunMode.SELF:
if (runnerlist.ContainsKey(name))
{
r = runnerlist[name];
}
break;
case RunMode.JOINT:
default:
r = aqiRunner;
break;
}
}
thisLock.ExitReadLock();
return r;
}
}
#endregion
public AqiManage()
{
thisLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
allDLLs = new List<Assembly>();
InitDll();
allAqiWebs = new Dictionary<string, IAqiWeb>();
InitAqiWeb();
allSrcUrls = new Dictionary<string, ISrcUrl>();
InitSrcUrl();
initSaver();
//aqiSaver = new AqiFileSaver(this);
aqiNoter = new AqiNoter(this);
//目前默认为一个Retryer
aqiRetryer = new AqiRetryer(this);
initRunner();
//目前默认为一个Runner
//aqiRunner = new AqiRunner(this, allSrcUrls);
//aqiRunner.RunEvent += new AqiRunner.RunEventHandler(aqiRunner_RunEvent);
}
#region 内部方法
/// <summary>
/// 初始化DLL程序集
/// </summary>
private void InitDll()
{
//DLL路径
string ExePath = Assembly.GetExecutingAssembly().Location;
int p = ExePath.LastIndexOf('\\');
string DllPath = ExePath.Substring(0, p);
DirectoryInfo TheFolder = new DirectoryInfo(DllPath);
//遍历文件
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
if (NextFile.Name.IndexOf("AQI") > 1)
{
try
{
Assembly ass = Assembly.LoadFrom(NextFile.FullName);
allDLLs.Add(ass);
Console.WriteLine("加载程序集:" + ass.FullName);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
/// <summary>
/// 初始化IAqiWeb接口
/// </summary>
private void InitAqiWeb()
{
foreach(Assembly ass in allDLLs)
{
Type[] types = ass.GetTypes();
foreach (Type type in types)
{
if (type.GetInterface("IAqiWeb") != null)
{
IAqiWeb iaw = Activator.CreateInstance(type) as IAqiWeb;
allAqiWebs.Add(iaw.TAG, iaw);
Console.WriteLine("加载数据源:" + iaw.NAME);
}
}
}
}
/// <summary>
/// 初始化ISrcUrl接口
/// </summary>
private void InitSrcUrl()
{
foreach (IAqiWeb iaw in allAqiWebs.Values)
{
Dictionary<string, ISrcUrl> temp = iaw.getAllSrcUrl();
allSrcUrls = allSrcUrls.Concat(temp).ToDictionary(x => x.Key, x => x.Value);
//TODO 对不同更新时间的数据接口加载到不同的集合里面
Console.WriteLine("整合数据源接口:" + iaw.NAME);
}
}
/// <summary>
/// 初始化存储者
/// </summary>
private void initSaver()
{
//读取配置文件,使用不同Saver
string name = AqiManage.Setting["Saver"];
Type type = Type.GetType("AQISet.Saver." + name);
aqiSaver = Activator.CreateInstance(type,new object[]{ this }) as IAqiSave;
}
/// <summary>
/// 初始化运行者
/// </summary>
private void initRunner()
{
//读取配置文件,独立或合并
RunMode rm = (RunMode)Enum.Parse(typeof(RunMode), AqiManage.Setting["RunMode"]);
switch (rm)
{
case RunMode.SELF:
runnerlist = new Dictionary<string, AqiRunner>();
foreach(IAqiWeb iaw in allAqiWebs.Values)
{
AqiRunner ar = new AqiRunner(this, iaw.getAllSrcUrl(), iaw.TAG + "_Runner");
runnerlist.Add(ar.Name, ar);
}
break;
case RunMode.JOINT:
default:
aqiRunner = new AqiRunner(this, allSrcUrls);
aqiRunner.RunEvent += new AqiRunner.RunEventHandler(aqiRunner_RunEvent);
break;
}
}
#endregion
#region 公共方法
#region 基本控制
/// <summary>
/// 运行所有
/// </summary>
/// <returns></returns>
public bool RunAll()
{
if (runnerlist ==null || runnerlist.Count <= 0)
{ return false; }
ThrowEvent(RunMessage.RunType.TIP,"开始全部运行");
foreach (AqiRunner runner in runnerlist.Values)
{
runner.RunAll();
}
return true;
}
/// <summary>
/// 结束所有
/// </summary>
/// <returns></returns>
public bool EndAll()
{
if (runnerlist == null || runnerlist.Count <= 0)
{ return false; }
ThrowEvent(RunMessage.RunType.TIP, "结束全部运行");
foreach (AqiRunner runner in runnerlist.Values)
{
runner.EndAll();
}
return true;
}
/// <summary>
/// 运行
/// </summary>
/// <param name="name">运行者名称</param>
/// <returns></returns>
public bool Run(string name)
{
if(String.IsNullOrEmpty(name))
{
if(aqiRunner != null)
{
ThrowEvent(RunMessage.RunType.TIP, "开始运行:" + aqiRunner.Name);
aqiRunner.RunAll();
return true;
}
}
else
{
if (runnerlist.ContainsKey(name))
{
ThrowEvent(RunMessage.RunType.TIP, "开始运行:" + name);
runnerlist[name].RunAll();
return true;
}
}
//默认不存在
ThrowEvent(RunMessage.RunType.ERR, "不存在运行者:" + name);
return false;
}
/// <summary>
/// 结束
/// </summary>
/// <param name="name">运行者名称</param>
/// <returns></returns>
public bool End(string name)
{
if (String.IsNullOrEmpty(name))
{
if (aqiRunner != null)
{
ThrowEvent(RunMessage.RunType.TIP, "结束运行:" + aqiRunner.Name);
aqiRunner.EndAll();
return true;
}
}
else
{
if (runnerlist.ContainsKey(name))
{
ThrowEvent(RunMessage.RunType.TIP, "结束运行:" + name);
runnerlist[name].EndAll();
return true;
}
}
//默认不存在
ThrowEvent(RunMessage.RunType.ERR, "不存在运行者:" + name);
return false;
}
#endregion
#region 动态控制
/// <summary>
/// 添加runner
/// </summary>
/// <param name="runner"></param>
public void Add(AqiRunner runner)
{
if (runnerlist.ContainsKey(runner.Name))
{
runnerlist[runner.Name].Union(runner);
}
else
{
runnerlist.Add(runner.Name, runner);
runner.RunAll();
}
}
/// <summary>
/// 删除runner
/// </summary>
/// <param name="runnername"></param>
public void Dele(string runnername)
{
if (runnerlist.ContainsKey(runnername))
{
runnerlist[runnername].EndAll();
runnerlist.Remove(runnername);
}
}
/// <summary>
/// 添加 数据源
/// </summary>
/// <param name="iaw">数据源</param>
public void AddAqiWeb(IAqiWeb iaw)
{
thisLock.EnterUpgradeableReadLock();
{
if (allAqiWebs.ContainsKey(iaw.TAG))
{
//移出旧数据
ThrowEvent(RunMessage.RunType.TIP, "数据源已经存在,进行更新:" + iaw.TAG);
allAqiWebs.Remove(iaw.TAG);
}
//1添加到All集合
Dictionary<string, ISrcUrl> temp = iaw.getAllSrcUrl();
thisLock.EnterWriteLock();
{
//添加到列表
allAqiWebs.Add(iaw.TAG, iaw);
allSrcUrls = allSrcUrls.Concat(temp).ToDictionary(x => x.Key, x => x.Value);
}
thisLock.ExitWriteLock();
//2应用到具体的Runner
AqiRunner runner = this[iaw.TAG + "_Runner"];
if (runner == null)
{
//独立模式&&不存在
runner = new AqiRunner(this, temp, iaw.TAG + "_Runner");
//新添加
Add(runner);
}
else
{
//只给数据
runner.AddSrcUrl(temp);
}
}
thisLock.ExitUpgradeableReadLock();
}
/// <summary>
/// 删除 数据源
/// </summary>
/// <param name="awname">数据源名称</param>
public IAqiWeb DeleAqiWeb(string awname)
{
IAqiWeb iaw = null;
//1删除具体的Runner
AqiRunner runner = this[awname + "_Runner"];
if (aqiRunner != null)
{
aqiRunner.DeleSrcUrl(awname);
}
//2删除All集合
if (allAqiWebs.ContainsKey(awname))
{
iaw = allAqiWebs[awname];
allAqiWebs.Remove(awname);
}
List<string> tlist = new List<string>();
foreach (ISrcUrl isu in allSrcUrls.Values)
{
if (isu.IAW.TAG == awname)
{
tlist.Add(isu.TAG);
}
}
foreach (string isutag in tlist)
{
allSrcUrls.Remove(isutag);
}
return iaw;
}
#endregion
/// <summary>
/// 添加插件
/// </summary>
/// <param name="path">程序集路径</param>
public IAqiWeb LoadDll(string dllpath)
{
try
{
//1过滤DLL
IAqiWeb iaw = null;
Assembly ass = Assembly.LoadFrom(dllpath);
Type[] types = ass.GetTypes();
foreach (Type type in types)
{
if (type.GetInterface("IAqiWeb") != null)
{
iaw = Activator.CreateInstance(type) as IAqiWeb;
}
}
if(iaw == null)
{
//无接口
ThrowEvent(RunMessage.RunType.ERR, "程序集未实现IAqiWeb接口:" + ass.FullName);
return null;
}
if (allDLLs.Exists(dll =>
{
if (dll.FullName == ass.FullName)
{
return true;
}
return false;
}))
{
//存在
ThrowEvent(RunMessage.RunType.TIP, "程序集已经存在:" + ass.FullName);
allDLLs.Remove(ass);
}
allDLLs.Add(ass);
//if (allAqiWebs.ContainsKey(iaw.TAG))
//{
// //存在
// ThrowEvent(RunMessage.RunType.TIP, "数据源已经存在:" + iaw.TAG);
// allAqiWebs.Remove(iaw.TAG);
//}
//allAqiWebs.Add(iaw.TAG,iaw);
return iaw;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
/// <summary>
/// 删除插件
/// 确保卸载之前已经从Runner移除
/// </summary>
/// <param name="name">IAW名称或程序集名称</param>
public void UnLoadDll(string name)
{
if (allAqiWebs.ContainsKey(name))
{
allDLLs.Remove(allAqiWebs[name].GetType().Assembly);
allAqiWebs.Remove(name);
ThrowEvent(RunMessage.RunType.ERR, "移除数据源插件成功:" + name);
}
else
{
ThrowEvent(RunMessage.RunType.ERR, "移除数据源插件失败:" + name);
}
}
/// <summary>
/// 显示信息
/// </summary>
public void ShowInfo()
{
}
#endregion
#region 事件
private void aqiRunner_RunEvent(RunMessage m)
{
TransferEvent(m);
}
#endregion
}
}