From cc252e5c166a7c4b28fce859c926ddb56d29d646 Mon Sep 17 00:00:00 2001 From: fros4943 Date: Mon, 14 Dec 2009 13:22:57 +0000 Subject: [PATCH] added support for multiple msp command line listeners --- .../src/se/sics/cooja/mspmote/MspMote.java | 40 +++++++++---------- .../se/sics/cooja/mspmote/plugins/MspCLI.java | 28 ++++--------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java index d7934f305..f73ca00f8 100644 --- a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMote.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MspMote.java,v 1.36 2009/12/02 17:12:32 fros4943 Exp $ + * $Id: MspMote.java,v 1.37 2009/12/14 13:22:57 fros4943 Exp $ */ package se.sics.cooja.mspmote; @@ -81,7 +81,7 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc private Simulation simulation; private CommandHandler commandHandler; - private LineListener commandListener; + private ArrayList commandListeners = new ArrayList(); private MSP430 myCpu = null; private MspMoteType myMoteType = null; private MspMoteMemory myMemory = null; @@ -148,11 +148,15 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc } public boolean hasCLIListener() { - return this.commandListener != null; + return !commandListeners.isEmpty(); } - public void setCLIListener(LineListener listener) { - this.commandListener = listener; + public void addCLIListener(LineListener listener) { + commandListeners.add(listener); + } + + public void removeCLIListener(LineListener listener) { + commandListeners.remove(listener); } /** @@ -237,11 +241,12 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc */ protected void prepareMote(File fileELF, GenericNode node) throws IOException { LineOutputStream lout = new LineOutputStream(new LineListener() { - @Override public void lineRead(String line) { - LineListener listener = commandListener; - if (listener != null) { - listener.lineRead(line); + for (LineListener l: commandListeners.toArray(new LineListener[0])) { + if (l == null) { + continue; + } + l.lineRead(line); } }}); PrintStream out = new PrintStream(lout); @@ -374,20 +379,15 @@ public abstract class MspMote extends AbstractEmulatedMote implements Mote, Watc }*/ } - private void sendCLICommandAndPrint(String comamnd) { - /* Backup listener */ - LineListener oldListener = commandListener; - - - setCLIListener(new LineListener() { + private void sendCLICommandAndPrint(String cmd) { + LineListener tmp = new LineListener() { public void lineRead(String line) { logger.fatal(line); } - }); - sendCLICommand(comamnd); - - /* Restore listener */ - setCLIListener(oldListener); + }; + commandListeners.add(tmp); + sendCLICommand(cmd); + commandListeners.remove(tmp); } public int getID() { diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/MspCLI.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/MspCLI.java index 63d28d361..5be5479a4 100644 --- a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/MspCLI.java +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/plugins/MspCLI.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: MspCLI.java,v 1.3 2009/09/17 13:19:08 fros4943 Exp $ + * $Id: MspCLI.java,v 1.4 2009/12/14 13:23:18 fros4943 Exp $ */ package se.sics.cooja.mspmote.plugins; @@ -166,29 +166,17 @@ public class MspCLI extends VisPlugin { }); panel.add(commandField, BorderLayout.SOUTH); - if (mspMote.hasCLIListener()) { - logArea.setText("*** CLI already occupied by another listener ***"); - } else { - myListener = new LineListener() { - @Override - public void lineRead(String line) { - addCLIData(line); - } - }; - mspMote.setCLIListener(myListener); - } - // Tries to select this plugin - try { - setSize(400, 200); - setSelected(true); - } catch (java.beans.PropertyVetoException e) { - // Could not select - } + myListener = new LineListener() { + public void lineRead(String line) { + addCLIData(line); + } + }; + mspMote.addCLIListener(myListener); } public void closePlugin() { if (myListener != null) { - mspMote.setCLIListener(null); + mspMote.addCLIListener(null); } }