polyorb_hi-transport_low_level_sockets.adb 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                          PolyORB HI COMPONENTS                           --
--                                                                          --
--       P O L Y O R B _ H I . T R A N S P O R T _ L O W _ L E V E L        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--    Copyright (C) 2006-2009 Telecom ParisTech, 2010-2015 ESA & ISAE.      --
--                                                                          --
-- 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/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27
28
--              PolyORB-HI/Ada is maintained by the TASTE project           --
--                      (taste-users@lists.tuxfamily.org)                   --
29
30
31
--                                                                          --
------------------------------------------------------------------------------

32
33
34
35
pragma SPARK_Mode (Off);
--  SPARK_Mode is disabled for this unit, it relies on OS-specific
--  libraries. We discard this unit for now.

36
37
38
39
40
41
42
43
44
45
with Ada.Synchronous_Task_Control;
with Ada.Exceptions;
with Ada.Real_Time;
with Ada.Streams;
with Interfaces;
with Ada.Unchecked_Conversion;

with GNAT.Sockets;

with PolyORB_HI.Output;
46
47
pragma Elaborate_All (PolyORB_HI.Output);

48
49
50
51
52
53
54
with PolyORB_HI.Messages;
with PolyORB_HI.Utils;
with PolyORB_HI_Generated.Naming;
with PolyORB_HI_Generated.Transport;

--  Transport low-level service of PolyORB HI, using BSD sockets

55
package body PolyORB_HI.Transport_Low_Level is
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

   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.Synchronous_Task_Control;
   use Ada.Real_Time;
   use Interfaces;
   use GNAT.Sockets;
   use PolyORB_HI.Messages;
   use PolyORB_HI.Utils;
   use PolyORB_HI_Generated.Naming;
   use PolyORB_HI.Output;

   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;

   Local_Node_Init : Suspension_Object;
   --  Used to indicate to the handler task that the part of the
   --  initialization is complete and that it can wait for the
   --  connections of the other nodes

   Other_Nodes_Init : Suspension_Object;
   --  Used to indicate to the main task that all the other nodes have
   --  reached their 'Local_Node_Init' point and that it can now try
   --  to connect to them to get their host information.

   Full_Init : Suspension_Object;
   --  Used to indicate to all the tasks in the distributed
   --  application that the initialization process is complete.

   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 is
      SEC       : AS_One_Element_Stream;
      SEO       : AS.Stream_Element_Offset;
      Next_Time : Time;
   begin
      pragma Warnings (Off);
      --  XXX shutdown warning on this now obscoleted function
      GNAT.Sockets.Initialize;
      pragma Warnings (On);

      for J in Naming_Table'Range loop
122
         if Length (Naming_Table (J).Location) = 0
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
           or else Naming_Table (J).Port = 0
         then
            Nodes (J).Address := (GNAT.Sockets.Family_Inet,
                                  GNAT.Sockets.No_Inet_Addr,
                                  GNAT.Sockets.No_Port);
         else
            Nodes (J).Address := (GNAT.Sockets.Family_Inet,
                                  GNAT.Sockets.Inet_Addr
                                    (PolyORB_HI.Utils.To_String
                                       (Naming_Table (J).Location)),
                                  GNAT.Sockets.Port_Type
                                    (Naming_Table (J).Port));
         end if;
      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 "
165
                        + Image (Nodes (My_Node).Address)));
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
      end if;

      --  Start the protocol handler task

      Set_True (Local_Node_Init);

      --  Connect to other nodes and send my node id

      for N in Nodes'Range loop
         if N /= My_Node
           and then Nodes (N).Address.Addr /= No_Inet_Addr
         then
            loop
               Create_Socket (Nodes (N).Socket_Send);

               Next_Time := Clock + Milliseconds (200);
               begin
                  pragma Debug
                    (Put_Line
                     (Verbose,
186
                      "Try to connect to " + Image (Nodes (N).Address)));
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

                  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

            SEC (1) := AS.Stream_Element (Internal_Code (My_Node));
            Send_Socket (Nodes (N).Socket_Send, SEC, SEO);

            pragma Debug (Put_Line
                            (Verbose,
205
                             "Connected to " + Image (Nodes (N).Address)));
