Skip to content

Commit 57d8cba

Browse files
committed
Cleanup of the server main and socket binding as server
1 parent 0054505 commit 57d8cba

3 files changed

Lines changed: 44 additions & 142 deletions

File tree

server/src/coap_server.adb

Lines changed: 31 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,10 @@ with Secure_Server;
77
with CoAP_SPARK.Channel;
88
with CoAP_SPARK.Server_Session;
99
with CoAP_SPARK.Log;
10-
with CoAP_SPARK.Messages;
11-
with CoAP_SPARK.URI;
1210
with CoAP_SPARK.Utils;
1311

14-
with Interfaces;
15-
1612
with RFLX.CoAP_Server.Main_Loop_Environment;
1713
with RFLX.CoAP_Server.Main_Loop.FSM;
18-
with RFLX.CoAP;
19-
with RFLX.RFLX_Types;
2014

2115
-- The following package is used, instead of Ada.Command_Line, for the reasons
2216
-- explained in the package itself (related to the use of SPARK).
@@ -29,12 +23,8 @@ with Server_Handling;
2923
procedure CoAP_Server is
3024
package FSM renames RFLX.CoAP_Server.Main_Loop.FSM;
3125
package Main_Loop_Environment renames RFLX.CoAP_Server.Main_Loop_Environment;
32-
package Types renames RFLX.RFLX_Types;
33-
package Channel renames CoAP_SPARK.Channel;
3426

3527
use type CoAP_SPARK.Status_Type;
36-
use type Types.Index;
37-
use type Types.Bytes_Ptr;
3828

3929
procedure Usage (Is_Failure : Boolean := True) is
4030
procedure Put (Text : String;
@@ -44,7 +34,7 @@ procedure CoAP_Server is
4434
Level : CoAP_SPARK.Log.Level_Type := CoAP_SPARK.Log.Info)
4535
renames CoAP_SPARK.Log.Put_Line;
4636
begin
47-
Put ("Usage: coap_server [-p PORT] [-A <Address>] ");
37+
Put ("Usage: coap_server [-p PORT] [-A <Address>] "); -- TODO add Address
4838
Put_Line ("[-k <PSK>] [-u <Identity>] [-v <verbosity>] ");
4939

5040
Ada.Command_Line.Set_Exit_Status
@@ -55,59 +45,20 @@ procedure CoAP_Server is
5545
end Usage;
5646

