summaryrefslogtreecommitdiffstats
path: root/pingpong/pingpong.adb
blob: f33814f136082214e6593f12566871959dcf518a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
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
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
204
205
206
207
208
209
210
211
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
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
279
280
281
282
283
284
285
286
287
288
289
290
with Ada.Text_IO;
with Ada.Exceptions; use Ada.Exceptions;
with GNAT.Sockets;   use GNAT.Sockets;

procedure PingPong is

   Group : constant String := "239.255.128.128";
   --  Multicast groupe: administratively scoped IP address
   --
   task Pong is
      entry Start;
      entry Stop;
   end Pong;

   task body Pong is
      Address  : Sock_Addr_Type;
      Server   : Socket_Type;
      Socket   : Socket_Type;
      Channel  : Stream_Access;

   begin
      accept Start;
      --
      --  Get an Internet address of a host (here the local host name).
      --  Note that a host can have several addresses. Here we get
      --  the first one which is supposed to be the official one.
      --
      Ada.Text_IO.Put_Line ("PONG: Get_Host_By_Name" & Host_Name);
      Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
      Ada.Text_IO.Put_Line ("PONG: Back from Get_Host_By_Name");

      --
      --  Get a socket address that is an Internet address and a port
      --
      Address.Port := 5432;
      --
      --  The first step is to create a socket. Once created, this
      --  socket must be associated to with an address. Usually only
      --  a server (Pong here) needs to bind an address explicitly.
      --  Most of the time clients can skip this step because the
      --  socket routines will bind an arbitrary address to an unbound
      --  socket.
      --
      Create_Socket (Server);
      --
      --  Allow reuse of local addresses.
      --
      Set_Socket_Option
        (Server,
         Socket_Level,
         (Reuse_Address, True));

      Bind_Socket (Server, Address);
      --
      --  A server marks a socket as willing to receive connect events.
      --
      Listen_Socket (Server);
      --
      --  Once a server calls Listen_Socket, incoming connects events
      --  can be accepted. The returned Socket is a new socket that
      --  represents the server side of the connection. Server remains
      --  available to receive further connections.
      --
      Accept_Socket (Server, Socket, Address);
      --
      --  Return a stream associated to the connected socket.
      --
      Channel := Stream (Socket);
      --
      --  Force Pong to block
      --
      delay 0.2;
      --
      --  Receive and print message from client Ping.
      --
      declare
         Message : String := String'Input (Channel);
      begin
         Ada.Text_IO.Put_Line (Message);
         --
         --  Send same message to server Pong.
         --
         String'Output (Channel, Message);
      end;

      Close_Socket (Server);
      Close_Socket (Socket);
      --
      --  Part of the multicast example
      --
      --  Create a datagram socket to send connectionless, unreliable
      --  messages of a fixed maximum length.
      --
      Create_Socket (Socket, Family_Inet, Socket_Datagram);
      --
      --  Allow reuse of local addresses.
      --
      Set_Socket_Option
        (Socket,
         Socket_Level,
         (Reuse_Address, True));
      --
      --  Join a multicast group.
      --
      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));
      --
      --  Controls the live time of the datagram to avoid it being
      --  looped forever due to routing errors. Routers decrement
      --  the TTL of every datagram as it traverses from one network
      --  to another and when its value reaches 0 the packet is
      --  dropped. Default is 1.
      --
      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Multicast_TTL, 1));
      --
      --  Want the data you send to be looped back to your host.
      --
      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Multicast_Loop, True));
      --
      --  If this socket is intended to receive messages, bind it to a
      --  given socket address.
      --
      Address.Addr := Any_Inet_Addr;
      Address.Port := 55505;

      Bind_Socket (Socket, Address);
      --
      --  If this socket is intended to send messages, provide the
      --  receiver socket address.
      --
      Address.Addr := Inet_Addr (Group);
      Address.Port := 55506;

      Channel := Stream (Socket, Address);
      --
      --  Receive and print message from client Ping.
      --
      declare
         Message : String := String'Input (Channel);

      begin
         --
         --  Get the address of the sender.
         --
         Address := Get_Address (Channel);
         Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
         --
         --  Send same message to server Pong.
         --
         String'Output (Channel, Message);
      end;

      Close_Socket (Socket);

      accept Stop;

   exception when E : others =>
      Ada.Text_IO.Put_Line
        (Exception_Name (E) & ": " & Exception_Message (E));
   end Pong;

   task Ping is
      entry Start;
      entry Stop;
   end Ping;

   task body Ping is
      Address  : Sock_Addr_Type;
      Socket   : Socket_Type;
      Channel  : Stream_Access;

   begin
      accept Start;
   --
   --  See comments in Ping section for the first steps.
   --
      Ada.Text_IO.Put_Line ("PING: Get_Host_By_Name" & Host_Name);
      Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
      Ada.Text_IO.Put_Line ("PING: Back from Get_Host_By_Name");
      Address.Port := 5432;
      Create_Socket (Socket);

      Set_Socket_Option
        (Socket,
         Socket_Level,
         (Reuse_Address, True));
      --
      --  Force Pong to block
      --
      delay 0.2;
      --
      --  If the client's socket is not bound, Connect_Socket will
      --  bind to an unused address. The client uses Connect_Socket to
      --  create a logical connection between the client's socket and
      --  a server's socket returned by Accept_Socket.
      --
      Connect_Socket (Socket, Address);

      Channel := Stream (Socket);
      --
      --  Send message to server Pong.
      --
      String'Output (Channel, "Hello world");
      --
      --  Force Ping to block
      --
      delay 0.2;
      --
      --  Receive and print message from server Pong.
      --
      Ada.Text_IO.Put_Line (String'Input (Channel));
      Close_Socket (Socket);
      --
      --  Part of multicast example. Code similar to Pong's one.
      --
      Create_Socket (Socket, Family_Inet, Socket_Datagram);

      Set_Socket_Option
        (Socket,
         Socket_Level,
         (Reuse_Address, True));

      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Add_Membership, Inet_Addr (Group), Any_Inet_Addr));

      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Multicast_TTL, 1));

      Set_Socket_Option
        (Socket,
         IP_Protocol_For_IP_Level,
         (Multicast_Loop, True));

      Address.Addr := Any_Inet_Addr;
      Address.Port := 55506;

      Bind_Socket (Socket, Address);

      Address.Addr := Inet_Addr (Group);
      Address.Port := 55505;

      Channel := Stream (Socket, Address);
      --
      --  Send message to server Pong.
      --
      String'Output (Channel, "Hello world");
      --
      --  Receive and print message from server Pong.
      --
      declare
         Message : String := String'Input (Channel);

      begin
         Address := Get_Address (Channel);
         Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
      end;

      Close_Socket (Socket);

      accept Stop;

   exception when E : others =>
      Ada.Text_IO.Put_Line
        (Exception_Name (E) & ": " & Exception_Message (E));
   end Ping;
   
begin
   --  Indicate whether the thread library provides process
   --  blocking IO. Basically, if you are not using FSU threads
   --  the default is ok.
   --
   Initialize (Process_Blocking_IO => False);
   Ping.Start;
   Pong.Start;
   Ping.Stop;
   Pong.Stop;
   Finalize;
end PingPong;