Crossfire JXClient, Trunk  R20561
CommandQueue.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.queue;
23 
29 import java.util.LinkedList;
30 import java.util.List;
31 import org.jetbrains.annotations.NotNull;
32 
37 public class CommandQueue {
38 
43  private static final int MAX_PENDING_COMMANDS = 10;
44 
48  @NotNull
50 
56  @NotNull
57  private final List<Integer> pendingCommands = new LinkedList<>();
58 
62  private int repeatCount;
63 
68  private boolean isRunning;
69 
73  @NotNull
74  @SuppressWarnings("FieldCanBeLocal")
75  private final CrossfireComcListener crossfireComcListener = (packetNo, time) -> {
76  synchronized (pendingCommands) {
77  final int index = pendingCommands.indexOf(packetNo);
78  if (index == -1) {
79  System.err.println("Error: got unexpected comc command #"+packetNo);
80  return;
81  }
82  if (index > 0) {
83  System.err.println("Warning: got out of order comc command #"+packetNo);
84  }
85 
86  for (int i = 0; i <= index; i++) {
87  pendingCommands.remove(0);
88  }
89  }
90  };
91 
96  @NotNull
97  @SuppressWarnings("FieldCanBeLocal")
99 
100  @Override
101  public void start() {
102  // ignore
103  }
104 
105  @Override
106  public void metaserver() {
107  // ignore
108  }
109 
110  @Override
111  public void preConnecting(@NotNull final String serverInfo) {
112  // ignore
113  }
114 
115  @Override
116  public void connecting(@NotNull final String serverInfo) {
117  clear();
118  }
119 
120  @Override
121  public void connecting(@NotNull final ClientSocketState clientSocketState) {
122  // ignore
123  }
124 
125  @Override
126  public void connected() {
127  // ignore
128  }
129 
130  @Override
131  public void connectFailed(@NotNull final String reason) {
132  // ignore
133  }
134 
135  };
136 
143  public CommandQueue(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final GuiStateManager guiStateManager) {
144  this.crossfireServerConnection = crossfireServerConnection;
145  crossfireServerConnection.addCrossfireComcListener(crossfireComcListener);
146  guiStateManager.addGuiStateListener(guiStateListener);
147  }
148 
153  private int getRepeatCount() {
154  final int oldRepeatCount = repeatCount;
156  return oldRepeatCount;
157  }
158 
162  public void resetRepeatCount() {
163  repeatCount = 0;
164  }
165 
170  public void addToRepeatCount(final int digit) {
171  assert 0 <= digit && digit <= 9;
172  repeatCount = (10*repeatCount+digit)%100000;
173  }
174 
178  private void clear() {
180  synchronized (pendingCommands) {
181  pendingCommands.clear();
182  isRunning = false;
183  }
184  }
185 
194  public void sendNcom(final boolean mustSend, @NotNull final String command) {
195  sendNcom(mustSend, getRepeatCount(), command);
196  }
197 
206  public void sendNcom(final boolean mustSend, final int repeat, @NotNull final String command) {
207  synchronized (pendingCommands) {
208  if (!mustSend && pendingCommands.size() >= MAX_PENDING_COMMANDS) {
209  return;
210  }
211 
212  final int packetNo = crossfireServerConnection.sendNcom(repeat, command);
213  pendingCommands.add(packetNo);
214 
215  if (command.startsWith("run ")) {
216  isRunning = true;
217  } else if (command.startsWith("run_stop")) {
218  isRunning = false;
219  }
220  }
221  }
222 
228  public boolean stopRunning() {
229  synchronized (pendingCommands) {
230  final boolean result = isRunning;
231  if (result) {
232  sendNcom(true, 0, "run_stop");
233  assert !isRunning;
234  }
235  return result;
236  }
237  }
238 
243  public boolean checkRun() {
244  synchronized (pendingCommands) {
245  return isRunning;
246  }
247  }
248 
254  public boolean checkFire() {
255  return false; // XXX: implement
256  }
257 
264  public void sendMove(final int to, final int tag) {
266  }
267 
268 }
boolean checkRun()
Returns whether the character is running.
Interface for listeners interested gui state changes.
void sendMove(final int to, final int tag)
Sends a "move" command to the server.
final List< Integer > pendingCommands
Records command ids of commands sent to the server for which no comc commands has been received...
void sendNcom(final boolean mustSend, @NotNull final String command)
Sends an "ncom" command to the server.
boolean stopRunning()
Tells the server to stop running.
final CrossfireComcListener crossfireComcListener
The listener to track comc commands.
int sendNcom(int repeat, @NotNull String command)
Sends a "ncom" command to the server.
void addCrossfireComcListener(@NotNull CrossfireComcListener listener)
Adds a listener to be notified about received comc commands.
CommandQueue(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final GuiStateManager guiStateManager)
Creates a new instance.
void clear()
Forgets about sent commands.
void addToRepeatCount(final int digit)
Adds a digit to the current repeat count.
boolean isRunning
Whether a "run" command has been sent without a following "run_stop" command.
final CrossfireServerConnection crossfireServerConnection
The server connection for sending ncom commands.
int repeatCount
The default repeat counter for ncom commands.
void resetRepeatCount()
Resets the current repeat count to zero.
final GuiStateListener guiStateListener
The GuiStateListener for detecting established or dropped connections.
void sendNcom(final boolean mustSend, final int repeat, @NotNull final String command)
Sends an "ncom" command to the server.
static final int MAX_PENDING_COMMANDS
Maximum number of pending commands sent to the server.
boolean checkFire()
Returns whether the character is firing.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
Maintains the pending (ncom) commands sent to the server.
Interface for listeners interested in comc commands received from the server.
int getRepeatCount()
Returns the current repeat count and reset it to zero.
Connection progress states of the Crossfire server connection.
void sendMove(int to, int tag, int nrof)
Sends a "move" command to the server.