00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.queue;
00023
00024 import com.realtime.crossfire.jxclient.guistate.ClientSocketState;
00025 import com.realtime.crossfire.jxclient.guistate.GuiStateListener;
00026 import com.realtime.crossfire.jxclient.guistate.GuiStateManager;
00027 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireComcListener;
00028 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection;
00029 import java.util.LinkedList;
00030 import java.util.List;
00031 import org.jetbrains.annotations.NotNull;
00032
00037 public class CommandQueue {
00038
00043 private static final int MAX_PENDING_COMMANDS = 10;
00044
00048 @NotNull
00049 private final CrossfireServerConnection crossfireServerConnection;
00050
00056 @NotNull
00057 private final List<Integer> pendingCommands = new LinkedList<Integer>();
00058
00062 private int repeatCount = 0;
00063
00068 private boolean isRunning = false;
00069
00073 @NotNull
00074 private final CrossfireComcListener crossfireComcListener = new CrossfireComcListener() {
00075
00076 @Override
00077 public void commandComcReceived(final int packetNo, final int time) {
00078 synchronized (pendingCommands) {
00079 final int index = pendingCommands.indexOf(packetNo);
00080 if (index == -1) {
00081 System.err.println("Error: got unexpected comc command #"+packetNo);
00082 return;
00083 }
00084 if (index > 0) {
00085 System.err.println("Warning: got out of order comc command #"+packetNo);
00086 }
00087
00088 for (int i = 0; i <= index; i++) {
00089 pendingCommands.remove(0);
00090 }
00091 }
00092 }
00093
00094 };
00095
00100 @NotNull
00101 private final GuiStateListener guiStateListener = new GuiStateListener() {
00102
00103 @Override
00104 public void start() {
00105
00106 }
00107
00108 @Override
00109 public void metaserver() {
00110
00111 }
00112
00113 @Override
00114 public void preConnecting(@NotNull final String serverInfo) {
00115
00116 }
00117
00118 @Override
00119 public void connecting(@NotNull final String serverInfo) {
00120 clear();
00121 }
00122
00123 @Override
00124 public void connecting(@NotNull final ClientSocketState clientSocketState) {
00125
00126 }
00127
00128 @Override
00129 public void connected() {
00130
00131 }
00132
00133 @Override
00134 public void connectFailed(@NotNull final String reason) {
00135
00136 }
00137
00138 };
00139
00146 public CommandQueue(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final GuiStateManager guiStateManager) {
00147 this.crossfireServerConnection = crossfireServerConnection;
00148 crossfireServerConnection.addCrossfireComcListener(crossfireComcListener);
00149 guiStateManager.addGuiStateListener(guiStateListener);
00150 }
00151
00156 private int getRepeatCount() {
00157 final int oldRepeatCount = repeatCount;
00158 resetRepeatCount();
00159 return oldRepeatCount;
00160 }
00161
00165 public void resetRepeatCount() {
00166 repeatCount = 0;
00167 }
00168
00173 public void addToRepeatCount(final int digit) {
00174 assert 0 <= digit && digit <= 9;
00175 repeatCount = (10*repeatCount+digit)%100000;
00176 }
00177
00181 private void clear() {
00182 resetRepeatCount();
00183 synchronized (pendingCommands) {
00184 pendingCommands.clear();
00185 isRunning = false;
00186 }
00187 }
00188
00197 public void sendNcom(final boolean mustSend, @NotNull final String command) {
00198 sendNcom(mustSend, getRepeatCount(), command);
00199 }
00200
00209 public void sendNcom(final boolean mustSend, final int repeat, @NotNull final String command) {
00210 synchronized (pendingCommands) {
00211 if (!mustSend && pendingCommands.size() >= MAX_PENDING_COMMANDS) {
00212 return;
00213 }
00214
00215 final int packetNo = crossfireServerConnection.sendNcom(repeat, command);
00216 pendingCommands.add(packetNo);
00217
00218 if (command.startsWith("run ")) {
00219 isRunning = true;
00220 } else if (command.startsWith("run_stop")) {
00221 isRunning = false;
00222 }
00223 }
00224 }
00225
00231 public boolean stopRunning() {
00232 final boolean result = isRunning;
00233 if (result) {
00234 sendNcom(true, 0, "run_stop");
00235 assert !isRunning;
00236 }
00237 return result;
00238 }
00239
00244 public boolean checkRun() {
00245 return isRunning;
00246 }
00247
00253 public boolean checkFire() {
00254 return false;
00255 }
00256
00263 public void sendMove(final int to, final int tag) {
00264 crossfireServerConnection.sendMove(to, tag, getRepeatCount());
00265 }
00266
00267 }