Skip to content

Commit

Permalink
Refactor testSuite, now multiple IOTransports can be tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gottox committed Feb 21, 2012
1 parent 9dbb23c commit 9f63ba8
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 93 deletions.
32 changes: 23 additions & 9 deletions src/io/socket/IOConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,11 @@ private void connectTransport() {
transport = WebsocketTransport.create(url, this);
else if (protocols.contains(XhrTransport.TRANSPORT_NAME))
transport = XhrTransport.create(url, this);
else
else {
error(new SocketIOException(
"Server supports no available transports. You should reconfigure the server to support a available transport"));
return;
}
transport.connect();
}

Expand Down Expand Up @@ -828,15 +830,27 @@ public boolean isConnected() {
return getState() == STATE_READY;
}

public int getState() {
synchronized (this) {
return state;
}
/**
* Gets the current state of this IOConnection
* @return current state
*/
private synchronized int getState() {
return state;
}

public void setState(int state) {
synchronized (this) {
this.state = state;
}
/**
* Sets the current state of this IOConnection
* @param new state
*/
private synchronized void setState(int state) {
this.state = state;
}

/**
* gets the currently used transport
* @return currently used transport
*/
public IOTransport getTransport() {
return transport;
}
}
2 changes: 2 additions & 0 deletions src/io/socket/IOTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ interface IOTransport {
* {@link IOConnection}
*/
void invalidate();

String getName();
}
10 changes: 10 additions & 0 deletions src/io/socket/SocketIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,14 @@ public void reconnect() {
public boolean isConnected() {
return this.connection.isConnected();
}

/**
* Returns the name of the used transport
*
* @return the name of the currently used transport
*/
public String getTransport() {
IOTransport transport = this.connection.getTransport();
return transport != null ? transport.getName() : null;
}
}
5 changes: 5 additions & 0 deletions src/io/socket/WebsocketTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ public void connect() {
public void send(String text) throws Exception {
websocket.send(text);
}

@Override
public String getName() {
return TRANSPORT_NAME;
}
}
133 changes: 88 additions & 45 deletions src/io/socket/XhrTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,59 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;


/**
* The Class XhrTransport.
*/
class XhrTransport implements IOTransport {

/** The String to identify this Transport. */
public static final String TRANSPORT_NAME = "xhr-polling";

/** The connection. */
private IOConnection connection;

/** The url. */
private URL url;

/** The queue holding elements to send. */
ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();

/** background thread for managing the server connection. */
PollThread pollThread = null;

/** Indicates whether the {@link IOConnection} wants us to be connected. */
private boolean connect;

/** Indicates whether {@link PollThread} is blocked. */
private boolean blocked;


HttpURLConnection urlConnection;

/**
* The Class ReceiverThread.
*/
private class PollThread extends Thread {

/**
* Instantiates a new receiver thread.
*/
public PollThread() {
super(TRANSPORT_NAME);
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
@Override
Expand All @@ -66,26 +72,40 @@ public void run() {
while (isConnect()) {
try {
String line;
URLConnection urlConnection = url.openConnection();
URL url = new URL(XhrTransport.this.url.toString() + "?t=" + System.currentTimeMillis());
urlConnection = (HttpURLConnection) url.openConnection();
if (!queue.isEmpty()) {
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST") ;
BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(
urlConnection.getOutputStream()));
while((line = queue.peek()) != null) {
output.write("�7�" + line);
queue.remove();
if (queue.size() == 1) {
line = queue.poll();
output.write(line);
System.out.println(line);
} else {
Iterator<String> iter = queue.iterator();
while (iter.hasNext()) {
line = iter.next();
output.write("\ufffd" + line.length()
+ "\ufffd" + line);
System.out.println("\ufffd" + line.length()
+ "\ufffd" + line);
iter.remove();
}
}
output.close();
}
else {
} else {
setBlocked(true);
BufferedReader input = new BufferedReader(
new InputStreamReader(
urlConnection.getInputStream()));
while ((line = input.readLine()) != null) {
if (connection != null)
connection.transportMessage(line);
InputStream plainInput = urlConnection.getInputStream();
if (plainInput != null) {
BufferedReader input = new BufferedReader(
new InputStreamReader(plainInput));
while ((line = input.readLine()) != null) {
if (connection != null)
connection.transportMessage(line);
}
}
setBlocked(false);
}
Expand All @@ -103,9 +123,11 @@ public void run() {

/**
* Creates a new Transport for the given url an {@link IOConnection}.
*
* @param url the url
* @param connection the connection
*
* @param url
* the url
* @param connection
* the connection
* @return the iO transport
*/
public static IOTransport create(URL url, IOConnection connection) {
Expand All @@ -123,16 +145,20 @@ public static IOTransport create(URL url, IOConnection connection) {

/**
* Instantiates a new xhr transport.
*
* @param url the url
* @param connection the connection
*
* @param url
* the url
* @param connection
* the connection
*/
public XhrTransport(URL url, IOConnection connection) {
this.connection = connection;
this.url = url;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#connect()
*/
@Override
Expand All @@ -142,7 +168,9 @@ public void connect() {
pollThread.start();
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#disconnect()
*/
@Override
Expand All @@ -151,35 +179,43 @@ public void disconnect() {
pollThread.interrupt();
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#send(java.lang.String)
*/
@Override
public void send(String text) throws IOException {
sendBulk(new String[] { text });
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#canSendBulk()
*/
@Override
public boolean canSendBulk() {
return true;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#sendBulk(java.lang.String[])
*/
@Override
public void sendBulk(String[] texts) throws IOException {
for (String text : texts) {
queue.add(text);
}
if(isBlocked())
queue.addAll(Arrays.asList(texts));
if (isBlocked()) {
pollThread.interrupt();
urlConnection.disconnect();
}
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see io.socket.IOTransport#invalidate()
*/
@Override
Expand All @@ -189,7 +225,7 @@ public void invalidate() {

/**
* Checks if is connect.
*
*
* @return true, if is connect
*/
private synchronized boolean isConnect() {
Expand All @@ -198,16 +234,17 @@ private synchronized boolean isConnect() {

/**
* Sets the connect.
*
* @param connect the new connect
*
* @param connect
* the new connect
*/
private synchronized void setConnect(boolean connect) {
this.connect = connect;
}

/**
* Checks if is blocked.
*
*
* @return true, if is blocked
*/
private synchronized boolean isBlocked() {
Expand All @@ -216,10 +253,16 @@ private synchronized boolean isBlocked() {

/**
* Sets the blocked.
*
* @param blocked the new blocked
*
* @param blocked
* the new blocked
*/
private synchronized void setBlocked(boolean blocked) {
this.blocked = blocked;
}

@Override
public String getName() {
return TRANSPORT_NAME;
}
}
Loading

0 comments on commit 9f63ba8

Please sign in to comment.