Add initial bits for an epoll-based echoserver test scenario

This commit is contained in:
R. Tyler Croy 2011-02-17 22:59:25 -08:00
parent 59a8f0433a
commit 8fac9b2f9e
7 changed files with 134 additions and 0 deletions

View File

@ -22,6 +22,7 @@ pre:
mkdir -p obj/debug obj/release
test: pre lib
for d in experiments/*; do echo "> $$d"; (cd $$d && make); done
for d in tests/*; do echo "> $$d"; (cd $$d && make run); done
.PHONY: syntax lib test

View File

@ -0,0 +1,19 @@
GPRBUILD=gprbuild
GPRCLEAN=gprclean
PROJECTGPR=echoserver.gpr
build: pre
$(GPRBUILD) -p $(PROJECTGPR)
syntax: pre lib
gnatmake -gnatc -gnat05 -P $(PROJECTGPR)
clean: pre
$(GPRCLEAN) $(PROJECTGPR)
rm -rf ob
pre:
mkdir -p obj
.PHONY: syntax build

View File

@ -0,0 +1,12 @@
private with Ada.Text_IO;
private with GNAT.Sockets;
private with Async.Epoll;
use Ada.Text_IO;
use GNAT.Sockets;
procedure Call_Me_Back (Sock : Socket_Type; Ctx : Async.Epoll.Context_Type) is
begin
Put_Line (">>> Starting Call_Me_Back");
end Call_Me_Back;

View File

@ -0,0 +1,14 @@
with Ada.Text_IO,
GNAT.Sockets,
Async.Epoll;
use Ada.Text_IO;
package body Echo is
procedure Call_Me_Back (Sock : GNAT.Sockets.Socket_Type;
Ctx : Async.Epoll.Context_Type) is
begin
Put_Line (">>> Starting Call_Me_Back");
end Call_Me_Back;
end Echo;

View File

@ -0,0 +1,7 @@
with GNAT.Sockets;
with Async.Epoll;
package Echo is
procedure Call_Me_Back (Sock : GNAT.Sockets.Socket_Type;
Ctx : Async.Epoll.Context_Type);
end Echo;

View File

@ -0,0 +1,67 @@
--
-- Echo server!
private with Ada.Text_IO;
private with Ada.Streams;
private with GNAT.Sockets;
private with Async.Epoll;
private with Echo;
use Ada.Text_IO;
use Ada.Streams;
use GNAT.Sockets;
procedure echoserver is
ServerSock : Socket_Type;
ClientSock : Socket_Type;
ServerAddr : Sock_Addr_Type;
Listen_Addr : constant String := "127.0.0.1";
Listen_Port : constant Integer := 2046;
The_Hub : Async.Epoll.Hub := Async.Epoll.Create;
begin
Initialize; -- Initialize the GNAT.Sockets library
ServerAddr.Addr := Inet_Addr (Listen_Addr);
ServerAddr.Port := Port_Type (Listen_Port);
Create_Socket (ServerSock);
Set_Socket_Option (ServerSock, Socket_Level, (Reuse_Address, True));
Put_Line (">>> Starting echo server on port" & Integer'Image (Listen_Port) & " ...");
Bind_Socket (ServerSock, ServerAddr);
Put_Line (".. bound to socket");
Listen_Socket (ServerSock);
Put_Line (".. listening for connections");
declare
Ctx : Async.Epoll.Callback_Tuple;
begin
The_Hub.Enable_Tracing;
Ctx.Callback := Echo.Call_Me_Back'Access;
Ctx.Socket := ServerSock;
The_Hub.Register (Ctx);
end;
loop
Accept_Socket (ServerSock, ClientSock, ServerAddr);
Put_Line (".. accepted connection");
declare
Channel : Stream_Access := Stream (ClientSock);
Char : Character;
Data : Ada.Streams.Stream_Element_Array (1 .. 1);
Offset : Ada.Streams.Stream_Element_Count;
begin
while true loop
Ada.Streams.Read (Channel.All, Data, Offset);
exit when Offset = 0;
Put (Character'Val (Data (1)));
end loop;
Put_Line (".. closing connection");
Close_Socket (ClientSock);
end;
end loop;
end;

View File

@ -0,0 +1,14 @@
project Echoserver is
for Languages use ("Ada");
for Source_Dirs use (".", "../../src");
for Object_Dir use "obj";
for Exec_Dir use ".";
for Main use ("echoserver.adb");
package Compiler is
for Default_Switches ("Ada") use
("-g", "-gnat05");
end Compiler;
end Echoserver;