xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/test_tools/simulator/actor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef QUICHE_QUIC_TEST_TOOLS_SIMULATOR_ACTOR_H_
6 #define QUICHE_QUIC_TEST_TOOLS_SIMULATOR_ACTOR_H_
7 
8 #include <string>
9 
10 #include "quiche/quic/core/quic_clock.h"
11 #include "quiche/quic/core/quic_time.h"
12 
13 namespace quic {
14 namespace simulator {
15 
16 class Simulator;
17 
18 // Actor is the base class for all participants of the simulation which can
19 // schedule events to be triggered at the specified time.  Every actor has a
20 // name assigned to it, which can be used for debugging and addressing purposes.
21 //
22 // The Actor object is scheduled as follows:
23 // 1. Every Actor object appears at most once in the event queue, for one
24 //    specific time.
25 // 2. Actor is scheduled by calling Schedule() method.
26 // 3. If Schedule() method is called with multiple different times specified,
27 //    Act() method will be called at the earliest time specified.
28 // 4. Before Act() is called, the Actor is removed from the event queue.  Act()
29 //    will not be called again unless Schedule() is called.
30 class Actor {
31  public:
32   Actor(Simulator* simulator, std::string name);
33   virtual ~Actor();
34 
35   // Trigger all the events the actor can potentially handle at this point.
36   // Before Act() is called, the actor is removed from the event queue, and has
37   // to schedule the next call manually.
38   virtual void Act() = 0;
39 
name()40   std::string name() const { return name_; }
simulator()41   Simulator* simulator() const { return simulator_; }
42 
43  protected:
44   // Calls Schedule() on the associated simulator.
45   void Schedule(QuicTime next_tick);
46 
47   // Calls Unschedule() on the associated simulator.
48   void Unschedule();
49 
50   Simulator* simulator_;
51   const QuicClock* clock_;
52   std::string name_;
53 
54  private:
55   // Since the Actor object registers itself with a simulator using a pointer to
56   // itself, do not allow it to be moved.
57   Actor(Actor&&) = delete;
58   Actor(const Actor&) = delete;
59   Actor& operator=(const Actor&) = delete;
60   Actor& operator=(Actor&&) = delete;
61 };
62 
63 }  // namespace simulator
64 }  // namespace quic
65 
66 #endif  // QUICHE_QUIC_TEST_TOOLS_SIMULATOR_ACTOR_H_
67