📄 scripter.java
字号:
moteIF.send(moteID, chunk); System.out.print("+"); } System.out.println(); programTable.put(selector.getSelected().toLowerCase(), programArea.getText()); compiledProgramTable.put(selector.getSelected().toLowerCase(), prog); gcSharedVariables(); gcBuffers(); variablePanel.setVariables(SymbolTable.getSharedAndBuffers()); } /* See if any shared variables are no longer in use. If this is the case, clear them. */ private void gcSharedVariables() { System.out.println("Garbage collecting variables"); Enumeration vars = SymbolTable.getSharedVariables().elements(); while (vars.hasMoreElements()) { // For each variable boolean used = false; String var = (String)vars.nextElement(); Enumeration progs = compiledProgramTable.keys(); //System.out.println("Checking variable " + var); while (progs.hasMoreElements()) { // In each program String progName = (String)progs.nextElement(); //System.out.println(" Checking program " + progName); Program prog = (Program)compiledProgramTable.get(progName); Enumeration referenced = prog.getSharedVariables().elements(); while (referenced.hasMoreElements()) { SharedDeclaration decl = (SharedDeclaration)referenced.nextElement(); //System.out.print(" Checking reference " + decl.getName() + ":"); if (decl.getName().toLowerCase().equals(var.toLowerCase())) { used = true; //System.out.println(" used"); break; } //System.out.println(); } if (used) { break; } } if (!used) { //System.err.println("Revoking shared variable " + var); SymbolTable.revokeSharedVariable(var); } } } private void gcBuffers() { System.out.println("Garbage collecting variables"); Enumeration vars = SymbolTable.getBuffers().elements(); while (vars.hasMoreElements()) { // For each variable boolean used = false; String var = (String)vars.nextElement(); Enumeration progs = compiledProgramTable.keys(); //System.out.println("Checking variable " + var); while (progs.hasMoreElements()) { // In each program String progName = (String)progs.nextElement(); //System.out.println(" Checking program " + progName); Program prog = (Program)compiledProgramTable.get(progName); Enumeration referenced = prog.getBuffers().elements(); while (referenced.hasMoreElements()) { BufferDeclaration decl = (BufferDeclaration)referenced.nextElement(); //System.out.print(" Checking reference " + decl.getName() + ":"); if (decl.getName().toLowerCase().equals(var.toLowerCase())) { used = true; //System.out.println(" used"); break; } //System.out.println(); } if (used) { break; } } if (!used) { //System.err.println("Revoking shared variable " + var); SymbolTable.revokeBuffer(var); } } } private void incrementVersion() { Integer iVal = new Integer(versionText.getText()); int i = iVal.intValue(); i++; versionText.setText("" + i); } protected String getProgramText() { return programArea.getText(); } protected void changeToCapsule(String capsule) { String version = (String)versionTable.get(capsule.toLowerCase()); if (version == null || version.equals("")) { version = "0"; } versionText.setText(version); programArea.setText((String)programTable.get(capsule.toLowerCase())); } private void createMotePanel() { moteIDPanel = new JPanel(); moteIDPanel.setBorder(new EtchedBorder()); moteIDPanel.setLayout(new BoxLayout(moteIDPanel, BoxLayout.X_AXIS)); moteIDPanel.setAlignmentX(CENTER_ALIGNMENT); moteIDLabel = new JLabel("Mote ID"); moteIDLabel.setBorder(new EmptyBorder(0, 10, 0, 10)); moteIDLabel.setFont(TinyLook.boldFont()); 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)); versionLabel = new JLabel("Handler Version"); versionLabel.setBorder(new EmptyBorder(0, 10, 0, 10)); versionLabel.setFont(TinyLook.boldFont()); versionText = new JTextField("0", 4); moteIDPanel.add(versionLabel); moteIDPanel.add(versionText); versionText.setMaximumSize(new Dimension(50, 25)); versionText.setMinimumSize(new Dimension(50, 25)); } private void createVersionPanel() { versionPanel = new JPanel(); //versionPanel.setLayout(new BoxLayout(versionPanel, BoxLayout.X_AXIS)); //versionPanel.setBorder(new EmptyBorder(5, 0, 10, 0)); //versionPanel.setAlignmentX(LEFT_ALIGNMENT); } private void createSelectionPanel() { selectionPanel = new JPanel(); JPanel subLeftPanel = new JPanel(); JPanel subRightPanel = new JPanel(); subLeftPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); subLeftPanel.setLayout(new BoxLayout(subLeftPanel, BoxLayout.Y_AXIS)); subRightPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); subRightPanel.setLayout(new BoxLayout(subRightPanel, BoxLayout.Y_AXIS)); selectionPanel.setLayout(new BoxLayout(selectionPanel, BoxLayout.X_AXIS)); selectionPanel.setBorder(new EtchedBorder()); selectorLabel = new JLabel("Event Handler"); selectorLabel.setFont(TinyLook.boldFont()); selectorLabel.setBorder(new EmptyBorder(0, 0, 10, 0)); selector = new CapsuleSelector(capsuleMap, this); selectorLabel.setAlignmentY(TOP_ALIGNMENT); selector.setAlignmentY(TOP_ALIGNMENT); optionLabel = new JLabel("Handler Options"); optionLabel.setFont(TinyLook.boldFont()); optionLabel.setBorder(new EmptyBorder(0, 0, 10, 0)); options = new OptionSelector(optionMap); 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(CENTER_ALIGNMENT); } private void createButtonPanel(Font font) { injectButton = new InjectButton(this); injectButton.setFont(font); injectButton.setAlignmentX(CENTER_ALIGNMENT); compileButton = new CompileButton(this); compileButton.setFont(font); compileButton.setAlignmentX(RIGHT_ALIGNMENT); compileButton.setEnabled(false); buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2,1)); buttonPanel.add(injectButton); buttonPanel.add(compileButton); buttonPanel.setAlignmentX(CENTER_ALIGNMENT); buttonPanel.setBorder(new EtchedBorder()); } private void startListener(String source) { moteIF = new MoteIF(BuildSource.makePhoenix(source, PrintStreamMessenger.err)); moteIF.registerListener(new BombillaErrorMsg(), this); moteIF.start(); } private class QuitButton extends JButton { private Scripter scripter; public QuitButton(Scripter s) { super("Quit"); scripter = s; addActionListener(new QuitActionListener(s)); setAlignmentX(CENTER_ALIGNMENT); } } private class QuitActionListener implements ActionListener { private Scripter scripter; public QuitActionListener(Scripter s) { scripter = s; } public void actionPerformed(ActionEvent e) { scripter.writeState(); System.exit(0); } } private class InjectButton extends JButton { public InjectButton(Scripter inject) { super("Inject"); addActionListener(new InjectListener(inject)); setAlignmentX(CENTER_ALIGNMENT); } private class InjectListener implements ActionListener { private Scripter injector; public InjectListener(Scripter 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("ERROR: Invalid instruction: " + exception); } } } } private class CompileButton extends JButton { public CompileButton(Scripter scripter) { super("Compile to Primitive"); addActionListener(new CompileListener(scripter)); setAlignmentX(RIGHT_ALIGNMENT); } private class CompileListener implements ActionListener { private Scripter scripter; public CompileListener(Scripter scripter) { this.scripter = scripter; } public void actionPerformed(ActionEvent e) { try { String text = scripter.getProgramText(); VMDescriptionDialog desc = new VMDescriptionDialog(scripter); OpcodeSaveDialog osDialog = new OpcodeSaveDialog(); int rval = osDialog.showSaveDialog(scripter); if (rval != JFileChooser.APPROVE_OPTION) {return;} CompositeOpcode composite = new CompositeOpcode(desc.getName(), text); String name = desc.getName(); osDialog.writeModule(name, composite.getModule()); osDialog.writeConfiguration(name, composite.getConfiguration()); osDialog.writeDescription(name, desc.getDesc()); } catch (IOException exception) { System.err.println("ERROR: Couldn't build instruction: " + exception); } catch (InvalidInstructionException exception) { System.err.println("Invalid instruction: " + exception.getMessage()); } catch (Exception exception) { System.err.println("Exception thrown when trying to create composite opcode."); exception.printStackTrace(); } } } } private class OpcodeSaveDialog extends JFileChooser { public OpcodeSaveDialog() { super("Save Composite Opcode"); setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); } protected void writeModule(String name, String text) throws IOException { File dir = getSelectedFile(); File module = new File(dir.getAbsolutePath() + "/OP" + name + "M.nc"); FileWriter writer = new FileWriter(module); System.err.println("Writing " + module); writer.write(text); writer.close(); } protected void writeConfiguration(String name, String text) throws IOException { File dir = getSelectedFile(); File conf = new File(dir.getAbsolutePath() + "/OP" + name + ".nc"); FileWriter writer = new FileWriter(conf); System.err.println("Writing " + conf); writer.write(text); writer.close(); } protected void writeDescription(String name, String desc) throws IOException { File dir = getSelectedFile(); File odf = new File(dir.getAbsolutePath() + "/OP" + name + ".odf"); FileWriter writer = new FileWriter(odf); String msg = "<PRIMITIVE "; msg += "NAME=" + name.toUpperCase() + " "; msg += "OPCODE=" + name + " "; msg += "DESC=\"" + desc + "\">"; System.err.println("Writing " + odf); writer.write(msg); writer.close(); } } public static void main(String[] args) { try { int index = 0; String source = "sf@localhost:9001"; String filename = "vm.vmdf"; String progName = "programs.vmdf"; while (index < args.length) { String arg = args[index]; if (arg.equals("-h") || arg.equals("--help")) { usage(); System.exit(0); } else if (arg.equals("-comm")) { index++; source = args[index]; } else { usage(); System.exit(1); } index++; } System.out.println("Starting Scripter with source " + source); Scripter window = new Scripter(source, filename, progName); } catch (Exception e) { System.err.println(e); System.err.println(); System.err.println("ERROR: Could not create a Scripter. Are you in an application directory?"); } } private static void usage() { System.err.println("usage: Scripter [-h|--help|-comm <source>] (default is SerialForwarder 1.1)"); } protected void writeState() { System.out.println("Writing state."); try { Writer writer = new FileWriter("programs.vmdf"); Enumeration handlers = versionTable.keys(); Date d = new Date(); writer.write("// Generated for " + config.vmName() + " at " + d + "\n"); while (handlers.hasMoreElements()) { String name = (String)handlers.nextElement(); writer.write("<HANDLER "); writer.write("name=\"" + name + "\" "); writer.write("version=\"" + versionTable.get(name.toLowerCase()) + "\" "); writer.write("code=\"" + programTable.get(name.toLowerCase()) + "\" "); writer.write(">\n"); } Enumeration vars = SymbolTable.getSharedVariables().elements(); while (vars.hasMoreElements()) { String var = (String)vars.nextElement(); writer.write("<VARIABLE "); writer.write("name=\"" + var + "\" "); writer.write("val=\"" + SymbolTable.getShared(var) + "\" "); writer.write(">\n"); } vars = SymbolTable.getBuffers().elements(); while (vars.hasMoreElements()) { String var = (String)vars.nextElement(); writer.write("<BUFFER "); writer.write("name=\"" + var + "\" "); writer.write("val=\"" + SymbolTable.getShared(var) + "\" "); writer.write(">\n"); } writer.write("\n"); writer.close(); } catch (IOException exception) { System.err.println("Error writing out state."); exception.printStackTrace(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -