----------------------------------------------------------------------------- -- -- Onions Network Streams Library -- -- O N I O N S . C O N N E C T I O N S -- -- S p e c -- -- Copyright (C) 1997-1998 Regents of the University of California -- -- Onions is free software; you can redistribute it and/or modify it under -- the terms of the GNU General Public License as published by the Free -- Software Foundation, with or without the single exception listed below; -- either version 2, or (at your option) any later version. Onions is -- distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; -- without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -- PARTICULAR PURPOSE. See the GNU General Public License for more details. -- You should have received a copy of the GNU General Public License -- distributed with Onions; see the file COPYING. If not, write to the -- Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -- 02111-1307, USA. -- -- As a special exception, if other files instantiate generics from this -- library, or you link this library with other files to produce an -- executable, this library does not by itself cause the resulting -- executable to be covered by the GNU General Public License. This -- exception does not however invalidate any other reasons why the -- executable file might be covered by the GNU General Public License. -- -- Created in 1997 by Roy T. Fielding and Kari Nies ----------------------------------------------------------------------------- -- -- The Onions Connection Manager controls the number of active TCP socket -- connections used by a client application and keeps track of persistent -- connections (sockets that are currently connected to a host and waiting -- to be reused). -- -- A Connection object is defined with methods for establishing the -- connection and aborting/releasing each half of the connection, thus -- providing an interface between the Channels and the Connection Manager. -- This is an important efficiency consideration because we don't want -- to be in the protected object during the calls to C_connect or C_close, -- since either could block for up to 60 seconds. The tricky part is making -- the Channel streams able to call the Connection methods, while at the -- same time having the Connection record include the two Pipe pointers -- for those two Channel streams. Fortunately, we avoid the circular -- dependency because the classwide Pipes are defined in a different spec -- than the Channel streams. -- -- A connection is opened via a call to Connect, which returns the -- Incoming and Outgoing pipes corresponding to the input and output -- channel stream objects, respectively. The caller closes a connection -- by closing or aborting both of those pipes. -- with Interfaces.C; with Onions.Thin; with Onions.Naming; with Onions.Sockets; with Onions.List_Queues; with Onions.Instreams; with Onions.Outstreams; package Onions.Connections is type Connection is limited private; type A_Connection is access Connection; -- Connect asks the Connection Manager for a new Connection and -- then, if it is not already connected, attempts to connect it. -- If successful, Incoming and Outgoing point to the input and -- output channels, respectively. Note that the caller will -- later "disconnect" a connection when they close both pipes. -- -- Raises Connection_Error if the attempt fails. -- procedure Connect (Server : in Onions.Sockets.Host_Location; Incoming : out Onions.Instreams.Input_Pipe; Outgoing : out Onions.Outstreams.Output_Pipe); -- Close_Incoming is used by the input channel to signal a close. -- procedure Close_Incoming (Conn : A_Connection); -- Close_Outgoing is used by the output channel to signal a close. -- procedure Close_Outgoing (Conn : A_Connection); -- Abort_Incoming is used by the input channel to signal an abort. -- procedure Abort_Incoming (Conn : A_Connection); -- Abort_Outgoing is used by the output channel to signal an abort. -- procedure Abort_Outgoing (Conn : A_Connection); -- Name returns a string corresponding to the connection's server -- function Name (Conn : A_Connection) return String; ------------------------------------------------------------------- -- The Connection Manager prevents the client from exceeding its -- own resource limits (too many open connections) while at the -- same time managing persistent connections. -- package Connection_Queues is new Onions.List_Queues (A_Connection); subtype Connection_List is Connection_Queues.List; protected Connection_Manager is function Total_Connections return Natural; function Active_Connections return Natural; function Get_Max_Connections return Natural; procedure Set_Max_Connections (Max : Natural); -- Calls to New_Connection will queue if max number of active -- connections is exceeded. -- entry New_Connection (Server : Onions.Sockets.Host_Location; Conn : out A_Connection); -- Free a Connection for later reuse. Raises Constraint_Error -- if we have no record of this connection. -- procedure Free_Connection (Conn : A_Connection); private Num_Active : Natural := 0; Num_Persist : Natural := 0; Max_Connections : Natural := 4; Free_List : Connection_List; Active_List : Connection_List; Persist_List : Connection_List; end Connection_Manager; private type Connection_Status is (Disconnected, Open, Closed, Aborted); type Connection is limited record Server : Onions.Sockets.Host_Location; Incoming : Onions.Instreams.Input_Pipe := null; Outgoing : Onions.Outstreams.Output_Pipe := null; In_State : Connection_Status := Disconnected; Out_State : Connection_Status := Disconnected; Persistent : Boolean := False; end record; end Onions.Connections;