polyorb_hi_drivers_native_tcp_ip.adb 15 KB
Newer Older
jhugues's avatar
jhugues committed
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                          PolyORB HI COMPONENTS                           --
--                                                                          --
--     P O L Y O R B _ H I _ D R I V E R S _ N A T I V E _ T C P _ I P      --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
yoogx's avatar
yoogx committed
9
--                   Copyright (C) 2012-2015 ESA & ISAE.                    --
jhugues's avatar
jhugues committed
10
--                                                                          --
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- PolyORB-HI is free software; you can redistribute it and/or modify under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. PolyORB-HI 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.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
jhugues's avatar
jhugues committed
26
27
28
29
30
31
--                                                                          --
--              PolyORB-HI/Ada is maintained by the TASTE project           --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

32
33
34
35
36
with Ada.Streams;
with Ada.Exceptions;
with Ada.Real_Time;
with Interfaces;
with Ada.Unchecked_Conversion;
37
with System;
38
39
40
41

with GNAT.Sockets;

with PolyORB_HI.Messages;
42
with PolyORB_HI.Output;
43
44
45

with PolyORB_HI_Generated.Transport;

46
47
with POHICDRIVER_IP;

48
49
50
--  This package provides support for the TCP_IP device driver as
--  defined in the tcp_protocol.aadl AADLv2 model.

yoogx's avatar
yoogx committed
51
52
53
package body PolyORB_HI_Drivers_Native_TCP_IP
  with SPARK_Mode => Off
is
54
55
56
57
58
59
60
61
62

   pragma Suppress (Elaboration_Check, PolyORB_HI_Generated.Transport);
   --  We do not want a pragma Elaborate_All to be implicitely
   --  generated for Transport.

   package AS renames Ada.Streams;

   use Ada.Real_Time;
   use Interfaces;
63
   use System;
64
65
66
67
68
   use GNAT.Sockets;
   use PolyORB_HI.Messages;
   use PolyORB_HI.Utils;
   use PolyORB_HI.Output;

69
70
71
72
73
74
   use POHICDRIVER_IP;

   type IP_Conf_T_Acc is access all POHICDRIVER_IP.IP_Conf_T;
   function To_IP_Conf_T_Acc is new Ada.Unchecked_Conversion
     (System.Address, IP_Conf_T_Acc);

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
   type Node_Record is record
      Address        : Sock_Addr_Type;
      Socket_Send    : Socket_Type;
      Socket_Receive : Socket_Type;
   end record;

   Nodes : array (Node_Type) of Node_Record;

   subtype AS_One_Element_Stream is AS.Stream_Element_Array (1 .. 1);
   subtype AS_Message_Length_Stream is AS.Stream_Element_Array
     (1 .. Message_Length_Size);
   subtype Message_Length_Stream is Stream_Element_Array
     (1 .. Message_Length_Size);
   subtype AS_Full_Stream is AS.Stream_Element_Array (1 .. PDU_Size);
   subtype Full_Stream is Stream_Element_Array (1 .. PDU_Size);

   function To_PO_HI_Message_Length_Stream is new Ada.Unchecked_Conversion
     (AS_Message_Length_Stream, Message_Length_Stream);
   function To_PO_HI_Full_Stream is new Ada.Unchecked_Conversion
     (AS_Full_Stream, Full_Stream);

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (Name_Table : PolyORB_HI.Utils.Naming_Table_Type) is
      SEC       : AS_One_Element_Stream;
      SEO       : AS.Stream_Element_Offset;
      Next_Time : Time;
   begin
      pragma Warnings (Off);
      GNAT.Sockets.Initialize;
      --  XXX shutdown warning on this now obscoleted function
      pragma Warnings (On);

      for J in Name_Table'Range loop
