diff --git a/src/Main.java b/src/Main.java index 71b235c..4b3770a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,17 +1,75 @@ import java.util.ArrayList; +import java.io.IOException; +import java.net.*; + import org.apache.commons.lang3.tuple.ImmutablePair; import org.oldcode.urt.MasterServer; - +import org.oldcode.urt.Util; +import org.oldcode.urt.ServerDetail; +import org.oldcode.urt.MessageResponse; public class Main { public static void main(String[] args) { + run_master_query(); + } + + public static void server_query() { System.out.println("start..."); - - ArrayList> list = MasterServer.getServerList(); + Util.test(); + + /* + ip:85.25.109.153 port:27960 + ip:91.121.6.213 port:27960 + ip:188.138.48.106 port:27960 + ip:195.110.8.164 port:27960 + ip:69.60.116.216 port:27960 + */ + ServerDetail sd = new ServerDetail("123.23.123.12", 91223); + System.out.println("server-detail: "+sd); + //sm: 64.74.97.153 + //master: 91.121.24.62 27950 + byte[] addr = new byte[4]; + addr[0] = (byte)91; + addr[1] = (byte)121; + addr[2] = (byte)24; + addr[3] = (byte)62; + + String host = "master.urbanterror.info"; + int lport = 27950; + + + MessageResponse mr = null; + try { + mr = new MessageResponse(addr, 27950, "you are gay"); + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + + + + System.out.println("msg-resp: "+mr); + try { + mr.sendMessage("getservers 68 empty full"); + //mr.sendMessage("getstatus"); + byte[] r = mr.getResponse(); + System.out.println("r.length:" +r.length); + for (byte b: r) { + System.out.println(b); + } + } catch(IOException e) { + e.printStackTrace(); + } + + } + public static void run_master_query() { + System.out.println("start test master query..."); + MasterServer ms = new MasterServer(); + ArrayList> list = ms.getServerList(); for (ImmutablePair pair: list) { System.out.println("ip:"+pair.left+" port:"+pair.right); } diff --git a/src/org/oldcode/urt/MasterServer.java b/src/org/oldcode/urt/MasterServer.java index dd14440..86ee312 100644 --- a/src/org/oldcode/urt/MasterServer.java +++ b/src/org/oldcode/urt/MasterServer.java @@ -14,15 +14,32 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.oldcode.urt.MessageResponse; + public class MasterServer { - static String host = "master.urbanterror.info"; - static int port = 27950; - static byte oob = (byte)0xff; + //private String host = "master.urbanterror.info"; + byte[] addr;// = new byte[4]; + private int port = 27950; + private byte oob = (byte)0xff; + + private DatagramPacket dp = null; + private InetAddress ia = null; + private DatagramSocket ds = null; + + public MasterServer() { + // use a static initializer: + this.addr = new byte[4]; + this.addr[0] = (byte)91; + this.addr[1] = (byte)121; + this.addr[2] = (byte)24; + this.addr[3] = (byte)62; + } - public static ArrayList> getServerList() + public ArrayList> getServerList() { - byte[] bytes = getServersResponse(); + byte[] bytes = getServersResponse(); + if (bytes != null) { //System.out.println("bytes.length:"+bytes.length); return parse(bytes); @@ -30,64 +47,34 @@ public static ArrayList> getServerList() return null; } - public static byte[] getServersResponse() { - - DatagramSocket ds = null; - DatagramPacket dp = null; - InetAddress ia = null; - + public byte[] getServersResponse() { + + MessageResponse mr = null; try { - ds = new DatagramSocket(); - ia = InetAddress.getByName(host); - - String out = "xxxxgetservers 68 empty full"; - byte[] buff = out.getBytes(); - buff[0] = oob; - buff[1] = oob; - buff[2] = oob; - buff[3] = oob; - dp = new DatagramPacket(buff, buff.length, ia, port); - - ds.send(dp); - - - //public String getResponse() { - DatagramPacket dpacket; - byte[] buffer = new byte[2048]; - String output = ""; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - while (true) { - //System.out.println("while (true)..."); - try { - dpacket = new DatagramPacket(buffer, buffer.length); - // Decrease value speeds things up, increase slows things down. - ds.setSoTimeout(800); - - //this is where we should preempt check for the exc. case: - ds.receive(dpacket); - - String packet = new String(dpacket.getData(), 0, dpacket.getLength()); - output += packet; - baos.write(dpacket.getData(), 0, dpacket.getLength()); - } catch (IOException e1) { //we shouldn't use an exception for flow control - //System.out.println("e1:"+e1); - break; - } - } - - //System.out.println(output); - //System.out.println(baos); - - byte[] bytes = baos.toByteArray(); - return bytes; - //parse(bytes); - + mr = new MessageResponse(this.addr, this.port);//, "you are gay"); } catch(Exception e) { e.printStackTrace(); + System.exit(1); } + + + System.out.println("msg-resp: "+mr); + try { + mr.sendMessage("getservers 68 empty full"); + //mr.sendMessage("getstatus"); + byte[] r = mr.getResponse(); + + //System.out.println("r.length:" +r.length); + //for (byte b: r) { + // System.out.println(b); + //} + return r; + } catch(IOException e) { + e.printStackTrace(); + } return null; + } @@ -100,7 +87,7 @@ public static byte[] getServersResponse() { four bytes are the ip and the last two bytes is the port number transmission of servers ends with '/EOF' at the end of the last packet */ - public static ArrayList> parse(byte[] bytes) + public ArrayList> parse(byte[] bytes) { ArrayList> list = new ArrayList>(); @@ -153,7 +140,7 @@ public static ArrayList> parse(byte[] bytes) } //series is intended to be the 7 bytes between /'s - public static ImmutablePair parse_ip_port(byte[] series) { + public ImmutablePair parse_ip_port(byte[] series) { ImmutablePair pair = null; //System.out.println("parse_ip_port()..." + series[0] + "::" + series[series.length-1] + " length:"+series.length); diff --git a/src/org/oldcode/urt/MessageResponse.java b/src/org/oldcode/urt/MessageResponse.java new file mode 100644 index 0000000..7525851 --- /dev/null +++ b/src/org/oldcode/urt/MessageResponse.java @@ -0,0 +1,91 @@ +package org.oldcode.urt; + +import java.net.DatagramSocket; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.io.IOException; +import java.io.ByteArrayOutputStream; + +public class MessageResponse { + + private InetAddress address = null; + private int port; + private String message; //not using this + static byte oob = (byte)0xff; + + private DatagramSocket ds; + //private DatagramPacket dp; + + public MessageResponse(byte[] ip, int port) + throws UnknownHostException + { + this.address = InetAddress.getByAddress(ip); + this.port = port; + } + + public MessageResponse(byte[] ip, int port, String message) + throws UnknownHostException + { + this(ip, port); + this.message = message; + } + + @Override + public String toString() { + return ""; + } + + public void sendMessage(String msg) + throws IOException + { + this.ds = new DatagramSocket(); + + String out = "xxxx" + msg; + byte[] buff = out.getBytes(); + buff[0] = this.oob; + buff[1] = this.oob; + buff[2] = this.oob; + buff[3] = this.oob; + DatagramPacket dp = new DatagramPacket(buff, buff.length, this.address, this.port); + + /*System.out.println("sending:" + buff); + for (byte b: buff) { + System.out.print((char)b + " "); + } + System.out.println();*/ + this.ds.send(dp); + } + + // assumes a message has been sent + public byte[] getResponse() { + DatagramPacket dpacket; + byte[] buffer = new byte[2048]; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + while (true) { + //System.out.println("getr ."); + try { + dpacket = new DatagramPacket(buffer, buffer.length); + // Decrease value speeds things up, increase slows things down. + this.ds.setSoTimeout(1000); + + //this is where we should preempt check for the exc. case: + this.ds.receive(dpacket); + + String packet = new String(dpacket.getData(), 0, dpacket.getLength()); + baos.write(dpacket.getData(), 0, dpacket.getLength()); + } catch (IOException e) { //we shouldn't use an exception for flow control + //System.out.println("e:"+e); + break; + } + } + + //System.out.println(baos); + + byte[] bytes = baos.toByteArray(); + return bytes; + } + +} +