aws_21.0.0_57fddf8f/regtests/0284_poll/poll.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                        Copyright (C) 2016, AdaCore                       --
--                                                                          --
--  This is free software;  you can redistribute it  and/or modify it       --
--  under terms of the  GNU General Public License as published  by the     --
--  Free Software  Foundation;  either version 3,  or (at your option) any  --
--  later version.  This software is distributed in the hope  that it will  --
--  be useful, but WITHOUT ANY WARRANTY;  without even the implied warranty --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     --
--  General Public License for  more details.                               --
--                                                                          --
--  You should have  received  a copy of the GNU General  Public  License   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

with Ada.Characters.Handling;
with Ada.Streams;         use Ada.Streams;
with Ada.Text_IO;         use Ada.Text_IO;
with AWS.Net.Poll_Events; use AWS.Net;
with AWS.Net.Std;
with AWS.OS_Lib;
with AWS.Utils;
with Interfaces.C.Strings;

procedure Poll is
   Set : Poll_Events.Set (45); -- Have to be odd
   Ss  : array (1 .. Set.Size) of Std.Socket_Type;
   Local : constant String := Localhost (IPv6_Available);
   Count : Integer;
   Idx   : Positive;
   State : Event_Set;
   Data  : Stream_Element_Array (1 .. 32);
   Last  : Stream_Element_Offset;
   More  : constant Stream_Element_Offset := 8;
   Rest  : Natural := 0;

   function Error_Message (Errno : Integer) return String;

   function Image_State return String;
   --  Returns image of State array in format like eIo or eiO or Eio

   -------------------
   -- Error_Message --
   -------------------

   function Error_Message (Errno : Integer) return String is
      use AWS;
      use Interfaces.C.Strings;
      Ptr  : constant chars_ptr := OS_Lib.Socket_StrError (Errno);
      Code : constant String    := '[' & Utils.Image (Errno) & "] ";
   begin
      if Ptr = Null_Ptr then
         return Code & "Unknown error";
      else
         return Code & Value (Ptr);
      end if;
   end Error_Message;

   -----------------
   -- Image_State --
   -----------------

   function Image_State return String is
      Result : String (1 .. 3);
      Idx : Positive := Result'First;
   begin
      for J in State'Range loop
         Result (Idx) := J'Img (1);

         if not State (J) then
            Result (Idx) := Ada.Characters.Handling.To_Lower (Result (Idx));
         end if;

         Idx := Idx + 1;
      end loop;

      return Result;
   end Image_State;

begin
   Ss (1).Bind (Host => Local, Port => 0);
   Ss (1).Listen (Queue_Size => Set.Size);
   Set.Add (Ss (1).Get_FD, (Input => True, others => False));

   ------------------
   -- Connect loop --
   ------------------

   for J in 2 .. Set.Size / 2 + 1 loop
      Ss (J).Connect (Local, Port => Ss (1).Get_Port, Wait => False);
      Set.Add (Ss (J).Get_FD, (Output => True, others => False));
   end loop;

   Put_Line ("Connection");

   -----------------
   -- Accept loop --
   -----------------

   loop
      Set.Wait (0.5, Count);

      Idx := 1;

      while Count > 0 loop
         Set.Next (Idx);
         exit when Idx > Set.Length;
         Count := Count - 1;

         State := Set.Status (Idx);

         if State (Error) then
            Put_Line
              ("Unexpected error on accept " & Error_Message (Ss (Idx).Errno)
               & Idx'Img);
            return;

         elsif State (Input) and then State (Output) then
            Put_Line ("Unexpected state on accept " & Image_State & Idx'Img);
            return;

         elsif State (Output) then
            Set.Set_Mode (Idx, (Input => True, others => False));

         elsif State (Input) then
            if Idx /= 1 then
               Put_Line ("Unexpected input index" & Idx'Img);
            end if;

            Std.Accept_Socket (Ss (1), Ss (Set.Length + 1));
            Set.Add
              (Ss (Set.Length + 1).Get_FD, (Input => True, others => False));
         end if;

         Idx := Idx + 1;
      end loop;

      if Count /= 0 then
         Put_Line ("Wrong sockets event count " & Count'Img);
         exit;
      end if;

      exit when Set.Length = Ss'Length;
   end loop;

   Put_Line ("Connected");

   ----------------------------------
   -- Write over whole sockets set --
   ----------------------------------

   for J in 2 .. Ss'Last loop
      Ss (J).Send ((1 => Stream_Element (J)));

      Set.Wait (0.5, Count);

      if Count /= 1 then
         Put_Line ("Unexpected number of activated sockets" & Count'Img);
      end if;

      Idx := 1;
      Set.Next (Idx);

      State := Set.Status (Idx);

      if State /= (Input => True, Output => False, Error => False) then
         Put_Line ("Unexpected state on send loop " & Image_State);

         if State (Error) then
            Put_Line
              ("Unexpected error on send loop "
               & Error_Message (Ss (Idx).Errno) & Idx'Img);
         end if;

         return;
      end if;

      Ss (Idx).Receive (Data, Last);

      if Last /= Data'First then
         Put_Line ("Unexpected received data length" & Last'Img);
      end if;

      if Integer (Data (Data'First)) /= J then
         Put_Line ("Unexpected received content");
      end if;
   end loop;

   Put_Line ("Transmitted");

   for J in 2 .. Ss'Last loop
      Ss (J).Send ((1 .. Data'Length + More => Stream_Element (J)));
      Ss (J).Set_Timeout (2.0);
   end loop;

   -------------------------------------------
   -- Test waiting neither Input nor Output --
   -------------------------------------------

   loop
      Set.Wait (0.25, Count);

      exit when Count = 0;

      Idx := 1;

      while Count > 0 loop
         Set.Next (Idx);
         exit when Idx > Set.Length;
         Count := Count - 1;

         State := Set.Status (Idx);

         if State /= (Input => True, Output => False, Error => False) then
            Put_Line ("Unexpected state on test " & Image_State);

            if State (Error) then
               Put_Line
                 ("Unexpected error on test " & Error_Message (Ss (Idx).Errno)
                  & Idx'Img);
            end if;

            return;
         end if;

         Ss (Idx).Receive (Data, Last);

         if Last = Data'Last then
            Put ('@');

            if Idx rem 2 = 0 then
               Set.Set_Event (Idx, Input, False);
            end if;

         elsif Last = More then
            Rest := Rest + 1;
         else
            Put (" Wrong additional size" & Last'Img);
            exit;
         end if;

         Idx := Idx + 1;
      end loop;

      if Count /= 0 then
         Put ("Wrong rest count" & Count'Img);
         exit;
      end if;
   end loop;

   Put_Line (Rest'Img);
end Poll;