1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
76 }
77 } catch (Exception e) {
78
79 }
80 if (getNetworkActor() != null) {
81 getNetworkActor().closeConnexion();
82 }
83
84 server = null;
85 client = null;
86
87 } catch (Exception e) {
88
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
126 return client;
127 }
128 if (server != null) {
129
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 }