Skip to content

Commit c3a9a53

Browse files
committed
Use zero port instead of Get_Free_Port in tests
* regtests/0024_tcom/tcom.adb regtests/0062_smtp_2/smtp_2.adb regtests/0116_test_hotplug/test_hotplug.adb regtests/0128_test_soap_hotplug/test_soap_hotplug.adb: Use zero port instead of Get_Free_Port. * regtests/common/get_free_port.adb: Source file removed. * src/extended/aws-communication-server.ad[sb] (Start): Port parameter is Natural now to be able to set zero. (Port): New routine. * src/extended/aws-server-hotplug.ad[sb] (Activate): Access parameter Bound_Port to get bound port on activation. * src/extended/aws-smtp.ad[sb] (Initialize): Port parameter is Natural to be able to put zero value. * src/extended/aws-smtp-server.ad[sb] (Port): New routine to get mail server bound port.
1 parent 7613d63 commit c3a9a53

File tree

13 files changed

+99
-109
lines changed

13 files changed

+99
-109
lines changed

regtests/0024_tcom/tcom.adb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ with AWS.Parameters;
3232
with AWS.Messages;
3333
with AWS.Utils;
3434

35-
with Get_Free_Port;
36-
3735
procedure Tcom is
3836

3937
use Ada;
@@ -47,8 +45,6 @@ procedure Tcom is
4745

4846
type Context_Access is access all Context;
4947

