View Javadoc

1   import java.io.*;
2   import java.lang.*;
3   
4   public class BallAmigosRunner{
5   	public BallAmigosRunner()
6   	{
7   	}
8   	
9   	private Process runCmd(String []cmd, String dir)
10  	{
11  		try{
12  			ProcessBuilder p = new ProcessBuilder(cmd);
13  			p.redirectErrorStream(true);
14  			if (dir != null){
15  				File d = new File(dir);
16  				p.directory(d);
17  			}
18  			return p.start();
19  		} 
20  		catch (Exception err) {
21  			System.err.println("Can't run " + cmd[0] + err.toString());
22  			return null;
23  		}
24  	}
25  	
26  	
27  	private void waitForCmd(String []cmd, String dir) 
28  	{
29  		try {
30  			String line;
31  			Process p = runCmd(cmd, dir);
32  			BufferedReader input = new BufferedReader
33  				(new InputStreamReader(p.getInputStream()));
34  			  
35  			while ((line = input.readLine()) != null) {
36  				System.out.println(line);
37  			}
38  			input.close();
39  		} 
40  		catch (Exception err) {
41  			System.err.println("Can't run " + cmd[0] + err.toString());
42  		}
43  	}
44  	
45  	public static void main(String argv[]) {
46  		BallAmigosRunner runner = new BallAmigosRunner();
47  		String sep = System.getProperty("path.separator");
48  		
49  		if (argv.length >= 1 && argv[0].equals("-server")){
50  			String []rmicmd = {"rmiregistry"};
51  			System.out.println(sep);
52  			String []servercmd = {"java", "-cp", "ballamigos-0.1.jar:db/hsqldb.jar", 
53  					      "pl.agh.iosr.ballamigos.server.GameServer"};
54  			String []dbcmd = {"java", "-cp", "hsqldb.jar", "org.hsqldb.Server", "-database.0",
55  					  "ballamigos", "-dbname.0", "ballamigos"};
56  			
57  			System.out.println("Trying to start rmiregistry");
58  			runner.runCmd(rmicmd, null);
59  
60  			System.out.println("Trying to start data base server in db directory");
61  			runner.runCmd(dbcmd, "db");
62  
63  			try{
64  				Thread.sleep(2000);
65  			}catch(InterruptedException ex){}
66  			
67  			System.out.println("Starting BallAmigos server");
68  			runner.waitForCmd(servercmd, null);
69  		}
70  		else{
71  			String []clientcmd = {"java", "-cp", "ballamigos-0.1.jar",  "pl.agh.iosr.ballamigos.client.GameClient"};
72  			System.out.println("Starting BallAmigos client...");
73  			runner.waitForCmd(clientcmd, null);
74  		}
75  	}
76  }