Crossfire JXClient, Trunk  R20561
MetaserverProcessor.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.metaserver;
23 
27 import org.jetbrains.annotations.NotNull;
28 
37 public class MetaserverProcessor {
38 
42  private static final int UPDATE_INTERVAL = 15*60;
43 
47  private static final int MIN_UPDATE_INTERVAL = 5*60;
48 
52  @NotNull
53  private final Metaserver metaserver;
54 
58  @NotNull
59  private final Object sync = new Object();
60 
64  private boolean running;
65 
73  private int counter = UPDATE_INTERVAL;
74 
79  private long nextQuery = System.currentTimeMillis();
80 
84  @NotNull
85  private final Runnable runnable = new Runnable() {
86 
87  @Override
88  public void run() {
89  try {
90  while (!Thread.currentThread().isInterrupted()) {
91  boolean executeProcess = false;
92  synchronized (sync) {
93  //noinspection UnconditionalWait,WaitWithoutCorrespondingNotify
94  sync.wait(1000);
95  if (counter > 0) {
96  counter--;
97  if (counter == 0) {
98  executeProcess = true;
99  counter = UPDATE_INTERVAL;
100  }
101  }
102  }
103  if (executeProcess) {
104  final long now = System.currentTimeMillis();
105  if (nextQuery <= now) {
106  nextQuery = now+MIN_UPDATE_INTERVAL;
107  metaserver.updateMetaList();
108  }
109  }
110  }
111  } catch (final InterruptedException ignored) {
112  // ignore
113  }
114  }
115 
116  };
117 
122  @NotNull
123  @SuppressWarnings("FieldCanBeLocal")
125 
126  @Override
127  public void start() {
128  // ignore
129  }
130 
131  @Override
132  public void metaserver() {
133  query();
134  }
135 
136  @Override
137  public void preConnecting(@NotNull final String serverInfo) {
138  // ignore
139  }
140 
141  @Override
142  public void connecting(@NotNull final String serverInfo) {
143  disable();
144  }
145 
146  @Override
147  public void connecting(@NotNull final ClientSocketState clientSocketState) {
148  // ignore
149  }
150 
151  @Override
152  public void connected() {
153  // ignore
154  }
155 
156  @Override
157  public void connectFailed(@NotNull final String reason) {
158  // ignore
159  }
160 
161  };
162 
168  public MetaserverProcessor(@NotNull final Metaserver metaserver, @NotNull final GuiStateManager guiStateManager) {
169  this.metaserver = metaserver;
170  guiStateManager.addGuiStateListener(guiStateListener);
171  query();
172  }
173 
178  public void query() {
179  synchronized (sync) {
180  if (!running) {
181  running = true;
182  new Thread(runnable, "JXClient:MetaserverProcessor").start();
183  }
184  counter = 1;
185  }
186  }
187 
192  public void disable() {
193  synchronized (sync) {
194  counter = 0;
195  }
196  }
197 
198 }
Interface for listeners interested gui state changes.
void query()
Immediately triggers a metaserver query and enables periodic re-queries.
long nextQuery
The timestamp at which a query is allowed.
MetaserverProcessor(@NotNull final Metaserver metaserver, @NotNull final GuiStateManager guiStateManager)
Creates a new instance.
static final int MIN_UPDATE_INTERVAL
The minimal update-interval in seconds.
void updateMetaList()
Updates the contents of metaserverModel.
Definition: Metaserver.java:80
boolean running
Whether the query runnable has been started.
Queries Crossfire&#39;s metaserver to learn about existing servers.
Definition: Metaserver.java:39
Asynchronously queries the metaserver and updates a MetaserverModel instance.
final Metaserver metaserver
The Metaserver instance to forward to.
final Object sync
The object used for synchronization.
Connection progress states of the Crossfire server connection.
final GuiStateListener guiStateListener
The GuiStateListener for detecting established or dropped connections.
static final int UPDATE_INTERVAL
The regular update-interval in seconds.