/*
* File : GameServerListWrapper.java
* Created : 01.04.2006
* By : kahless
*
* GOIM - Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul
* (JabberId: kahless@sphene.net / Email: herbert.poul@gmail.com)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package net.sphene.goim.serverbrowser.models;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.sphene.goim.rcp.extensionpoints.GameExtensionPoint.GameExtensionProxy;
import net.sphene.goim.rcp.extensionpoints.game.GameAdapterWithServerName;
import net.sphene.goim.rcp.extensionpoints.game.GameAdapterWithServerName.ServerQueryInformation;
/**
* A Wrapper for a ServerList. as returned by {@link net.sphene.goim.serverbrowser.models.MasterServer}
*/
public class GameServerListWrapper {
GameServer[] serverList;
static ExecutorService threadPool = Executors.newFixedThreadPool(5);
/**
* Means that the server list was already queried.
* (or is currently in the process of beeing queried ..)
*/
boolean queriedServerList = false;
public GameServerListWrapper(GameServer[] serverList) {
this.serverList = serverList;
}
/**
* Returns the list of game servers.
* @return list of game servers.
*/
public GameServer[] getServerList() {
return serverList;
}
/**
* Refreshes the information about the servers. (ie. query all servers.
* if forceRefresh is false it will do nothing except the first time called.)
* @param forceRefresh
*/
public void refresh(boolean forceRefresh) {
if(!forceRefresh && queriedServerList) {
return;
}
queriedServerList = true;
for (int i = 0; i < serverList.length; i++) {
GameServer server = serverList[i];
threadPool.execute(new ServerQuery(server));
}
}
protected static class ServerQuery implements Runnable {
private GameServer server;
public ServerQuery(GameServer server) {
this.server = server;
}
public void run() {
GameExtensionProxy extension = server.getGameExtension();
if (extension.getDelegate() instanceof GameAdapterWithServerName) {
GameAdapterWithServerName gameAdapter = (GameAdapterWithServerName) extension
.getDelegate();
try {
ServerQueryInformation query = gameAdapter
.queryServer(server.address);
if (query == null) {
server.ping = -200;
return;
}
server.ping = query.ping;
if (server.ping < 0)
server.ping = -100;
server.mapname = query.mapName;
server.lastQuery = query;
server.fireChangeEvent();
} catch (Exception e) {
server.ping = -300;
e.printStackTrace();
}
}
}
}
}