diff --git a/assets/Queen - Another One Bites the Dust.mp3 b/assets/Queen - Another One Bites the Dust.mp3 new file mode 100644 index 0000000..1c48e8e Binary files /dev/null and b/assets/Queen - Another One Bites the Dust.mp3 differ diff --git a/source/Context.hpp b/source/Context.hpp index d1ad568..691a566 100644 --- a/source/Context.hpp +++ b/source/Context.hpp @@ -7,15 +7,20 @@ #include "RemotePeer.hpp" -struct Context { - int socket = -1; - addrinfo* broadcastAddressInfo = nullptr; - std::shared_ptr server = nullptr; +/** + * Information about the current state of our machine. + * Used everywhere to determinate how to behave. + */ +class Context { +public: + int socket = -1; /// current socket file descriptor, used to communicate + addrinfo* broadcastAddressInfo = nullptr; /// address used to broadcast messages + std::shared_ptr server = nullptr; /// peer currently used as the server - Peer me; + Peer me; /// information about our own machine std::map< std::uint32_t, std::shared_ptr - > remotePeers {}; - std::chrono::high_resolution_clock::time_point latestPeerDiscovery; + > remotePeers {}; /// information about other machines + std::chrono::high_resolution_clock::time_point latestPeerDiscovery; /// time of the latest discovered machine }; diff --git a/source/EventManager.cpp b/source/Manager.cpp similarity index 83% rename from source/EventManager.cpp rename to source/Manager.cpp index 6c133e4..bb53854 100644 --- a/source/EventManager.cpp +++ b/source/Manager.cpp @@ -1,4 +1,4 @@ -#include "EventManager.hpp" +#include "Manager.hpp" #include #include @@ -11,20 +11,19 @@ #include #include -#include "events/types.hpp" -#include "events/audio/AudioEvent.hpp" -#include "events/info/InfoEvent.hpp" -#include "events/pong/PongEvent.hpp" -#include "events/search/SearchEvent.hpp" +#include "behavior/events/types.hpp" +#include "behavior/events/audio/AudioEvent.hpp" +#include "behavior/events/info/InfoEvent.hpp" +#include "behavior/events/pong/PongEvent.hpp" +#include "behavior/events/search/SearchEvent.hpp" #include "packets/base/Packet.hpp" -#include "tasks/client/ClientTask.hpp" -#include "tasks/server/ServerTask.hpp" -#include "tasks/undefined/UndefinedTask.hpp" +#include "behavior/tasks/client/ClientTask.hpp" +#include "behavior/tasks/server/ServerTask.hpp" +#include "behavior/tasks/undefined/UndefinedTask.hpp" -EventManager::EventManager(const std::string& address, const std::string& port, const bool useIpv6) { - std::cout << "address: " << address << " (" << (useIpv6 ? "IPv6" : "IPv4") << ")" << std::endl; - std::cout << "port: " << port << std::endl; +Manager::Manager(const std::string& address, const std::string& port, const bool useIpv6) { + std::cout << "Broadcast address: " << address << ":" << port << " (" << (useIpv6 ? "IPv6" : "IPv4") << ")" << std::endl; // register the different events type this->eventRegistry = { @@ -121,10 +120,10 @@ EventManager::EventManager(const std::string& address, const std::string& port, } -void EventManager::loop() { +void Manager::loop() { // run an event receiver and sender - this->senderThread = std::thread(&EventManager::loopSender, this); - this->receiverThread = std::thread(&EventManager::loopReceiver, this); + this->senderThread = std::thread(&Manager::loopSender, this); + this->receiverThread = std::thread(&Manager::loopReceiver, this); this->senderThread.join(); this->receiverThread.join(); @@ -133,9 +132,9 @@ void EventManager::loop() { } -void EventManager::loopSender() { +void Manager::loopSender() { while (true) { - std::cout << "[Sender] Status: " + std::to_string(static_cast(this->context.me.status)) << std::endl; + std::cout << "[Sender] Handling status: " + std::to_string(static_cast(this->context.me.status)) << std::endl; // get the corresponding task class std::shared_ptr task; @@ -152,7 +151,7 @@ void EventManager::loopSender() { } -void EventManager::loopReceiver() { +void Manager::loopReceiver() { // prepare space for the sender address sockaddr_storage fromAddress {}; socklen_t fromAddressLength = sizeof(fromAddress); @@ -185,10 +184,12 @@ void EventManager::loopReceiver() { try { event = this->eventRegistry.at(static_cast(packetContent.eventType)); } catch (const std::out_of_range& exception) { - std::cerr << "Unsupported event type." << std::endl; + std::cerr << "[Receiver] Unsupported event type." << std::endl; continue; } + std::cout << "[Receiver] handling event: " << std::to_string(packetContent.eventType) << std::endl; + // ask the event class to handle the event event->handle( this->context, diff --git a/source/EventManager.hpp b/source/Manager.hpp similarity index 60% rename from source/EventManager.hpp rename to source/Manager.hpp index 6d4263d..909839e 100644 --- a/source/EventManager.hpp +++ b/source/Manager.hpp @@ -5,15 +5,20 @@ #include #include "Context.hpp" -#include "events/types.hpp" -#include "events/base/BaseEvent.hpp" -#include "tasks/types.hpp" -#include "tasks/base/BaseTask.hpp" +#include "behavior/events/types.hpp" +#include "behavior/events/base/BaseEvent.hpp" +#include "behavior/tasks/types.hpp" +#include "behavior/tasks/base/BaseTask.hpp" -class EventManager { +/** + * The Manager. + * Manage how should the program behave depending on its current state, or the message it receive. + */ +// TODO(Faraphel): could be split in two part. +class Manager { public: - EventManager(const std::string& address, const std::string& port, bool useIpv6 = false); + Manager(const std::string& address, const std::string& port, bool useIpv6 = false); void loop(); void loopSender(); diff --git a/source/RemotePeer.hpp b/source/RemotePeer.hpp index df72d3a..8a39364 100644 --- a/source/RemotePeer.hpp +++ b/source/RemotePeer.hpp @@ -3,7 +3,10 @@ #include #include -#include "tasks/types.hpp" +#include "behavior/tasks/types.hpp" + + +// TODO(Faraphel): should be split in multiple files. /** diff --git a/source/events/README.md b/source/behavior/events/README.md similarity index 100% rename from source/events/README.md rename to source/behavior/events/README.md diff --git a/source/events/audio/AudioEvent.cpp b/source/behavior/events/audio/AudioEvent.cpp similarity index 83% rename from source/events/audio/AudioEvent.cpp rename to source/behavior/events/audio/AudioEvent.cpp index 081b8e4..93a8884 100644 --- a/source/events/audio/AudioEvent.cpp +++ b/source/behavior/events/audio/AudioEvent.cpp @@ -3,8 +3,8 @@ #include #include -#include "../../packets/audio/AudioPacketData.hpp" -#include "../../packets/base/PacketData.hpp" +#include "packets/audio/AudioPacketData.hpp" +#include "packets/base/PacketData.hpp" namespace drp::event { @@ -27,7 +27,7 @@ AudioEvent::~AudioEvent() { Pa_StopStream(this->stream); // close the audio stream if (const PaError error = Pa_CloseStream(this->stream)) - std::cerr << "[Client] Could not close the stream: " << std::string(Pa_GetErrorText(error)) << std::endl; + std::cerr << "[Event - Audio] Could not close the stream: " << std::string(Pa_GetErrorText(error)) << std::endl; } @@ -71,7 +71,7 @@ void AudioEvent::updateAudioStream(const int channels, const std::uint32_t sampl nullptr, nullptr ) != paNoError) - throw std::runtime_error("[Client] Could not open the stream: " + std::string(Pa_GetErrorText(error))); + throw std::runtime_error("[Event - Audio] Could not open the stream: " + std::string(Pa_GetErrorText(error))); // update the new audio values this->streamChannels = channels; @@ -101,7 +101,7 @@ void AudioEvent::loopPlay() { std::this_thread::sleep_until(audioPacket.timePlay); auto cTimePlay = std::chrono::high_resolution_clock::to_time_t(audioPacket.timePlay); - std::cout << "[Client] Playing: " << std::ctime(&cTimePlay) << std::endl; + std::cout << "[Event - Audio] Playing: " << std::ctime(&cTimePlay) << std::endl; // immediately stop playing music // this avoids an offset created if this client's clock is too ahead of the others @@ -110,7 +110,7 @@ void AudioEvent::loopPlay() { // play the new audio data if (const int error = Pa_StartStream(this->stream) != paNoError) - throw std::runtime_error("[Client] Could not start the PortAudio stream: " + std::string(Pa_GetErrorText(error))); + throw std::runtime_error("[Event - Audio] Could not start the PortAudio stream: " + std::string(Pa_GetErrorText(error))); // write the new audio data into the audio buffer const int error = Pa_WriteStream( @@ -125,10 +125,10 @@ void AudioEvent::loopPlay() { // the output might be very slightly underflowed, // causing a very small period where no noise will be played. case paOutputUnderflowed: - std::cerr << "[Client] Underflowed!" << std::endl; + break; default: - std::cerr << "[Client] Could not write to the audio stream: " << Pa_GetErrorText(error) << std::endl; + std::cerr << "[Event - Audio] Could not write to the audio stream: " << Pa_GetErrorText(error) << std::endl; } // remove the audio chunk diff --git a/source/events/audio/AudioEvent.hpp b/source/behavior/events/audio/AudioEvent.hpp similarity index 100% rename from source/events/audio/AudioEvent.hpp rename to source/behavior/events/audio/AudioEvent.hpp diff --git a/source/events/audio/AudioPacketsComparator.cpp b/source/behavior/events/audio/AudioPacketsComparator.cpp similarity index 100% rename from source/events/audio/AudioPacketsComparator.cpp rename to source/behavior/events/audio/AudioPacketsComparator.cpp diff --git a/source/events/audio/AudioPacketsComparator.hpp b/source/behavior/events/audio/AudioPacketsComparator.hpp similarity index 77% rename from source/events/audio/AudioPacketsComparator.hpp rename to source/behavior/events/audio/AudioPacketsComparator.hpp index b1118fe..3af9b70 100644 --- a/source/events/audio/AudioPacketsComparator.hpp +++ b/source/behavior/events/audio/AudioPacketsComparator.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../../packets/audio/AudioPacketData.hpp" +#include "packets/audio/AudioPacketData.hpp" namespace drp::event { diff --git a/source/events/base/BaseEvent.hpp b/source/behavior/events/base/BaseEvent.hpp similarity index 81% rename from source/events/base/BaseEvent.hpp rename to source/behavior/events/base/BaseEvent.hpp index b105e5e..4a9df86 100644 --- a/source/events/base/BaseEvent.hpp +++ b/source/behavior/events/base/BaseEvent.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../../packets/base/Packet.hpp" -#include "../../Context.hpp" +#include "packets/base/Packet.hpp" +#include "Context.hpp" namespace drp::event { diff --git a/source/events/info/InfoEvent.cpp b/source/behavior/events/info/InfoEvent.cpp similarity index 91% rename from source/events/info/InfoEvent.cpp rename to source/behavior/events/info/InfoEvent.cpp index 57b1bdd..5291fee 100644 --- a/source/events/info/InfoEvent.cpp +++ b/source/behavior/events/info/InfoEvent.cpp @@ -3,7 +3,7 @@ #include #include -#include "../../packets/info/InfoPacketData.hpp" +#include "packets/info/InfoPacketData.hpp" namespace drp::event { @@ -15,7 +15,7 @@ void InfoEvent::handle( const sockaddr_storage& fromAddress, const socklen_t fromAddressLength ) { - std::cout << "Received peer information." << std::endl; + std::cout << "[Event - Info] Received peer information." << std::endl; // get the peer information const auto packetData = packet::info::InfoPacketData::fromGeneric(content); diff --git a/source/events/info/InfoEvent.hpp b/source/behavior/events/info/InfoEvent.hpp similarity index 100% rename from source/events/info/InfoEvent.hpp rename to source/behavior/events/info/InfoEvent.hpp diff --git a/source/events/pong/PongEvent.cpp b/source/behavior/events/pong/PongEvent.cpp similarity index 82% rename from source/events/pong/PongEvent.cpp rename to source/behavior/events/pong/PongEvent.cpp index 911f2aa..10e0399 100644 --- a/source/events/pong/PongEvent.cpp +++ b/source/behavior/events/pong/PongEvent.cpp @@ -12,7 +12,7 @@ void PongEvent::handle( const sockaddr_storage& fromAddress, const socklen_t fromAddressLength ) { - std::cout << "[Receiver] Pong." << std::endl; + std::cout << "[Event - Pong] Pong." << std::endl; } diff --git a/source/events/pong/PongEvent.hpp b/source/behavior/events/pong/PongEvent.hpp similarity index 100% rename from source/events/pong/PongEvent.hpp rename to source/behavior/events/pong/PongEvent.hpp diff --git a/source/events/search/SearchEvent.cpp b/source/behavior/events/search/SearchEvent.cpp similarity index 80% rename from source/events/search/SearchEvent.cpp rename to source/behavior/events/search/SearchEvent.cpp index de36723..739feed 100644 --- a/source/events/search/SearchEvent.cpp +++ b/source/behavior/events/search/SearchEvent.cpp @@ -6,8 +6,8 @@ #include #include -#include "../../packets/base/SecurityMode.hpp" -#include "../../packets/info/InfoPacketData.hpp" +#include "packets/base/SecurityMode.hpp" +#include "packets/info/InfoPacketData.hpp" namespace drp { @@ -42,11 +42,11 @@ void event::SearchEvent::handle( reinterpret_cast(&fromAddress), fromAddressLength ) == -1) { - std::cerr << "[Receiver] Could not send information: " << strerror(errno) << std::endl; + std::cerr << "[Event - Search] Could not send information: " << strerror(errno) << std::endl; return; } - std::cout << "[Receiver] Sent back information." << std::endl; + std::cout << "[Event - Search] Sent back information." << std::endl; } diff --git a/source/events/search/SearchEvent.hpp b/source/behavior/events/search/SearchEvent.hpp similarity index 100% rename from source/events/search/SearchEvent.hpp rename to source/behavior/events/search/SearchEvent.hpp diff --git a/source/events/types.hpp b/source/behavior/events/types.hpp similarity index 100% rename from source/events/types.hpp rename to source/behavior/events/types.hpp diff --git a/source/tasks/README.md b/source/behavior/tasks/README.md similarity index 100% rename from source/tasks/README.md rename to source/behavior/tasks/README.md diff --git a/source/tasks/base/BaseTask.hpp b/source/behavior/tasks/base/BaseTask.hpp similarity index 84% rename from source/tasks/base/BaseTask.hpp rename to source/behavior/tasks/base/BaseTask.hpp index f4d5688..5ae38c1 100644 --- a/source/tasks/base/BaseTask.hpp +++ b/source/behavior/tasks/base/BaseTask.hpp @@ -1,6 +1,6 @@ #pragma once -#include "../../Context.hpp" +#include "Context.hpp" namespace drp::task { diff --git a/source/tasks/client/ClientTask.cpp b/source/behavior/tasks/client/ClientTask.cpp similarity index 90% rename from source/tasks/client/ClientTask.cpp rename to source/behavior/tasks/client/ClientTask.cpp index efdf2e0..4f6e618 100644 --- a/source/tasks/client/ClientTask.cpp +++ b/source/behavior/tasks/client/ClientTask.cpp @@ -22,7 +22,7 @@ void ClientTask::handle(Context& context) { // TODO(Faraphel): only once ? FILE* chronyProcess = popen(("chronyc add server " + std::string(host) + " 2>&1").c_str(), "r"); if (pclose(chronyProcess) == -1) - std::cerr << "Failed to connect to chrony server !" << std::endl; + std::cerr << "[Task - Client] Failed to connect to chrony server !" << std::endl; // TODO(Faraphel): check if the server is still reachable. // if connection lost, go back to undefined mode. diff --git a/source/tasks/client/ClientTask.hpp b/source/behavior/tasks/client/ClientTask.hpp similarity index 100% rename from source/tasks/client/ClientTask.hpp rename to source/behavior/tasks/client/ClientTask.hpp diff --git a/source/tasks/server/ServerTask.cpp b/source/behavior/tasks/server/ServerTask.cpp similarity index 82% rename from source/tasks/server/ServerTask.cpp rename to source/behavior/tasks/server/ServerTask.cpp index c8b1617..46baf61 100644 --- a/source/tasks/server/ServerTask.cpp +++ b/source/behavior/tasks/server/ServerTask.cpp @@ -10,10 +10,10 @@ #include #include -#include "../../packets/audio/AudioPacketData.hpp" -#include "../../packets/base/Packet.hpp" -#include "../../packets/base/SecurityMode.hpp" -#include "../../utils/audio.hpp" +#include "packets/audio/AudioPacketData.hpp" +#include "packets/base/Packet.hpp" +#include "packets/base/SecurityMode.hpp" +#include "utils/audio.hpp" namespace drp::task { @@ -28,7 +28,7 @@ ServerTask::ServerTask() { int error; this->mpgHandle = mpg123_new(nullptr, &error); if (this->mpgHandle == nullptr) - throw std::runtime_error("[Server] Could not create a mpg123 handle."); + throw std::runtime_error("[Task - Server] Could not create a mpg123 handle."); // open the mp3 file // TODO(Faraphel): mp3 file as argument @@ -36,7 +36,7 @@ ServerTask::ServerTask() { this->mpgHandle, "./assets/Queen - Another One Bites the Dust.mp3" ) != MPG123_OK) - throw std::runtime_error("[Server] Could not open file."); + throw std::runtime_error("[Task - Server] Could not open file."); // get the format of the file if (mpg123_getformat( @@ -45,7 +45,7 @@ ServerTask::ServerTask() { &this->channels, &this->encoding ) != MPG123_OK) - throw std::runtime_error("[Server] Could not get the format of the file."); + throw std::runtime_error("[Task - Server] Could not get the format of the file."); } ServerTask::~ServerTask() { @@ -79,7 +79,7 @@ void ServerTask::handle(Context& context) { std::size(audioPacket.content), &done ) != MPG123_OK) { - std::cerr << "[Server] Could not read audio data from file." << std::endl; + std::cerr << "[Task - Server] Could not read audio data from file." << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); return; } @@ -109,12 +109,12 @@ void ServerTask::handle(Context& context) { context.broadcastAddressInfo->ai_addr, context.broadcastAddressInfo->ai_addrlen ) == -1) { - std::cerr << "[Server] Could not send audio packet: " << strerror(errno) << std::endl; + std::cerr << "[Task - Server] Could not send audio packet: " << strerror(errno) << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); return; } - std::cout << "[Server] Sent: " << done << " bytes" << std::endl; + std::cout << "[Task - Server] Sent: " << done << " bytes" << std::endl; // wait for the duration of the audio chunk std::this_thread::sleep_for(std::chrono::milliseconds(static_cast( diff --git a/source/tasks/server/ServerTask.hpp b/source/behavior/tasks/server/ServerTask.hpp similarity index 100% rename from source/tasks/server/ServerTask.hpp rename to source/behavior/tasks/server/ServerTask.hpp diff --git a/source/tasks/types.hpp b/source/behavior/tasks/types.hpp similarity index 100% rename from source/tasks/types.hpp rename to source/behavior/tasks/types.hpp diff --git a/source/tasks/undefined/UndefinedTask.cpp b/source/behavior/tasks/undefined/UndefinedTask.cpp similarity index 86% rename from source/tasks/undefined/UndefinedTask.cpp rename to source/behavior/tasks/undefined/UndefinedTask.cpp index b155c0d..c5d4fda 100644 --- a/source/tasks/undefined/UndefinedTask.cpp +++ b/source/behavior/tasks/undefined/UndefinedTask.cpp @@ -9,17 +9,17 @@ #include #include -#include "../../Context.hpp" -#include "../../packets/base/Packet.hpp" -#include "../../packets/base/SecurityMode.hpp" -#include "../../packets/search/SearchPacketData.hpp" +#include "Context.hpp" +#include "packets/base/Packet.hpp" +#include "packets/base/SecurityMode.hpp" +#include "packets/search/SearchPacketData.hpp" namespace drp::task { void UndefinedTask::handle(Context& context) { - std::cout << "List of peers: " << std::endl; + std::cout << "[Task - Undefined] List of peers: " << std::endl; for (const auto& peer : context.remotePeers) std::cout << "\tPeer(id=" << peer.second->information.id << ", " << @@ -63,7 +63,7 @@ void UndefinedTask::handle(Context& context) { // check if we are this peer if (context.me.id == serverCandidate->first) { - std::cout << "Becoming server..." << std::endl; + std::cout << "[Task - Undefined] Becoming server..." << std::endl; // set ourselves as the server context.server = serverCandidate->second; context.me.status = TaskType::SERVER; @@ -87,7 +87,7 @@ void UndefinedTask::handle(Context& context) { packet.setContent(packetData.toGeneric()); - std::cout << "Looking for new peers." << std::endl; + std::cout << "[Task - Undefined] Looking for new peers." << std::endl; // send the search message if (sendto( @@ -98,7 +98,7 @@ void UndefinedTask::handle(Context& context) { context.broadcastAddressInfo->ai_addr, context.broadcastAddressInfo->ai_addrlen ) == -1) - std::cerr << "[Sender] Could not send search event: " << strerror(errno) << std::endl; + std::cerr << "[Task - Undefined] Could not send search event: " << strerror(errno) << std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); } diff --git a/source/tasks/undefined/UndefinedTask.hpp b/source/behavior/tasks/undefined/UndefinedTask.hpp similarity index 100% rename from source/tasks/undefined/UndefinedTask.hpp rename to source/behavior/tasks/undefined/UndefinedTask.hpp diff --git a/source/main.cpp b/source/main.cpp index af35f29..03f6542 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,7 +2,7 @@ #include #include #include "argparse/argparse.hpp" -#include "EventManager.hpp" +#include "Manager.hpp" int main(int argc, char* argv[]) { @@ -26,7 +26,7 @@ int main(int argc, char* argv[]) { return EXIT_FAILURE; } - auto eventManager = EventManager( + auto eventManager = Manager( parser.get("--host"), parser.get("--port"), parser.get("-6") diff --git a/source/packets/audio/AudioPacketData.hpp b/source/packets/audio/AudioPacketData.hpp index 5c0fcf9..7a4480d 100644 --- a/source/packets/audio/AudioPacketData.hpp +++ b/source/packets/audio/AudioPacketData.hpp @@ -3,7 +3,7 @@ #include #include -#include "../../events/types.hpp" +#include "behavior/events/types.hpp" #include "../base/PacketData.hpp" diff --git a/source/packets/base/PacketData.hpp b/source/packets/base/PacketData.hpp index 1cb876a..6b7f934 100644 --- a/source/packets/base/PacketData.hpp +++ b/source/packets/base/PacketData.hpp @@ -3,7 +3,7 @@ #include #include "PacketContent.hpp" -#include "../../events/types.hpp" +#include "behavior/events/types.hpp" namespace drp::packet::base { diff --git a/source/packets/info/InfoPacketData.hpp b/source/packets/info/InfoPacketData.hpp index c0bce73..cda5a7a 100644 --- a/source/packets/info/InfoPacketData.hpp +++ b/source/packets/info/InfoPacketData.hpp @@ -1,7 +1,7 @@ #pragma once -#include "../../RemotePeer.hpp" -#include "../../events/types.hpp" +#include "RemotePeer.hpp" +#include "behavior/events/types.hpp" #include "../base/PacketData.hpp"