Hi,
Ignite Realtime community i am new to openfire i want to perform a load test in openfire .So, i have searched a followin article in our site.
http://community.igniterealtime.org/message/133171#133171
I did not understand the coding written below .Can any one tell me how this below code works.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sun.misc.BASE64Encoder;
/**
* A simple HTTP Binding (XEP-0124) client
* @author Tim (freetmp (at) msn.com)
*
*/
public class HttpBindClient {
public static final String URL_BASE = "http://dev1:8080/";
public static final String XMPP_DOMAIN = "dev2";
private String username = "tim";
private String password = "test";
public static final int HOLD = 1;
public static final int WAIT = 15;
private int rid;
private String sid;
private boolean hasAuth = false;
private URL u;
private HttpURLConnection con;
private long bootTime = System.currentTimeMillis();
public HttpBindClient() {
rid = new Random(System.currentTimeMillis()).nextInt(1000000);
}
public void run() {
try {
u = new URL(URL_BASE);
} catch (MalformedURLException e) {
e.printStackTrace();
}
createSession();
auth();
if (hasAuth)
presence();
else
System.out.println("Auth failed.");
}
public void createSession() {
String result = routePacket(null);
Pattern p = Pattern.compile(".*\\<body.*sid=[''\"]([^''\"]+)[''\"].*\\>.*");
Matcher m = p.matcher(result);
if (m.matches()) {
this.sid = m.group(1);
System.out.println("Get sid: " + this.sid);
}
}
public void auth() {
// use sasl plain
String jid = username + "@" + XMPP_DOMAIN;
String src = jid + "\0" + username + "\0" + password;
BASE64Encoder enc = new BASE64Encoder();
String encStr = enc.encode(src.getBytes());
String auth = "<auth mechanism=\"PLAIN\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" +
encStr + "</auth>";
String result = routePacket(auth);
if (result.indexOf("<success") != -1)
this.hasAuth = true;
}
public void presence() {
String xmlBind = "<iq id=\"420000-1\" type=\"set\">" +
"<bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\">" +
"<resource>eclipse</resource>" +
"</bind></iq>";
String xmlPresence = "<presence id=\"420000-2\"></presence>";
routePacket(xmlBind);
routePacket(xmlPresence);
// keep the connection every 10 sec
while (true) {
routePacket(" ");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private String routePacket(String src) {
String result = null;
try {
String packet = null;
if (src == null)
packet = buildCreatePacket();
else
packet = buildPacket(src);
byte[] cmd = packet.getBytes("utf-8");
con = (HttpURLConnection) u.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
con.setRequestProperty("Content-Length", String.valueOf(cmd.length));
con.connect();
OutputStream os = con.getOutputStream();
os.write(cmd);
int retCode = con.getResponseCode();
if (retCode != 200) {
System.out.println("ERROR: " + retCode);
}
InputStream in = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
StringBuilder buf = new StringBuilder();
while ((line = br.readLine()) != null)
buf.append(line);
br.close();
os.close();
result = buf.toString();
System.out.println("R: " + result);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Get error, time elapsed: " + (System.currentTimeMillis() - bootTime) / 1000);
}
return result;
}
private String buildCreatePacket() {
StringBuilder sb = new StringBuilder();
return sb.append("<body content=\"text/xml; charset=utf-8\" hold=\"").append(HOLD)
.append("\" rid=\"").append(rid++).append("\" to=\"").append(XMPP_DOMAIN)
.append("\" secure=\"false\" wait=\"").append(WAIT)
.append("\" xml:lang=\"en\" xmlns=\"http://jabber.org/protocol/httpbind\"></body>")
.toString();
}
private String buildPacket(String content) {
StringBuilder sb = new StringBuilder();
sb.append("<body rid=\"").append(rid++).append("\" sid=\"").append(sid).append("\"")
.append(" xmlns=\"http://jabber.org/protocol/httpbind\">");
sb.append(content);
sb.append("</body>");
System.out.println("S: " + sb.toString());
return sb.toString();
}
public static void main(String[] args) {
new HttpBindClient().run();
}
}
Thank you