Crossfire JXClient, Trunk
NumLookObjects.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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.server.crossfire;
24 
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
30 
35 public class NumLookObjects {
36 
41  private static final int DEFAULT_NUM_LOOK_OBJECTS = 50;
42 
46  @NotNull
48 
53  @Nullable
54  private final DebugWriter debugProtocol;
55 
59  @NotNull
60  private final Object sync = new Object();
61 
66  private boolean connected;
67 
72 
77  private int pendingNumLookObjects;
78 
82  private int currentNumLookObjects;
83 
87  private boolean pending;
88 
97  this.crossfireServerConnection = crossfireServerConnection;
98  this.debugProtocol = debugProtocol;
99  }
100 
104  public void connected() {
105  synchronized (sync) {
106  connected = false;
108  if (debugProtocol != null) {
109  debugProtocol.debugProtocolWrite("connected: defaulting to pending_num_look_objects="+pendingNumLookObjects);
110  }
112  if (debugProtocol != null) {
113  debugProtocol.debugProtocolWrite("connected: defaulting to num_look_objects="+currentNumLookObjects);
114  }
115  sync.notifyAll();
116  }
117  }
118 
122  private void negotiateNumLookObjects() {
123  final int numLookObjects;
124  synchronized (sync) {
125  pending = false;
126  numLookObjects = preferredNumLookObjects;
127  if (debugProtocol != null) {
128  debugProtocol.debugProtocolWrite("negotiateNumLookObjects: "+numLookObjects);
129  }
130 
131  if (!connected) {
132  if (debugProtocol != null) {
133  debugProtocol.debugProtocolWrite("negotiateNumLookObjects: not connected, ignoring");
134  }
135  return;
136  }
137  if (pendingNumLookObjects != 0) {
138  if (debugProtocol != null) {
139  debugProtocol.debugProtocolWrite("negotiateNumLookObjects: already negotiating pending_num_look_objects="+pendingNumLookObjects+", ignoring");
140  }
141  return;
142  }
143  if (currentNumLookObjects == numLookObjects) {
144  if (debugProtocol != null) {
145  debugProtocol.debugProtocolWrite("negotiateNumLookObjects: unchanged from num_look_objects="+currentNumLookObjects+", ignoring");
146  }
147  return;
148  }
149  pendingNumLookObjects = numLookObjects;
150  if (debugProtocol != null) {
151  debugProtocol.debugProtocolWrite("negotiateNumLookObjects: pending_num_look_objects="+pendingNumLookObjects+", sending setup command");
152  }
153  sync.notifyAll();
154  }
155  crossfireServerConnection.sendSetup("num_look_objects "+numLookObjects);
156  }
157 
164  public void processSetupNumLookObjects(@NotNull final String value) throws UnknownCommandException {
165  if (value.equals("FALSE")) {
166  System.err.println("Warning: the server is too old for this client since it does not support the num_look_objects setup option.");
167  System.err.println("Expect issues with the ground view display.");
168  synchronized (sync) {
170  sync.notifyAll();
171  }
172  if (debugProtocol != null) {
173  debugProtocol.debugProtocolWrite("processSetup: pending_num_look_objects=0 [server didn't understand setup command]");
174  }
175  } else {
176  final int thisNumLookObjects;
177  try {
178  thisNumLookObjects = Integer.parseInt(value);
179  } catch (final NumberFormatException ignored) {
180  throw new UnknownCommandException("the server returned 'setup num_look_objects "+value+"'.");
181  }
182  final boolean negotiate;
183  synchronized (sync) {
184  if (pendingNumLookObjects == 0) {
185  System.err.println("the server sent an unexpected 'setup num_look_objects "+value+"'.");
186  negotiate = false;
187  } else {
188  if (pendingNumLookObjects != thisNumLookObjects) {
189  System.err.println("Warning: the server didn't accept the num_look_objects setup option: requested "+pendingNumLookObjects+", returned "+thisNumLookObjects+".");
190  System.err.println("Expect issues with the ground view display.");
191  }
193  if (debugProtocol != null) {
194  debugProtocol.debugProtocolWrite("processSetup: pending_num_look_objects="+pendingNumLookObjects+" [ok]");
195  }
196  currentNumLookObjects = thisNumLookObjects;
197  if (debugProtocol != null) {
198  debugProtocol.debugProtocolWrite("processSetup: num_look_objects="+currentNumLookObjects);
199  }
201  if (negotiate) {
202  pending = true;
203  }
204  sync.notifyAll();
205  }
206  }
207  if (negotiate) {
209  }
210  }
211  }
212 
218  synchronized (sync) {
219  final int preferredNumLookObjects2 = Math.max(3, preferredNumLookObjects);
220  if (this.preferredNumLookObjects == preferredNumLookObjects2) {
221  return;
222  }
223 
224  this.preferredNumLookObjects = preferredNumLookObjects2;
225  pending = true;
226  }
228  }
229 
235  synchronized (sync) {
236  return currentNumLookObjects;
237  }
238  }
239 
246  public void waitForCurrentNumLookObjectsValid() throws InterruptedException {
247  synchronized (sync) {
248  while (!connected || pendingNumLookObjects != 0 || pending) {
249  sync.wait();
250  }
251  }
252  }
253 
258  public void setClientSocketState(@NotNull final ClientSocketState clientSocketState) {
259  synchronized (sync) {
260  connected = clientSocketState == ClientSocketState.CONNECTED;
261  sync.notifyAll();
262  if (!connected) {
263  return;
264  }
265  pending = true;
266  }
268  }
269 
270 }
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.preferredNumLookObjects
int preferredNumLookObjects
Definition: NumLookObjects.java:71
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.sync
final Object sync
Definition: NumLookObjects.java:60
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.currentNumLookObjects
int currentNumLookObjects
Definition: NumLookObjects.java:82
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.processSetupNumLookObjects
void processSetupNumLookObjects(@NotNull final String value)
Definition: NumLookObjects.java:164
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.negotiateNumLookObjects
void negotiateNumLookObjects()
Definition: NumLookObjects.java:122
com.realtime.crossfire.jxclient.guistate.ClientSocketState
Definition: ClientSocketState.java:30
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
Definition: NumLookObjects.java:47
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.sendSetup
void sendSetup(@NotNull String... options)
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.setPreferredNumLookObjects
void setPreferredNumLookObjects(final int preferredNumLookObjects)
Definition: NumLookObjects.java:217
com.realtime.crossfire.jxclient.guistate
Definition: ClientSocketState.java:23
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects
Definition: NumLookObjects.java:35
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.DEFAULT_NUM_LOOK_OBJECTS
static final int DEFAULT_NUM_LOOK_OBJECTS
Definition: NumLookObjects.java:41
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.connected
void connected()
Definition: NumLookObjects.java:104
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.pending
boolean pending
Definition: NumLookObjects.java:87
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.NumLookObjects
NumLookObjects(@NotNull final CrossfireServerConnection crossfireServerConnection, @Nullable final DebugWriter debugProtocol)
Definition: NumLookObjects.java:96
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.setClientSocketState
void setClientSocketState(@NotNull final ClientSocketState clientSocketState)
Definition: NumLookObjects.java:258
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.waitForCurrentNumLookObjectsValid
void waitForCurrentNumLookObjectsValid()
Definition: NumLookObjects.java:246
com.realtime.crossfire
com.realtime
com.realtime.crossfire.jxclient.server.socket
Definition: ClientSocket.java:23
com
com.realtime.crossfire.jxclient.guistate.ClientSocketState.CONNECTED
CONNECTED
Definition: ClientSocketState.java:65
com.realtime.crossfire.jxclient.server.socket.UnknownCommandException
Definition: UnknownCommandException.java:34
com.realtime.crossfire.jxclient.util.DebugWriter
Definition: DebugWriter.java:36
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.getCurrentNumLookObjects
int getCurrentNumLookObjects()
Definition: NumLookObjects.java:234
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.pendingNumLookObjects
int pendingNumLookObjects
Definition: NumLookObjects.java:77
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.connected
boolean connected
Definition: NumLookObjects.java:66
com.realtime.crossfire.jxclient.server.crossfire.NumLookObjects.debugProtocol
final DebugWriter debugProtocol
Definition: NumLookObjects.java:54
com.realtime.crossfire.jxclient.util.DebugWriter.debugProtocolWrite
void debugProtocolWrite(@NotNull final CharSequence str)
Definition: DebugWriter.java:68