------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 199x by International Computer Science Institute            --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  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 Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------

-- Author: David Bailey <dbailey@icsi.berkeley.edu>
--
-- January 1993   Sather 0.2 version
-- April 1995     Sather 1.0 version
-- June 1996      Sather 1.1 version

--------------------------------------------------------------------
-- Test the SOCKET class.
--
-- Class: TEST_SOCKET
-- Required files: socket.sa, socket_support.c
--
----------------------------------------------------------------------   

class TEST_SOCKET is
   -- Test class for SOCKET.  Two concurrent copies must be run,
   -- one initiating and one connecting.  The test concludes after
   -- a given number of alternating exchanges of messages.
   
   attr is_unix,is_init:BOOL;
   attr skt:SOCKET;
   attr skt_type:STR;
   attr iterations:INT;

   main(args:ARRAY{STR}):INT is
      -- Formats:  test_socket -<iterations> -unix -init name
      --           test_socket -<iterations> -unix -conn name
      --           test_socket -<iterations> -inet -init portnum
      --           test_socket -<iterations> -inet -conn hostname portnum
      if args.size<5 then print_usage_and_exit end;
      iterations:=#INT(args[1].tail(args[1].length-1));
      #OUT+"Testing sockets for "+iterations+" iterations.\n";
      if args[2]="-unix" then is_unix:=true; skt_type:="UNIX"
      elsif args[2]="-inet" then is_unix:=false; skt_type:="INTERNET"
      else print_usage_and_exit
      end;
      if args[3]="-init" then is_init:=true
      elsif args[3]="-conn" then is_init:=false
      else print_usage_and_exit
      end;
      #OUT+"Creating socket...\n";
      protect 
	 if is_unix and is_init then
	    skt:=SOCKET::create_initiating_unix(args[4]);
	    skt.use_exceptions(true)
	 elsif is_unix and ~is_init then
	    skt:=SOCKET::create_connecting_unix(args[4]);
--	    skt.use_exceptions(true)
	 elsif ~is_unix and is_init then
	    skt:=SOCKET::create_initiating_inet(#INT(args[4]));
	    skt.use_exceptions(true)
	 else
	    skt:=SOCKET::create_connecting_inet(args[4],#INT(args[5]));
--	    skt.use_exceptions(true)
	 end;
	 if skt.is_dead then 
	    #OUT+"Socket was stillborn.\n"; fail
	 end;
	 #OUT+"Created.  Now exchanging messages...\n";
	 if is_init then write_read_loop else read_write_loop end;
	 #OUT+skt_type+" SOCKET TEST SUCCESSFULLY COMPLETED.\n"
      when SOCKET_EXCEPTION then
	 #OUT+exception+"\n"+skt_type+" SOCKET TEST FAILED.\n";
	 return 1
      end;
      return 0
   end;

   write_read_loop is
      loop
	 iterations.times!;
	 check_skt_status;
	 skt+"Hello world "+42;
	 #OUT+"Socket now blocking until can read:\n";
	 skt.block_until_can_read;
	 #OUT+"Socket can now read:\n";
	 msg1::=skt.get_str;
	 msg2::=skt.get_str;
	 msg3::=skt.get_str;
	 #OUT+0.up!; 
	 if ~void(msg1) and ~void(msg2) and ~void(msg3) then 
	    #OUT+' '+msg1.pretty+' '+msg2.pretty+' '+msg3.pretty+'\n'
	 else
	    #OUT+" Something went wrong receiving in write_read_loop.\n";
	    fail
	 end
      end
   end;

   read_write_loop is
--    #OUT+"Sleeping 10 ...\n"; junk::=UNIX::system("sleep 10");
      loop
	 iterations.times!;
	 check_skt_status;
	 msg1::=skt.get_str;
	 msg2::=skt.get_str;
	 s:STR;
	 #OUT+"Socket blocking until it can write:\n";
	 skt.block_until_can_write;
	 #OUT+"Socket can now write:\n";
	 skt+"Two empties:"+""+s;
	 #OUT+0.up!; 
	 if ~void(msg1) and ~void(msg2) then 
	    #OUT+' '+msg1.pretty+' '+msg2.pretty+'\n'
	 else
	    #OUT+" Something went wrong receiving in read_write_loop.\n";
	    fail
	 end
      end
   end;

   print_usage_and_exit is
      #OUT+"Formats: test_socket -<iterations> -unix -init name\n"
          +"         test_socket -<iterations>-unix -conn name\n"
          +"         test_socket -<iterations>-inet -init portnum\n"
          +"         test_socket -<iterations>-inet -conn hostname portnum\n";
      fail
   end;

   check_skt_status is
      if skt.is_dead then 
	 #OUT+"Socket died prematurely.\n";
	 fail
      end
   end;

   fail is
      #OUT+skt_type+" SOCKET TEST FAILED.\n";
      UNIX::exit(1)
   end;
   
end;
   
