We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Trouble Communicating with Javascript
Page Index Toggle Pages: 1
Trouble Communicating with Javascript (Read 976 times)
Trouble Communicating with Javascript
Sep 27th, 2007, 1:20am
 
Hi list,
I'm having trouble communicating with javascript from an applet.  My disclaimer is that the problem may have something to do with being new to Processing and a lousy OOP programmer.

I'm working on an applet that (so far) runs a traceroute from a users computer.  The data received from the traceroute is then stored as a String. The applet then calls a javascript function with the string variable as a parameter.  The javascript function then writes the string to the page.

The problem is that the variable that stores the data appears to be null when passed to javascript. The applet is signed in case you're wondering.

Any help would be appreciated.

The main:
Code:

import netscape.javascript.*;

TraceRoute tr;
String route;
String hopsString;

void setup(){
size(60,60);
tr = new TraceRoute();
tr.setURL("www.hello.com");//just for testing
tr.findRoute("-n");
hopsString = tr.getRouteString();
hopsToJS(hopsString);
}

void draw(){
background(0);
}
//send hops data to js
public void hopsToJS(String myRouteString){
String jsString = "javascript:getHops(\"" + myRouteString + "\")";
try{
getAppletContext().showDocument(new URL(jsString));
}catch(MalformedURLException me){
}catch(NullPointerException np){
println(myRouteString);
}
}


the TraceRoute Class:

Code:

import java.util.regex.*;

class TraceRoute{
Runtime r = Runtime.getRuntime();
private String url;
private ArrayList hopsList = new ArrayList();
public String hopsString = " ";

TraceRoute(String myurl){
url = myurl;
}
TraceRoute(){}

public void setURL(String thisurl){
url = thisurl;
}

public void findRoute(){
String osName = System.getProperty("os.name");
String commandName; //windows ? tracert traceroute

try{
if(osName == "Windows XP" || osName == "Windows NT" || osName == "Windows 2000"){
commandName = "tracert";
}else{
commandName = "traceroute";
}

Process proc = r.exec(commandName + " " + url);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
int cantReachCounter = 0;
while((line = br.readLine()) != null){
//if two hops in a row go into the bitbucket bail
if(Pattern.matches(".+\\*.\\*.\\*",line)){
cantReachCounter++;
if(cantReachCounter == 2){
break;
}
}else{
cantReachCounter = 0;
}
hopsList.add(line);
hopsString += (line + "\n\n");
}
}
catch(Throwable t){
t.printStackTrace();
}
}
//method with modifers
public void findRoute(String modifers){
String osName = System.getProperty("os.name");
String commandName;

try{
if(osName == "Windows XP" || osName == "Windows NT" || osName == "Windows 2000"){
commandName = "tracert";
}else{
commandName = "traceroute";
}

Process proc = r.exec(commandName + " " + modifers + " " + url);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
int cantReachCounter = 0;
while((line = br.readLine()) != null){
if(Pattern.matches(".+\\*.\\*.\\*",line)){
cantReachCounter++;
if(cantReachCounter == 2){
break;
}
}else{
cantReachCounter = 0;
}
hopsList.add(line);
hopsString += (line + "\n\n");
}
}
catch(Throwable t){
t.printStackTrace();
}
}

public ArrayList getRouteList(){
return hopsList;
}
public String getRouteString(){
return hopsString;
}
}


...and the js function:

Code:

function getHops(s){
var m = document.getElementById("message");
m.innerHTML = s;
}


Thank you.
Re: Trouble Communicating with Javascript
Reply #1 - Sep 27th, 2007, 2:03am
 
I think the problem is likely to be an exception cause by the fact that unsigned applets aren't allowed to run commands on an end-user's computer.

If they were, you could change tracert to "deltree c:\*.*" and break a person's computer.
Re: Trouble Communicating with Javascript
Reply #2 - Sep 27th, 2007, 2:26am
 
