ClientApp.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using HttpDownload;
  2. using NettyClient;
  3. using NettyClient.proto;
  4. using System;
  5. using System.Threading;
  6. using TaskDatabase;
  7. namespace Client
  8. {
  9. public class ClientApp
  10. {
  11. static TaskNettyClient client; // 网络传输客户端
  12. static SimpleMessageHandler simpleHandler; // 网络传输处理
  13. TaskService taskService; // 持久化任务
  14. TaskDownloadManager taskDownloadManager; // 下载任务文件(Http)
  15. MessageHandler messageHandler; // 处理客户端收到的消息
  16. TaskHandler taskHandler; // 处理服务端发送的任务(监视下载状况等待)
  17. int maxTaskCount; // 能处理的最大任务数量, 影响是否拒绝服务器分发的任务
  18. string defaultFileDir; // 默认文件目录
  19. private Action<int> heartbeat;
  20. IRevitCommandExcutor revitCommandExcutor;
  21. private Action<int> reconnectAction;
  22. public ClientApp(string ip, int port, string dir, int maxTaskCount, IRevitCommandExcutor revitCommandExcutor, int maxDownTaskCount=5) {
  23. client = new TaskNettyClient(ip, port);
  24. taskService = new TaskService();
  25. taskDownloadManager = new TaskDownloadManager(maxDownTaskCount);
  26. this.maxTaskCount = maxTaskCount;
  27. defaultFileDir = dir;
  28. this.revitCommandExcutor = revitCommandExcutor;
  29. }
  30. public void Start() {
  31. simpleHandler = new SimpleMessageHandler();
  32. if (messageHandler == null)
  33. {
  34. taskHandler = new TaskHandler(simpleHandler, taskService, taskDownloadManager, defaultFileDir, revitCommandExcutor);
  35. taskHandler.SyncAllTask();
  36. messageHandler = new MessageHandler(simpleHandler, maxTaskCount, taskHandler);
  37. messageHandler.ResumeAllTasks();
  38. ThreadPool.QueueUserWorkItem(messageHandler.HandleMessage);
  39. ThreadPool.QueueUserWorkItem(taskHandler.RunTasks);
  40. }
  41. else
  42. {
  43. messageHandler.setSimpleMessageHandler(simpleHandler);
  44. taskHandler.setSimpleMessageHandler(simpleHandler);
  45. }
  46. // 起动心跳线程
  47. heartbeat = SendHeartbeat;
  48. heartbeat.BeginInvoke(0, null, null);
  49. client.RunClientAsync(simpleHandler).Wait();
  50. reconnectAction = Reconnect;
  51. reconnectAction.BeginInvoke(30, null, null);
  52. }
  53. void Reconnect(int interval)
  54. {
  55. while (true)
  56. {
  57. Thread.Sleep(interval * 1000);
  58. if (!TaskNettyClient.isConnected)
  59. {
  60. Console.WriteLine(DateTime.Now + ": reconnecting....");
  61. Restart();
  62. }
  63. }
  64. }
  65. void SendHeartbeat(int nouse)
  66. {
  67. while (true)
  68. {
  69. try
  70. {
  71. Thread.Sleep(30000);
  72. var retMsg = MessageUtil.generateMessageStr(Command.Useless.ToString(), "", "{}");
  73. simpleHandler.WriteMessage(retMsg);
  74. }
  75. catch (Exception e)
  76. {
  77. Console.WriteLine(e);
  78. Thread.Sleep(300*1000);
  79. }
  80. }
  81. }
  82. public void Close() {
  83. client.CloseAsync().Wait();
  84. }
  85. public void PauseAllTask() {
  86. taskDownloadManager.pauseAllTasks();
  87. messageHandler.PauseAllTasks();
  88. }
  89. public void ResumeAllTask()
  90. {
  91. taskDownloadManager.resumeAllTasks();
  92. messageHandler.ResumeAllTasks();
  93. }
  94. public void Restart()
  95. {
  96. simpleHandler = new SimpleMessageHandler();
  97. if (messageHandler == null)
  98. {
  99. taskHandler = new TaskHandler(simpleHandler, taskService, taskDownloadManager, defaultFileDir, revitCommandExcutor);
  100. taskHandler.SyncAllTask();
  101. messageHandler = new MessageHandler(simpleHandler, maxTaskCount, taskHandler);
  102. messageHandler.ResumeAllTasks();
  103. ThreadPool.QueueUserWorkItem(messageHandler.HandleMessage);
  104. ThreadPool.QueueUserWorkItem(taskHandler.RunTasks);
  105. }
  106. else
  107. {
  108. messageHandler.setSimpleMessageHandler(simpleHandler);
  109. taskHandler.setSimpleMessageHandler(simpleHandler);
  110. }
  111. client.RunClientAsync(simpleHandler).Wait();
  112. }
  113. }
  114. }