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
|
task type T_Thread_server(Socket : Socket_Access) is
entry Get_Id(Id : out Task_Id);
end T_Thread_server;
type Thread_server_Access is access T_Thread_server;
task body Server is
type T_Thread_Server_Task is
record
Id : Task_Id := Null_Task_id;
thread_server : thread_server_access;
end record;
type thread_server_Task_Access is access T_Thread_Server_Task;
procedure Unchecked_Deallocation_thread_server is
new Ada.Unchecked_Deallocation(T_Thread_Server,thread_server_Access);
procedure Unchecked_Deallocation_thread_server_task is
new Ada.Unchecked_Deallocation(T_Thread_Server_Task,thread_server_Task_Access);
Max : constant Positive := 2;
Liste : array(1..max) of Socket_access;
Liste_Thread_task : array(1..max) of Thread_Server_Task_Access;
Bliste : array(1..max) of Boolean;
Reads,Write : Socket_Set_Type;
Selector : Selector_Type;
Srv_Adr,Adr : Sock_Addr_Type;
Srv_Socket : Socket_Type;
Status : Selector_Status;
Ptr : Integer := 0;
function Chercher return Natural is
begin
for I in 1..max loop
if Liste_Thread_Task(I) = null then
return I;
elsif Is_Callable(Liste_Thread_Task(I).Id) then
null;
elsif Is_Terminated(Liste_Thread_Task(I).Id) then
Unchecked_Deallocation_Thread_Server(Liste_Thread_Task(I).Thread_Server);
Unchecked_Deallocation_Thread_Server_task(Liste_Thread_Task(I));
Close_Socket(Liste(I).all);
return I;
end if;
end loop;
return 0;
end Chercher;
thread_Suivant : Natural := 0;
begin
for I in 1..max loop
liste(I) := new Socket_Type;
end loop;
Bliste := (others => False);
Initialize;
Srv_Adr.Addr := Any_Inet_Addr;
Srv_Adr.Port := 5877;
Create_Socket(Srv_Socket);
Bind_Socket(Srv_Socket,Srv_Adr);
Listen_Socket(Srv_Socket);
Create_Selector(Selector);
Set(Write,Srv_Socket);
loop
Put_Line("1");
Empty(Reads);
Put_Line("2");
Set(Reads,Srv_Socket);
Put_Line("3");
for I in 1..max loop
if Bliste(I) then
Set(Reads,Liste(I).all);
end if;
end loop;
Put_Line("4");
Check_Selector(Selector,Reads,Write,Status);
Put_Line("5, ");
case Status is
when Completed =>
if Is_Set(Reads,Srv_Socket) then
Thread_Suivant := Chercher;
Put("6, ");
if Thread_Suivant /= 0 then
Ptr := Thread_Suivant;
Put("7");
Accept_Socket(Srv_Socket,Liste(Ptr).all,Adr);
Put_line("8");
Bliste(Ptr) := True;
end if;
else
for I in 1..max loop
Put_Line("9");
if bliste(I) then
if Is_Set(Reads,Liste(I).all) then
Put_Line("10");
Liste_Thread_Task(I) := new T_Thread_Server_Task;
Put_Line("11");
Liste_Thread_Task(I).thread_server := new T_Thread_Server(Liste(i));
Put_Line("12");
Liste_Thread_Task(i).thread_server.Get_Id(Liste_Thread_Task(i).Id);
Put_Line("13");
Bliste(i) := false;
Put_Line("14");
end if;
end if;
delay 1.0;
end loop;
end if;
when Expired =>
Put_Line("expired");
when Aborted =>
Put_Line("aborted");
end case;
exit when End_Of_program;
end loop;
end Server; |
Partager