-
+ ABF3A0700182A7622A7468465C37F038B72818DDD9500EFE683A923E7CA8F020C08A236BFDB0BCE0C0D65FBC2425D90251688E21B834B1EF8EF5982C6F94071C
smg_comms/src/test_comms.adb
(0 . 0)(1 . 87)
173 -- S.MG, 2018
174 -- prototype implementation of S.MG communication protocol
175
176 with GNAT.Sockets; use GNAT.Sockets;
177 with Ada.Text_IO; use Ada.Text_IO;
178 with Ada.Streams; use Ada.Streams;
179 with Interfaces; use Interfaces;
180
181 with SMG_comms_types; use SMG_comms_types;
182
183 procedure test_comms is
184 Port_No : constant := 2222;
185
186 task type Client is
187 entry Send;
188 end Client;
189
190 task type Server is
191 entry Listen;
192 entry Ready;
193 end Server;
194
195 task body Client is
196 Sock: Socket_Type;
197 Address: Sock_Addr_Type;
198 Data: Ada.Streams.Stream_Element_Array(1..10) := (others => 42);
199 Last: Ada.Streams.Stream_Element_Offset;
200 N : Integer_8 := -36;
201 begin
202 accept Send; -- task WILL block here until asked to send
203 Address.Port := Port_No;
204 Address.Addr := Inet_Addr("127.0.0.1");
205 Create_Socket(Sock, Family_Inet, Socket_Datagram);
206
207 ToNetworkFormat( N, Data(1..1));
208
209 Send_Socket(Sock, Data, Last, Address);
210 Put_Line("Client sent data " & "last: " & Last'Img);
211 end Client;
212
213 task body Server is
214 Sock: Socket_Type;
215 Address, From: Sock_Addr_Type;
216 Data: Ada.Streams.Stream_Element_Array(1..512);
217 Last: Ada.Streams.Stream_Element_Offset;
218 N : Integer_8;
219 begin
220 accept Listen; -- wait to be started!
221 Put_Line("Server started!");
222 -- create UDP socket
223 Create_Socket( Sock, Family_Inet, Socket_Datagram );
224
225 -- set options on UDP socket
226 Set_Socket_Option( Sock, Socket_Level, (Reuse_Address, True));
227 Set_Socket_Option( Sock, Socket_Level, (Receive_Timeout, Timeout => 10.0));
228
229 -- set address and bind
230 Address.Addr := Any_Inet_Addr;
231 Address.Port := Port_No;
232 Bind_Socket( Sock, Address );
233
234 accept Ready; -- server IS ready, when here
235 -- receive on socket
236 begin
237 Receive_Socket( Sock, Data, Last, From );
238 Put_Line("last: " & Last'Img);
239 Put_Line("from: " & Image(From.Addr));
240 Put_Line("data is:");
241 for I in Data'First .. Last loop
242 FromNetworkFormat(Data(I..I), N);
243 Put_Line(N'Image);
244 end loop;
245 exception
246 when Socket_Error =>
247 Put_Line("Socket error! (timeout?)");
248 end; -- end of receive
249
250 end Server;
251
252 S: Server;
253 C: Client;
254 begin
255 S.Listen;
256 S.Ready; -- WAIT for server to be ready!
257 C.Send; -- client is started only after server!
258 end test_comms;
259