Skip to content

Commit

Permalink
Make HTTP timeouts configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ibauersachs committed Sep 22, 2015
1 parent 7c51b32 commit 4b5317f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
28 changes: 25 additions & 3 deletions src/main/java/org/bitlet/weupnp/GatewayDevice.java
Expand Up @@ -51,7 +51,7 @@ public class GatewayDevice {
/**
* Receive timeout when requesting data from device
*/
private static final int HTTP_RECEIVE_TIMEOUT = 7000;
private static final int DEFAULT_HTTP_RECEIVE_TIMEOUT = 7000;

private String st;
private String location;
Expand Down Expand Up @@ -104,6 +104,11 @@ public class GatewayDevice {
*/
private String modelName;

/**
* Timeout in milliseconds for HTTP reads
*/
private static int httpReadTimeout = DEFAULT_HTTP_RECEIVE_TIMEOUT;

/**
* Creates a new instance of GatewayDevice
*/
Expand All @@ -124,7 +129,7 @@ public GatewayDevice() {
public void loadDescription() throws SAXException, IOException {

URLConnection urlConn = new URL(getLocation()).openConnection();
urlConn.setReadTimeout(HTTP_RECEIVE_TIMEOUT);
urlConn.setReadTimeout(httpReadTimeout);

XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(new GatewayDeviceHandler(this));
Expand Down Expand Up @@ -198,7 +203,8 @@ public static Map<String, String> simpleUPnPcommand(String url,
HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();

conn.setRequestMethod("POST");
conn.setReadTimeout(HTTP_RECEIVE_TIMEOUT);
conn.setConnectTimeout(httpReadTimeout);
conn.setReadTimeout(httpReadTimeout);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("SOAPAction", soapAction);
Expand Down Expand Up @@ -634,6 +640,22 @@ public void setModelNumber(String modelNumber) {
this.modelNumber = modelNumber;
}

/**
* Gets the timeout for actions on the device.
* @return timeout in milliseconds
*/
public static int getHttpReadTimeout() {
return httpReadTimeout;
}

/**
* Sets the timeout for actions on the device.
* @param milliseconds the new timeout in milliseconds
*/
public static void setHttpReadTimeout(int milliseconds) {
httpReadTimeout = milliseconds;
}

// private methods
private String copyOrCatUrl(String dst, String src) {
if (src != null) {
Expand Down
27 changes: 24 additions & 3 deletions src/main/java/org/bitlet/weupnp/GatewayDiscover.java
Expand Up @@ -63,9 +63,14 @@ public class GatewayDiscover {
public static final String IP = "239.255.255.250";

/**
* The timeout to set for the initial broadcast request
* The default timeout for the initial broadcast request
*/
private static final int TIMEOUT = 3000;
private static final int DEFAULT_TIMEOUT = 3000;

/**
* The timeout for the initial broadcast request
*/
private int timeout = DEFAULT_TIMEOUT;

/**
* The gateway types the discover have to search.
Expand Down Expand Up @@ -117,7 +122,7 @@ public void run() {
ssdpDiscoverPacket.setPort(PORT);

ssdp.send(ssdpDiscoverPacket);
ssdp.setSoTimeout(TIMEOUT);
ssdp.setSoTimeout(GatewayDiscover.this.timeout);

boolean waitingPacket = true;
while (waitingPacket) {
Expand Down Expand Up @@ -183,6 +188,22 @@ public GatewayDiscover(String[] types) {
this.searchTypes = types;
}

/**
* Gets the timeout for socket connections of the initial broadcast request.
* @return timeout in milliseconds
*/
public int getTimeout() {
return this.timeout;
}

/**
* Sets the timeout for socket connections of the initial broadcast request.
* @param milliseconds the new timeout in milliseconds
*/
public void setTimeout(int milliseconds) {
this.timeout = milliseconds;
}

/**
* Discovers Gateway Devices on the network(s) the executing machine is
* connected to.
Expand Down

0 comments on commit 4b5317f

Please sign in to comment.