206
207
208
209
210
211
         end if;
      end loop;

      Suspend_Until_True (Other_Nodes_Init);

      pragma Debug (Put_Line (Verbose, "Initialization of socket subsystem"
212
                              + " is complete"));
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

      --  Unblock the receiver task

      Set_True (Full_Init);
   end Initialize;

   task Receiver_Task is
   end Receiver_Task;
   --  Receive all the incoming messages

   -------------------
   -- Receiver_Task --
   -------------------

   task body Receiver_Task is
      use type AS.Stream_Element_Offset;

      SEC       : AS_One_Element_Stream;
      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;
      Node      : Node_Type;
      Socket    : Socket_Type;
      Address   : Sock_Addr_Type;
   begin
      Suspend_Until_True (Local_Node_Init);

      --  Wait for the connection of all the other nodes. We do this
      --  in the handler task to avoid deadlock when there is a
      --  connection cycle between processes.

      if Nodes (My_Node).Address.Addr /= No_Inet_Addr then
         pragma Debug (Put_Line
                       (Verbose, "Waiting on "
251
                        + Image (Nodes (My_Node).Address)));
252
253
254
255
256
257
258
259
260
261
262
263
264
265

         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);

               if SEO = SEC'First - 1 then
                  Put_Line ("Could not initialize socket");
               end if;

               --  Identify peer node

266
               Node := Corresponding_Node (Unsigned_8 (SEC (SEC'First)));
267
268
               Nodes (Node).Socket_Receive := Socket;
               pragma Debug (Put_Line (Verbose, "Connection from node "
269
                                       + Node_Image (Node)));
270
271
272
273
274
275
276
277
278
279
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

            end if;
         end loop;
      end if;

      Set_True (Other_Nodes_Init);

      Suspend_Until_True (Full_Init);

      --  Wait on several descriptors at a time

      Create_Selector (Selector);
      Empty (W_Sockets);

      Main_Loop :
      loop

         --  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);
            end if;
         end loop;

         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);
311
312
               pragma Debug (Put_Line (Verbose, "SEL " + SEL'Length'Img
                                         + "MLS" + Message_Length_Size'Img));
313
               --  Receiving zero byte means that peer is dead
314
315
316
317

               if SEO = 0 then
                  pragma Debug (Put_Line
                                  (Verbose,
318
319
                                   "Node " + Node_Image (N)
                                     + " is dead"));
320
321
322
323
324
325
326
327
                  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"
328
329
                                + AS.Stream_Element_Offset'Image (SEO)
                                + " bytes from node " + Node_Image (N)));
330
331
332
333
334
335
336
337
338
339
340
341

               --  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
342
                  (Unsigned_8 (SEA (Message_Length_Size + 1))),
343
344
345
346
347
348
349
350
351
352
                  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 "
353
                        + Ada.Exceptions.Exception_Name (E)));
354
355
         pragma Debug (Put_Line
                       (Normal, "Message "
356
                        + Ada.Exceptions.Exception_Message (E)));
357
358
359
360
361
362
363
364
365
366
367
368
369
         null;
   end Receiver_Task;

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

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

370
371
372
373
374
      --  We cannot cast both array types using 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.
375
376
377
378
379
      Msg : AS.Stream_Element_Array (1 .. Message'Length);
      pragma Import (Ada, Msg);
      for Msg'Address use Message'Address;

   begin
380
      pragma Debug (Put_Line (Verbose, "Sending message, size:" + L'Img));
381
382
      Send_Socket (Nodes (Node).Socket_Send, Msg, L);

383
384
385
386
387
      return Error_Kind'(Error_None);
   exception
      when E : others =>
         pragma Debug (Put_Line
                       (Normal, "Exception "
388
                          + Ada.Exceptions.Exception_Name (E)));
389
390
         pragma Debug (Put_Line
                       (Normal, "Message "
391
                          + Ada.Exceptions.Exception_Message (E)));
392
393
394
395
      return Error_Kind'(Error_Transport);
   end Send;

end PolyORB_HI.Transport_Low_Level;