Hi,
It was a bit tricky, but I managed to do something. In the code below I
replace the default error handler by a custom one, as the default error
handler would simply terminate the program (with a call to
System.exit(2)). But then we have the problem that the PhoenixSource
continuously tries to open the connection. To stop this there is a call
to shutdown the PhoenixSource. Still, the constructor of MoteIF is
waiting for the connection to be started, which now will never happen. I
think that the correct implementation should be that if the source is
shut down while somebody is waiting for it to be started (with a call to
awaitStartup()), then there should be an exception or something. However
this situation is currently not handled. Without modifying the code, the
only possibility to interrupt the blocking thread is by explicitly
interrupt it. To do this we first need a reference to the thread that
might get blocked. We know that the thread might get blocked on calling
the constructor of MoteIF, so we get a reference to this thread right
before calling the constructor. When the error occurs, our own error
handler is called in a separate thread. Since it now has a reference to
the blocking thread, it can interrupt it.
I hope this is what you were looking for.
Cheers,
Urs
// ---------------------------------------
import java.io.*;
import net.tinyos.message.*;
import net.tinyos.packet.*;
import net.tinyos.util.PrintStreamMessenger;
public class Test implements PhoenixError {
private PhoenixSource ps = null;
private Thread thread = null;
public static void main(String[] args) {
new Test();
}
public Test() {
String comm = "[EMAIL PROTECTED]:9002";
MoteIF mif = null;
try {
ps = BuildSource.makePhoenix(comm, PrintStreamMessenger.err);
ps.setPacketErrorHandler(this);
thread = Thread.currentThread();
mif = new MoteIF(ps);
} catch(Exception ex) {
System.out.println(">>> We got an exception: " +
ex.getClass().getName());
ex.printStackTrace();
}
System.out.println("We are at the end.");
System.exit(0);
}
public void error(IOException ioe) {
System.out.println("=== We got a Phoenix Error");
ioe.printStackTrace();
ps.shutdown();
thread.interrupt();
}
}
// ---------------------------------------
ZhuoHao Sum schrieb:
> Hi,
>
> Is this segment of code working?
> I cannot seem to make this work.. Still unable to catch the exception when
> the SerialForwarder is not on.
>
> regards,
> Zhuohao
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help