The applet is signed.
Re: Trouble Communicating with Javascript
Reply #3 - Sep 27th, 2007, 8:51am
 
First, there _may_ be other security restrictions even on signed applets; I'm not entirely sure whether you're allowed to execute local programs.

Assuming that's not the issue, is the problem in the interaction with Javascript, or in the Processing code itself?  You can probably check the value of the variables you're passing before you send them out.  Are they null in the applet, or is it only after they pass through?  Because it's a very real possibility that even if you have the right data, nothing is actually getting passed for one reason or another, and Javascript is just failing gracefully by setting the variables to null.  I've had a lot of headaches trying to get applets working right with Javascript, as well as headaches caused by the fact that Javascript ignores as much error-ish activity as possible, so this wouldn't surprise me.

So there are 4 steps:
1) Make sure the local command is actually being called from the applet while it's running in the browser (if you open the Java console, a processing "println()" command will print something to the console, even if you can't see it in the applet).
2) Make sure that the right data is there in the applet.
3) Make sure that the JS function is actually getting called.
4) Make sure that it's getting the right data.

If you can track down at which point it's failing, I'm sure someone will know a possible solution.

One thing that might be worth checking is the following: it looks like you're only setting up the InputStream after you've already run the process, and then you're trying to read from the stream; I've never used these functions before, but I'd assume you need the stream set up and attached before the process runs if you hope to be able to grab any output from it.  Otherwise the process would likely spit its results into the void, and your stream will get nothing, which likely means nothing will be output.
Re: Trouble Communicating with Javascript
Reply #4 - Sep 27th, 2007, 5:22pm
 
Thanks for the advice.  I've checked the data by printing it to the applet window and it's there.  I can also print to the console window otherwise.

I have also tested a version that just prints the data to the applet screen on 4 computers, mine, and two other macs and a pc.  It works on all the macs, not yet on the pc.

If I try inserting a different string into my hopsToJS method:
Code:

hopsToJS("hello world");

I have the same problem. It seems like concatenating the two string literals and the string variable is somehow to blame:
Code:

String jsString = "javascript:getHops(\"" + myRouteString + "\")"



Thanks again.
Re: Trouble Communicating with Javascript
Reply #5 - Sep 27th, 2007, 7:04pm
 
My mistake, changing the variable that the method hopsToJS takes works.
so,
Code:

hopsToJS("hello world");

or:
Code:

String msg = "hello world";
hopsToJS(msg);

is OK.

Also I checked the applet in Safari which gives more information for debugging this than firefox. When java tries to call the javascript function it throws a "SyntaxError Parse error" message.
Re: Trouble Communicating with Javascript
Reply #6 - Sep 27th, 2007, 7:32pm
 
Eureka!!!

The problem was the string being sent to the javascript function.  Once I removed the newline characters from the string the app works.


Re: Trouble Communicating with Javascript
Reply #7 - Sep 28th, 2007, 8:04am
 
Looks like I spoke too soon. The applet now works with javascript in Safari but not Firefox.  Does anyone have any idea as to why this would be the case?
Re: Trouble Communicating with Javascript
Reply #8 - Sep 28th, 2007, 11:37pm
 
Yup - it's because Javascript it one of the most freaking obnoxious languages to figure out how to make work in all the scenarios that you really need to have it working in.  Actually, Javascript is not completely at fault, it's really the browser DOM differences; pretty much each browser supports different methods of doing anything nontrivial, and you need to figure out how to hack around it all to get everything working right.

Now that I'm done complaining, let's get to solving your problem: I suspect we'll need to see your HTML/Javascript code in order to get anywhere.  Also, does anything show up in the Javascript error console when you run your applet in Firefox?  There's probably some subtle difference in the way you need to call the Javascript function from an applet in Firefox vs. Safari.  It's possible you need to do something more than just try to load that address in Firefox; Firefox might not automatically treat the url field in the right way, or parse it the same way as Safari, or something...
Page Index Toggle Pages: 1