Example #1
0
        public void HandleMessage(object obj)
        {
            while (true)
            {
                Message message;
                bool    hasMsg = SimpleMessageHandler.messageQueue.TryDequeue(out message);
                if (!hasMsg)
                {
                    Thread.Sleep(500);
                    continue;
                }
                Command command = Command.Useless;
                try
                {
                    command = (Command)Enum.Parse(typeof(Command), message.Cmd);
                }
                catch { command = Command.Useless; }
                Message retMsg;
                switch (command)
                {
                case Command.SendTask:
                    // 1. 检测是否接受该任务
                    if (checkIsAcceptTask(message.TaskId))
                    {
                        // 2. 如果接受, 保存任务到数据库, 并同步该任务到内存, 并交由任务执行线程开始执行
                        bool isSuccess = taskHandler.addOneTask(message.TaskId, message.Content);
                        if (isSuccess)
                        {
                            retMsg = MessageUtil.generateMessage(Command.AcceptTask.ToString(), message.TaskId, "");
                            simpleHandler.WriteMessage(retMsg);
                        }
                    }
                    else
                    {
                        // 3. 如果拒绝, 直接返回拒绝消息
                        retMsg = MessageUtil.generateMessage(Command.RefuseTask.ToString(), message.TaskId, "");
                        simpleHandler.WriteMessage(retMsg);
                    }
                    break;

                case Command.ClientInfo:     // 服务端要求客户端返回客户端信息
                    ClientInfo info = new ClientInfo();
                    info.Ipv4    = ClientInfoUtil.GetClientLocalIPv4Address();
                    info.MacAddr = ClientInfoUtil.GetMacAddress();
                    info.Name    = ClientInfoUtil.GetUserName();
                    retMsg       = MessageUtil.generateMessage(Command.ClientInfo.ToString(), 0, JsonConvert.SerializeObject(info, Formatting.None));
                    simpleHandler.WriteMessage(retMsg);
                    break;

                default:
                    break;
                }
            }
        }
Example #2
0
 public void RunTasks(object obj)
 {
     while (true)
     {
         try
         {
             for (int i = taskModels.Count - 1; i > -1; --i)
             {
                 TaskModel task = taskModels[i];
                 TaskModel downloadException = null, commandException = null;
                 bool      isAllDownloaded = true;
                 // 看当前任务依赖的文件下载状况
                 foreach (DownloadTaskModel downloadTaskModel in task.DownloadTaskModelList)
                 {
                     DownloadTask download = taskDownloadManager.getDownloadTaskByDownloadTaskId(downloadTaskModel.Tid);
                     if (download == null) // 如果还未加入下载队列
                     {
                         // 开始下载
                         if (downloadTaskModel.Local_dir == null)
                         {
                             downloadTaskModel.Local_dir = defaultFileDir + downloadTaskModel.Tid + ".rvt";
                         }
                         download = taskDownloadManager.enqueueTask(task.Id, downloadTaskModel.Tid, downloadTaskModel.Task_url,
                                                                    downloadTaskModel.Local_dir, downloadTaskModel.Task_md5, downloadTaskModel.Downloaded_bytes);
                         taskDownloadManager.runTask(download);
                         isAllDownloaded &= false;
                     }
                     else
                     {
                         // 如果该下载发生异常
                         if (download.Status == DownloadStatus.ExceptionStopped)
                         {
                             isAllDownloaded  &= false;
                             downloadException = task;
                             break;
                         }
                         // 同步当前状态到数据库
                         if (download.Status == DownloadStatus.Completed)
                         {
                             downloadTaskModel.Finish_time = GetTimeStampSeconds();
                             isAllDownloaded &= true;
                         }
                         else
                         {
                             isAllDownloaded &= false;
                         }
                         syncSingleDownTask(download, downloadTaskModel);
                     }
                 } // foreach end
                 // 如果该下载任务下载失败
                 if (downloadException != null)
                 {
                     task.Task_status = -1;
                     taskService.UpdateTask(task);
                     taskModels.RemoveAt(i);
                     simpleHandler.WriteMessage(MessageUtil.generateMessage(Command.DownloadError.ToString(), task.Id, ""));
                     continue;
                 }
                 // 如果该任务的所有下载文件均已完成, 开始执行Revit命令
                 if (isAllDownloaded)
                 {
                     try
                     {
                         HashSet <string> fileSet = new HashSet <string>();
                         foreach (var downloadModel in task.DownloadTaskModelList)
                         {
                             fileSet.Add(downloadModel.Local_dir);
                         }
                         try
                         {
                             string resultJson = revitCommandExcutor.ExecuteCmd(task.Task_cmd, task.Task_param, fileSet);
                             simpleHandler.WriteMessage(MessageUtil.generateMessage(Command.TaskSuccess.ToString(), task.Id, resultJson));
                             task.Task_status      = 1;
                             task.Task_result_json = resultJson;
                             taskService.UpdateTask(task);
                             taskModels.RemoveAt(i);
                         }
                         catch {
                             commandException = task;
                         }
                     }
                     catch {
                     }
                 }
                 if (commandException != null)
                 {
                     task.Task_status = -2;
                     taskService.UpdateTask(task);
                     taskModels.RemoveAt(i);
                     simpleHandler.WriteMessage(MessageUtil.generateMessage(Command.CommandError.ToString(), task.Id, ""));
                     continue;
                 }
             }
             taskService.removeErrorTask();
         }
         catch (Exception ex) {
             Console.WriteLine(ex.StackTrace);
         }
         Thread.Sleep(1000);
     }
 }