task Ecran is
entry First_Line(Message : String);
entry Ajouter_output(Message : String);
entry Ajouter_Input(Message : String);
entry Clear_input;
entry Clear_Output;
end Ecran;
task body Ecran is
First : String(1..80) := (others => Ascii.Nul);
Page_Input : String(1..920) := (others => ' ');
Last_Input : positive := 1;
Page_Output : String(1..920) := (others => ' ');
Last_Output : Positive := 1;
begin
while not End_Of_Program.End_Of_Program loop
select
accept First_Line(Message : String) do
First := Message(1..80);
end First_Line;
or
accept Ajouter_output(Message : String) do
if Message'Length /= 0 then
Page_Output(Last_Output+2..Last_Output+2+Message'Length-1) := Message;
Last_Output := Last_Output+ Message'Length+1;
end if;
end Ajouter_Output;
or
accept Ajouter_input(Message : String) do
Page_input(1..Message'Length) := Message;
end Ajouter_Input;
or
accept Clear_input do
First := (others => Ascii.nul);
Page_Input := (others => ' ');
end Clear_input;
or
accept Clear_Output do
Page_Output := (others => ' ');
Last_Output := 1;
end Clear_output;
or
delay 0.0;
if Current_Mode = Mode_Ia then
Res := System("clear" & Ascii.Nul);
Put(First);
Put(Page_Output);
Put("--------------------------------------------------------------------------------" );
Put(Page_input);
delay 0.075;
else
delay 0.5;
end if;
end select;
end loop;
Put_Line("exit ecran ..." );
end Ecran;
task type T_T_Ia(Id : T_Ia_Id) is
entry Recevoir(Char : Character);
entry recevoir(Msg : String);
end T_T_Ia;
task body T_T_Ia is
task Server is
entry Srv_Recevoir(Message : in string);
end Server;
task body Server is
type Socket_Access is access Socket_Type;
task type T_T_Pong(Socket : Socket_access) is
entry Get_Id(Id : out Task_Id);
end T_T_Pong;
task body T_T_Pong is
Channel : Stream_Access;
begin
accept Get_Id(Id : out Task_Id) do
Id := Current_Task;
end Get_Id;
Channel := Stream (Socket.all);
Srv_Recevoir(String'Input(Channel));
Ecran.Ajouter_Output("exit pong" );
end T_T_Pong;
type Pong_Task(Socket : Socket_access) is
record
Id : Task_Id := Null_Task_id;
Pong : T_T_Pong(Socket);
end record;
type Pong_Access is access Pong_task;
type T_Bliste is array(1..10) of Boolean;
Serv_Socket,Client_socket : Socket_Type;
Serv_Addr,Client_addr : Sock_Addr_type;
Message : String_access;
Liste : array(1..10) of Socket_access;
Pong_Liste : array(1..10) of Pong_access;
Bliste : T_Bliste;
Reads,Write : Socket_Set_Type;
Selector : Selector_Type;
Status : Selector_Status;
Ptr : Integer := 0;
Channel_Client : Stream_Access;
function Chercher return Natural is
begin
for I in 1..10 loop
if bliste(I) and then
Pong_liste(I) /= null and then
Is_Terminated(Pong_liste(I).Id) then
Pong_liste(I) := null;
Pong_Liste(I).Id := Null_Task_Id;
return I;
else
return I;
end if;
end loop;
return 0;
end Chercher;
begin
Serv_Addr.Addr := Addresses(Get_Host_By_Name("localhost" ));
Serv_Addr.Port := 5876+Port_Type(Id);
Create_Socket(Serv_socket);
Bind_socket(Serv_Socket,Serv_addr);
Listen_socket(Serv_Socket);
Create_Selector(Selector);
Set(Write,Serv_Socket);
for I in 1..Liste'length loop
liste(I) := new Socket_Type;
end loop;
Bliste := (others => False);
loop
Empty(Reads);
Set(Reads,Serv_Socket);
for I in 1..10 loop
if Bliste(I) then
Set(Reads,Liste(I).all);
end if;
end loop;
select
accept Srv_Recevoir(Message : String) do
Ecran.Ajouter_Output("Tita" );
Recevoir(Message);
Ecran.Ajouter_Output("Totu" );
end Srv_Recevoir;
or
delay(0.0);
Ecran.Ajouter_Output("TATA" );
Check_Selector(Selector,Reads,Write,Status);
Ecran.Ajouter_Output("TOTA" );
case Status is
when Completed =>
if Is_Set(Reads,Serv_Socket) then
Ptr := Chercher;
-------------
-- DEBUG --
Ecran.Ajouter_Output("A" );
Accept_Socket(Serv_Socket,Liste(Ptr).all,Serv_Addr);
Bliste(Ptr) := True;
else
for I in 1..Bliste'length loop
if Bliste(I) then
if Is_Set(Reads,Liste(i).all) then
Ecran.Ajouter_Output("serveur 1." & Integer'Image(I));
Pong_Liste(i) := new Pong_task(Liste(i));
Ecran.Ajouter_Output("serveur 2." & Integer'Image(I));
Pong_Liste(i).Pong.Get_Id(Pong_Liste(i).Id);
Ecran.Ajouter_Output("serveur 3." & Integer'Image(I));
Bliste(I) := False;
Ecran.Ajouter_Output("B" );
end if;
end if;
end loop;
end if;
when Expired =>
null;
when Aborted =>
null;
end case;
end select;
end loop;
end Server;
First_Line : String(1..80) := (others => Ascii.Nul);
Line : String(1..880);
Last : Natural := 0;
type T_Cmd is (Null_Item, Help, Quit,Ia,Clear);
function Value(Command : String) return T_Cmd is
First_Space : positive := 1;
begin
while Command(First_Space) /= ' ' and then
First_space < Command'Length loop
First_Space := First_Space + 1;
end loop;
return T_Cmd'Value(Command(Index_Non_Blank(Command)..First_Space));
exception
when others =>
return Null_Item;
end Value;
Command : T_Cmd;
Client_socket : Socket_Type;
Client_addr : Sock_Addr_type;
Channel_Client : Stream_Access;
begin
Create_Socket(Client_Socket);
-- Set_Socket_Option
-- (Client_Socket,
-- Socket_Level,
-- (Reuse_Address, ));
Client_Addr.Port := 5876+Port_Type(Id);
while not End_Of_Program.End_Of_Program loop
select
when Current_Mode = Mode_ia and then
Current_Ia_Id = 0 =>
accept Recevoir(Char : Character) do
case Char is
when Character'Val(10) =>
if Last /= 0 then
Command := Value(Line(1..Last));
case Command is
when Null_Item =>
begin
Ecran.Ajouter_Output("C" );
Client_Addr.Addr := Addresses(Get_Host_By_Name("localhost" ));
Ecran.Ajouter_Output("C.1" );
Connect_Socket(Client_Socket, Client_Addr);
Ecran.Ajouter_Output("C.2" );
Channel_client := Stream (Client_Socket);
Ecran.Ajouter_Output("C.3" );
String'output(Channel_client,Line(1..Last));
Ecran.Ajouter_Output("D" );
Close_socket(Client_Socket);
exception
when others => raise;
end;
when Quit =>
if Data /= null then
P_Reel.Save(Data.all);
end if;
if prev /= null then
P_Previsionnel.Save(Prev.all);
end if;
End_Of_Program.End_Of_Program := True;
when Help =>
declare
Char : Character;
begin
New_Line;
--Help;
New_Line(12);
Put("press any key to continue" );
Get_Immediate(Char);
end;
when Ia =>
Current_Ia_Id := T_Ia_Id'Value(Line(Index(Line," " )+1..last));
Current_Mode := Mode_Ia;
when Clear =>
Ecran.Clear_output;
when others =>
Put(Ascii.Bel);
end case;
end if;
Last := 0;
Line := (others => Ascii.Nul);
when Ascii.Del | Ascii.Bs =>
if last /= 0 then
Line(last) := ' ' ;
last := last - 1 ;
else
Put(Ascii.Bel);
end if;
when others =>
Last := Last + 1;
Line(Last) := char;
end case;
end Recevoir;
or
when Current_Mode = Mode_ia and then
Current_Ia_Id = Id =>
accept Recevoir(Char : Character) do
case Char is
when Character'Val(10) =>
if Last /= 0 then
Command := Value(Line(1..Last));
case Command is
when Null_Item =>
begin
Ecran.Ajouter_Output("C" );
Client_Addr.Addr := Addresses(Get_Host_By_Name("localhost" ));
Ecran.Ajouter_Output("C.1" );
Connect_Socket(Client_Socket, Client_Addr);
Ecran.Ajouter_Output("C.2" );
Channel_client := Stream (Client_Socket);
Ecran.Ajouter_Output("C.3" );
String'output(Channel_client,Line(1..Last));
Ecran.Ajouter_Output("D" );
Close_Socket(Client_Socket);
exception
when others => raise;
end;
when Quit =>
if Data /= null then
P_Reel.Save(Data.all);
end if;
if prev /= null then
P_Previsionnel.Save(Prev.all);
end if;
End_Of_Program.End_Of_Program := True;
when Help =>
declare
Char : Character;
begin
New_Line;
--Help;
New_Line(12);
Put("press any key to continue" );
Get_Immediate(Char);
end;
when Ia =>
Current_Ia_Id := T_Ia_Id'Value(Line(Index(Line," " )+1..last));
Current_Mode := Mode_Ia;
when others =>
Put(Ascii.Bel);
end case;
end if;
Last := 0;
Line := (others => Ascii.Nul);
when Ascii.Del | Ascii.Bs =>
if last /= 0 then
Line(last) := ' ' ;
last := last - 1 ;
else
Put(Ascii.Bel);
end if;
when others =>
Last := Last + 1;
Line(Last) := char;
end case;
end Recevoir;
or
accept Recevoir(Msg : in String) do
Ecran.Ajouter_Output(Msg);
end Recevoir;
or
delay 0.075;
if Current_Mode = Mode_ia then
Move(Full_Datify_String(Clock),First_Line,Error,Center);
if Current_Ia_Id = Id then
Insert(First_Line,60, "Ia_Id: " & T_Ia_Id'Image(Id),right);
Ecran.Clear_Input;
Ecran.first_Line(First_Line);
Ecran.Ajouter_Input(Line(1..Last));
elsif Current_Ia_Id = 0 then
if Id = 1 then
Insert(First_Line,60,"Ia GLOBAL",right);
Ecran.Clear_Input;
Ecran.first_Line(First_Line);
Ecran.Ajouter_Input(Line(1..Last));
end if;
end if;
end if;
delay 0.075;
end select;
end loop;
Put_Line("exit ia" );
abort Server;
exception
when others =>
raise;
end T_T_Ia;