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