Advertisement
austinwebber

ZeroMQ-String-Formatting-Server

Aug 20th, 2022
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | Source Code | 0 0
  1. #include <iostream>
  2. // zeromq header files, installed and integrated through vcpkg
  3. #include <zmq.hpp>
  4. #include <zmq_addon.hpp>
  5.  
  6. int main()
  7. {
  8.     // preparing our context
  9.     zmq::context_t context;
  10.     // deriving a request socket from the global context
  11.     zmq::socket_t socket(context, zmq::socket_type::rep);
  12.     // connect that socket over TCP to an endpoint
  13.     socket.bind("tcp://127.0.0.1:5555");
  14.  
  15.     while (true)
  16.     {
  17.         zmq::message_t request;
  18.  
  19.         // waiting for next request from client
  20.         std::cout << "Waiting for request..." << std::endl;
  21.         socket.recv(request, zmq::recv_flags::none);
  22.  
  23.         // request received
  24.         std::string client_request = request.to_string();
  25.         std::string request_type = client_request.substr(0, 6);
  26.  
  27.         // server expects format request to come in the format "format<streak>*<habit>%<daysleft>"
  28.         if (request_type == "format")
  29.         {
  30.             std::cout << "Received: " << request.to_string() << std::endl;
  31.  
  32.             // gets the whole string after "format"
  33.             std::string request_body = client_request.substr(6);
  34.             // grabbing the different variables in the message
  35.             int streak_end = request_body.find("*");
  36.             int habit_end = request_body.find("%");
  37.             std::string streak = request_body.substr(0, streak_end);
  38.             std::string habit = request_body.substr(streak_end + 1, habit_end - 2);
  39.             std::string days_left = request_body.substr(habit_end + 1);
  40.  
  41.             // creating the response
  42.             std::string response_message = "I am " + streak + " days into my habit: " + habit + "! There are " + days_left + " days left until I hit my goal.";
  43.             int response_size = response_message.length();
  44.  
  45.             // send response back to client
  46.             std::cout << "Sending reply..." << std::endl;
  47.             zmq::message_t reply(response_size);
  48.             memcpy(reply.data(), response_message.data(), response_size);
  49.             socket.send(reply, zmq::send_flags::none);
  50.         }
  51.         // if request not in correct format, send an error message back
  52.         else
  53.         {
  54.             std::cout << "Error: Message received is not in proper format." << std::endl;
  55.             std::cout << "Sending reply..." << std::endl;
  56.             zmq::message_t reply(56);
  57.             memcpy(reply.data(), "ERROR: The message you sent is not in the proper format.", 56);
  58.             socket.send(reply, zmq::send_flags::none);
  59.         }
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement