agpl_1.0.0_b5da3320/src/agpl-bandwidth_throttle.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 

--  Class for bandwidth throttling.
--  Provides no arbitration; first come first served.

with Ada.Streams;
with Ada.Calendar;
use  Ada;

package Agpl.Bandwidth_Throttle is

   pragma Elaborate_Body;

   --  Bandwidth is the elements/second to allow
   --  Period is the period for feedback. Longer ones allow the use
   --    of unused bandwidth for more time. (In milliseconds)
   protected type Object (
      Bandwidth : Ada.Streams.Stream_Element_Count;
      Period    : Positive)
   is
      --  Gives the available bandwidth at this moment (real + extra):
      procedure Available (Bandwidth : out Ada.Streams.Stream_Element_Count);

      --  Issue a bandwidth petition. Awarded can be less that solicited.
      --  Awarded will never be more than Natural'Last / 2 so you can
      --    add Awarded + Awarded_Extra.
      --  Extra flag requests bandwidth from unused past cycles.
      --  Past cycles will faint in exponential fashion.
      procedure Commit (
         Desired : in     Ada.Streams.Stream_Element_Count;
         Awarded :    out Ada.Streams.Stream_Element_Count;
         Extra   : in     Boolean := False);

   private

      Gap      : Duration      := Duration (Period) / 1000.0;

      Remanent : Ada.Streams.Stream_Element_Count := 0; -- Remanent in this cycle
      Unused   : Ada.Streams.Stream_Element_Count := 0; -- Remanent from past cycles
      Last_Req : Calendar.Time                    := Calendar.Clock;

   end Object;

   type Object_access is access all Object;

   Unlimited : aliased Object (Ada.Streams.Stream_Element_Count'Last, 1000);

end Agpl.Bandwidth_Throttle;