ClientApp.cs 2.6 KB

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