///////////////////////////////////////////////////////////////////// // CommPeer1.cpp - Demonstrates message passing communication // // ver 1.0 // // Jim Fawcett, CSE687 - Object Oriented Design, Fall 2018 // ///////////////////////////////////////////////////////////////////// /* * Package Operations: * ------------------- * This package demonstrates use of classes Comm, Message, and EndPoint. * It uses a thread to collect messages from Peer2 while sending messages * to Peer2. * * Required Files: * --------------- * CommPeer1.cpp, CommPeer2.cpp * * Required Projects: * ------------------ * - Message constructs and parses HTTP style messages * - MsgPassingComm Sends and Retrieves messages * - Sockets Used by Comm to send and receive strings and bytes * - Utilities This will be replaced by an updated Utilities module * * Maintenance History: * -------------------- * ver 1.0 : 11 Oct 2018 * - first release * * Planned Changes: * ---------------- * Convert to new Utilities Module. That will affect: * - Message will use properties * - MsgPassingComm will use new singletonlogger * - Sockets will use new singletonlogger */ #include "../CppCommWithFileXfer/MsgPassingComm/Comm.h" #include "../CppCommWithFileXfer/Message/Message.h" #include #include #include #include using namespace MsgPassingCommunication; void sndMsgs(Comm& comm, EndPoint epTo, EndPoint epFrom) { Message msg; msg.to(epTo); msg.from(epFrom); for (size_t i = 0; i < 25; ++i) { std::string countStr = Utilities::Converter::toString(i); msg.name("msg" + countStr); std::cout << "\n Peer1 posted message \"" << msg.name() << "\""; comm.postMessage(msg); std::this_thread::sleep_for(std::chrono::milliseconds(500)); } } void getMsgs(Comm& comm) { Message msg; while (true) { msg = comm.getMessage(); std::cout << "\n Peer1 recvd message \"" << msg.name() << "\""; if (msg.command() == "stop") break; } } int main() { std::cout << "\n Demonstrating Peer1 in Peer-To-Peer Comm Demo"; std::cout << "\n ==============================================="; ::SetConsoleTitle(L"Peer1"); EndPoint epPeer1("localhost", 8081); EndPoint epPeer2("localhost", 8082); Comm comm(epPeer1, "Peer1"); comm.start(); std::string savePath = comm.setSaveFilePath("../SaveFile"); std::cout << "\n saving files to \"" << savePath << "\""; std::string sendPath = comm.setSendFilePath("../SendFile"); std::cout << "\n sending files from \"" << sendPath << "\""; Message fileMsg; fileMsg.to(epPeer2); fileMsg.from(epPeer1); fileMsg.value("sendingFile"); // required to send file fileMsg.name("Peer1"); // for display fileMsg.file("Comm.h"); // defines file to send from sendPath std::cout << "\n " + comm.name() + " posting: " + fileMsg.name(); comm.postMessage(fileMsg); // tproc, below, will compile to a functor // - operator() contains the code between { and } // - comm will be a reference member std::function tproc = [&]() { getMsgs(comm); }; std::thread tget(tproc); tget.detach(); sndMsgs(comm, epPeer2, epPeer1); Message msg; msg.command("stop"); // stop receiver comm.postMessage(msg); msg.command("quit"); // stop sender comm.postMessage(msg); std::cout << "\n Press enter to quit: "; std::cin.get(); comm.stop(); std::cout << "\n\n"; }