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

📄 messageinjector.java

📁 用于传感器网络的节点操作系统 TinyOS 结构设计非常有意思
💻 JAVA
字号:
/*                                                                      tab:4 * * * "Copyright (c) 2000-2002 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, the following two paragraphs and the author 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:  Philip Levis  <pal@cs.berkeley.edu> * *//** * MessageInjector is a Java GUI for filling in and sending TinyOS * packets. */package net.tinyos.message;import java.awt.*;import java.awt.event.*;import java.io.IOException;import java.lang.reflect.*;import java.util.*;import javax.swing.*;import javax.swing.text.*;import javax.swing.event.*;import net.tinyos.util.*;public class MessageInjector extends JFrame {    private JScrollPane pane;    private JPanel buttonPanel;    private JPanel nestedPanel;    private MessageSelectionPanel selection;    private Sender sender;    public MessageInjector() {	super("TinyOS Message Injector");	try {	    selection = new MessageSelectionPanel();	    SerialForwarderStub stub = new SerialForwarderStub("localhost", 9000);	    stub.Open();	    sender = new Sender(stub, 0x7d);	    initialize();	}	catch (IOException exception) {	    exception.printStackTrace();	}    }    public MessageInjector(MessageSelectionPanel panel) {	super("TinyOS Message Injector");	try {	    selection = panel;	    SerialForwarderStub stub = new SerialForwarderStub("localhost", 9000);	    stub.Open();	    sender = new Sender(stub, 0x7d);	    initialize();    	}	catch (IOException exception) {	    exception.printStackTrace();	}    }    public MessageInjector(SerialStub stub) throws Exception {	super("TinyOS Message Injector");	stub.Open();	selection = new MessageSelectionPanel();	sender = new Sender(stub, 0x7d);	initialize();    }        public MessageInjector(MessageSelectionPanel panel,			   SerialStub stub) throws Exception {	super("TinyOS Message Injector");	stub.Open();	selection = panel;	sender = new Sender(stub, 0x7d);	initialize();    }    public MessageInjector(SerialStub stub, int gid) throws Exception {	super("TinyOS Message Injector");	stub.Open();	selection = new MessageSelectionPanel();	sender = new Sender(stub, gid);	initialize();    }    public MessageInjector(MessageSelectionPanel panel,			   SerialStub stub,			   int gid) throws Exception {	super("TinyOS Message Injector");	selection = panel;	sender = new Sender(stub, gid);	stub.Open();	initialize();    }    private void initialize() {	nestedPanel = new JPanel();	nestedPanel.setForeground(Color.black);	nestedPanel.setLayout(new BoxLayout(nestedPanel, BoxLayout.Y_AXIS));	selection.setForeground(Color.black);	JScrollPane pane = new JScrollPane(selection);	Dimension size = pane.getPreferredSize();	if (size.getHeight() > 380) {	    size.setSize(size.getWidth(), 400);	}	else {	    size.setSize(size.getWidth(), size.getHeight() + 20);	}	if (size.getWidth() > 980) {	    size.setSize(1000, size.getHeight());	}	else {	    size.setSize(size.getWidth() + 20, size.getHeight());	}	pane.setPreferredSize(size);	nestedPanel.add(pane);	buttonPanel = new ButtonPanel(selection, sender);	nestedPanel.add(buttonPanel);	getContentPane().add(nestedPanel);	pack();	setVisible(true);    }    private class ButtonPanel extends JPanel {	MessageSelectionPanel panel;	Sender sender;		JButton quitButton;	JLabel label;	JTextPane text;	JButton sendButton;		public ButtonPanel(MessageSelectionPanel panel, Sender sender) {	    this.panel = panel;	    this.sender = sender;	    this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));	    	    quitButton = new JButton("Quit");	    quitButton.addActionListener(new QuitListener());	    label = new JLabel("Mote ID");	    label.setForeground(Color.black);	    	    text = new JTextPane(new LimitedStyledDocument(4));	    text.setText("ffff");	    	    sendButton = new JButton("Send");	    sendButton.addActionListener(new InjectListener(panel, sender, text));	    add(sendButton);	    add(label);	    add(text);	    add(quitButton);	}	    }    protected class QuitListener implements ActionListener {	public QuitListener() {}		public void actionPerformed(ActionEvent e) {	    System.exit(0);	}    }    protected class InjectListener implements ActionListener {	private MessageSelectionPanel panel;	private Sender sender;	private JTextPane text;		public InjectListener(MessageSelectionPanel panel, Sender sender, JTextPane text) {	    this.panel = panel;	    this.sender = sender;	    this.text = text;	}		public void actionPerformed(ActionEvent e) {	    try {		int moteID = Integer.parseInt(text.getText(), 16);	        Message msg = panel.getMessage();		sender.send(moteID, msg);		System.out.println("Sending packet to address 0x" + text.getText() + ":\n" + msg);		System.out.print("Raw bytes: ");		byte[] data  = msg.dataGet();		for (int i = 0; i < data.length; i++) {		    String datum = Integer.toHexString((int)(data[i] & 0xff));		    if (datum.length() < 2) {datum = "0" + datum;}		    if (datum.length() < 2) {datum = "0" + datum;}		    datum += " ";		    System.out.print(datum);		}		System.out.println();	    }	    catch (Exception exception) {		System.err.println(exception);		//exception.printStackTrace();	    }	}    }    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 MessageInjector with group id 0x" + Integer.toHexString(gid));	    MessageInjector window = new MessageInjector(stub, gid);	}	catch (Exception e) {	    e.printStackTrace();	}    }    private static void usage() {	System.err.println("usage: MessageInjector [-h|--help|-gid=gid|[-sf=port|-serial=port]");    }    }

⌨️ 快捷键说明

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