[jetrix-cvs] jetrix/src/java/net/jetrix/agent QueryAgent.java,1.4,1.5
Brought to you by:
smanux
From: Emmanuel B. <sm...@us...> - 2005-06-07 15:09:01
|
Update of /cvsroot/jetrix/jetrix/src/java/net/jetrix/agent In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20876/src/java/net/jetrix/agent Modified Files: QueryAgent.java Log Message: More robust parsing for servers sending invalid information: - support quotes in channel descriptions (seen on tetrinet.org) - support N/A as a value for the channel priority (sent by Java Tetrinet Server) Index: QueryAgent.java =================================================================== RCS file: /cvsroot/jetrix/jetrix/src/java/net/jetrix/agent/QueryAgent.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** QueryAgent.java 26 Apr 2005 20:29:25 -0000 1.4 --- QueryAgent.java 7 Jun 2005 15:08:52 -0000 1.5 *************** *** 20,25 **** package net.jetrix.agent; - import net.jetrix.protocols.QueryProtocol; import net.jetrix.Message; import java.io.*; --- 20,25 ---- package net.jetrix.agent; import net.jetrix.Message; + import net.jetrix.protocols.QueryProtocol; import java.io.*; *************** *** 27,30 **** --- 27,33 ---- import java.util.ArrayList; import java.util.List; + import java.util.logging.Logger; + import java.util.regex.Matcher; + import java.util.regex.Pattern; /** *************** *** 41,44 **** --- 44,49 ---- private Writer out; + private Logger log = Logger.getLogger("net.jetrix"); + public void connect(String hostname) throws IOException { *************** *** 129,132 **** --- 134,140 ---- public List<ChannelInfo> getChannels() throws IOException { + // prepare the pattern matcher + Pattern pattern = Pattern.compile("\"(.*)\" \"(.*)\" ([0-9]+) ([0-9]+) ([0-9]+|N/A) ([0-9]+)"); + // send the command send("listchan"); *************** *** 139,153 **** while (!QueryProtocol.OK.equals(line = protocol.readLine(in))) { ! List<String> tokens = parseQuotedTokens(line); ! ChannelInfo channel = new ChannelInfo(); ! channel.setName(tokens.get(0)); ! channel.setDescription(tokens.get(1)); ! channel.setPlayernum(Integer.parseInt(tokens.get(2))); ! channel.setPlayermax(Integer.parseInt(tokens.get(3))); ! channel.setPriority(Integer.parseInt(tokens.get(4))); ! channel.setStatus(Integer.parseInt(tokens.get(5))); ! channels.add(channel); } --- 147,173 ---- while (!QueryProtocol.OK.equals(line = protocol.readLine(in))) { ! Matcher matcher = pattern.matcher(line); ! if (matcher.matches()) ! { ! int i = 1; ! ChannelInfo channel = new ChannelInfo(); ! channel.setName(matcher.group(i++)); ! channel.setDescription(matcher.group(i++)); ! channel.setPlayernum(Integer.parseInt(matcher.group(i++))); ! channel.setPlayermax(Integer.parseInt(matcher.group(i++))); ! String priority = matcher.group(i++); ! if (!"N/A".equals(priority)) ! { ! channel.setPriority(Integer.parseInt(priority)); ! } ! channel.setStatus(Integer.parseInt(matcher.group(i++))); ! channels.add(channel); ! } ! else ! { ! log.warning("Invalid response for the listchan message (" + hostname + ") : " + line); ! } } *************** *** 157,160 **** --- 177,183 ---- public List<PlayerInfo> getPlayers() throws IOException { + // prepare the pattern matcher + Pattern pattern = Pattern.compile("\"(.*)\" \"(.*)\" \"(.*)\" ([0-9]+) ([0-9]+) ([0-9]+) \"(.*)\""); + // send the command send("listuser"); *************** *** 167,182 **** while (!QueryProtocol.OK.equals(line = protocol.readLine(in))) { ! List<String> tokens = parseQuotedTokens(line); ! PlayerInfo player = new PlayerInfo(); ! player.setNick(tokens.get(0)); ! player.setTeam(tokens.get(1)); ! player.setVersion(tokens.get(2)); ! player.setSlot(Integer.parseInt(tokens.get(3))); ! player.setStatus(Integer.parseInt(tokens.get(4))); ! player.setAuthenticationLevel(Integer.parseInt(tokens.get(5))); ! player.setChannel(tokens.get(6)); ! players.add(player); } --- 190,213 ---- while (!QueryProtocol.OK.equals(line = protocol.readLine(in))) { ! Matcher matcher = pattern.matcher(line); ! if (matcher.matches()) ! { ! int i = 1; ! PlayerInfo player = new PlayerInfo(); ! player.setNick(matcher.group(i++)); ! player.setTeam(matcher.group(i++)); ! player.setVersion(matcher.group(i++)); ! player.setSlot(Integer.parseInt(matcher.group(i++))); ! player.setStatus(Integer.parseInt(matcher.group(i++))); ! player.setAuthenticationLevel(Integer.parseInt(matcher.group(i++))); ! player.setChannel(matcher.group(i++)); ! players.add(player); ! } ! else ! { ! log.warning("Invalid response for the listuser message (" + hostname + ") : " + line); ! } } *************** *** 194,246 **** } - List<String> parseQuotedTokens(String s) - { - List<String> tokens = new ArrayList<String>(); - - if (s == null) - { - return new ArrayList<String>(); - } - - StringBuilder token = new StringBuilder(); - boolean quote = false; - - for (int i = 0; i < s.length(); i++) - { - char c = s.charAt(i); - - if (c == '"') - { - if (quote) - { - tokens.add(token.toString()); - token = new StringBuilder(); - } - - quote = !quote; - } - else if (c == ' ') - { - if (quote) - { - token.append(c); - } - else if (token.length() > 0) - { - tokens.add(token.toString()); - token = new StringBuilder(); - } - } - else - { - token.append(c); - if (i == s.length() - 1) - { - tokens.add(token.toString()); - } - } - } - - return tokens; - } } --- 225,227 ---- |