View Javadoc

1   package nl.tudelft.goal.ut2004.visualizer.connection.client;
2   
3   import java.rmi.RemoteException;
4   
5   import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
6   import nl.tudelft.goal.ut2004.visualizer.connection.VisualizerService;
7   import cz.cuni.amis.utils.flag.Flag;
8   
9   /**
10   * Wrapper class that represents the {@link VisualizerService} to the
11   * {@link UnrealGoalEnvironment}.
12   * 
13   * @author M.P. Korstanje
14   * 
15   */
16  public class RemoteVisualizer {
17  
18  	private final Flag<Boolean> connectedFlag = new Flag<Boolean>();
19  	private VisualizerService visualizer;
20  	private EnvironmentService environment;
21  
22  	protected void setVisualizerService(VisualizerService visualizer) {
23  		this.visualizer = visualizer;
24  		setConnected(true);
25  	}
26  
27  	public void setEnvironment(EnvironmentService client) {
28  		try {
29  			this.environment = client;
30  			this.visualizer.registerEnvironment(client);
31  		} catch (RemoteException e) {
32  			// Connection failed? Update status.
33  			disconnect();
34  			e.printStackTrace();
35  		}
36  	}
37  
38  	private void setConnected(boolean connected) {
39  		connectedFlag.setFlag(connected);
40  	}
41  
42  	public Flag<Boolean> getConnectedFlag() {
43  		return connectedFlag;
44  	}
45  
46  	public void disconnect() {
47  		try {
48  			if (visualizer != null)
49  				visualizer.unregisterEnvironment(environment);
50  		} catch (RemoteException e) {
51  			// Ignore, we are disconnected now.
52  			// e.printStackTrace();
53  		}
54  
55  		environment = null;
56  
57  		setConnected(false);
58  	}
59  
60  }