Asynchronous i/o, Garbage Collection, Funtion Instance references
Hi again,
Any advice regarding problems/solutions to the following
requirements-specification would be appreciated.
a) I want to implement an Asynchronous I/O strategy between my Java Applet's
TCP/IP Socket and my JavaScript anonymous function instances. (So that they
fulfill the role of "Asynchronous System Traps - ASTs" or "Asynchronous
Call-backs")
b) My JavaScript will call an Applet method to perform synchronous writes to
the Socket and I plan to have a separate thread constantly reading for
responses.
c) I don't want to lock up the browser EDT while I'm waiting for a Read to
complete and have previously used a single-thread and the Socket Timeout
method as a sort of delayed poll which let user events/clicks through every
"n" seconds. What I want to do this time is use the separate thread to
invoke JSObject.call() for each message received. No problem with using a
static JavaScript function (I've done so successfully with async UDP
messages) but, as there may be many messages received per message sent and
there is no restriction on additional messages/requests (or message types)
being sent before all messages/responses for a prior message have been read,
I was hoping to use closures and anonymous function instances to preserve
and pass message-specific parameters to asynchronous read-completion
routine/callback.
d) The good news is the requests will be on a single connection-oriented
Socket and will be processed on a FIFO basis, so storing parameter objects
in an JavaScript/Java Array certainly seems doable. (I'd pass a serial
number with the outgoing message and check it against what we're expecting
to read next and use push() and shift() to manage the Array)
e) I have to admit to being a bit bamboozled as to whether Java has received
the function instance by reference, and once the JS function's local
variable that passed the reference to Java has RETURNed (or that local
variable re-assigned) and nobody on the JavaScript side is left referencing
that instance of the function, that the garbage-collector might clean-up the
function even though my Applet would be expecting to call it some time in
the future.
f) Even if longevity was assured, stuffed if I know of a method that would
deserialize the function (or function reference) so that it could accompany
the outgoing message to the remote 3GL routine and come back in a reply and
be serialized into a useful form. As a procedural guy at heart, to me it
looks like a great case for malloc, pointer, free but hopefully someone can
show me the optimal OO way of achieving this.
g) Would the performance of a JavaScript Array that acts like a queue and
gets fed from the tail and diminished from the head, degrade over time? (At
least with an Array I could use a static/named JS function and keep it
simple.)
I guess the first thing I should've asked is has anyone done this or have an
example? It is my understanding that Async i/o would appear to be "new" in
Java 7 and if that is the case then I'm guessing there's a definite
requirement but perhaps no historic solution at present?
Cheers Richard Maher
"Richard Maher" <maher_rj@hotspamnotmail.com> wrote in message
news:gpf5mv$nf3$1@news-01.bur.connect.com.au...
Hi,
I replaced the pilot-error: -
public void javaMethod (String jsNum, String jsFunc) {
with
public void javaMethod (String jsNum, Object jsFunc) {
and it seems to be giving me a bit more. Sorry for the distraction.
Cheers Richard Maher
"Richard Maher" <maher_rj@hotspamnotmail.com> wrote in message
news:gpevll$jcd$1@news-01.bur.connect.com.au...
Hi,
Using netscape.javascript.JSObject (LiveConnect) I want/need to pass a
Function Reference to my Java Applet so that it can either call that
directly, or pass it to a static JavaScript function that will then
redirect
the call to the appropriate anonymous function that formed a closure.
Please forgive me if the terminology is not strictly correct, but I hope
you
can still understand what I'm trying to do.
I thought the second argument to JSObject.call could be an array of
Strings
*or* Objects but trying to get a JavaScript object (or a
reference/pointer
to it) to survive the Java Applet membrane (without some sort of
serialize/deserialize) is proving difficult. Am I missing something
obvious?
Does anyone have a small example I can look at?
Maybe it's as easy as the first argument to JSObject.call can be a
JavaScript VARiable? I'll give it a go. . .
Cheers Richard Maher
//This is called from the "onload" event
function load() {
myFunc = setClosure();
myFunc();
chan = document.getElementById("AnonCallApplet");
return;
}
// This is called from the Applet
function jsMethod(javaInt, javaFunc) {
alert('Integer ' + javaInt);
alert('JavaFunc ' + javaFunc.toString());
javaFunc();
return;
}
function setClosure() {
var lclNum = 0;
return function(){
lclNum++;
alert("lclNum = " + lclNum);
}
}
// This is called by some user-driven event (onchange, onclick. . ).
function callApplet() {
alert(myFunc.toString());
chan.javaMethod(step, myFunc)
return true;
}
//Here's the Applet
import java.applet.Applet;
import java.io.IOException;
import netscape.javascript.JSObject;
import netscape.javascript.JSException;
public class AnonCallApplet extends Applet {
private JSObject browser;
private static int sumNum = 0;
private int addNum;
public void init(){
try {
browser = JSObject.getWindow(this); }
catch (netscape.javascript.JSException e) {
e.printStackTrace(); }
catch (Exception e) {
e.printStackTrace(); }
}
public void javaMethod (String jsNum, String jsFunc) {
addNum = Integer.parseInt(jsNum);
sumNum += addNum;
Object args[] = {(Object)new Integer(sumNum), (Object)jsFunc};
try {
browser.call("jsMethod", args);}
catch (JSException e){
System.out.println("Error when calling jsMethod()"); }
}
public void destroy() {
super.destroy();
}
}