Ignore outputs

Minor reformats
Added multi_threaded_server
Use short names for executables
This commit is contained in:
Per Sandberg 2010-05-19 22:08:30 +02:00
parent b7772ee477
commit 8200ef3971
9 changed files with 31 additions and 183 deletions

5
examples/.gitignore vendored
View File

@ -1,8 +1,5 @@
debug
bin
.#*#
gnatcheck.out
zmq-examples-client
zmq-examples-display
zmq-examples-prompt
zmq-examples-server

View File

@ -1,100 +0,0 @@
package body Radar is
function "=" (X, Y : Ship) return Boolean is
begin
return X.Unique_ID = Y.Unique_ID;
end "=";
protected body Short_List is
procedure Add_Ship (E : Ship) is
begin
if Num_Tracked_Ships < Max_Tracked_Ships then
Ships (Num_Tracked_Ships) := E;
Num_Tracked_Ships := Num_Tracked_Ships + 1;
else
Num_Untracked_Ships := Num_Untracked_Ships + 1;
Saturated_Combat_Zone := True;
end if;
end Add_Ship;
procedure Remove_Ship (E : Ship) is
begin
remove_loop : for J in Ships'First .. Num_Tracked_Ships loop
if Ships (J) = E then
-- Ship E was found. First remove it.
for K in J + 1 .. Num_Tracked_Ships loop
Ships (K - 1) := Ships (K);
end loop;
-- Then update the number of tracked ships and return.
Num_Tracked_Ships := Num_Tracked_Ships - 1;
exit remove_loop;
end if;
end loop remove_loop;
-- Ship E was not found, so it is an untracked ship.
-- Update the number of untracked ships and possibly leave the mode
-- where combat zone is saturated.
if Num_Untracked_Ships = 0 then
Saturated_Combat_Zone := False;
end if;
Num_Untracked_Ships := Num_Untracked_Ships - 1;
end Remove_Ship;
function Biggest_Ship return Ship is
Current : Class_Of_Ship := Not_A_Ship;
Index : Integer;
begin
for J in Ships'First .. Num_Tracked_Ships loop
if Current < Ships (J).Category then
-- Found a bigger ship. Update Current and Index.
Current := Ships (J).Category;
Index := J;
end if;
end loop;
return Ships (Index);
end Biggest_Ship;
end Short_List;
task body Track_Ship is
Current : Ship;
begin
-- Define the ship being tracked.
accept New_Ship (E : Ship)
do
Current := E;
end New_Ship;
loop
-- Following a movement of the ship entering or leaving the short
-- range, add or remove it from the corresponding short list.
accept Update_Position (Distance : Kilometer)
do
Current.Distance := Distance;
if Distance < Short_Distance then
case Current.Quality is
when Friend =>
Friends.Add_Ship (Current);
when Foe =>
Foes.Add_Ship (Current);
when Unknown =>
null;
end case;
else
case Current.Quality is
when Friend =>
Friends.Remove_Ship (Current);
when Foe =>
Foes.Remove_Ship (Current);
when Unknown =>
null;
end case;
end if;
end Update_Position;
end loop;
end Track_Ship;
end Radar;

View File

