File: sendset.adb

package info (click to toggle)
libaws 20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: ada: 95,505; python: 2,270; ansic: 1,017; makefile: 829; xml: 235; javascript: 202; java: 112; sh: 106
file content (168 lines) | stat: -rw-r--r-- 4,879 bytes parent folder | download
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
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                        Copyright (C) 2015, 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.Calendar;
with Ada.Text_IO;
with Ada.Exceptions;
with Ada.Streams;

with AWS.Net.Std;

procedure SendSet is

   use AWS;
   use Ada;
   use Ada.Streams;

   use type Calendar.Time;

   Max_Client : constant := 20;

   Start, Stop : Calendar.Time;

   Sample : Stream_Element_Array (1 .. 4_000_000);

   Server : Net.Socket_Type'Class := Net.Socket (False);
   Peers  : Net.Socket_Set (1 .. Max_Client + 1) :=
              (others => new Net.Socket_Type'Class'(Net.Socket (False)));

   task type Client_Side is
      entry Start (Index : Positive);
      entry Stop;
   end Client_Side;

   -----------------
   -- Client_Side --
   -----------------

   task body Client_Side is
      First  : Stream_Element_Offset := Sample'First;
      Client : Net.Socket_Type'Class := Net.Socket (False);
      I      : Positive;
      F      : Positive := 1;
   begin
      accept Start (Index : Positive) do
         I := Index;
      end Start;

      --  Client.Set_Timeout (20.0);

      Client.Connect (Server.Get_Addr, Server.Get_Port);

--      if I = 1 then
--         delay 5.0;
--      else
--         F := 4096;
--      end if;

      loop
         declare
            Buffer : Stream_Element_Array :=
                       Net.Receive
                         (Client,
                          Max => Stream_Element_Offset (Sample'Last / 3));
            Next   : Stream_Element_Offset := First + Buffer'Length;
         begin
            if Buffer'Length = 0 then
               Text_IO.Put_Line ("short data");
            elsif Sample (First .. Next - 1) /= Buffer then
               Text_IO.Put_Line ("wrong data");
            end if;

            exit when Next > Sample'Last;

--            if I = 1 and then Next > 2 * (Sample'Last / 3) then
--               delay 2.0;
--            end if;

            First := Next;
         end;
      end loop;

      Text_IO.Put_Line ("client task done:" & Positive'Image (I));
      Text_IO.Flush;

      accept Stop;

      Net.Shutdown (Client);
   exception
      when E : others =>
         Text_IO.Put_Line (Exceptions.Exception_Information (E));

         accept Stop;
   end Client_Side;

   Clients : array (1 .. Max_Client) of Client_Side;

begin
   for J in Sample'Range loop
      Sample (J) := Stream_Element
                      (J mod Stream_Element_Offset (Stream_Element'Last));
   end loop;

   Text_IO.Put_Line ("start");

   Net.Bind (Server, 0, "roxy");
   Net.Listen (Server);

   Text_IO.Put_Line ("port: " & Positive'Image (Server.Get_Port));

   Server.Accept_Socket (Peers (1).all);
   --  Peers (1).Set_Timeout (3.0);
   Peers (1).Set_Send_Buffer_Size (Integer (Sample'Last / 3));

   for K in Clients'Range loop
      Clients (K).Start (K);
   end loop;

   --  Server.Set_Timeout (2.0);

   for K in 2 .. Peers'Last loop
      Server.Accept_Socket (Peers (K).all);
      --  Peers (K).Set_Timeout (3.0);
      --  Peers (K).Set_Send_Buffer_Size (Integer (Sample'Last / 3));
   end loop;

   Start := Calendar.Clock;

   Net.Send (Peers, Sample);

   for K in Clients'Range loop
      Clients (K).Stop;
   end loop;

   Stop := Calendar.Clock;

   for K in Clients'Range loop
      Net.Shutdown (Peers (K).all);
   end loop;

   Text_IO.Put_Line ("Send timing: " & Duration'Image (Stop - Start));

   Net.Shutdown (Server);

   Text_IO.Put_Line ("done.");

exception
   when E : others =>
      Text_IO.Put_Line (Exceptions.Exception_Information (E));

      for K in Clients'Range loop
         Clients (K).Stop;
      end loop;
end SendSet;