| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "engine/SimplxShim.hpp" |
| #include <iostream> |
| #include <string> |
|
|
| using namespace tredzone; |
|
|
| |
| struct PingEvent : Actor::Event { |
| int counter; |
| explicit PingEvent(int c) : counter(c) {} |
| }; |
|
|
| struct PongEvent : Actor::Event { |
| int counter; |
| explicit PongEvent(int c) : counter(c) {} |
| }; |
|
|
| |
| class PongActor : public Actor { |
| public: |
| PongActor() { |
| registerEventHandler<PingEvent>(*this); |
| std::cout << "[Pong] Created on core " << (int)getCore() << "\n"; |
| } |
|
|
| void onEvent(const PingEvent& event) { |
| std::cout << "[Pong] Received ping #" << event.counter << "\n"; |
| Event::Pipe pipe(*this, event.getSourceActorId()); |
| pipe.push<PongEvent>(event.counter); |
| } |
| }; |
|
|
| |
| class PingActor : public Actor { |
| public: |
| PingActor(const ActorId& pongId, int maxRounds) |
| : pongPipe_(*this, pongId), maxRounds_(maxRounds) |
| { |
| registerEventHandler<PongEvent>(*this); |
| std::cout << "[Ping] Created on core " << (int)getCore() << "\n"; |
|
|
| |
| std::cout << "[Ping] Sending ping #1\n"; |
| pongPipe_.push<PingEvent>(1); |
| } |
|
|
| void onEvent(const PongEvent& event) { |
| std::cout << "[Ping] Received pong #" << event.counter << "\n"; |
| if (event.counter < maxRounds_) { |
| int next = event.counter + 1; |
| std::cout << "[Ping] Sending ping #" << next << "\n"; |
| pongPipe_.push<PingEvent>(next); |
| } else { |
| std::cout << "[Ping] Done after " << maxRounds_ << " rounds.\n"; |
| } |
| } |
|
|
| private: |
| Event::Pipe pongPipe_; |
| int maxRounds_; |
| }; |
|
|
| |
| int main() { |
| std::cout << "=== Ping-Pong Actor Example ===\n\n"; |
|
|
| |
| auto pong = std::make_unique<PongActor>(); |
| auto ping = std::make_unique<PingActor>(pong->getActorId(), 5); |
|
|
| std::cout << "\n=== Complete ===\n"; |
| return 0; |
| } |
|
|