/*
 * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED 
 * All rights reserved.
 * 
 * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
 * non-exclusive and royalty-free license to use, copy, modify and
 * redistribute, solely for non-commercial and non-profit purposes, the
 * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
 * conditions of this Agreement. For the avoidance of doubt, you acquire
 * only a limited right to use the SOFTWARE hereunder, and FLL or any
 * third party developer retains all rights, including but not limited to
 * copyrights, in and to the SOFTWARE.
 * 
 * (1) FLL does not grant you a license in any way for commercial
 * purposes. You may use the SOFTWARE only for non-commercial and
 * non-profit purposes only, such as academic, research and internal
 * business use.
 * (2) The SOFTWARE is protected by the Copyright Law of Japan and
 * international copyright treaties. If you make copies of the SOFTWARE,
 * with or without modification, as permitted hereunder, you shall affix
 * to all such copies of the SOFTWARE the above copyright notice.
 * (3) An explicit reference to this SOFTWARE and its copyright owner
 * shall be made on your publication or presentation in any form of the
 * results obtained by use of the SOFTWARE.
 * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
 * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
 * for such modification or the source code of the modified part of the
 * SOFTWARE.
 * 
 * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
 * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
 * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
 * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
 * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
 * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
 * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
 * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
 * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
 * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
 * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
 * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
 * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
 * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
 * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
 *
 * $OpenXM: OpenXM_contrib2/asir2000/lib/launch,v 1.3 2000/08/22 05:04:22 noro Exp $ 
*/
/* 
	usage of 

	generate_port()
	try_bind_listen()
	try_connect()
	try_accept()
	register_server()
*/

def local_launch(Server,Use_X,Conn_To_Server,Use_Unix)
{
	Host = Use_Unix ? "." : "127.1";
	Display = getenv("DISPLAY");
	if ( !Display )
		Display = "0";
	do {
		Control_Sock = -1;
		Server_Sock = -1;
		do {
			Control_Port = generate_port(Use_Unix);
			Server_Port = generate_port(Use_Unix);
			if ( !Conn_To_Server ) {
				if ( Control_Sock < 0 )
					Control_Sock = try_bind_listen(Control_Port);
				if ( Server_Sock < 0 )
					Server_Sock = try_bind_listen(Server_Port);
			} else
				break;
		} while ( Control_Sock < 0 || Server_Sock < 0 );
		ServerCmd = get_rootdir()+"/ox_launch"
			+" "+Host
			+" "+rtostr(Conn_To_Server) +" "+rtostr(Control_Port)
			+" "+rtostr(Server_Port) +" "+Server +" "+Display;
		if ( Use_X && getenv("DISPLAY") )
			Cmd = "xterm -name ox_xterm -display "+Display
				+" -geometry 60x10 -e "
				+ ServerCmd+"\&";
		else
			Cmd =ServerCmd+"\&";
		shell(Cmd);
		if ( Conn_To_Server ) {
			Control_Sock = try_connect(Host,Control_Port);
			Server_Sock = try_connect(Host,Server_Port);
		} else {
			Control_Sock = try_accept(Control_Sock,Control_Port);
			Server_Sock = try_accept(Server_Sock,Control_Port);
		}
	} while ( Control_Sock < 0 || Server_Sock < 0 );
	return register_server(Control_Sock,Control_Port,Server_Sock,Server_Port);
}

def remote_launch(Host,Server,Use_X,Conn_To_Server)
{
	Display = getenv("DISPLAY");
	if ( !Display )
		Display = "0";
	do {
		Control_Sock = -1;
		Server_Sock = -1;
		do {
			Control_Port = generate_port();
			Server_Port = generate_port();
			if ( !Conn_To_Server ) {
				if ( Control_Sock < 0 )
					Control_Sock = try_bind_listen(Control_Port);
				if ( Server_Sock < 0 )
					Server_Sock = try_bind_listen(Server_Port);
			} else
				break;
		} while ( Control_Sock < 0 || Server_Sock < 0 );
		ServerCmd = get_rootdir()+"/ox_launch"
			+" "+Host
			+" "+rtostr(Conn_To_Server) +" "+rtostr(Control_Port)
			+" "+rtostr(Server_Port) +" "+Server +" "+Display;
		if ( Use_X && getenv("DISPLAY") )
			Cmd = "xterm -name ox_xterm -display "+Display
				+" -geometry 60x10 -e "
				+ ServerCmd+">&/dev/null&";
		else
			Cmd =ServerCmd+">&/dev/null&";
		shell("rsh -n "+Host+" \""+Cmd+"\""+">/dev/null");
		if ( Conn_To_Server ) {
			Control_Sock = try_connect(Host,Control_Port);
			Server_Sock = try_connect(Host,Server_Port);
		} else {
			Control_Sock = try_accept(Control_Sock,Control_Port);
			Server_Sock = try_accept(Server_Sock,Control_Port);
		}
	} while ( Control_Sock < 0 || Server_Sock < 0 );
	return register_server(Control_Sock,Control_Port,Server_Sock,Server_Port);
}
end$