yoogx's avatar
yoogx committed
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
         if Name_Table (J).Variable = System.Null_Address then
            if Length (Name_Table (J).Location) = 0 then
               Nodes (J).Address := (GNAT.Sockets.Family_Inet,
                                     GNAT.Sockets.No_Inet_Addr,
                                     GNAT.Sockets.No_Port);
            else
               --  The structure of the location information is
               --  "ip address port"

               declare
                  S : constant String := PolyORB_HI.Utils.To_String
                    (Name_Table (J).Location);
                  Addr_First, Addr_Last : Integer;
                  Port : Integer;

                  First : Integer;
                  Last : Integer;
               begin
                  First := S'First;

                  --  First parse the prefix "ip"

                  Last := Parse_String (S, First, ' ');

                  if S (First .. Last) /= "ip" then
                     raise Program_Error with "Invalid configuration";
                  end if;

                  --  Then, parse the address

                  First := Last + 2;
                  Last := Parse_String (S, First, ' ');
                  Addr_First := First;
                  Addr_Last := Last;

                  --  Finally the port

                  First := Last + 2;
                  Last := Parse_String (S, First, ' ');
                  Port := Integer'Value (S (First .. Last));

                  Nodes (J).Address := (GNAT.Sockets.Family_Inet,
                                        GNAT.Sockets.Inet_Addr
                                          (S (Addr_First .. Addr_Last)),
                                        GNAT.Sockets.Port_Type (Port));
               end;
            end if;
         else
            declare
               Configuration : constant IP_Conf_T
                 := To_IP_Conf_T_Acc (Name_Table (J).Variable).all;
            begin
               Nodes (J).Address :=
                 (GNAT.Sockets.Family_Inet,
                  GNAT.Sockets.Inet_Addr (Configuration.Address),
                  GNAT.Sockets.Port_Type (Configuration.Port));
            end;
         end if;
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
      end loop;

      --  Create the local socket if the node is remote-callable

      if Nodes (My_Node).Address.Addr /= No_Inet_Addr then
         Create_Socket (Nodes (My_Node).Socket_Receive);
         Set_Socket_Option
           (Nodes (My_Node).Socket_Receive,
            Socket_Level,
            (Reuse_Address, True));

         --  Since we always send small bursts of data and we do not
         --  get reponse (all communications are asynchronous), we
         --  disable the TCP "Nagle" algorithm and send ACK packets
         --  immediately.

         Set_Socket_Option
           (Nodes (My_Node).Socket_Receive,
            IP_Protocol_For_TCP_Level,
            (No_Delay, True));

         Bind_Socket
           (Nodes (My_Node).Socket_Receive,
            Nodes (My_Node).Address);
         Listen_Socket (Nodes (My_Node).Socket_Receive);

         pragma Debug (Put_Line
                       (Verbose,
                        "Local socket created for "
                        & Image (Nodes (My_Node).Address)));
      end if;

      --  Connect to other nodes and send my node id

      for N in Nodes'Range loop
204
205
206
         if N /= My_Node
           and then Nodes (N).Address.Addr /= No_Inet_Addr
         then
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
            loop
               Create_Socket (Nodes (N).Socket_Send);

               Next_Time := Clock + Milliseconds (200);
               begin
                  pragma Debug
                    (Put_Line
                     (Verbose,
                      "Try to connect to " & Image (Nodes (N).Address)));

                  delay until Next_Time;

                  Connect_Socket (Nodes (N).Socket_Send, Nodes (N).Address);
                  exit;
               exception
                  when Socket_Error =>
                     Close_Socket (Nodes (N).Socket_Send);
               end;
            end loop;

            --  Send my node number

229
            SEC (1) := AS.Stream_Element (Internal_Code (My_Node));
230
231
232
233
234
235
236
237
            Send_Socket (Nodes (N).Socket_Send, SEC, SEO);

            pragma Debug (Put_Line
                            (Verbose,
                             "Connected to " & Image (Nodes (N).Address)));
         end if;
      end loop;

238
      Initialize_Receiver;
239
240
      pragma Debug (Put_Line (Verbose, "Initialization of socket subsystem"
                              & " is complete"));
241
242
243
244
245
246
247
248
249
   exception
      when E : others =>
         pragma Debug (Put_Line
                       (Normal, "Exception "
                        & Ada.Exceptions.Exception_Name (E)));
         pragma Debug (Put_Line
                       (Normal, "Message "
                        & Ada.Exceptions.Exception_Message (E)));
      null;
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
   end Initialize;

   -------------------------
   -- Initialize_Receiver --
   -------------------------

   procedure Initialize_Receiver is
      SEC       : AS_One_Element_Stream;
      SEO       : AS.Stream_Element_Offset;
      Node      : Node_Type;
      Socket    : Socket_Type;
      Address   : Sock_Addr_Type;
   begin
      --  Wait for the connection of all the other nodes.

      if Nodes (My_Node).Address.Addr /= No_Inet_Addr then
         pragma Debug (Put_Line
                       (Verbose, "Waiting on "
                        & Image (Nodes (My_Node).Address)));

         for N in Nodes'Range loop
            if N /= My_Node then
               Address := No_Sock_Addr;
               Socket  := No_Socket;
               Accept_Socket (Nodes (My_Node).Socket_Receive, Socket, Address);
               Receive_Socket (Socket, SEC, SEO);

               --  Identify peer node

