polyorb_hi-transport_low_level_sockets.adb 14 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
36
37
38
39
40
41
42
43
44
45
46
47
48
--                                                                          --
------------------------------------------------------------------------------

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

49
50
51
52
53
54
package body PolyORB_HI.Transport_Low_Level
  with Spark_Mode => Off
  --  SPARK_Mode is disabled for this unit, it relies on OS-specific
  --  libraries. We discard this unit for now.

is
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

   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
121
         if Length (Naming_Table (J).Location) = 0
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
           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 "
164
                        + Image (Nodes (My_Node).Address)));
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
      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,
185
                      "Try to connect to " + Image (Nodes (N).Address)));
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

                  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,
204
                             "Connected to " + Image (Nodes (N).Address)));
205
206
207
208
209
210
         end if;
      end loop;

      Suspend_Until_True (Other_Nodes_Init);

      pragma Debug (Put_Line (Verbose, "Initialization of socket subsystem"
211
                              + " is complete"));
212
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

      --  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 "
250
                        + Image (Nodes (My_Node).Address)));
251
252
253
254
255
256
257
258
259
260
261
262
263
264

         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

265
               Node := Corresponding_Node (Unsigned_8 (SEC (SEC'First)));
266
267
               Nodes (Node).Socket_Receive := Socket;
               pragma Debug (Put_Line (Verbose, "Connection from node "
268
                                       + Node_Image (Node)));
269
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

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

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

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

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

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

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

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

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

end PolyORB_HI.Transport_Low_Level;