5747
procedure Run_Session
58-
(Method : RFLX.CoAP.Method_Code;
59-
URI_String : String;
60-
Payload : in out CoAP_SPARK.Messages.Payload_Ptr)
61-
with
62-
Pre =>
63-
URI_String'Length <= CoAP_SPARK.Max_URI_Length
48+
(Port : CoAP_SPARK.Channel.Port_Type;
49+
Is_Secure : Boolean)
6450
is
65-
URI : constant CoAP_SPARK.URI.URI :=
66-
CoAP_SPARK.URI.Create (URI_String);
67-
Ctx : FSM.Context;
68-
Skt :
69-
CoAP_SPARK.Channel.Socket_Type
70-
(Is_Secure =>
71-
CoAP_SPARK.URI.Scheme (URI) = CoAP_SPARK.Secure_Scheme);
72-
Valid_URI : Boolean := True;
73-
begin
74-
75-
if URI_String = "" or else URI_String (URI_String'First) = '-' then
76-
CoAP_SPARK.Log.Put ("Unrecognized option: ", CoAP_SPARK.Log.Error);
77-
CoAP_SPARK.Log.Put_Line (URI_String, CoAP_SPARK.Log.Error);
78-
Valid_URI := False;
79-
elsif not CoAP_SPARK.URI.Is_Valid (URI)
80-
or else not CoAP_SPARK.URI.Has_Valid_Lengths (URI)
81-
then
82-
CoAP_SPARK.Log.Put ("Invalid URI: ", CoAP_SPARK.Log.Error);
83-
CoAP_SPARK.Log.Put_Line (URI_String, CoAP_SPARK.Log.Error);
84-
Valid_URI := False;
85-
end if;
8651

87-
if not Valid_URI then
88-
Usage;
89-
return;
90-
end if;
91-
92-
CoAP_SPARK.Log.Put ("Method: ");
93-
CoAP_SPARK.Log.Put_Line (RFLX.CoAP.Method_Code'Image (Method));
94-
CoAP_SPARK.Log.Put ("Scheme: ");
95-
CoAP_SPARK.Log.Put_Line (CoAP_SPARK.URI.Scheme (URI));
96-
CoAP_SPARK.Log.Put ("Host: ");
97-
CoAP_SPARK.Log.Put_Line (CoAP_SPARK.URI.Host (URI));
98-
CoAP_SPARK.Log.Put ("Port:");
99-
CoAP_SPARK.Log.Put_Line
100-
(Interfaces.Unsigned_16'Image (CoAP_SPARK.URI.Port (URI)));
101-
CoAP_SPARK.Log.Put ("Path: ");
102-
CoAP_SPARK.Log.Put_Line (CoAP_SPARK.URI.Path (URI));
103-
CoAP_SPARK.Log.Put ("Query: ");
104-
CoAP_SPARK.Log.Put_Line (CoAP_SPARK.URI.Query (URI));
52+
Ctx : FSM.Context;
53+
Skt : CoAP_SPARK.Channel.Socket_Type (Is_Secure);
54+
begin
10555

106-
Secure_Server.Initialize (Socket => Skt);
56+
Secure_Server.Initialize
57+
(Socket => Skt,
58+
Port => Port);
10759
if not CoAP_SPARK.Channel.Is_Valid (Skt) then
10860
CoAP_SPARK.Log.Put_Line
10961
("Communication problems.", CoAP_SPARK.Log.Error);
110-
RFLX.RFLX_Types.Free (Payload);
11162
return;
11263
end if;
11364

@@ -118,29 +69,13 @@ procedure CoAP_Server is
11869
if Ctx.E.Current_Status /= CoAP_SPARK.OK then
11970
CoAP_SPARK.Log.Put_Line
12071
(Ctx.E.Current_Status'Image, CoAP_SPARK.Log.Error);
121-
RFLX.RFLX_Types.Free (Payload);
12272
-- Main_Loop_Environment.Finalize (Ctx.E);
12373
-- pragma Assert (Main_Loop_Environment.Is_Finalized (Ctx.E));
12474
return;
12575
end if;
12676
pragma Assert (FSM.Uninitialized (Ctx));
12777

12878
FSM.Initialize (Ctx);
129-
Channel.Connect
130-
(Socket => Skt,
131-
Server => CoAP_SPARK.URI.Host (URI),
132-
Port => CoAP_SPARK.Channel.Port_Type (CoAP_SPARK.URI.Port (URI)));
133-
134-
if not CoAP_SPARK.Channel.Is_Valid (Skt) then
135-
CoAP_SPARK.Log.Put_Line
136-
("Connection problems.", CoAP_SPARK.Log.Error);
137-
RFLX.RFLX_Types.Free (Payload);
138-
FSM.Finalize (Ctx);
139-
pragma Assert (FSM.Uninitialized (Ctx));
140-
-- Main_Loop_Environment.Finalize (Ctx.E);
141-
-- pragma Assert (Main_Loop_Environment.Is_Finalized (Ctx.E));
142-
return;
143-
end if;
14479

14580
CoAP_SPARK.Server_Session.Run_Session_Loop (Ctx, Skt);
14681

@@ -154,17 +89,16 @@ procedure CoAP_Server is
15489
-- pragma Assert (Main_Loop_Environment.Is_Finalized (Ctx.E));
15590
end Run_Session;
15691

157-
Method : RFLX.CoAP.Method_Code := RFLX.CoAP.Get;
158-
Payload : CoAP_SPARK.Messages.Payload_Ptr := null;
159-
92+
Port : CoAP_SPARK.Channel.Port_Type := CoAP_SPARK.Default_Port;
16093
Argument_Index : Natural := 1;
16194

16295
Valid_Command_Line : Boolean := True;
96+
Is_Secure : Boolean := False;
16397
begin
16498

165-
if Ada.Command_Line.Argument_Count = 0
166-
or else Ada.Command_Line.Argument (1) = "-h"
167-
or else Ada.Command_Line.Argument (1) = "--help"
99+
if Ada.Command_Line.Argument_Count = 1
100+
and then (Ada.Command_Line.Argument (1) = "-h"
101+
or else Ada.Command_Line.Argument (1) = "--help")
168102
then
169103
Usage (Is_Failure => False);
170104
return;
@@ -181,45 +115,29 @@ begin
181115
return;
182116
end if;
183117

118+
-- Any other single command line argument is an error, since all remaining
119+
-- ones are an option and a value.
120+
if Ada.Command_Line.Argument_Count = 1 then
121+
Usage (Is_Failure => True);
122+
return;
123+
end if;
124+
184125
while Argument_Index < Ada.Command_Line.Argument_Count loop
185126

186-
if Ada.Command_Line.Argument (Argument_Index) = "-m" then
127+
if Ada.Command_Line.Argument (Argument_Index) = "-p" then
187128
Argument_Index := @ + 1;
188-
if CoAP_SPARK.Utils.Is_Valid_As_Method
129+
if CoAP_SPARK.Utils.Valid_Unsigned_16_Values.Is_Valid_As_Number
189130
(Ada.Command_Line.Argument (Argument_Index))
190131
then
191-
Method := CoAP_SPARK.Utils.Value
192-
(Ada.Command_Line.Argument (Argument_Index));
132+
Port :=
133+
CoAP_SPARK.Channel.Port_Type
134+
(CoAP_SPARK.Utils.Valid_Unsigned_16_Values.Value
135+
(Ada.Command_Line.Argument (Argument_Index)));
193136
else
194-
CoAP_SPARK.Log.Put_Line ("Invalid method", CoAP_SPARK.Log.Error);
137+
CoAP_SPARK.Log.Put_Line ("Invalid specified port", CoAP_SPARK.Log.Error);
195138
Valid_Command_Line := False;
196139
end if;
197140

198-
elsif Ada.Command_Line.Argument (Argument_Index) = "-e" then
199-
Argument_Index := @ + 1;
200-
declare
201-
Payload_Length : constant Natural :=
202-
Ada.Command_Line.Argument (Argument_Index)'Length;
203-
begin
204-
if Payload_Length = 0
205-
or else Payload_Length > CoAP_SPARK.Max_Payload_Length
206-
then
207-
CoAP_SPARK.Log.Put_Line
208-
("Payload too long", CoAP_SPARK.Log.Error);
209-
Valid_Command_Line := False;
210-
elsif Payload /= null then
211-
CoAP_SPARK.Log.Put_Line
212-
("Payload already provided", CoAP_SPARK.Log.Error);
213-
Valid_Command_Line := False;
214-
else
215-
Payload :=
216-
new Types.Bytes'(1 .. Types.Index (Payload_Length) => 0);
217-
CoAP_SPARK.Utils.Copy_String
218-
(Source => Ada.Command_Line.Argument (Argument_Index),
219-
Target => Payload.all);
220-
end if;
221-
end;
222-
223141
elsif Ada.Command_Line.Argument (Argument_Index) = "-v" then
224142
Argument_Index := @ + 1;
225143
if CoAP_SPARK.Utils.Valid_Natural_Values.Is_Valid_As_Number
@@ -251,16 +169,11 @@ begin
251169
Valid_Command_Line := False;
252170
end if;
253171

254-
elsif Ada.Command_Line.Argument (Argument_Index) = "-B" then
255-
-- This is allowed but not implemented. For compatibility to
256-
-- libcoap's coap-server, which uses this switch for specifying a
257-
-- timeout.
258-
Argument_Index := @ + 1;
259-
260172
elsif Ada.Command_Line.Argument (Argument_Index) = "-k"
261173
or else Ada.Command_Line.Argument (Argument_Index) = "-u"
262174
then
263175
Argument_Index := @ + 1;
176+
Is_Secure := True;
264177

265178
if Argument_Index = Ada.Command_Line.Argument_Count then
266179
CoAP_SPARK.Log.Put ("Missing argument for ", CoAP_SPARK.Log.Error);
@@ -277,37 +190,20 @@ begin
277190
Valid_Command_Line := False;
278191
end if;
279192

280-
if Valid_Command_Line and Argument_Index = Ada.Command_Line.Argument_Count
281-
then
282-
CoAP_SPARK.Log.Put_Line ("URI is missing", CoAP_SPARK.Log.Error);
283-
Valid_Command_Line := False;
284-
end if;
285-
286193
exit when not Valid_Command_Line;
287194

288195
pragma Loop_Invariant (Argument_Index < Ada.Command_Line.Argument_Count);
289196
Argument_Index := @ + 1;
290197
end loop;
291198

292-
if Ada.Command_Line.Argument (Argument_Index)'Length
293-
> CoAP_SPARK.Max_URI_Length
294-
then
295-
CoAP_SPARK.Log.Put_Line ("URI too long", CoAP_SPARK.Log.Error);
296-
Valid_Command_Line := False;
297-
end if;
298-
299199
if not Valid_Command_Line then
300-
RFLX.RFLX_Types.Free (Payload);
301200
Usage;
302201
return;
303202
end if;
304203

305204
Run_Session
306-
(URI_String => Ada.Command_Line.Argument (Argument_Index),
307-
Method => Method,
308-
Payload => Payload);
309-
310-
RFLX.RFLX_Types.Free (Payload);
205+
(Port => Port,
206+
Is_Secure => Is_Secure);
311207

312208
-- This has no effect, but it is needed to avoid a linking error with
313209
-- SPARKLib in the validation profile.

server/src/secure_server.adb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,19 @@ is
9292
end;
9393
end PSK_Server_Callback;
9494

95-
procedure Initialize (Socket : out CoAP_SPARK.Channel.Socket_Type)
96-
with SPARK_Mode => Off
97-
-- Access to function with side effects is not allowed in SPARK
95+
procedure Initialize
96+
(Socket : out CoAP_SPARK.Channel.Socket_Type;
97+
Port : CoAP_SPARK.Channel.Port_Type)
98+
with
99+
SPARK_Mode => Off
100+
-- Access to function with side effects is not allowed in SPARK
98101
is
99102
begin
100103
CoAP_SPARK.Channel.Initialize
101104
(Socket => Socket,
102-
PSK_Server_Callback => Secure_Server.PSK_Server_Callback'Access);
105+
PSK_Server_Callback => Secure_Server.PSK_Server_Callback'Access,
106+
Port => Port,
107+
Server => True);
103108
end Initialize;
104109

105110
end Secure_Server;

server/src/secure_server.ads

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ is
2626
Convention => C,
2727
Side_Effects;
2828

29-
procedure Initialize (Socket : out CoAP_SPARK.Channel.Socket_Type) with
30-
Global =>
31-
null;
29+
procedure Initialize
30+
(Socket : out CoAP_SPARK.Channel.Socket_Type;
31+
Port : CoAP_SPARK.Channel.Port_Type)
32+
with Global => null;
3233

3334
end Secure_Server;

0 commit comments

Comments
 (0)