279
               Node := Corresponding_Node (Unsigned_8 (SEC (SEC'First)));
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
               Nodes (Node).Socket_Receive := Socket;
               pragma Debug (Put_Line (Verbose, "Connection from node "
                                       & Node_Type'Image (Node)));
            end if;
         end loop;
      end if;
   end Initialize_Receiver;

   -------------
   -- Receive --
   -------------

   procedure Receive is
      use type AS.Stream_Element_Offset;

      SEL       : AS_Message_Length_Stream;
      SEA       : AS_Full_Stream;
      SEO       : AS.Stream_Element_Offset;
      R_Sockets : Socket_Set_Type;
      W_Sockets : Socket_Set_Type;
      Selector  : Selector_Type;
      Status    : Selector_Status;
   begin
      --  Wait on several descriptors at a time

      Create_Selector (Selector);
      Empty (W_Sockets);

      Main_Loop :
      loop
         pragma Debug (Put_Line (Verbose, "****"));

         --  Build the socket descriptor set

         Empty (R_Sockets);
         for N in Node_Type'Range loop
            if N /= My_Node then
               Set (R_Sockets, Nodes (N).Socket_Receive);
318
319
               pragma Debug (Put_Line (Verbose, "Will wait on " &
                                       Image (Nodes (N).Address)));
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
            end if;
         end loop;

         Put_Line ("Using user-provided TCP/IP stack to receive");
         Check_Selector (Selector, R_Sockets, W_Sockets, Status);

         for N in Node_Type'Range loop
            pragma Debug (Put_Line (Verbose, "Check mailboxes"));

            --  If there is something to read on a node different from
            --  the current node.

            if N /= My_Node
              and then Is_Set (R_Sockets, Nodes (N).Socket_Receive)
            then
               --  Receive message length

               Receive_Socket (Nodes (N).Socket_Receive, SEL, SEO);

               --  Receive zero bytes means that peer is dead

               if SEO = 0 then
                  pragma Debug (Put_Line
                                  (Verbose,
                                   "Node " & Node_Type'Image (N)
                                   & " is dead"));
                  exit Main_Loop;
               end if;

               SEO := AS.Stream_Element_Offset
                 (To_Length (To_PO_HI_Message_Length_Stream (SEL)));
               pragma Debug (Put_Line
                               (Verbose,
                                "received"
                                & AS.Stream_Element_Offset'Image (SEO)
                                & " bytes from node " & Node_Type'Image (N)));

               --  Get the message and preserve message length to keep
               --  compatible with a local message delivery.

               SEA (1 .. Message_Length_Size) := SEL;
               Receive_Socket (Nodes (N).Socket_Receive,
                               SEA (Message_Length_Size + 1 .. SEO + 1), SEO);

               --  Deliver to the peer handler

               PolyORB_HI_Generated.Transport.Deliver
                 (Corresponding_Entity
368
                  (Unsigned_8 (SEA (Message_Length_Size + 1))),
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
                  To_PO_HI_Full_Stream (SEA)
                    (1 .. Stream_Element_Offset (SEO)));
            end if;
         end loop;
      end loop Main_Loop;

   exception
      when E : others =>
         pragma Debug (Put_Line
                       (Normal, "Exception "
                        & Ada.Exceptions.Exception_Name (E)));
         pragma Debug (Put_Line
                       (Normal, "Message "
                        & Ada.Exceptions.Exception_Message (E)));
      null;
   end Receive;

   ----------
   -- Send --
   ----------

   function Send
     (Node    : Node_Type;
      Message : Stream_Element_Array;
      Size    : Stream_Element_Offset)
     return Error_Kind
   is
      L : AS.Stream_Element_Offset;
      pragma Unreferenced (L);

      --  Note: we cannot cast both array types using
      --  Ada.Unchecked_Conversion because they are unconstrained
      --  types. We cannot either use direct casting because component
      --  types are incompatible. The only time efficient manner to do
      --  the casting is to use representation clauses.
      Msg : AS.Stream_Element_Array (1 .. AS.Stream_Element_Offset (Size));
      pragma Import (Ada, Msg);
      for Msg'Address use Message'Address;

   begin
      Put_Line ("Using user-provided TCP/IP stack to send");
      Send_Socket (Nodes (Node).Socket_Send, Msg, L);
      return Error_Kind'(Error_None);
   exception
      when E : others =>
         pragma Debug (Put_Line
                       (Normal, "Exception "
                          & Ada.Exceptions.Exception_Name (E)));
         pragma Debug (Put_Line
                       (Normal, "Message "
                          & Ada.Exceptions.Exception_Message (E)));
      return Error_Kind'(Error_Transport);
   end Send;

423
end PolyORB_HI_Drivers_Native_TCP_IP;