+ LinkedBlockingQueue<String> command_queue;
+
+ class IgniteHandler implements Runnable {
+ AltosIgnite ignite;
+ JFrame owner;
+
+ void send_exception(Exception e) {
+ final Exception f_e = e;
+ Runnable r = new Runnable() {
+ public void run() {
+ ignite_exception(f_e);
+ }
+ };
+ SwingUtilities.invokeLater(r);
+ }
+
+ public void run () {
+ try {
+ ignite = new AltosIgnite(device);
+ } catch (Exception e) {
+ send_exception(e);
+ return;
+ }
+ ignite.set_frame(owner);
+
+ for (;;) {
+ Runnable r;
+
+ try {
+ String command = command_queue.take();
+ String reply = null;
+
+ if (command.equals("get_status")) {
+ apogee_status = ignite.status(AltosIgnite.Apogee);
+ main_status = ignite.status(AltosIgnite.Main);
+ reply = "status";
+ } else if (command.equals("main")) {
+ ignite.fire(AltosIgnite.Main);
+ reply = "fired";
+ } else if (command.equals("apogee")) {
+ ignite.fire(AltosIgnite.Apogee);
+ reply = "fired";
+ } else if (command.equals("quit")) {
+ ignite.close();
+ break;
+ } else {
+ throw new ParseException(String.format("invalid command %s", command), 0);
+ }
+ final String f_reply = reply;
+ r = new Runnable() {
+ public void run() {
+ ignite_reply(f_reply);
+ }
+ };
+ SwingUtilities.invokeLater(r);
+ } catch (Exception e) {
+ send_exception(e);
+ }
+ }
+ }
+
+ public IgniteHandler(JFrame in_owner) {
+ owner = in_owner;
+ }
+ }
+
+ void ignite_exception(Exception e) {
+ if (e instanceof FileNotFoundException) {
+ JOptionPane.showMessageDialog(owner,
+ ((FileNotFoundException) e).getMessage(),
+ "Cannot open target device",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (e instanceof AltosSerialInUseException) {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Device \"%s\" already in use",
+ device.toShortString()),
+ "Device in use",
+ JOptionPane.ERROR_MESSAGE);
+ } else if (e instanceof IOException) {
+ IOException ee = (IOException) e;
+ JOptionPane.showMessageDialog(owner,
+ device.toShortString(),
+ ee.getLocalizedMessage(),
+ JOptionPane.ERROR_MESSAGE);
+ } else {
+ JOptionPane.showMessageDialog(owner,
+ String.format("Connection to \"%s\" failed",
+ device.toShortString()),
+ "Connection Failed",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ close();
+ }
+
+ void ignite_reply(String reply) {
+ if (reply.equals("status")) {
+ set_ignite_status();
+ } else if (reply.equals("fired")) {
+ fired();
+ }
+ }
+