bool feedback(float fProgress, DWORD_PTR pUser1, DWORD_PTR pUser2) { auto surfaceWork = reinterpret_cast<ImporterSurfaceWork*>(pUser1); float progress = fProgress / 100.0f; if (progress != surfaceWork->lastProgress) { surfaceWork->lastProgress = progress; ProcessorTaskMessageType type; type.set_type(ProcessorTaskMessageType::TaskProgress); vector<char> type_message(type.ByteSizeLong()); if (type_message.size() > 0) type.SerializeToArray(&type_message[0], type_message.size()); ProcessorTaskProgress prog; prog.set_taskid(surfaceWork->request->taskid()); prog.set_progress(progress); vector<char> progData(prog.ByteSizeLong()); if (progData.size() > 0) prog.SerializeToArray(&progData[0], progData.size()); MqMessage msg; msg.emplace_back(surfaceWork->hostId); msg.emplace_back(""); msg.emplace_back(std::move(type_message)); msg.emplace_back(std::move(progData)); msg.send(*surfaceWork->socket); } return false; }
int ResourceTask::join() { while (true) { poll(m_polls, 20); if (m_polls[0].revents & ZMQ_POLLIN) { m_state = TaskState::PerformingTask; MqMessage msg(m_socket); string id = string(msg.parts()[0].begin(), msg.parts()[0].end()); ProcessorTaskMessageType type; type.ParseFromArray(msg.parts()[1].data(), msg.parts()[1].size()); switch (type.type()) { case ProcessorTaskMessageType::TaskRequest: { ProcessorTaskRequest requesttype; requesttype.ParseFromArray(msg.parts()[2].data(), msg.parts()[2].size()); vector<char> responseData; switch (requesttype.type()) { case ProcessorTaskRequest::Image: { ProcessorTaskImageRequest imageReq; imageReq.ParseFromArray(msg.parts()[3].data(), msg.parts()[3].size()); LOG("Task was asked to perform Image encoding: %s", imageReq.taskid().c_str()); auto response = m_imageTask.process(imageReq, m_hostid, &m_socket); responseData.resize(response.ByteSizeLong()); if(responseData.size() > 0) response.SerializeToArray(&responseData[0], responseData.size()); break; } case ProcessorTaskRequest::Model: { ProcessorTaskModelRequest modelReq; modelReq.ParseFromArray(msg.parts()[3].data(), msg.parts()[3].size()); LOG("Task was asked to perform Model encoding: %i", modelReq.taskid().c_str()); auto response = m_modelTask.process(modelReq, m_hostid, &m_socket); responseData.resize(response.ByteSizeLong()); if (responseData.size() > 0) response.SerializeToArray(&responseData[0], responseData.size()); break; } } ProcessorTaskMessageType resulttype; resulttype.set_type(ProcessorTaskMessageType::TaskResponse); vector<char> type_message(resulttype.ByteSizeLong()); if (type_message.size() > 0) resulttype.SerializeToArray(&type_message[0], type_message.size()); MqMessage responseMsg; responseMsg.emplace_back(id); responseMsg.emplace_back(""); responseMsg.emplace_back(std::move(type_message)); responseMsg.emplace_back(std::move(msg.parts()[2])); responseMsg.emplace_back(std::move(responseData)); responseMsg.send(m_socket); break; } case ProcessorTaskMessageType::DoneRequest: { ProcessorTaskMessageType resulttype; resulttype.set_type(ProcessorTaskMessageType::DoneResponse); vector<char> type_message(resulttype.ByteSizeLong()); if (type_message.size() > 0) resulttype.SerializeToArray(&type_message[0], type_message.size()); MqMessage responseMsg; responseMsg.emplace_back(id); responseMsg.emplace_back(""); responseMsg.emplace_back(std::move(type_message)); responseMsg.send(m_socket); return 0; } } //EncodeTaskHeloAnswer } if (m_state == TaskState::WaitingForHelo) { auto now = std::chrono::high_resolution_clock::now(); auto durationMs = static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>(now - m_lastCheck).count()) / 1000000.0; if (durationMs > 300) { m_lastCheck = now; ProcessorTaskMessageType type; type.set_type(ProcessorTaskMessageType::Helo); vector<char> type_message(type.ByteSizeLong()); if (type_message.size() > 0) type.SerializeToArray(&type_message[0], type_message.size()); MqMessage msg; msg.emplace_back(m_hostid); msg.emplace_back(""); msg.emplace_back(move(type_message)); msg.send(m_socket); LOG("Sent TASK helo to ID: %s", m_hostid.c_str()); } } } LOG("Task exiting"); return 0; }