Skip to content

Commit

Permalink
refactor the master server network stuff into MessageResponse class.
Browse files Browse the repository at this point in the history
  • Loading branch information
krsanky committed May 17, 2012
1 parent a8b4e28 commit 2fdff07
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 62 deletions.
64 changes: 61 additions & 3 deletions 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<ImmutablePair<String, Integer>> 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<ImmutablePair<String, Integer>> list = ms.getServerList();
for (ImmutablePair<String, Integer> pair: list) {
System.out.println("ip:"+pair.left+" port:"+pair.right);
}
Expand Down
105 changes: 46 additions & 59 deletions src/org/oldcode/urt/MasterServer.java
Expand Up @@ -14,80 +14,67 @@
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<ImmutablePair<String, Integer>> getServerList()
public ArrayList<ImmutablePair<String, Integer>> getServerList()
{
byte[] bytes = getServersResponse();
byte[] bytes = getServersResponse();

if (bytes != null) {
//System.out.println("bytes.length:"+bytes.length);
return parse(bytes);
}
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;

}


Expand All @@ -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<ImmutablePair<String, Integer>> parse(byte[] bytes)
public ArrayList<ImmutablePair<String, Integer>> parse(byte[] bytes)
{
ArrayList<ImmutablePair<String, Integer>> list = new ArrayList<ImmutablePair<String, Integer>>();

Expand Down Expand Up @@ -153,7 +140,7 @@ public static ArrayList<ImmutablePair<String, Integer>> parse(byte[] bytes)
}

//series is intended to be the 7 bytes between /'s
public static ImmutablePair<String, Integer> parse_ip_port(byte[] series) {
public ImmutablePair<String, Integer> parse_ip_port(byte[] series) {
ImmutablePair<String, Integer> pair = null;
//System.out.println("parse_ip_port()..." + series[0] + "::" + series[series.length-1] + " length:"+series.length);

Expand Down
91 changes: 91 additions & 0 deletions 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 "<MessageResponse address:"+this.address+" port:"+this.port+" message:"+message+">";
}

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;
}

}

0 comments on commit 2fdff07

Please sign in to comment.