/*
* File : MasterServerProvider1.java
* Created : 25.03.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.masterserverprovider.hlsw;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;
import net.sphene.goim.rcp.extensionpoints.GameExtensionPoint;
import net.sphene.goim.serverbrowser.extensionpoints.IMasterServerProvider;
import net.sphene.goim.serverbrowser.masterserverprovider.DefaultAddMasterServerWizardNode;
import net.sphene.goim.serverbrowser.models.GameServer;
import net.sphene.goim.serverbrowser.models.MasterServer;
import org.eclipse.jface.wizard.IWizardNode;
public class HLSWMasterServerProvider implements IMasterServerProvider {
static String PROTOCOL_ID = "net.sphene.goim.serverbrowser.protocol.hlsw";
/**
* Mapping between game IDs of HLSW and GOIM game ids.
* (key needs to be lower case !)
*/
static Map<String,String> GAME_MAPPINGS = new HashMap<String,String>();
static {
GAME_MAPPINGS.put("aao","aao");
GAME_MAPPINGS.put("ut2004","ut2k4");
}
public IWizardNode getAddMasterServerWizardNode() {
return new DefaultAddMasterServerWizardNode(PROTOCOL_ID);
}
public GameServer[] retrieveServerList(MasterServer server) {
try {
URL url = new URL(server.address);
URLConnection connection = url.openConnection();
String contentType = connection.getContentType();
InputStream stream;
if(contentType.equals("application/zip")) {
ZipInputStream zipInput = new ZipInputStream(connection.getInputStream());
zipInput.getNextEntry();
stream = zipInput;
} else {
stream = connection.getInputStream();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
List<GameServer> gameServerList = new ArrayList<GameServer>();
while((line = reader.readLine()) != null) {
line = line.trim();
if(line.startsWith("#") || line.startsWith("//")) continue; // Ignore comments
int equal = line.indexOf('=');
if(equal < 0) continue;
String key = line.substring(0,equal).toLowerCase();
String value = line.substring(equal+1);
if(!key.equals("server")) continue;
String[] args = StringUtils.splitArguments(value);
if(args.length < 4) {
System.out.println("Value does not have 4 arguments: " + value);
continue;
}
String game = args[0];
String address = args[1];
String password = args[2];
String name = args[3];
GameServer gameServer = new GameServer();
String[] inetsocket = address.split(":");
if(inetsocket.length < 0) {
System.out.println("Invalid socket address: " + address);
continue;
}
gameServer.address = new InetSocketAddress(inetsocket[0],Integer.parseInt(inetsocket[1]));
gameServer.gameid = GAME_MAPPINGS.get(game.toLowerCase());
if(gameServer.gameid == null) {
if(GameExtensionPoint.getGameExtension(game.toLowerCase()) != null) {
gameServer.gameid = game.toLowerCase();
}
}
gameServer.name = name;
gameServer.password = password;
if(gameServer.gameid != null) {
gameServerList.add(gameServer);
}
}
return gameServerList.toArray(new GameServer[gameServerList.size()]);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}