garlic_90ef4b6f/Examples/Filters/s-gafish.adb

  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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
with Ada.Streams; use Ada.Streams;
with Ada.Numerics.Discrete_Random;

with System.Garlic.Debug;   use System.Garlic.Debug;
with System.Garlic.Filters;
pragma Elaborate (System.Garlic.Filters);
with System.Garlic.Streams; use System.Garlic.Streams;
with System.Garlic.Types; use System.Garlic.Types;

package body System.Garlic.Filters.Shift is

   Private_Debug_Key : constant Debug_Key :=
     Debug_Initialize ("SHIFT", "(s-gafish): ");

   procedure D
     (Message : String;
      Key     : Debug_Key := Private_Debug_Key)
     renames Print_Debug_Info;

   package Shift_Filter_Params is
     new Ada.Numerics.Discrete_Random (Stream_Element);

   Generator : Shift_Filter_Params.Generator;

   Shift_Filter : aliased Shift_Filter_Type;

   ---------------------
   -- Filter_Incoming --
   ---------------------

   function Filter_Incoming
     (Filter : Shift_Filter_Type;
      Params : Filter_Params_Access;
      Stream : Stream_Element_Access;
      Offset : Stream_Element_Offset)
      return Stream_Element_Access
   is
      pragma Unreferenced (Filter);
      Shift_Params  : Shift_Filter_Params_Type;
      Target_Buffer : Stream_Element_Access;

      F : constant Stream_Element_Offset := Stream'First + Offset;
      L : constant Stream_Element_Offset := Stream'Last;

   begin
      D ("Entering shift incoming filter");

      Shift_Params  := Shift_Filter_Params_Type (Params.all);
      D ("Use shift params" & Shift_Params.Times'Img);

      Target_Buffer := new Stream_Element_Array'(Stream (F .. L));
      Dump (Target_Buffer, Private_Debug_Key);

      for I in Target_Buffer'Range loop
         Target_Buffer (I) := Target_Buffer (I) xor Shift_Params.Times;
      end loop;

      D ("Leaving shift incoming filter");
      Dump (Target_Buffer, Private_Debug_Key);

      return Target_Buffer;
   end Filter_Incoming;

   ---------------------
   -- Filter_Outgoing --
   ---------------------

   function Filter_Outgoing
     (Filter : Shift_Filter_Type;
      Params : Filter_Params_Access;
      Stream : access System.Garlic.Streams.Params_Stream_Type)
     return Stream_Element_Access
   is
      pragma Unreferenced (Filter);
      Shift_Params  : Shift_Filter_Params_Type;
      Target_Buffer : Stream_Element_Access;

   begin
      D ("Entering shift outgoing filter");

      Shift_Params  := Shift_Filter_Params_Type (Params.all);
      D ("Use shift params" & Shift_Params.Times'Img);

      Target_Buffer := To_Stream_Element_Access (Stream);
      Dump (Target_Buffer, Private_Debug_Key);

      for I in Target_Buffer'Range loop
         Target_Buffer (I) := Target_Buffer (I) xor Shift_Params.Times;
      end loop;

      D ("Leaving shift outgoing filter");
      Dump (Target_Buffer, Private_Debug_Key);

      return Target_Buffer;
   end Filter_Outgoing;

   ------------------------
   -- Filter_Params_Read --
   ------------------------

   function Filter_Params_Read
     (Filter : Shift_Filter_Type;
      Stream : Ada.Streams.Stream_Element_Array)
     return Filter_Params_Access
   is
      pragma Unreferenced (Filter);
   begin
      D ("Read filter params" & Stream (Stream'First)'Img);
      return new Shift_Filter_Params_Type'(Times => Stream (Stream'First));
   end Filter_Params_Read;

   -------------------------
   -- Filter_Params_Write --
   -------------------------

   function Filter_Params_Write
     (Filter : Shift_Filter_Type;
      Params : Filter_Params_Access) return
     Streams.Stream_Element_Access
   is
      pragma Unreferenced (Filter);
      Shift_Filter_Params : Shift_Filter_Params_Type;

   begin
      Shift_Filter_Params := Shift_Filter_Params_Type (Params.all);
      D ("Write filter params" & Shift_Filter_Params.Times'Img);
      return new Stream_Element_Array'(1 .. 1 => Shift_Filter_Params.Times);
   end Filter_Params_Write;

   ---------------------
   -- Generate_Params --
   ---------------------

   procedure Generate_Params
     (Filter          : Shift_Filter_Type;
      Public_Params   : out Filter_Params_Access;
      Private_Params  : out Filter_Params_Access;
      Exchange_Params : out Boolean)
   is
      pragma Unreferenced (Filter);
      Element : Stream_Element;

   begin
      for I in 1 .. Self_PID loop
         Element := Shift_Filter_Params.Random (Generator);
      end loop;
      D ("Generate shift params" & Element'Img);
      Public_Params   := new Shift_Filter_Params_Type'(Times => Element);
      Private_Params  := new Shift_Filter_Params_Type'(Times => Element);
      Exchange_Params := True;
   end Generate_Params;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      Register_Filter (Shift_Filter'Access, "shift");
   end Initialize;

end System.Garlic.Filters.Shift;