worker.cpp 696 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "worker.hpp"
  2. Task
  3. Worker::get_task(){
  4. Task task;
  5. while(true){
  6. listen();
  7. if(has_message){
  8. has_message=false;
  9. switch(message.get_type()){
  10. case DATA:
  11. task.set_input(message.get_data_buffer(),message.get_data_size());
  12. return task;
  13. break;
  14. case CODE:
  15. if(message.get_code()=='K'){
  16. cout<<"I quit !"<<endl;
  17. exit(0);
  18. }
  19. cerr<<"[Error] Receive unexpected code "<<message.get_code()<<endl;
  20. exit(-1);
  21. break;
  22. default:
  23. cerr<<"[Error] Receive unexpected message"<<endl;
  24. break;
  25. }
  26. }
  27. }
  28. return task;
  29. }
  30. void
  31. Worker::send_task(Task& task){
  32. Message msg;
  33. msg.set_data(task.get_output(),task.get_output_size());
  34. send_message(msg);
  35. }