File: wait_proc.adb

package info (click to toggle)
libaws 2.2dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,624 kB
  • ctags: 1,173
  • sloc: ada: 61,829; ansic: 6,483; makefile: 1,282; xml: 196; sh: 119; java: 112; python: 66; sed: 40
file content (162 lines) | stat: -rw-r--r-- 5,160 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
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                         Copyright (C) 2004-2006                          --
--                                 AdaCore                                  --
--                                                                          --
--  This library is free software; you can redistribute it and/or modify    --
--  it under the terms of the GNU General Public License as published by    --
--  the Free Software Foundation; either version 2 of the License, or (at   --
--  your option) any later version.                                         --
--                                                                          --
--  This library 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       --
--  along with this library; if not, write to the Free Software Foundation, --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.          --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Exceptions;
with Ada.Streams;
with Ada.Text_IO;
with AWS.Net.Sets;
with AWS.Net.SSL;
with Get_Free_Port;

procedure Wait_Proc (Security : Boolean; Port : Positive) is
   use Ada.Text_IO;
   use AWS;
   use AWS.Net;

   use type Sets.Socket_Count;

   Set_Size    : constant := 20;
   Sample_Size : constant := 10;

   Free_Port : Positive := Port;

   task Client_Side is
      entry Start;
   end Client_Side;

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

   task body Client_Side is
      use type Ada.Streams.Stream_Element;
      Set    : Sets.Socket_Set_Type;
      Index  : Sets.Socket_Index;
      Data   : Ada.Streams.Stream_Element_Array (1 .. Sample_Size);
      Sum    : Ada.Streams.Stream_Element := 0;
   begin
      accept Start;

      for J in 1 .. Set_Size loop
         declare
            Socket : Net.Socket_Type'Class := Net.Socket (Security);
         begin
            Net.Connect (Socket, "localhost", Free_Port, Wait => False);

            Net.Set_Timeout (Socket, 1.0);

            Sets.Add (Set, Socket, Sets.Output);
         end;
      end loop;

      Main : while Sets.Count (Set) > 0 loop
         Sets.Wait (Set, 2.0);

         Index := 1;

         loop
            Sets.Next (Set, Index);

            exit when not Sets.In_Range (Set, Index);

            declare
               Socket : Socket_Type'Class := Sets.Get_Socket (Set, Index);
            begin
               if Sets.Is_Write_Ready (Set, Index) then
                  Sets.Set_Mode (Set, Index, Sets.Input);

                  if Security then
                     SSL.Do_Handshake (SSL.Socket_Type (Socket));
                  end if;

                  Index := Index + 1;

               elsif Sets.Is_Read_Ready (Set, Index) then
                  begin
                     Data  := Net.Receive (Socket);

                     Sum := Sum + Data (Data'First);

                     Put_Line ("Read");

                     Index := Index + 1;

                  exception when Net.Socket_Error =>
                     Net.Shutdown (Socket);
                     Sets.Remove_Socket (Set, Index);
                  end;

               elsif Sets.Is_Error (Set, Index) then
                  Put_Line ("Socket error" & Integer'Image (Errno (Socket)));

                  Net.Shutdown (Socket);
                  Sets.Remove_Socket (Set, Index);

               else
                  Put_Line ("Wait error.");
                  exit Main;
               end if;
            end;
         end loop;
      end loop Main;

      Put_Line (Ada.Streams.Stream_Element'Image (Sum));

   exception
      when E : others =>
         Put_Line
           ("Client side " & Ada.Exceptions.Exception_Information (E));
   end Client_Side;

   Server : Net.Socket_Type'Class := Net.Socket (False);

begin
   Get_Free_Port (Free_Port);

   Net.Bind (Server, Free_Port);
   Net.Listen (Server, Set_Size);

   Net.Set_Timeout (Server, 1.0);

   Client_Side.Start;

   for J in 1 .. Set_Size loop
      declare
         New_Sock : Net.Socket_Type'Class := Net.Socket (Security);
      begin
         Net.Accept_Socket (Server, New_Socket => New_Sock);

         Net.Send
           (New_Sock, (1 .. Sample_Size => Ada.Streams.Stream_Element (J)));

         delay 0.5;
         Net.Shutdown (New_Sock);
      end;
   end loop;

   Net.Shutdown (Server);

exception
   when E : others =>
      Put_Line
        ("Server side " & Ada.Exceptions.Exception_Information (E));
end Wait_Proc;