-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdp2ServerCollection.cs
525 lines (433 loc) · 15.2 KB
/
dp2ServerCollection.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using DigitalPlatform;
using DigitalPlatform.Text;
using Newtonsoft.Json;
namespace DigitalPlatform.CirculationClient
{
[Serializable()]
public class dp2Server
{
public string Name = ""; // 服务器名
public string Url = ""; // 服务器URL,应是webservice endpoint
public string DefaultUserName = "";
// [NonSerialized]
public string StorageDefaultPassword = Cryptography.Encrypt("", "dp2rms");
public bool SavePassword = false;
// 2020/2/26
public string UID { get; set; }
[NonSerialized]
public bool Verified = false; // 是验证过序列号?
public dp2Server()
{
}
// 拷贝构造函数
public dp2Server(dp2Server refServer)
{
this.Name = refServer.Name;
this.Url = refServer.Url;
this.DefaultUserName = refServer.DefaultUserName;
this.StorageDefaultPassword = refServer.StorageDefaultPassword;
this.SavePassword = refServer.SavePassword;
this.Verified = refServer.Verified;
this.UID = refServer.UID;
}
[JsonIgnore]
public string DefaultPassword
{
get
{
if (SavePassword == false)
return "";
return Cryptography.Decrypt(StorageDefaultPassword, "dp2rms");
}
set
{
StorageDefaultPassword = Cryptography.Encrypt(value, "dp2rms");
}
}
}
/// <summary>
/// 储存服务器信息的容器类
/// 打算取代HostList
/// </summary>
[Serializable()]
public class dp2ServerCollection : ArrayList
{
[NonSerialized]
string m_strFileName = "";
[NonSerialized]
bool m_bChanged = false;
[NonSerialized]
public IWin32Window ownerForm = null;
public event dp2ServerChangedEventHandle ServerChanged = null;
public dp2ServerCollection()
{
}
/// <summary>
/// 内容是否发生过修改
/// </summary>
public bool Changed
{
get
{
return m_bChanged;
}
set
{
m_bChanged = value;
}
}
public string FileName
{
get
{
return m_strFileName;
}
set
{
this.m_strFileName = value;
}
}
// 索引器 字符串作索引
public dp2Server this[string strUrl]
{
get
{
return this.GetServer(strUrl);
}
}
/// <summary>
/// 根据 URL 获得 dp2Server 对象
/// </summary>
/// <param name="strUrl">服务器 URL</param>
/// <returns>dp2Server 对象</returns>
public dp2Server GetServer(string strUrl)
{
strUrl = StringUtil.CanonicalizeHostUrl(strUrl);
dp2Server server = null;
for (int i = 0; i < this.Count; i++)
{
server = (dp2Server)this[i];
string strCurrentUrl = StringUtil.CanonicalizeHostUrl(server.Url);
if (String.Compare(strCurrentUrl, strUrl, true) == 0)
return server;
}
return null;
}
// 2007/9/14
public dp2Server GetServerByName(string strServerName)
{
dp2Server server = null;
for (int i = 0; i < this.Count; i++)
{
server = (dp2Server)this[i];
if (server.Name == strServerName)
return server;
}
return null;
}
// 根据 UID 查找
public List<dp2Server> FindServerByUID(string uid)
{
List<dp2Server> results = new List<dp2Server>();
foreach(dp2Server server in this)
{
if (server.UID == uid)
results.Add(server);
}
return results;
}
// 克隆。
// 新数组中的对象完全是新创建的。
public dp2ServerCollection Dup()
{
dp2ServerCollection newServers = new dp2ServerCollection();
for (int i = 0; i < this.Count; i++)
{
dp2Server newServer = new dp2Server((dp2Server)this[i]);
newServers.Add(newServer);
}
newServers.m_strFileName = this.m_strFileName;
newServers.m_bChanged = this.m_bChanged;
newServers.ownerForm = this.ownerForm;
return newServers;
}
// 将另一对象的数组内容灌入本对象
public void Import(dp2ServerCollection servers)
{
this.Clear();
this.AddRange(servers);
this.m_bChanged = true;
// 新增加的动作
dp2ServerChangedEventArgs e = new dp2ServerChangedEventArgs();
e.Url = "";
e.ServerChangeAction = dp2ServerChangeAction.Import;
OnServerChanged(this, e);
}
// 创建一个新的Server对象
// return:
// -1 出错
// 0 加入了
// 1 发现重复,没有加入
public int NewServer(
string strName,
string strUrl,
int nInsertPos)
{
dp2Server server = null;
// 暂时不去重
server = new dp2Server();
server.Url = strUrl;
server.Name = strName;
if (nInsertPos == -1)
this.Add(server);
else
this.Insert(nInsertPos, server);
m_bChanged = true;
dp2ServerChangedEventArgs e = new dp2ServerChangedEventArgs();
e.Url = strUrl;
e.ServerChangeAction = dp2ServerChangeAction.Add;
OnServerChanged(this, e);
return 0;
}
public void OnServerChanged(object sender, dp2ServerChangedEventArgs e)
{
if (this.ServerChanged != null)
{
this.ServerChanged(sender, e);
}
}
// 创建一个新的Server对象
// return:
public dp2Server NewServer(int nInsertPos)
{
dp2Server server = null;
server = new dp2Server();
if (nInsertPos == -1)
this.Add(server);
else
this.Insert(nInsertPos, server);
m_bChanged = true;
return server;
}
// 从文件中装载创建一个ServerCollection对象
// parameters:
// bIgnorFileNotFound 是否不抛出FileNotFoundException异常。
// 如果==true,函数直接返回一个新的空ServerCollection对象
// Exception:
// FileNotFoundException 文件没找到
// SerializationException 版本迁移时容易出现
public static dp2ServerCollection Load(
string strFileName,
bool bIgnoreFileNotFound)
{
dp2ServerCollection servers = null;
try
{
lock (_syncRoot_file)
{
using (Stream stream = File.Open(strFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
BinaryFormatter formatter = new BinaryFormatter();
servers = (dp2ServerCollection)formatter.Deserialize(stream);
servers.m_strFileName = strFileName;
return servers;
}
}
}
catch (FileNotFoundException ex)
{
if (bIgnoreFileNotFound == false)
throw ex;
servers = new dp2ServerCollection();
servers.m_strFileName = strFileName;
// 让调主有一个新的空对象可用
return servers;
}
}
static object _syncRoot_file = new object();
// 保存到文件
// parameters:
// strFileName 文件名。如果==null,表示使用装载时保存的那个文件名
public void Save(string strFileName)
{
if (m_bChanged == false)
return;
if (strFileName == null)
strFileName = m_strFileName;
if (strFileName == null)
{
throw (new Exception("ServerCollection.Save()没有指定保存文件名"));
}
//using (var temp = new Form())
{
// 2020/5/20
// 避免抛出异常 程序集“dp2Catalog, Version=3.2.7445.28152, Culture=neutral, PublicKeyToken=null”中的类型“dp2Catalog.MainForm”未标记为可序列化。
//var form = this.ownerForm;
//this.ownerForm = temp;
try
{
lock (_syncRoot_file)
{
using (Stream stream = File.Open(strFileName,
FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
}
}
}
finally
{
//this.ownerForm = form;
}
}
}
public void SetAllVerified(bool bVerified)
{
for (int i = 0; i < this.Count; i++)
{
dp2Server server = (dp2Server)this[i];
server.Verified = bVerified;
}
}
/*
// 获得缺省帐户信息
// return:
// 2 already login succeed
// 1 dialog return OK
// 0 dialog return Cancel
// -1 other error
public void OnAskAccountInfo(object sender,
AskAccountInfoEventArgs e)
{
bool bFirst = true;
bool bAutoLogin = (e.LoginStyle & LoginStyle.AutoLogin) == LoginStyle.AutoLogin;
bool bFillDefault = (e.LoginStyle & LoginStyle.FillDefaultInfo) == LoginStyle.FillDefaultInfo;
e.Owner = this.ownerForm;
e.UserName = "";
e.Password = "";
LoginDlg dlg = new LoginDlg();
Server server = this[e.Url];
dlg.textBox_serverAddr.Text = e.Url;
if (bFillDefault == true)
{
if (server != null)
{
dlg.textBox_userName.Text = (server.DefaultUserName == "" ? "public" : server.DefaultUserName);
dlg.textBox_password.Text = server.DefaultPassword;
dlg.checkBox_savePassword.Checked = server.SavePassword;
}
else
{
dlg.textBox_userName.Text = "public";
dlg.textBox_password.Text = "";
dlg.checkBox_savePassword.Checked = false;
}
}
if (e.Comment != null)
dlg.textBox_comment.Text = e.Comment;
DOLOGIN:
if (e.Channels != null)
{
if (bAutoLogin == false && bFirst == true)
goto REDOINPUT;
// 找到Channel
RmsChannel channel = e.Channels.GetChannel(dlg.textBox_serverAddr.Text);
Debug.Assert(channel != null, "Channels.GetChannel()异常...");
string strError;
// 登录
int nRet = channel.Login(dlg.textBox_userName.Text,
dlg.textBox_password.Text,
out strError);
if (nRet != 1)
{
strError = "以用户名 '" + dlg.textBox_userName.Text + "' 登录到 '" + dlg.textBox_serverAddr.Text + "' 失败: " + strError;
if (this.ownerForm != null)
{
MessageBox.Show(this.ownerForm, strError);
}
else
{
e.ErrorInfo = strError;
e.Result = -1;
}
goto REDOINPUT;
}
else // 登录成功
{
if (String.Compare(e.Url, dlg.textBox_serverAddr.Text, true) != 0)
{
// 创建一个新的Server对象
// return:
// -1 出错
// 0 加入了
// 1 发现重复,没有加入
nRet = this.NewServer(dlg.textBox_serverAddr.Text, -1);
if (nRet == 0)
e.Url = channel.Url;
}
server = this[dlg.textBox_serverAddr.Text];
if (server == null) // 2006/8/19 add
{
// 创建一个新的Server对象
// return:
// -1 出错
// 0 加入了
// 1 发现重复,没有加入
nRet = this.NewServer(dlg.textBox_serverAddr.Text, -1);
if (nRet == 0)
e.Url = channel.Url;
server = this[dlg.textBox_serverAddr.Text];
}
Debug.Assert(server != null, "此时server不可能为null");
server.DefaultUserName = dlg.textBox_userName.Text;
server.DefaultPassword = dlg.textBox_password.Text;
server.SavePassword = dlg.checkBox_savePassword.Checked;
this.m_bChanged = true;
e.Result = 2;
return;
}
}
REDOINPUT:
bFirst = false;
dlg.ShowDialog(ownerForm);
if (dlg.DialogResult != DialogResult.OK)
{
e.Result = 0;
return;
}
if (e.Channels == null)
{
e.UserName = dlg.textBox_userName.Text;
e.Password = dlg.textBox_password.Text;
e.Result = 1;
return;
}
goto DOLOGIN;
}
*/
}
// 事件: 增添或者删除了服务器
public delegate void dp2ServerChangedEventHandle(object sender,
dp2ServerChangedEventArgs e);
public class dp2ServerChangedEventArgs : EventArgs
{
public string Url = ""; // 服务器URL
public dp2ServerChangeAction ServerChangeAction = dp2ServerChangeAction.None; // 所发生的改变类型
}
public enum dp2ServerChangeAction
{
None = 0,
Add = 1,
Remove = 2,
Import = 3,
}
}