Skip to main content

Network Test

2 replies [Last post]
hooligancat
Offline
Joined: 2009-10-20
Points: 0

In our Jar file, we want to be able to test the success of a network connection. To do this first we check the player profile:

if ("YES".equals(System.getProperty("bluray.profile.2"))) ...

Then we run the test to see if the player is actually connected. To do this I have tried both the HttpUtil class from the twitter example, and also this instead:

public boolean doSimulationTest(String path)
{
boolean success = false;
InputStream is = null;
try
{
URL url = new URL(path);
HttpURLConnection server = (HttpURLConnection) url.openConnection();
server.setDoInput(true);
server.setDoOutput(true);
server.setRequestMethod("GET");
server.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
server.connect();
is = server.getInputStream();

String response = getUpdates(server, is, null);
server.disconnect();

if (response.trim().equals("connected"))
success = true;
}
catch (Exception e)
{
DEBUG.LOG(e.toString());
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (Exception ex)
{
}
}
}
return success;
}

In each version the results are as we would expect on all players except the Panasonic BD15 and Panasonic BD35. Both players appear to hang for upto 40seconds before returning. However, every other player returns almost instantly. So..

Is there an alternative approach to do a connection test?
Or are there known workarounds for these 2 players?
Or is there a way to set the timeout of a http connection? I have seen references to the system property sun.net.client.defaultReadTimeout. Can this be used?

Many thanks for any suggestions.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hooligancat
Offline
Joined: 2009-10-20
Points: 0

Thanks, but unfortunately that didn't work. In half the cases the content length always returns -1 until getInputStream is opened.

This is getUpdates:

public String getUpdates(HttpURLConnection con, InputStream is,
OutputStream os) throws IOException
{
StringBuffer stb = new StringBuffer();
char[] buffer = new char[1024];
InputStreamReader rdr = new InputStreamReader(is, "UTF-8");
try
{
for (;;)
{
int n = rdr.read(buffer, 0, buffer.length);
if (n == -1)
{
break;
}
stb.append(buffer, 0, n);
}
}
catch (IOException ioe)
{
throw ioe;
}
finally
{
try
{
if (os != null)
{
os.close();
}
if (rdr != null)
{
rdr.close();
}
if (con != null)
{
con.disconnect();
}
}
catch (IOException ioe)
{
throw ioe;
}
}
return stb.toString();
}

However, what I don't understand is why it is taking so long for an exception to be thrown or the connection request to timeout. Surely if there is no network cable plugged into the player, it should return immediately - as it does with every other player tested.

Gunnar Adler

Have you tried without setting the additional connection parameters?

HttpURLConnection server = (HttpURLConnection) url.openConnection();
is = server.getInputStream();
is.read();

Or if you do not need the actual content you could use
server.getContentLength() instead of reading the InputStream.

What does getUpdates() do?

---------------------------------------------------------------------
To unsubscribe, e-mail: bd-j-dev-unsubscribe@hdcookbook.dev.java.net
For additional commands, e-mail: bd-j-dev-help@hdcookbook.dev.java.net