ClientApp.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using DotNetty.Transport.Channels;
  2. using HttpDownload;
  3. using NettyClient;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using TaskDatabase;
  12. namespace Client
  13. {
  14. public class ClientApp
  15. {
  16. TaskNettyClient client; // 网络传输客户端
  17. SimpleMessageHandler simpleHandler; // 网络传输处理
  18. TaskService taskService; // 持久化任务
  19. TaskDownloadManager taskDownloadManager; // 下载任务文件(Http)
  20. MessageHandler messageHandler; // 处理客户端收到的消息
  21. TaskHandler taskHandler; // 处理服务端发送的任务(监视下载状况等待)
  22. int maxTaskCount; // 能处理的最大任务数量, 影响是否拒绝服务器分发的任务
  23. string defaultFileDir; // 默认文件目录
  24. IRevitCommandExcutor revitCommandExcutor;
  25. public ClientApp(string ip, int port, string dir, int maxTaskCount, IRevitCommandExcutor revitCommandExcutor, int maxDownTaskCount=5) {
  26. client = new TaskNettyClient(ip, port);
  27. taskService = new TaskService();
  28. taskDownloadManager = new TaskDownloadManager(maxDownTaskCount);
  29. this.maxTaskCount = maxTaskCount;
  30. defaultFileDir = dir;
  31. this.revitCommandExcutor = revitCommandExcutor;
  32. }
  33. public void Start() {
  34. simpleHandler = new SimpleMessageHandler();
  35. if (messageHandler == null)
  36. {
  37. taskHandler = new TaskHandler(simpleHandler, taskService, taskDownloadManager, defaultFileDir, revitCommandExcutor);
  38. taskHandler.SyncAllTask();
  39. messageHandler = new MessageHandler(simpleHandler, maxTaskCount, taskHandler);
  40. messageHandler.ResumeAllTasks();
  41. ThreadPool.QueueUserWorkItem(messageHandler.HandleMessage);
  42. ThreadPool.QueueUserWorkItem(taskHandler.RunTasks);
  43. }
  44. else {
  45. messageHandler.setSimpleMessageHandler(simpleHandler);
  46. taskHandler.setSimpleMessageHandler(simpleHandler);
  47. }
  48. client.RunClientAsync(simpleHandler).Wait();
  49. }
  50. public void Close() {
  51. client.CloseAsync().Wait();
  52. }
  53. public void PauseAllTask() {
  54. taskDownloadManager.pauseAllTasks();
  55. messageHandler.PauseAllTasks();
  56. }
  57. public void ResumeAllTask()
  58. {
  59. taskDownloadManager.resumeAllTasks();
  60. messageHandler.ResumeAllTasks();
  61. }
  62. }
  63. }