View Javadoc

1   /*
2    *   Magic-Project 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.magicproject.network;
21  
22  import javax.swing.JOptionPane;
23  
24  import net.sf.magicproject.stack.StackManager;
25  import net.sf.magicproject.ui.MagicUIComponents;
26  import net.sf.magicproject.ui.i18n.LanguageManager;
27  
28  /***
29   * Maintains/close connection of connected players.
30   * 
31   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
32   * @since 0.80
33   */
34  public final class ConnectionManager {
35  
36  	/***
37  	 * Creates a new instance of ConnectionManager <br>
38  	 */
39  	private ConnectionManager() {
40  		super();
41  	}
42  
43  	/***
44  	 * Notify the disconnection with a warning message. this message is displayed
45  	 * once per disconnection
46  	 */
47  	public static void notifyDisconnection() {
48  		if (connected) {
49  			JOptionPane.showMessageDialog(MagicUIComponents.magicForm,
50  					LanguageManager.getString("wiz_network.connectionpb"),
51  					LanguageManager.getString("wiz_network.gamestatus"),
52  					JOptionPane.WARNING_MESSAGE);
53  		}
54  	}
55  
56  	/***
57  	 * Close all opened connexions
58  	 * 
59  	 * @since 0.2c
60  	 */
61  	public static void closeConnexions() {
62  		try {
63  			try {
64  				if (connected) {
65  					connected = false;
66  					if (MSocketListener.getInstance() != null) {
67  						MSocketListener.getInstance().closeConnections();
68  					}
69  					if (StackManager.idHandedPlayer == 0) {
70  						MagicUIComponents.chat.sendMessage("");
71  					}
72  					enableConnectingTools(false);
73  					// send to opponent that we leave the play
74  				}
75  			} catch (Exception e) {
76  				// Nothing to do
77  			}
78  			if (getNetworkActor() != null) {
79  				getNetworkActor().closeConnexion();
80  			}
81  			// free pointer
82  			server = null;
83  			client = null;
84  			// enable disconnect menu
85  		} catch (Exception e) {
86  			// Nothing to do
87  		}
88  	}
89  
90  	/***
91  	 * Enable/Disable connection ability
92  	 * 
93  	 * @param really
94  	 *          if true, the connection ability is enabled. Disable it otherwise.
95  	 */
96  	public static void enableConnectingTools(boolean really) {
97  		MagicUIComponents.skipMenu.setEnabled(really);
98  		MagicUIComponents.skipButton.setEnabled(really);
99  		MagicUIComponents.sendButton.setEnabled(really);
100 		ConnectionManager.connected = really;
101 	}
102 
103 	/***
104 	 * send an event to opponent. Since we send an event, we send too (if not
105 	 * done) settings changed. Data is necessary sent now.
106 	 * 
107 	 * @param msg
108 	 *          is the message sent
109 	 */
110 	public static void sendToOpponent(int... msg) {
111 		getNetworkActor().send(msg);
112 		getNetworkActor().flush();
113 	}
114 
115 	/***
116 	 * return the current net.sf.magicproject.network actor
117 	 * 
118 	 * @return the current net.sf.magicproject.network actor
119 	 */
120 	public static NetworkActor getNetworkActor() {
121 		if (client != null) {
122 			// this is a client
123 			return client;
124 		}
125 		if (server != null) {
126 			// this is a server
127 			return server;
128 		}
129 		return null;
130 	}
131 
132 	/***
133 	 * this is the server
134 	 */
135 	public static Server server = null;
136 
137 	/***
138 	 * this is the client (mono client for this version)
139 	 */
140 	public static Client client = null;
141 
142 	/***
143 	 * Indicates if we are connected
144 	 */
145 	private static boolean connected = false;
146 
147 	/***
148 	 * Indicates if we are connected to a game.
149 	 * 
150 	 * @return true if we are connected to a game.
151 	 */
152 	public static boolean isConnected() {
153 		return connected;
154 	}
155 
156 }