⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capsuleinjector.java

📁 用于传感器网络的节点操作系统 TinyOS 结构设计非常有意思
💻 JAVA
字号:
/*									tab:2 * * * "Copyright (c) 2002 and The Regents of the University  * of California.  All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright * notice and the following two paragraphs appear in all copies of * this software. *  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Authors:	Phil Levis <pal@cs.berkeley.edu> * Date:        Aug 21 2002 * Desc:        Main window for Bombilla code injector. * */package net.tinyos.vm_asm;import java.awt.*;import java.awt.event.*;import java.awt.font.*;import java.io.*;import java.net.*;import java.util.*;import javax.swing.*;import net.tinyos.message.*;import net.tinyos.util.*;public class CapsuleInjector extends JFrame implements MessageListener {    private JPanel      leftPanel;    private JPanel      moteIDPanel;    private JLabel      moteIDLabel;    private JTextField   moteIDText;        private JPanel      versionPanel;    private JLabel      versionLabel;    private JTextField  versionText;    private JLabel      spacerLabel;        private JPanel      selectionPanel;    private JLabel      selectorLabel;    private JLabel      optionLabel;    private CapsuleSelector selector;    private OptionSelector options;    private JPanel      buttonPanel;    private JButton     injectButton;    private JButton     quitButton;        private JPanel      rightPanel;    private JLabel      programLabel;    private JPanel      programLabelPanel;    private JTextArea   programArea;    private MoteIF      moteIF;        private ErrorDialog dialog = null;    private String context = "";    private String cause = "";    private String capsule = "";    private String instruction = "";        private BombillaAssembler assembler;            public CapsuleInjector(SerialStub stub, int gid) throws Exception {	super("TinyOS VM Code Injector");	moteIF = new MoteIF(stub, gid, MoteIF.maxMessageSize, false);	assembler = new BombillaAssembler();		Font font = new Font("Courier", Font.PLAIN, 12);		programArea = new JTextArea(24, 16);	programArea.setFont(font);	programLabel = new JLabel("Program Text");	programLabel.setAlignmentX(LEFT_ALIGNMENT);	programLabelPanel = new JPanel();	programLabelPanel.add(programLabel);		leftPanel = new JPanel();	leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));	leftPanel.setFont(font);		createMotePanel();	createVersionPanel();	spacerLabel = new JLabel("       ");	createSelectionPanel();	createButtonPanel(font);		leftPanel.add(moteIDPanel);	leftPanel.add(versionPanel);	leftPanel.add(spacerLabel);	leftPanel.add(selectionPanel);	leftPanel.add(buttonPanel);		rightPanel = new JPanel();		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));	rightPanel.add(programLabelPanel);	rightPanel.add(programArea);	rightPanel.setFont(font);		getContentPane().setLayout(new GridLayout(0, 2));		getContentPane().add(leftPanel);	getContentPane().add(rightPanel);	setVisible(true);	startListener(stub);	pack();    }    private String getType(int type) {	switch (type) {	case 0:	    return "SUBROUTINE 0";	case 1:	    return "SUBROUTINE 1";	case 2:	    return "SUBROUTINE 2";	case 3:	    return "SUBROUTINE 3";	case 64:	    return "CLOCK";	case 65:	    return "SEND";	case 66:	    return "RECEIVE";	case 67:	    return "ONCE";	default:	    return "UNKNOWN: " + type;	}    }        private String getCause(int cause) {	switch(cause) {	case 0:	    return "TRIGGERED";	case 1:	    return "INVALID_RUNNABLE";	case 2:	    return "STACK_OVERFLOW ";	case 3:	    return "STACK_UNDERFLOW";	case 4:	    return "BUFFER_OVERFLOW";	case 5:	    return "BUFFER_UNDERFLOW";	case 6:	    return "INDEX_OUT_OF_BOUNDS";	case 7:	    return "INSTRUCTION_RUNOFF ";	case 8:	    return "LOCK_INVALID";	case 9:	    return "LOCK_STEAL";	case 10:	    return "UNLOCK_INVALID";	case 11:	    return "QUEUE_ENQUEUE";	case 12:	    return "QUEUE_DEQUEUE";	case 13:	    return "QUEUE_REMOVE";	case 14:	    return "QUEUE_INVALID";	case 15:	    return "RSTACK_OVERFLOW";	case 16:	    return "RSTACK_UNDERFLOW";	case 17:	    return "INVALID_ACCESS";	case 18:	    return "TYPE_CHECK";	case 19:	    return "INVALID_TYPE";	case 20:	    return "INVALID_LOCK";	case 21:	    return "INVALID_INSTRUCTION";	default:	    return "UNKNOWN ERROR TYPE: " + cause;	}    }        public void messageReceived(int to, Message m) {	try {	    BombillaErrorMsg msg = (BombillaErrorMsg)m;	    String context = getType(msg.get_context());	    String cause = getCause((int)msg.get_reason());	    String capsule = getType(msg.get_capsule());	    String instruction = "" + msg.get_instruction();	    if ((!this.context.equals(context)) ||		(!this.cause.equals(cause)) ||		(!this.capsule.equals(capsule)) ||		(!this.instruction.equals(instruction))) {		this.context = context;		this.cause = cause;		this.capsule = capsule;		this.instruction = instruction;		System.out.println("Error received:");		System.out.println("  Context:     " + context);		System.out.println("  Cause:       " + cause);		System.out.println("  Capsule:     " + capsule);		System.out.println("  Instruction: " + instruction);		System.out.println();		updateDialog(context, cause, capsule, instruction);	    }	}	catch (ClassCastException e) {	    System.err.println("Erroneously received a non-error message.");	    System.err.println(m);	}	catch (Exception e) {	    System.err.println("Exception thrown when receiving packets.");	    e.printStackTrace();	}    }    private void updateDialog(String context, String cause, String capsule, String instruction) {	Point p = null;	if (dialog != null) {	    p = dialog.getLocation();	    dialog.dispose();	}	dialog = new ErrorDialog(context, cause, capsule, instruction);	if (p != null) {	    dialog.setLocation(p);	}		dialog.show();    }        protected void inject() throws IOException, InvalidInstructionException  {	String program = programArea.getText();	StringReader reader = new StringReader(program);	ProgramTokenizer tok = new ProgramTokenizer(reader);	byte[] code = assembler.toByteCodes(tok);	reader = new StringReader(program);	tok = new ProgramTokenizer(reader);	String codeStr = assembler.toHexString(tok);	System.out.println("Sending program: " + codeStr);		short type = selector.getType();	int version = Integer.parseInt(versionText.getText(), 16);		BombillaCapsuleMsg msg = new BombillaCapsuleMsg();	msg.set_type(type);	msg.set_options(options.getOptions());	msg.set_version((char)(version & 0xffff));	byte[] sCode = new byte[code.length];	for (int i = 0; i < code.length; i++) {	    sCode[i] = code[i];	}	msg.set_code(sCode);		int moteID = Integer.parseInt(moteIDText.getText(), 16);	moteIF.send(moteID, msg);    }        private void createMotePanel() {	moteIDPanel = new JPanel();	moteIDPanel.setLayout(new BoxLayout(moteIDPanel, BoxLayout.X_AXIS));	moteIDLabel = new JLabel("Mote ID");	moteIDText = new JTextField("0", 4);	moteIDText.setFont(new Font("Courier", Font.PLAIN, 12));	moteIDPanel.add(moteIDLabel);	moteIDPanel.add(moteIDText);	moteIDText.setMaximumSize(new Dimension(34, 25));	moteIDText.setMinimumSize(new Dimension(34, 25));	moteIDPanel.setAlignmentX(RIGHT_ALIGNMENT);    }        private void createVersionPanel() {	versionPanel = new JPanel();	versionPanel.setLayout(new BoxLayout(versionPanel, BoxLayout.X_AXIS));		versionLabel = new JLabel("Capsule Version");	versionText = new JTextField("0", 4);	versionPanel.add(versionLabel);	versionPanel.add(versionText);	versionText.setMaximumSize(new Dimension(50, 25));	versionText.setMinimumSize(new Dimension(50, 25));	versionPanel.setAlignmentX(RIGHT_ALIGNMENT);    }    private void createSelectionPanel() {	selectionPanel = new JPanel();	JPanel subLeftPanel = new JPanel();	JPanel subRightPanel = new JPanel();	subLeftPanel.setLayout(new BoxLayout(subLeftPanel, BoxLayout.Y_AXIS));	subRightPanel.setLayout(new BoxLayout(subRightPanel, BoxLayout.Y_AXIS));	selectionPanel.setLayout(new BoxLayout(selectionPanel, BoxLayout.X_AXIS));		selectorLabel = new JLabel("Capsule Type");	selector = new CapsuleSelector();	selectorLabel.setAlignmentY(TOP_ALIGNMENT);	selector.setAlignmentY(TOP_ALIGNMENT);		optionLabel = new JLabel("Capsule Options");	options = new OptionSelector();	optionLabel.setAlignmentY(TOP_ALIGNMENT);	options.setAlignmentY(TOP_ALIGNMENT);	subLeftPanel.add(selectorLabel);	subLeftPanel.add(selector);	subRightPanel.add(optionLabel);	subRightPanel.add(options);	subLeftPanel.setAlignmentY(TOP_ALIGNMENT);	subRightPanel.setAlignmentY(TOP_ALIGNMENT);	subRightPanel.setAlignmentX(RIGHT_ALIGNMENT);		selectionPanel.add(subLeftPanel);	selectionPanel.add(subRightPanel);	selectionPanel.setAlignmentX(RIGHT_ALIGNMENT);    }    private void createButtonPanel(Font font) {	injectButton = new InjectButton(this);	injectButton.setFont(font);	injectButton.setAlignmentX(LEFT_ALIGNMENT);		quitButton = new QuitButton();	quitButton.setFont(font);	injectButton.setAlignmentX(RIGHT_ALIGNMENT);		buttonPanel = new JPanel();	buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));	buttonPanel.add(injectButton);	buttonPanel.add(quitButton);    }    private void startListener(SerialStub stub) {	moteIF.registerListener(new BombillaErrorMsg(), this);	moteIF.start();    }    private class QuitButton extends JButton {	public QuitButton() {	    super("Quit");	    addActionListener(new QuitListener());	    setAlignmentX(CENTER_ALIGNMENT);	}	private class QuitListener implements ActionListener {	    public void actionPerformed(ActionEvent e) {		System.exit(0);	    }	}    }    private class InjectButton extends JButton {	public InjectButton(CapsuleInjector inject) {	    super("Inject");	    addActionListener(new InjectListener(inject));	    setAlignmentX(CENTER_ALIGNMENT);	}	private class InjectListener implements ActionListener {	    private CapsuleInjector injector;	    	    public InjectListener(CapsuleInjector injector) {		this.injector = injector;	    }	    public void actionPerformed(ActionEvent e) {		try {		    injector.inject();		}		catch (IOException exception) {		    System.err.println("ERROR: Couldn't inject packet: " + exception);		}		catch (InvalidInstructionException exception) {		    System.err.println("Invalid instruction: " + exception.getMessage());		}	    }	}	    }    public static void main(String[] args) {	try {	    int gid = 0x7d;	    SerialStub stub = stub = new SerialForwarderStub("localhost", 9000);	    int index = 0;	    while (index < args.length) {		String arg = args[index];		if (arg.equals("-h") || arg.equals("--help")) {		    usage();		    System.exit(0);		}		if ((arg.length() > 4) && 		    arg.substring(0, 4).equals("-sf=")) {		    String port = arg.substring(4);		    short sval = (short)Integer.parseInt(port);		    stub = new SerialForwarderStub("localhost", sval);		}		else if ((arg.length() > 8) && 			 arg.substring(0, 8).equals("-serial=")) {		    String port = arg.substring(8);		    System.out.println("Opening port: " + port);		    stub = new SerialPortStub(port);		}		else if ((arg.length() > 4) &&			 (arg.substring(0,5).equals("-gid="))) {		    gid = Integer.parseInt(arg.substring(5));		}		else {		    usage();		    System.exit(1);		}		index++;	    }	    System.out.println("Starting CapsuleInjector with group id 0x" + Integer.toHexString(gid));	    CapsuleInjector window = new CapsuleInjector(stub, gid);	} 		catch (Exception e) {	    e.printStackTrace();	}    }        private static void usage() {	System.err.println("usage: CapsuleInjector [-h|--help|-gid=gid|-sf=port|-serial=port]");    }        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -