Crossfire JXClient, Trunk  R20561
JXCConnection.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.window;
23 
35 import java.awt.Frame;
36 import org.jetbrains.annotations.NotNull;
37 import org.jetbrains.annotations.Nullable;
38 
39 public class JXCConnection {
40 
44  private static final int DEFAULT_CROSSFIRE_PORT = 13327;
45 
49  @NotNull
50  private static final String TITLE_PREFIX = "jxclient";
51 
55  @NotNull
57 
61  @NotNull
62  private final Shortcuts shortcuts;
63 
67  @NotNull
68  private final Settings settings;
69 
73  @Nullable
74  private Frame frame;
75 
79  @NotNull
80  private final Pickup characterPickup;
81 
86  @NotNull
88 
92  @NotNull
93  private final Logger logger;
94 
98  @Nullable
99  private String hostname;
100 
104  private int port;
105 
109  @Nullable
110  private String character;
111 
116  @NotNull
117  @SuppressWarnings("FieldCanBeLocal")
119 
120  @Override
121  public void start() {
122  disconnect("display start screen");
123  }
124 
125  @Override
126  public void metaserver() {
127  disconnect("display metaserver screen");
128  }
129 
130  @Override
131  public void preConnecting(@NotNull final String serverInfo) {
132  // ignore
133  }
134 
135  @Override
136  public void connecting(@NotNull final String serverInfo) {
137  connect();
138  }
139 
140  @Override
141  public void connecting(@NotNull final ClientSocketState clientSocketState) {
142  // ignore
143  }
144 
145  @Override
146  public void connected() {
147  // ignore
148  }
149 
150  @Override
151  public void connectFailed(@NotNull final String reason) {
152  // ignore
153  }
154 
155  };
156 
160  @NotNull
162 
163  @Override
164  public void pickupChanged(final int pickupOptions) {
165  characterPickup.updatePickupMode(pickupOptions, false);
166  }
167 
168  };
169 
180  public JXCConnection(@NotNull final KeybindingsManager keybindingsManager, @NotNull final Shortcuts shortcuts, @NotNull final Settings settings, @NotNull final Pickup characterPickup, @NotNull final CrossfireServerConnection server, @NotNull final GuiStateManager guiStateManager, @NotNull final Logger logger) {
181  this.keybindingsManager = keybindingsManager;
182  this.shortcuts = shortcuts;
183  this.settings = settings;
184  this.characterPickup = characterPickup;
185  this.server = server;
186  this.logger = logger;
187  guiStateManager.addGuiStateListener(guiStateListener);
188  }
189 
194  public void setFrame(@Nullable final Frame frame) {
195  this.frame = frame;
196  updateTitle();
197  }
198 
203  @Nullable
204  public String getHostname() {
205  return hostname;
206  }
207 
212  public int getPort() {
213  return port;
214  }
215 
220  public void setCharacter(@Nullable final String character) {
221  if (this.character == null ? character == null : this.character.equals(character)) {
222  return;
223  }
224 
225  keybindingsManager.unloadPerCharacterBindings();
226  ShortcutsLoader.saveShortcuts(shortcuts);
227 
228  if (hostname != null && this.character != null) {
229  server.removeCrossfirePickupListener(crossfirePickupListener);
230  final long pickupMode = characterPickup.getPickupMode();
231  if (pickupMode == Pickup.PU_NOTHING) {
232  settings.remove("pickup_"+hostname+"_"+this.character);
233  } else {
234  settings.putLong(SettingsEntries.getPickupSettingsEntry(hostname, this.character), pickupMode);
235  }
236  }
237 
238  this.character = character;
239  updateTitle();
240 
241  if (hostname != null && character != null) {
242  keybindingsManager.loadPerCharacterBindings(hostname, character);
243  assert hostname != null;
244  ShortcutsLoader.loadShortcuts(shortcuts, hostname, character);
245  characterPickup.updatePickupMode(settings.getLong(SettingsEntries.getPickupSettingsEntry(hostname, character)), true);
246  server.addCrossfirePickupListener(crossfirePickupListener);
247  }
248  }
249 
253  @SuppressWarnings("IfMayBeConditional")
254  private void updateTitle() {
255  if (frame == null) {
256  return;
257  }
258  final String title;
259  if (hostname == null) {
260  title = TITLE_PREFIX;
261  } else if (character == null) {
262  title = TITLE_PREFIX+" - "+hostname;
263  } else {
264  title = TITLE_PREFIX+" - "+hostname+" - "+character;
265  }
266  frame.setTitle(title);
267  }
268 
273  public void setHost(@Nullable final String serverInfo) {
274  @Nullable final String newHostname;
275  final int newPort;
276  if (serverInfo == null) {
277  newHostname = null;
278  newPort = 0;
279  } else {
281  final String[] tmp = serverInfo.split(":", 2);
282  newHostname = tmp[0];
283  newPort = tmp.length < 2 ? DEFAULT_CROSSFIRE_PORT : NumberParser.parseInt(tmp[1], DEFAULT_CROSSFIRE_PORT, 1, 65535);
284  }
285 
286  if ((hostname == null ? newHostname == null : hostname.equals(newHostname)) && port == newPort) {
287  return;
288  }
289 
290  setCharacter(null);
291  hostname = newHostname;
292  port = newPort;
293  updateTitle();
294  logger.setHostname(newHostname);
295  }
296 
301  private void disconnect(@NotNull final String reason) {
302  server.disconnect(reason);
303  setHost(null);
304  }
305 
309  private void connect() {
310  assert hostname != null;
312  }
313 
314 }
String hostname
The currently connected server.
Frame frame
The Frame for updating the title or.
Interface for listeners interested gui state changes.
void remove(@NotNull final String key)
Removes a key.
Definition: Settings.java:168
void connect()
Connects to the Crossfire server.
void addCrossfirePickupListener(@NotNull CrossfirePickupListener listener)
Adds a listener to be notified about received "pickup" messages.
long getLong(@NotNull final SettingsEntry< Long > key)
Returns the long associated with the specified key at a node or.
Definition: Settings.java:115
final Logger logger
The Logger that is notified about changed server names.
long getPickupMode()
Returns the pickup mode.
Definition: Pickup.java:348
String getHostname()
Returns the currently connected server.
final CrossfirePickupListener crossfirePickupListener
The CrossfirePickupListener for tracking pickup mode changes.
JXCConnection(@NotNull final KeybindingsManager keybindingsManager, @NotNull final Shortcuts shortcuts, @NotNull final Settings settings, @NotNull final Pickup characterPickup, @NotNull final CrossfireServerConnection server, @NotNull final GuiStateManager guiStateManager, @NotNull final Logger logger)
Creates a new instance.
static final int DEFAULT_CROSSFIRE_PORT
The default port number for Crossfire servers.
static void loadShortcuts(@NotNull final Shortcuts shortcuts, @NotNull final CharSequence hostname, @NotNull final CharSequence character)
Load shortcut info from the backing file.
void setHostname(@Nullable final String hostname)
Updates the hostname.
Definition: Logger.java:114
Defines constants for pickup mode.
Definition: Pickup.java:33
void setCharacter(@Nullable final String character)
Updates the active character name.
int getPort()
Returns the currently connected port.
static void saveShortcuts(@NotNull final Shortcuts shortcuts)
Save all shortcut info to the backing file.
final GuiStateListener guiStateListener
The GuiStateListener for detecting established or dropped connections.
void disconnect(@NotNull String reason)
Disconnects from the server.
void putLong(@NotNull final SettingsEntry< Long > key, final long value)
Stores a key/value pair.
Definition: Settings.java:160
All defined entries in the settings file.
Maintains a set of key/value pairs.
Definition: Settings.java:43
Interface for listeners interested in "pickup" messages.
String character
The currently logged in character.
static int parseInt(@NotNull final String string, final int defaultValue)
Converts a string into an int value.
void connect(@NotNull String hostname, int port)
Attempts to connect the client to a server.
final CrossfireServerConnection server
The CrossfireServerConnection instance used to connect to the Crossfire server.
static final String TITLE_PREFIX
The prefix for the window title.
void disconnect(@NotNull final String reason)
Disconnects from the Crossfire server.
static SettingsEntry< Long > getPickupSettingsEntry(@NotNull final String hostname, @NotNull final String characterName)
Returns the SettingsEntry for the default pickup mode of a character.
void setFrame(@Nullable final Frame frame)
Sets the Frame for updating the title.
Utility class for parsing strings into numbers.
void setHost(@Nullable final String serverInfo)
Updates information about the connected host.
final Settings settings
The settings instance to use.
void putString(@NotNull final SettingsEntry<?> key, @NotNull final String value)
Stores a key/value pair.
Definition: Settings.java:124
Logs received messages to a file.
Definition: Logger.java:42
static final SettingsEntry< String > SERVER
The server to which the previous connection was made.
void unloadPerCharacterBindings()
Unloads (clears and saves) the per-character key bindings.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
final Pickup characterPickup
The Pickup instance to update.
void updateTitle()
Updates the window title to reflect the current connection state.
static final long PU_NOTHING
Pickup mode: nothing.
Definition: Pickup.java:43
void removeCrossfirePickupListener(@NotNull CrossfirePickupListener listener)
Removes a listener to be notified about received "pickup" messages.
final Shortcuts shortcuts
The Shortcuts to update.
Connection progress states of the Crossfire server connection.
void loadPerCharacterBindings(@NotNull final CharSequence hostname, @NotNull final CharSequence character)
Loads the per-character key bindings.
void updatePickupMode(final long pickupMode, final boolean sendToServer)
Sets the pickup mode.
Definition: Pickup.java:331
final KeybindingsManager keybindingsManager
The KeybindingsManager to update.