View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.firemox.network;
21  
22  import javax.swing.JOptionPane;
23  
24  import net.sf.firemox.network.message.CoreMessage;
25  import net.sf.firemox.network.message.CoreMessageType;
26  import net.sf.firemox.stack.StackManager;
27  import net.sf.firemox.ui.MagicUIComponents;
28  import net.sf.firemox.ui.i18n.LanguageManager;
29  
30  /***
31   * Maintains/close connection of connected players.
32   * 
33   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34   * @since 0.80
35   */
36  public final class ConnectionManager {
37  
38  	/***
39  	 * Creates a new instance of ConnectionManager <br>
40  	 */
41  	private ConnectionManager() {
42  		super();
43  	}
44  
45  	/***
46  	 * Notify the disconnection with a warning message. this message is displayed
47  	 * once per disconnection
48  	 */
49  	public static void notifyDisconnection() {
50  		if (connected) {
51  			JOptionPane.showMessageDialog(MagicUIComponents.magicForm,
52  					LanguageManager.getString("wiz_network.connectionpb"),
53  					LanguageManager.getString("wiz_network.gamestatus"),
54  					JOptionPane.WARNING_MESSAGE);
55  		}
56  	}
57  
58  	/***
59  	 * Close all opened connections
60  	 * 
61  	 * @since 0.2c
62  	 */
63  	public static void closeConnexions() {
64  		try {
65  			try {
66  				if (connected) {
67  					connected = false;
68  					if (MSocketListener.getInstance() != null) {
69  						MSocketListener.getInstance().closeConnections();
70  					}
71  					if (StackManager.idHandedPlayer == 0) {
72  						MChat.getInstance().sendMessage("");
73  					}
74  					enableConnectingTools(false);
75  					// send to opponent that we leave the play
76  				}
77  			} catch (Exception e) {
78  				// Nothing to do
79  			}
80  			if (getNetworkActor() != null) {
81  				getNetworkActor().closeConnexion();
82  			}
83  			// free pointer
84  			server = null;
85  			client = null;
86  			// enable disconnect menu
87  		} catch (Exception e) {
88  			// Nothing to do
89  		}
90  	}
91  
92  	/***
93  	 * Enable/Disable connection ability
94  	 * 
95  	 * @param really
96  	 *          if true, the connection ability is enabled. Disable it otherwise.
97  	 */
98  	public static void enableConnectingTools(boolean really) {
99  		MagicUIComponents.skipMenu.setEnabled(really);
100 		MagicUIComponents.skipButton.setEnabled(really);
101 		MagicUIComponents.sendButton.setEnabled(really);
102 		ConnectionManager.connected = really;
103 	}
104 
105 	/***
106 	 * Send an event to opponent. Since we send an event, we send too (if not
107 	 * done) settings changed. Data is necessary sent now.
108 	 * 
109 	 * @param type
110 	 *          is the message type.
111 	 * @param data
112 	 *          the data.
113 	 */
114 	public static void send(CoreMessageType type, byte... data) {
115 		getNetworkActor().send(new CoreMessage(type, data));
116 	}
117 
118 	/***
119 	 * Return the current network actor.
120 	 * 
121 	 * @return the current network actor.
122 	 */
123 	public static NetworkActor getNetworkActor() {
124 		if (client != null) {
125 			// this is a client
126 			return client;
127 		}
128 		if (server != null) {
129 			// this is a server
130 			return server;
131 		}
132 		return null;
133 	}
134 
135 	/***
136 	 * this is the server
137 	 */
138 	public static Server server = null;
139 
140 	/***
141 	 * this is the client (mono client for this version)
142 	 */
143 	public static Client client = null;
144 
145 	/***
146 	 * Indicates if we are connected
147 	 */
148 	private static boolean connected = false;
149 
150 	/***
151 	 * Indicates if we are connected to a game.
152 	 * 
153 	 * @return true if we are connected to a game.
154 	 */
155 	public static boolean isConnected() {
156 		return connected;
157 	}
158 
159 }