50-
Free_Port : Positive := 1254;
51-
5248
function Callback
5349
(Server : String;
5450
Name : String;
@@ -105,24 +101,21 @@ procedure Tcom is
105101
LH : constant String := Net.Localhost (Net.IPv6_Available);
106102

107103
begin
108-
Get_Free_Port (Free_Port);
109-
110-
Com_Server.Start
111-
(Context => C'Access, Host => LH, Port => Free_Port);
104+
Com_Server.Start (Context => C'Access, Host => LH, Port => 0);
112105

113-
R := Communication.Client.Send_Message (LH, Free_Port, "zero");
106+
R := Communication.Client.Send_Message (LH, Com_Server.Port, "zero");
114107

115108
Put_Line ("R1 : " & Response.Message_Body (R));
116109
New_Line;
117110

118111
R := Communication.Client.Send_Message
119-
(LH, Free_Port, "one", (1 => +"first"));
112+
(LH, Com_Server.Port, "one", (1 => +"first"));
120113

121114
Put_Line ("R2 : " & Response.Message_Body (R));
122115
New_Line;
123116

124117
R := Communication.Client.Send_Message
125-
(LH, Free_Port, "two", (+"first", +"second"));
118+
(LH, Com_Server.Port, "two", (+"first", +"second"));
126119

127120
Put_Line ("R3 : " & Response.Message_Body (R));
128121
New_Line;

regtests/0062_smtp_2/smtp_2.adb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ with AWS.SMTP.Server;
2929

3030
with SMTP_Pck;
3131

32-
with Get_Free_Port;
33-
3432
procedure SMTP_2 is
3533

3634
use Ada;
@@ -41,7 +39,6 @@ procedure SMTP_2 is
4139
From_Email : constant String := "my.name@righthere.fr";
4240
Filename : constant String := "ada.gif";
4341

44-
Port : Positive := 9025;
4542
Family : Net.Family_Type;
4643
Host : SMTP.Receiver;
4744
Server : SMTP.Server.Handle;
@@ -51,20 +48,24 @@ procedure SMTP_2 is
5148
EOL : constant String := ASCII.CR & ASCII.LF;
5249

5350
begin
54-
Get_Free_Port (Port);
55-
5651
if Net.IPv6_Available then
5752
Family := Net.Family_Inet6;
5853
else
5954
Family := Net.Family_Inet;
6055
end if;
6156

6257
Host := SMTP.Initialize
63-
(Net.Localhost (Net.IPv6_Available), Port,
58+
(Net.Localhost (Net.IPv6_Available), 0,
6459
AWS.Net.SSL.Is_Supported, Family => Family, Timeout => 1.0);
6560

6661
SMTP.Server.Start (Server, Host, SMTP_Pck.Dump_Mail'Access);
6762

63+
-- Recreate Host with port defined
64+
65+
Host := SMTP.Initialize
66+
(Net.Localhost (Net.IPv6_Available), SMTP.Server.Port (Server),
67+
AWS.Net.SSL.Is_Supported, Family => Family, Timeout => 1.0);
68+
6869
-- Send simple message
6970

7071
Text_IO.Put_Line ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1");

regtests/0116_test_hotplug/test_hotplug.adb

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ with AWS.Server.Status;
3030
with AWS.Utils;
3131

3232
with Hotplug_Pack;
33-
with Get_Free_Port;
3433

3534
procedure Test_Hotplug is
3635

@@ -43,8 +42,8 @@ procedure Test_Hotplug is
4342
procedure Request (URI : String);
4443
-- Request URI resource to main server, output result
4544

46-
Hotplug_Port : Natural := 1235;
47-
Com_Port : Natural := 2222;
45+
46+
Com_Port : aliased Positive;
4847

4948
-------------
5049
-- Request --
@@ -64,24 +63,12 @@ procedure Test_Hotplug is
6463
CFG : Config.Object;
6564
R : Response.Data;
6665

66+
function Hotplug_Port return Positive is
67+
(AWS.Server.Status.Port (WS));
68+
6769
begin
6870
Text_IO.Put_Line ("Starting main server...");
6971

70-
Get_Free_Port (Hotplug_Port);
71-
Get_Free_Port (Com_Port);
72-
73-
-- Write access file
74-
75-
declare
76-
F : Text_IO.File_Type;
77-
begin
78-
Text_IO.Create (F, Text_IO.Out_File, "hotplug_access.ini");
79-
Text_IO.Put_Line
80-
(F, "hp_test:f8de61f1f97df3613fbe29b031eb52c6:" & Localhost & ':'
81-
& Utils.Image (Hotplug_Port));
82-
Text_IO.Close (F);
83-
end;
84-
8572
Config.Set.Server_Name (CFG, "Main");
8673
Config.Set.Admin_URI (CFG, "/Admin-Page");
8774
Config.Set.Server_Host (CFG, Localhost);
@@ -90,10 +77,6 @@ begin
9077

9178
Server.Start (Hotplug_Pack.Main_Server, Hotplug_Pack.Main'Access, CFG);
9279

93-
Server.Hotplug.Activate
94-
(Hotplug_Pack.Main_Server'Access, Com_Port, "hotplug_access.ini",
95-
Host => Localhost);
96-
9780
-- Send some requests
9881

9982
Request ("MkHuoi");
@@ -103,10 +86,26 @@ begin
10386
-- Start hotplug now
10487

10588
Config.Set.Server_Name (CFG, "Hotplug");
106-
Config.Set.Server_Port (CFG, Hotplug_Port);
89+
Config.Set.Server_Port (CFG, 0);
10790

10891
Server.Start (WS, Hotplug_Pack.Hotplug'Access, CFG);
10992

93+
-- Write access file
94+
95+
declare
96+
F : Text_IO.File_Type;
97+
begin
98+
Text_IO.Create (F, Text_IO.Out_File, "hotplug_access.ini");
99+
Text_IO.Put_Line
100+
(F, "hp_test:f8de61f1f97df3613fbe29b031eb52c6:" & Localhost & ':'
101+
& Utils.Image (Hotplug_Port));
102+
Text_IO.Close (F);
103+
end;
104+
105+
Server.Hotplug.Activate
106+
(Hotplug_Pack.Main_Server'Access, 0, "hotplug_access.ini",
107+
Host => Localhost, Bound_Port => Com_Port'Access);
108+
110109
R := Client.Hotplug.Register
111110
("hp_test", Hotplug_Pack.Password,
112111
"http://" & Localhost & ':' & Utils.Image (Com_Port),

regtests/0128_test_soap_hotplug/test_soap_hotplug.adb

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ with AWS.Utils;
3232
with SOAP_Hotplug_Pack;
3333
with SOAP_Hotplug_CB;
3434
with SOAP_Hotplug_Pack_Service.Client;
35-
with Get_Free_Port;
3635

3736
procedure Test_SOAP_Hotplug is
3837

@@ -47,8 +46,7 @@ procedure Test_SOAP_Hotplug is
4746

4847
Localhost : constant String := "127.0.0.1";
4948
HTTP_Local : constant String := "http://" & Localhost & ':';
50-
Hotplug_Port : Natural := 1135;
51-
Com_Port : Natural := 2122;
49+
Com_Port : aliased Positive;
5250

5351
-------------
5452
-- Request --
@@ -71,24 +69,12 @@ procedure Test_SOAP_Hotplug is
7169
R : Response.Data;
7270
CF : Config.Object;
7371

72+
function Hotplug_Port return Positive is
73+
(AWS.Server.Status.Port (WS));
74+
7475
begin
7576
Text_IO.Put_Line ("Starting main server...");
7677

77-
Get_Free_Port (Hotplug_Port);
78-
Get_Free_Port (Com_Port);
79-
80-
-- Write access file
81-
82-
declare
83-
F : Text_IO.File_Type;
84-
begin
85-
Text_IO.Create (F, Text_IO.Out_File, "hotplug_access.ini");
86-
Text_IO.Put_Line
87-
(F, "hp_test:f8de61f1f97df3613fbe29b031eb52c6:" & Localhost & ':'
88-
& Utils.Image (Hotplug_Port));
89-
Text_IO.Close (F);
90-
end;
91-
9278
Config.Set.Server_Name (CF, "Main");
9379
Config.Set.Admin_URI (CF, "/Admin-Page");
9480
Config.Set.Server_Host (CF, Localhost);
@@ -97,10 +83,6 @@ begin
9783

9884
Server.Start (SOAP_Hotplug_CB.Main_Server, SOAP_Hotplug_CB.Main'Access, CF);
9985

100-
Server.Hotplug.Activate
101-
(SOAP_Hotplug_CB.Main_Server'Access, Com_Port, "hotplug_access.ini",
102-
Host => Localhost);
103-
10486
-- Send some requests
10587

10688
Request (3, 7);
@@ -109,10 +91,26 @@ begin
10991
-- Start hotplug now
11092

11193
Config.Set.Server_Name (CF, "Hotplug");
112-
Config.Set.Server_Port (CF, Hotplug_Port);
94+
Config.Set.Server_Port (CF, 0);
11395

11496
Server.Start (WS, SOAP_Hotplug_CB.Hotplug'Access, CF);
11597

98+
-- Write access file
99+
100+
declare
101+
F : Text_IO.File_Type;
102+
begin
103+
Text_IO.Create (F, Text_IO.Out_File, "hotplug_access.ini");
104+
Text_IO.Put_Line
105+
(F, "hp_test:f8de61f1f97df3613fbe29b031eb52c6:" & Localhost & ':'
106+
& Utils.Image (Hotplug_Port));
107+
Text_IO.Close (F);
108+
end;
109+
110+
Server.Hotplug.Activate
111+
(SOAP_Hotplug_CB.Main_Server'Access, 0, "hotplug_access.ini",
112+
Host => Localhost, Bound_Port => Com_Port'Access);
113+
116114
R := Client.Hotplug.Register
117115
("hp_test", SOAP_Hotplug_CB.Password,
118116
HTTP_Local & Utils.Image (Com_Port), Filter,

regtests/common/get_free_port.adb

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/extended/aws-communication-server.adb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ with Ada.Unchecked_Conversion;
3232
with AWS.Config.Set;
3333
with AWS.Messages;
3434
with AWS.Parameters;
35-
with AWS.Server;
35+
with AWS.Server.Status;
3636
with AWS.Status;
3737
with AWS.Utils;
3838

@@ -60,6 +60,15 @@ package body AWS.Communication.Server is
6060
function Receive (Request : Status.Data) return Response.Data;
6161
-- Handle communication server message
6262

63+
----------
64+
-- Port --
65+
----------
66+
67+
function Port return Positive is
68+
begin
69+
return AWS.Server.Status.Port (Com_Server);
70+
end Port;
71+
6372
-------------
6473
-- Receive --
6574
-------------
@@ -125,7 +134,7 @@ package body AWS.Communication.Server is
125134
-----------
126135

127136
procedure Start
128-
(Port : Positive; Context : T_Access; Host : String := "")
137+
(Port : Natural; Context : T_Access; Host : String := "")
129138
is
130139
CB : constant Internal_Callback := Receive'Access;
131140
CNF : Config.Object;

src/extended/aws-communication-server.ads

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ package AWS.Communication.Server is
4848
-- parameter. This communication server must be started with the Start
4949
-- procedure and can be stopped with the procedure Shutdown below.
5050

51-
procedure Start (Port : Positive; Context : T_Access; Host : String := "");
51+
procedure Start (Port : Natural; Context : T_Access; Host : String := "");
5252
-- Start communication HTTP server listening at the given port
53+
-- If Port is zero, server started at any free port and it can be taken by
54+
-- the Port call.
55+
56+
function Port return Positive;
57+
-- Get the port where the server is binded
5358

5459
procedure Shutdown;
5560
-- Shutdown the communication HTTP server

src/extended/aws-server-hotplug.adb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ package body AWS.Server.Hotplug is
100100

101101
procedure Activate
102102
(Web_Server : not null access HTTP;
103-
Port : Positive;
103+
Port : Natural;
104104
Authorization_File : String;
105105
Register_Mode : AWS.Hotplug.Register_Mode := AWS.Hotplug.Add;
106-
Host : String := "")
106+
Host : String := "";
107+
Bound_Port : access Positive := null)
107108
is
108109
use Ada.Characters.Handling;
109110

@@ -121,6 +122,11 @@ package body AWS.Server.Hotplug is
121122
N : Natural := 0;
122123
begin
123124
Hotplug_Server.Start (Port, HTTP_Access (Web_Server), Host => Host);
125+
126+
if Bound_Port /= null then
127+
Bound_Port.all := Hotplug_Server.Port;
128+
end if;
129+
124130
AWS.Hotplug.Set_Mode (Web_Server.Filters, Register_Mode);
125131

126132
Text_IO.Open (File, Text_IO.In_File, Authorization_File);

src/extended/aws-server-hotplug.ads

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ package AWS.Server.Hotplug is
5252

5353
procedure Activate
5454
(Web_Server : not null access HTTP;
55-
Port : Positive;
55+
Port : Natural;
5656
Authorization_File : String;
5757
Register_Mode : AWS.Hotplug.Register_Mode := AWS.Hotplug.Add;
58-
Host : String := "");
58+
Host : String := "";
59+
Bound_Port : access Positive := null);
5960
-- Start hotplug server listening at the specified Port for the Web_Server.
6061
-- Only client modules listed in the authorization file will be able to
6162
-- connect to this server. For better securite the host of redictection
6263
-- must also be specified.
64+
-- If Port is zero then the hotplug will be bound on any free port. The
65+
-- Bound_Port access parameter should be defined in this case and bound
66+
-- port will be written there.
6367

6468
procedure Shutdown;
6569
-- Shutdown hotplug server

0 commit comments

Comments
 (0)