% 
% 	moz - The "Moo in OZ" project.
%	Version: 1.0.C021
% 	Copyright (C) 2002 Robin Lee Powell.  All rights reserved.
% 	Written by rlpowell@digitalkingdom.org
% 
% 	Redistribution and use in source and binary forms, with or without
% 	modification, are permitted provided that the following conditions are
% 	met:
% 	
% 	 1. Redistributions of source code must retain the above copyright
% 	 notice, this list of conditions and the following disclaimer.
% 	
% 	 2. Redistributions in binary form must reproduce the above copyright
% 	 notice, this list of conditions and the following disclaimer in the
% 	 documentation and/or other materials provided with the distribution.
% 	
% 	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
% 	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% 	WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% 	DISCLAIMED. IN NO EVENT SHALL THE AUTHER OR CONTRIBUTORS BE LIABLE FOR
% 	ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
% 	DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
% 	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
% 	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
% 	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
% 	ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% 	POSSIBILITY OF SUCH DAMAGE.
%
Connection = class $ from MozBase
    %%%%%%%%%
    %
    % Properties
    %
    %%%%%%%%%

    % We do _not_ want normal users using the functionality available to
    % the Connection class.
    prop final

    %%%%%%%%%
    %
    % Attributes
    %
    %%%%%%%%%
    attr 

	% An object of class Open.socket, containing the socket
	% connection.
	accept

	% The parser object to pass the data to.
	parser

	% Module attributes:
	% These attributes are used to store pointers to the modules
	% that Server needs access to, but that we don't want in
	% Server's scope in general because then objects it creates
	% might be able to see it.
	system

    %%%%%%%%%%%%%%
    %
    % Features
    %
    %%%%%%%%%%%%%%
    feat

	% The name of this class.
	className: 'Connection'

    %%%%%%%%%%%%%%
    %
    % Methods
    %
    %%%%%%%%%%%%%%

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % start:
    % Handles the connection, reading from the TCP/IP port and passing
    % to the parser, and then back.
    %
    % Arguments:
    %
    % port:  The TCP/IP Open.socket object for the port.
    %
    % storageRef:  An attribute of type objectRef for the Storage object.
    %
    % modules:  A record of system modules we get access to.
    %
    % parser:  The parser object for this connection.
    % 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth start( socket: AcceptObject storageRef: StorageObj modules: Modules
	    parser: ParserObject outputStream: OutputStream )
	% This procedure actually handles the incoming data.
	proc {Input}
	    String
	in
	    try
	    {@accept getS( String )}
	    catch _ then skip end
	    if {IsList String} then
		{@parser parse( input: String ) }
		{Input}
	    end
	end
    in
	% Variable initialization

	accept <- AcceptObject
	parser <- ParserObject

	storageRef <- StorageObj

	system <- Modules.system

	thread
	    {Input}
	end

	thread
	    {ForAll OutputStream
		proc {$ String}
		    try
		    {@accept send( vs: String )}
		    catch _ then skip end
		end
	    }
	end
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % stop:
    %
    % Does nothing.
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    meth stop( ... )
	skip
    end

end