@ -1,58 +0,0 @@
package Radar is
type Friend_Or_Foe is (Friend, Foe, Unknown);
type Class_Of_Ship is
(Not_A_Ship, Coastal_Patrol, Fast_Attack_Craft, Corvette, Frigate,
Destroyer, Cruiser, Pocket_Battleship, Battlecruiser, Helicopter_Carrier,
Battleship, Dreadnought, Aircraft_Carrier, Supercarrier);
type Kilometer is new Natural;
-- Every ship detected by the radar is assigned a unique identifier that
-- allows tracking it until it espaces the radar's scope.
type Ship is record
Quality : Friend_Or_Foe := Unknown;
Category : Class_Of_Ship := Not_A_Ship;
Distance : Kilometer := 0;
Unique_ID : Natural := 0;
end record;
function "=" (X, Y : Ship) return Boolean;
type Ship_Array is array (Positive range <>) of Ship;
-- Detect when too many ships are sailing in the combat zone, which makes
-- it very likely to be hit by enemy fire or friendly fire. This triggers
-- a different combat mode where the ship principally attempts to escape
-- the crowded zone.
Saturated_Combat_Zone : Boolean := False;
-- Track ships closer than this distance
Short_Distance : constant Kilometer := 20;
-- Short list of nearest ships that should be dealt with higher priority,
-- whether it is to engage combat for enemies or to agree on maneuvers
-- for friends.
protected type Short_List (Max_Tracked_Ships : Positive) is
-- Add a ship when entering the short range
procedure Add_Ship (E : Ship);
-- Remove a ship when leaving the short range
procedure Remove_Ship (E : Ship);
-- Return the biggest ship in the short range
function Biggest_Ship return Ship;
private
Num_Tracked_Ships : Natural := 0;
Num_Untracked_Ships : Natural := 0;
-- Only tracked ships are included in Ships
Ships : Ship_Array (1 .. Max_Tracked_Ships);
end Short_List;
Friends : Short_List (50); -- 50 friendly ships tracked for maneuvers
Foes : Short_List (18); -- 18 enemy ships tracked (# of weapon systems)
-- Track a ship entering and leaving the short range
task type Track_Ship is
entry New_Ship (E : Ship);
entry Update_Position (Distance : Kilometer);
end Track_Ship;
end Radar;

View File

@ -16,7 +16,7 @@ begin
-- Bind to the TCP transport and port 5555 on the 'lo' interface
s.Connect ("tcp://localhost:5555");
for i in 1 .. 5 loop
for i in 1 .. 10 loop
declare
query_string : constant String := "SELECT * FROM mytable";
query : ZMQ.Messages.Message;

View File

@ -1,33 +1,36 @@
with ZMQ.Sockets;
with ZMQ.Contexts;
with ZMQ.devices;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
procedure ZMQ.examples.Multi_Thread_Server is
task type server_task (ctx : not null access ZMQ.Contexts.Context;
id : Integer) is
entry start;
end server_task;
task body server_task is
msg : Ada.Strings.Unbounded.Unbounded_String;
s : ZMQ.Sockets.Socket;
begin
accept start;
s.Initialize (ctx.all, Sockets.REP);
s.Connect ("inproc://workers");
loop
msg := s.recv;
delay 1.0;
Append (msg, "<Served by threa:" & id'Img & ">");
s.Send (msg);
end loop;
end server_task;
Number_Of_Servers : constant := 10;
ctx : aliased ZMQ.Contexts.Context;
Number_Of_Servers : constant := 10;
servers : array (1 .. Number_Of_Servers) of access server_task;
workers : ZMQ.Sockets.Socket;
clients : ZMQ.Sockets.Socket;
-- dev : ZMQ.devices.device;
servers : array (1 .. Number_Of_Servers) of access server_task;
dev : ZMQ.devices.device;
begin
-- Initialise 0MQ context, requesting a single application thread
-- and a single I/O thread
@ -42,13 +45,9 @@ begin
clients.Bind ("tcp://lo:5555");
for i in servers'Range loop
declare
s : access server_task;
begin
s := new server_task (ctx'Access, i);
s.start;
end;
servers (i) := new server_task (ctx'Access, i);
end loop;
-- dev.initialize (QUEUE, cliets, workers);
dev.initialize (devices.Queue, clients, workers);
end ZMQ.Examples.Multi_Thread_Server;

View File

@ -5,10 +5,20 @@ project ZMQ.Examples is
for Main use ("zmq-examples-client.adb",
"zmq-examples-display.adb",
"zmq-examples-prompt.adb",
"zmq-examples-server.adb");
"zmq-examples-server.adb",
"zmq-examples-multi_thread_server.adb");
for Object_Dir use ".obj";
for Exec_Dir use "bin";
package Builder is
for Executable("zmq-examples-client.adb") use "client";
for Executable("zmq-examples-display.adb") use "display";
for Executable("zmq-examples-prompt.adb") use "prompt";
for Executable("zmq-examples-server.adb") use "server";
for Executable("zmq-examples-multi_thread_server.adb") use "multi_thread_server";
end Builder;
package Compiler is
for Default_Switches ("ada") use ZMQ.Compiler'Default_Switches ("ada");

View File

@ -9,8 +9,8 @@ package body ZMQ.devices is
map : constant array (Device_Kind) of int :=
(Streamer => Low_Level.defs.ZMQ_STREAMER,
forwarder => Low_Level.defs.ZMQ_FORWARDER,
queue => Low_Level.defs.ZMQ_QUEUE);
Forwarder => Low_Level.defs.ZMQ_FORWARDER,
Queue => Low_Level.defs.ZMQ_QUEUE);
procedure initialize
(this : in out device;
Kind : Device_Kind;

View File

@ -2,7 +2,7 @@ private with Interfaces.C;
with ZMQ.Sockets;
package ZMQ.devices is
type device is tagged private;
type Device_Kind is (Streamer, forwarder, queue);
type Device_Kind is (Streamer, Forwarder, Queue);
procedure initialize (this : in out device;
Kind : Device_Kind;
insocket : ZMQ.Sockets.Socket;

View File

@ -165,8 +165,8 @@ package ZMQ.Sockets is
-- Send the message over the socet
-- not overriding
-- procedure flush (This : in out Socket);
-- not overriding
-- procedure flush (This : in out Socket);
not overriding
procedure recv (This : in Socket;
@ -199,7 +199,7 @@ private
end record;
function img (item : Ada.Streams.Stream_Element_Array) return String;
type Socket_Opt is
type Socket_Opt is
(HWM, -- Set high water mark
LWM, -- Set low water mark
SWAP,