Commit in jevio-base on MAIN | |||
pom.xml | +6 | -1 | 1.11 -> 1.12 |
src/main/java/org/jlab/coda/jevio/graphics/EventTreeFrame.java | +164 | added 1.1 | |
/EventTreeMenu.java | +1237 | added 1.1 | |
/cMsgHandler.java | +391 | added 1.1 | |
src/main/java/org/jlab/coda/jevio/test/cMsgEventProducer.java | +323 | added 1.1 | |
+2121 | -1 |
add classes from jevio that depend on cMsg which has now been deployed to maven repo
diff -u -r1.11 -r1.12 --- pom.xml 28 Feb 2012 19:41:57 -0000 1.11 +++ pom.xml 8 Mar 2012 21:05:14 -0000 1.12 @@ -36,7 +36,7 @@
<configuration> <source>1.5</source> <target>1.5</target>
- <showDeprecation>true</showDeprecation>
+ <showDeprecation>false</showDeprecation>
</configuration> </plugin> <plugin>
@@ -54,5 +54,10 @@
</plugins> </build> <dependencies>
+ <dependency> + <groupId>org.lcsim</groupId> + <artifactId>cmsg</artifactId> + <version>3.0</version> + </dependency>
</dependencies> </project>
diff -N EventTreeFrame.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ EventTreeFrame.java 8 Mar 2012 21:05:14 -0000 1.1 @@ -0,0 +1,164 @@
+package org.jlab.coda.jevio.graphics; + + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.EventQueue; +import java.awt.Toolkit; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.UIManager.LookAndFeelInfo; + +/** + * This is a simple GUI that displays an evio event in a tree. It allows the user to open event files and + * dictionaries, and go event-by-event showing the event in a JTree. It can also view an evio event contained + * in a cMsg message using any dictionary also contained in the message. + * @author heddle + */ +@SuppressWarnings("serial") +public class EventTreeFrame extends JFrame { + + /** + * Constructor for a simple tree viewer for evio files. + */ + public EventTreeFrame() { + super("Jevio Event Tree"); + initializeLookAndFeel(); + + // set the close to call System.exit + WindowAdapter wa = new WindowAdapter() { + @Override + public void windowClosing(WindowEvent event) { + System.out.println("Exiting."); + System.exit(0); + } + }; + addWindowListener(wa); + + // add all the components + addComponents(); + + // size to the screen + sizeToScreen(this, 0.85); + } + + /** + * Add the components to the frame. + */ + protected void addComponents() { + // Panel which holds the tree display and related widgets + EventTreePanel eventTreePanel = new EventTreePanel(); + addMenus(eventTreePanel); + add(eventTreePanel, BorderLayout.CENTER); + } + + /** + * Add the menus to the frame. + * @param eventTreePanel panel which holds the tree display and related widgets. + */ + protected void addMenus(EventTreePanel eventTreePanel) { + JMenuBar menuBar = new JMenuBar(); + EventTreeMenu eventTreeMenu = new EventTreeMenu(eventTreePanel); + menuBar.add(eventTreeMenu.createFileMenu()); + menuBar.add(eventTreeMenu.createViewMenu()); + menuBar.add(eventTreeMenu.createEventMenu()); + setJMenuBar(menuBar); + } + + + /** + * Size and center a JFrame relative to the screen. + * + * @param frame the frame to size. + * @param fractionalSize the fraction desired of the screen--e.g., 0.85 for 85%. + */ + private void sizeToScreen(JFrame frame, double fractionalSize) { + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + d.width = (int) (fractionalSize * .65 * d.width); + d.height = (int) (fractionalSize * d.height); + frame.setSize(d); + centerComponent(frame); + } + + /** + * Center a component. + * + * @param component the Component to center. + */ + private void centerComponent(Component component) { + + if (component == null) + return; + + try { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + Dimension componentSize = component.getSize(); + if (componentSize.height > screenSize.height) { + componentSize.height = screenSize.height; + } + if (componentSize.width > screenSize.width) { + componentSize.width = screenSize.width; + } + + int x = ((screenSize.width - componentSize.width) / 2); + int y = ((screenSize.height - componentSize.height) / 2); + + component.setLocation(x, y); + + } + catch (Exception e) { + component.setLocation(200, 200); + e.printStackTrace(); + } + } + + /** + * Initialize the look and feel. + */ + private void initializeLookAndFeel() { + + LookAndFeelInfo[] lnfinfo = UIManager.getInstalledLookAndFeels(); + + if ((lnfinfo == null) || (lnfinfo.length < 1)) { + return; + } + + String desiredLookAndFeel = "Windows"; + + for (int i = 0; i < lnfinfo.length; i++) { + if (lnfinfo[i].getName().equals(desiredLookAndFeel)) { + try { + UIManager.setLookAndFeel(lnfinfo[i].getClassName()); + } + catch (Exception e) { + e.printStackTrace(); + } + return; + } + } + } + + + /** + * Main program for launching the frame. + * + * @param args command line arguments--ignored. + */ + public static void main(String args[]) { + + final EventTreeFrame frame = new EventTreeFrame(); + + // now make the frame visible, in the AWT thread + EventQueue.invokeLater(new Runnable() { + + @Override + public void run() { + frame.setVisible(true); + } + + }); + + } + +}
\ No newline at end of file
diff -N EventTreeMenu.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ EventTreeMenu.java 8 Mar 2012 21:05:14 -0000 1.1 @@ -0,0 +1,1237 @@
+package org.jlab.coda.jevio.graphics; + +import java.awt.event.*; +import java.awt.*; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; +import javax.swing.border.LineBorder; +import javax.swing.border.EmptyBorder; + +import org.jlab.coda.jevio.*; +import org.jlab.coda.cMsg.cMsgException; + +/** + * This class creates the menus used in the GUI. + * @author Heddle + * @author Timmer + */ +public class EventTreeMenu { + + //---------------------- + // gui stuff + //---------------------- + + /** A menu item for selecting "next event". */ + private JMenuItem nextEventItem; + + /** A menu item for going to a specific event. */ + private JMenu gotoEventItem; + + /** Menu item for exporting file to XML. */ + private JMenuItem xmlExportItem; + + /** Menu item for opening event file. */ + private JMenuItem openEventFile; + + /** Menu item allowing configuration of cmsg and other event sources. */ + private JMenu sourceConfig; + + /** Menu item setting the number of the event (from a file) to be displayed. */ + private JTextField eventNumberInput; + + /** The panel that holds the tree and all associated widgets. */ + private EventTreePanel eventTreePanel; + + //---------------------- + // file stuff + //---------------------- + + /** + * Source of the evio events being displayed. + * By default this GUI is setup to look at files. + */ + private EventSource eventSource = EventSource.FILE; + + /** Last selected data file. */ + private String dataFilePath; + + /** Last selected dictionary file. */ + private String dictionaryFilePath; + + /** Last selected xml file to export event file into. */ + private String xmlFilePath; + + /** Filter so only files with specified extensions are seen in file viewer. */ + private FileNameExtensionFilter evioFileFilter; + + /** The reader object for the currently viewed evio file. */ + private EvioReader evioFileReader; + + //---------------------- + // dictionary stuff + //---------------------- + + /** Is the user-selected or cmsg/file-embedded dictionary currently used? */ + private boolean isUserDictionary; + + /** User-selected dictionary file. */ + private INameProvider userDictionary; + + /** Dictionary embedded with opened evio file. */ + private INameProvider fileDictionary; + + /** Dictionary embedded with cmsg message. */ + private INameProvider cmsgDictionary; + + /** Dictionary currently in use. */ + private INameProvider currentDictionary; + + //---------------------- + // cmsg stuff + //---------------------- + + /** Panel to handle input into CmsgHandler object. */ + private JPanel cmsgPanel; + + /** Object to handle cMsg communications. */ + private cMsgHandler cmsgHandler; + + /** Button to connect-to / disconnect-from cMsg server. */ + private JButton connectButton; + + /** Will pressing button connect-to or disconnect-from cMsg server? */ + private boolean buttonWillConnect = true; + + /** Thread to update cMsg queue size in GUI. */ + private UpdateThread cmsgUpdateThread; + + + + /** + * This class is a thread which updates the number of events existing in the queue + * and displays it every second. + */ + private class UpdateThread extends Thread { + // update queue size in GUI + Runnable r = new Runnable() { + public void run() { + if (cmsgHandler != null) { //TODO: make sure all values updated properly + eventTreePanel.getEventInfoPanel().setNumberOfEvents(cmsgHandler.getQueueSize()); + } + } + }; + + // update queue size in GUI every 1 second + public void run() { + while (true) { + if (isInterrupted()) { break; } + SwingUtilities.invokeLater(r); + try { Thread.sleep(1000); } + catch (InterruptedException e) { break; } + } + } + } + + + + /** + * Constructor. Holds the menus for a frame or internal frame that wants to manage a tree panel. + * @param eventTreePanel holds the tree and all associated the widgets. + */ + public EventTreeMenu(final EventTreePanel eventTreePanel) { + this.eventTreePanel = eventTreePanel; + } + + /** + * Get the main event display panel. + * @return main event display panel. + */ + public EventTreePanel getEventTreePanel() { + return eventTreePanel; + } + + + /** + * Switch between different event sources (file, cmsg, et). + */ + private void setEventSource(EventSource source) { + + // do nothing if same source already selected + if (source == eventSource) { + return; + } + + // remember the current source + eventSource = source; + + // clear display of any event + eventTreePanel.setEvent(null); + + switch (source) { + + case CMSG: + + // show "cMsg config" menu item + sourceConfig.setEnabled(true); + sourceConfig.removeAll(); + sourceConfig.add(cmsgPanel); + sourceConfig.setText("cMsg config"); + + // turn menu items off/on + openEventFile.setEnabled(false); + gotoEventItem.setEnabled(true); + nextEventItem.setEnabled(true); + if (xmlExportItem.isEnabled()) { + xmlExportItem.setEnabled(false); + } + + // update event info + eventTreePanel.getEventInfoPanel().setDisplay("cMsg messages", 0, 0, null); + + // start thread that tells how many messages are in queue + cmsgUpdateThread = new UpdateThread(); + cmsgUpdateThread.start(); + + break; + + case FILE: + + // interrupt cmsg queue message count thread + if (cmsgUpdateThread != null) { + cmsgUpdateThread.interrupt(); + } + + // disconnect from cMsg server (thru GUI button so GUI consistant with program) + if (!buttonWillConnect) { + connectButton.doClick(); + } + + EvioReader evioFile = evioFileReader; + + // turn menu items off/on + sourceConfig.setText(" "); + sourceConfig.setEnabled(false); + openEventFile.setEnabled(true); + if (evioFile == null) { + gotoEventItem.setEnabled(false); + nextEventItem.setEnabled(false); + } + else { + gotoEventItem.setEnabled(true); + nextEventItem.setEnabled(true); + xmlExportItem.setEnabled(true); + } + + // remove any cmsg dictionary used last + cmsgDictionary = null; + + // get values for display + String fileName = ""; + String dictSource = " "; + int eventCount = 0; + + if (evioFile != null) { + // switch data back to last file (which is still loaded) + fileName = dataFilePath; + + // switch dictionary back to last one loaded from a file + if (userDictionary != null) { + currentDictionary = userDictionary; + NameProvider.setProvider(currentDictionary); + dictSource = dictionaryFilePath; + eventTreePanel.getEventInfoPanel().setDictionary("from file"); + isUserDictionary = true; + } + + // get event count + try { + eventCount = evioFile.getEventCount(); + } + catch (EvioException e) { /* should never happen */ } + } + + // update display + eventTreePanel.getEventInfoPanel().setDisplay(fileName, 0, eventCount, dictSource); + + break; + + case ET: + + if (cmsgUpdateThread != null) { + cmsgUpdateThread.interrupt(); + } + if (!buttonWillConnect) { + connectButton.doClick(); + } + + sourceConfig.setText(" "); + sourceConfig.setEnabled(false); + eventTreePanel.getEventInfoPanel().setDisplay("", 0, 0, null); //TODO: remove?? + if (xmlExportItem.isEnabled()) { + xmlExportItem.setEnabled(false); + } + eventTreePanel.getEventInfoPanel().setDisplay("ET buffers", 0, 0, null); + + break; + + default: + } + } + + /** + * Create the event menu. + * + * @return the event menu. + */ + public JMenu createEventMenu() { + final JMenu menu = new JMenu(" Event "); + + // next event menu item + ActionListener al_ne = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + switch (eventSource) { + // If we're looking at a file, there are multiple events contained in it + case FILE: + if (evioFileReader != null) { + try { + EvioEvent event = evioFileReader.parseNextEvent(); + if (event != null) { + eventTreePanel.setEvent(event); + } + } + catch (EvioException e1) { + e1.printStackTrace(); + } + } + break; + + // If we're looking at cmsg messages, there is a queue of events + // extracted from the messages. + case CMSG: + if (cmsgHandler != null) { + // Get next event (messages not containing events are ignored). + EvioEvent event = cmsgHandler.getNextEvent(); + if (event != null) { + eventTreePanel.setEvent(event); + // If there's a dictionary in this event, use it and keep track. + if (event.hasDictionaryXML()) { + cmsgDictionary = NameProviderFactory.createNameProvider(event.getDictionaryXML()); + currentDictionary = cmsgDictionary; + NameProvider.setProvider(currentDictionary); + eventTreePanel.getEventInfoPanel().setDictionary("from cMsg message"); + eventTreePanel.refreshDescription(); + isUserDictionary = false; + } + } + } + break; + + default: + + } + } + }; + nextEventItem = new JMenuItem("Next Event"); + nextEventItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK)); + nextEventItem.addActionListener(al_ne); + nextEventItem.setEnabled(false); + menu.add(nextEventItem); + + // goto event dialog box (show with Ctrl-G) + // This used to be a menu item accelerator, but put it here since gotoEventItem changed + Action gotoEventAction = new AbstractAction("gotoEvent") { + public void actionPerformed(ActionEvent e) { + switch (eventSource) { + // If we're looking at a file, go to the specified event number + case FILE: + try { + String prompt = "Go to Event (1.." + evioFileReader.getEventCount() + ")"; + String inputValue = JOptionPane.showInputDialog(prompt); + if (inputValue != null) { + int eventNum = Integer.parseInt(inputValue); + if ((eventNum > 0) && (eventNum <= evioFileReader.getEventCount())) { + EvioEvent event = evioFileReader.gotoEventNumber(eventNum); + if (event != null) { + getEventTreePanel().setEvent(event); + } + } + } + } + catch (Exception e1) { + } + break; + + // If we're looking at cmsg messages, there is a queue of messages. + // Skip over (delete) events and go to the selected place in the queue. + case CMSG: + if (cmsgHandler != null) { + int qsize = cmsgHandler.getQueueSize(); + String prompt = "Go to Event (1.." + qsize + ")"; + String inputValue = JOptionPane.showInputDialog(prompt); + if (inputValue != null) { + int eventNum = Integer.parseInt(inputValue); + if ((eventNum > 0) && (eventNum <= qsize)) { + // delete messages before the one we want + cmsgHandler.clearQueue(eventNum-1); + + // Get next event (messages not containing events are discarded from queue). + EvioEvent event = cmsgHandler.getNextEvent(); + if (event != null) { + eventTreePanel.setEvent(event); + // If there's a dictionary in the this message, use it and keep track. + if (event.hasDictionaryXML()) { + cmsgDictionary = NameProviderFactory.createNameProvider(event.getDictionaryXML()); + currentDictionary = cmsgDictionary; + NameProvider.setProvider(currentDictionary); + eventTreePanel.getEventInfoPanel().setDictionary("from cMsg message"); + eventTreePanel.refreshDescription(); + isUserDictionary = false; + } + } + } + } + } + break; + + default: + } + } + }; + // (can use any JComponent to do this) + menu.getActionMap().put(gotoEventAction.getValue(Action.NAME), gotoEventAction); + menu.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_G, + ActionEvent.CTRL_MASK), + "gotoEvent"); + // goto event menu item + ActionListener al_numIn = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + String num = eventNumberInput.getText(); + if (num != null) { + int eventNum = Integer.parseInt(num); + + switch (eventSource) { + // If we're looking at a file, go to the specified event number + case FILE: + if ((eventNum > 0) && (eventNum <= evioFileReader.getEventCount())) { + EvioEvent event = evioFileReader.gotoEventNumber(eventNum); + if (event != null) { + eventTreePanel.setEvent(event); + } + } + else { + eventNumberInput.setText(""); + } + + break; + + // If we're looking at cmsg messages, there is a queue of messages. + // Skip over (delete) events and go to the selected place in the queue. + case CMSG: + if (cmsgHandler != null) { + int qsize = cmsgHandler.getQueueSize(); + if ((eventNum > 0) && (eventNum <= qsize)) { + // delete messages before the one we want + cmsgHandler.clearQueue(eventNum-1); + + // Get next event (messages not containing events are discarded from queue). + EvioEvent event = cmsgHandler.getNextEvent(); + if (event != null) { + eventTreePanel.setEvent(event); + // If there's a dictionary in the this message, use it and keep track. + if (event.hasDictionaryXML()) { + cmsgDictionary = NameProviderFactory.createNameProvider(event.getDictionaryXML()); + currentDictionary = cmsgDictionary; + NameProvider.setProvider(currentDictionary); + eventTreePanel.getEventInfoPanel().setDictionary("from cMsg message"); + eventTreePanel.refreshDescription(); + isUserDictionary = false; + } + } + } + } + + break; + + default: + } + } + } + catch (NumberFormatException e1) { + // bad entry in widget + eventNumberInput.setText(""); + } + catch (Exception e1) { + e1.printStackTrace(); + } + } + }; + gotoEventItem = new JMenu("Go to Event ... "); + eventNumberInput = new JTextField(); + eventNumberInput.setPreferredSize(new Dimension(100,20)); + eventNumberInput.addActionListener(al_numIn); + gotoEventItem.add(eventNumberInput); + gotoEventItem.setEnabled(false); + menu.add(gotoEventItem); + + menu.addSeparator(); + + // Select between different evio event sources + JLabel jl = new JLabel("Event Sources"); + jl.setBorder(new EmptyBorder(3,20,3,0)); + jl.setHorizontalTextPosition(JLabel.CENTER); + menu.add(jl); + + JRadioButtonMenuItem fileItem = new JRadioButtonMenuItem("File"); + JRadioButtonMenuItem cmsgItem = new JRadioButtonMenuItem("cMsg"); + JRadioButtonMenuItem etItem = new JRadioButtonMenuItem("ET (not available)"); + EmptyBorder eBorder = new EmptyBorder(3,30,3,0); + fileItem.setBorder(eBorder); + cmsgItem.setBorder(eBorder); + etItem.setBorder(eBorder); + + // action listener for selecting cmsg source + ActionListener cmsgListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (cmsgPanel == null) { + cmsgPanel = createCmsgPanel(); + } + setEventSource(EventSource.CMSG); + // keep this menu up (displayed) so user can go to config item + menu.doClick(); + } + }; + cmsgItem.addActionListener(cmsgListener); + + // action listener for selecting file source + ActionListener fileListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + setEventSource(EventSource.FILE); + menu.doClick(); + } + }; + fileItem.addActionListener(fileListener); + + // action listener for selecting ET source + ActionListener etListener = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + setEventSource(EventSource.ET); + menu.doClick(); + } + }; + etItem.addActionListener(etListener); + etItem.setEnabled(false); // turn off ET source for now + + ButtonGroup group = new ButtonGroup(); + group.add(fileItem); + group.add(cmsgItem); + group.add( etItem); + // file source selected by default + group.setSelected(fileItem.getModel(), true); + + menu.add(fileItem); + menu.add(cmsgItem); + menu.add( etItem); + + menu.addSeparator(); + + sourceConfig = new JMenu(""); + sourceConfig.setText(" "); + sourceConfig.setEnabled(false); + menu.add(sourceConfig); + + return menu; + } + + /** + * Create the view menu. + * + * @return the view menu. + */ + public JMenu createViewMenu() { + final JMenu menu = new JMenu(" View "); + + // ints-viewed-as-hex menu item + ActionListener al_hex = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + JMenuItem item = (JMenuItem) e.getSource(); + String txt = item.getText(); + if (txt.equals("Hexidecimal")) { + eventTreePanel.setIntsInHex(true); + item.setText("Decimal"); + } + else { + eventTreePanel.setIntsInHex(false); + item.setText("Hexidecimal"); + } + eventTreePanel.refreshDisplay(); + } + }; + + JMenuItem hexItem = new JMenuItem("Hexidecimal"); + hexItem.addActionListener(al_hex); + hexItem.setEnabled(true); + menu.add(hexItem); + + + // switch dictionary menu item + ActionListener al_switchDict = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (switchDictionary()) { + eventTreePanel.refreshDisplay(); + } + } + }; + + JMenuItem switchDictItem = new JMenuItem("Switch dictionary"); + switchDictItem.addActionListener(al_switchDict); + switchDictItem.setEnabled(true); + menu.add(switchDictItem); + + return menu; + } + + + /** + * Create the file menu. + * + * @return the file menu. + */ + public JMenu createFileMenu() { + JMenu menu = new JMenu(" File "); + + // open event file menu item + ActionListener al_oef = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + doOpenEventFile(); + } + }; + openEventFile = new JMenuItem("Open Event File..."); + openEventFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK)); + openEventFile.addActionListener(al_oef); + menu.add(openEventFile); + + // open dictionary menu item + ActionListener al_odf = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + doOpenDictionary(); + } + }; + JMenuItem df_item = new JMenuItem("Open Dictionary..."); + df_item.addActionListener(al_odf); + menu.add(df_item); + + // separator + menu.addSeparator(); + + // open dictionary menu item + ActionListener al_xml = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + exportToXML(); + } + }; + xmlExportItem = new JMenuItem("Export File to XML..."); + xmlExportItem.addActionListener(al_xml); + xmlExportItem.setEnabled(false); + menu.add(xmlExportItem); + menu.addSeparator(); + + // Quit menu item + ActionListener al_exit = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } + }; + JMenuItem exit_item = new JMenuItem("Quit"); + exit_item.addActionListener(al_exit); + menu.add(exit_item); + + return menu; + } + + + /** + * Select and open an event file. + */ + private void doOpenEventFile() { + EvioReader eFile = evioFileReader; + EvioReader evioFile = openEventFile(); + // handle cancel button properly + if (eFile == evioFile) { + return; + } + nextEventItem.setEnabled(evioFile != null); + gotoEventItem.setEnabled(evioFile != null); + xmlExportItem.setEnabled(evioFile != null); + eventTreePanel.setEvent(null); + try { + if (evioFile != null) { + gotoEventItem.setText("Go to Event (1.." + evioFileReader.getEventCount() + ")"); + } + } + catch (EvioException e) {} + } + + /** + * Convenience method to open a file programmatically. + * @param file the file to open + */ + public void manualOpenEventFile(File file) { + EvioReader eFile = evioFileReader; + EvioReader evioFile = openEventFile(file); + // handle cancel button properly + if (eFile == evioFile) { + return; + } + nextEventItem.setEnabled(evioFile != null); + gotoEventItem.setEnabled(evioFile != null); + xmlExportItem.setEnabled(evioFile != null); + eventTreePanel.setEvent(null); + try { + if (evioFile != null) { + gotoEventItem.setText("Go to Event (1.." + evioFileReader.getEventCount() + ")"); + } + } + catch (EvioException e) {} + } + + /** + * Select and open a dictionary. + */ + private void doOpenDictionary() { + openDictionary(); + } + + + /** + * Create the panel/menuitem used to handle communications with a cmsg server. + * @return the panel/menuitem used to handle communications with a cmsg server. + */ + public JPanel createCmsgPanel() { + // custom colors + final Color darkGreen = new Color(0, 160,0); + final Color darkRed = new Color(160, 0, 0); + + // put in a default UDL for connection to cmsg server + final JTextField UDL = new JTextField("cMsg://localhost/cMsg/myNameSpace"); + // put in a default subscription subject + final JTextField Subject = new JTextField("evio"); + // put in a default subscription type (* means everything) + final JTextField Type = new JTextField("*"); + + UDL.setEditable(true); + UDL.setMargin(new Insets(2, 5, 2, 5)); + + // update subscription when hit enter + ActionListener al_sub = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + cmsgHandler.subscribe(Subject.getText(), Type.getText()); + } + catch (cMsgException e1) { + e1.printStackTrace(); + Subject.setText("evio"); + } + } + }; + Subject.addActionListener(al_sub); + + // update subscription when move mouse out of widget + MouseListener ml_sub = new MouseAdapter() { + public void mouseExited(MouseEvent e) { + try { + if (cmsgHandler != null) cmsgHandler.subscribe(Subject.getText(), Type.getText()); + } + catch (cMsgException e1) { + e1.printStackTrace(); + Subject.setText("evio"); + } + } + }; + Subject.addMouseListener(ml_sub); + Subject.setEditable(true); + Subject.setMargin(new Insets(2, 5, 2, 5)); + + // update subscription when hit enter + ActionListener al_typ = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + cmsgHandler.subscribe(Subject.getText(), Type.getText()); + } + catch (cMsgException e1) { + e1.printStackTrace(); + Subject.setText("*"); + } + } + }; + Type.addActionListener(al_typ); + + // update subscription when move mouse out of widget + MouseListener ml_typ = new MouseAdapter() { + public void mouseExited(MouseEvent e) { + try { + if (cmsgHandler != null) cmsgHandler.subscribe(Subject.getText(), Type.getText()); + } + catch (cMsgException e1) { + e1.printStackTrace(); + Subject.setText("*"); + } + } + }; + Type.addMouseListener(ml_typ); + Type.setEditable(true); + Type.setMargin(new Insets(2, 5, 2, 5)); + + final JLabel status = new JLabel(" Press button to connect to cMsg server "); + status.setVerticalTextPosition(SwingConstants.CENTER); + status.setBorder(new LineBorder(Color.black)); + + // button panel + final JPanel p1 = new JPanel(); + + // connect/disconnect button + ActionListener al_con = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // get singleton object for cmsg communication + JButton button = (JButton) e.getSource(); + + if (button.getText().equals("Connect")) { + if (cmsgHandler == null) { + cmsgHandler = cMsgHandler.getInstance(); + } + + // connect to cmsg server, and subscribe to receive messages + try { + cmsgHandler.connect(UDL.getText()); + cmsgHandler.subscribe(Subject.getText(), Type.getText()); + } + catch (cMsgException e1) { + // handle failure + status.setForeground(Color.red); + status.setText(" Failed to connect to cmsg server"); + return; + } + + // success connecting to cmsg server + UDL.setEnabled(false); + status.setForeground(darkGreen); + status.setText(" Connected to cmsg server"); + connectButton.setText("Disconnect"); + buttonWillConnect = false; + } + else { + // disconnect from cmsg server + cmsgHandler.disconnect(); + // reset sources + UDL.setEnabled(true); + status.setForeground(darkRed); + status.setText(" Disconnected from cmsg server"); + connectButton.setText("Connect"); + buttonWillConnect = true; + } + } + }; + connectButton = new JButton("Connect"); + connectButton.setAlignmentX(Component.CENTER_ALIGNMENT); + connectButton.addActionListener(al_con); + connectButton.setEnabled(true); + + // clear queue button + ActionListener al_cq = new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + cmsgHandler.clearQueue(); + } + }; + final JButton clearQButton = new JButton("Clear Message Queue"); + clearQButton.setAlignmentX(Component.CENTER_ALIGNMENT); + clearQButton.addActionListener(al_cq); + + p1.setBorder(new EmptyBorder(2, 5, 2, 5)); + p1.setLayout(new GridLayout(0, 2)); + p1.add(connectButton); + p1.add(clearQButton); + + // label panel + JPanel p3 = new JPanel(); + p3.setLayout(new GridLayout(4, 0)); + JLabel label1 = new JLabel("UDL "); + label1.setHorizontalAlignment(SwingConstants.RIGHT); + p3.add(label1); + JLabel label2 = new JLabel("Subject "); + label2.setHorizontalAlignment(SwingConstants.RIGHT); + p3.add(label2); + JLabel label3 = new JLabel("Type "); + label3.setHorizontalAlignment(SwingConstants.RIGHT); + p3.add(label3); + JLabel label4 = new JLabel("Status "); + label4.setHorizontalAlignment(SwingConstants.RIGHT); + p3.add(label4); + + // textfield panel + JPanel p4 = new JPanel(); + p4.setLayout(new GridLayout(4, 0)); + p4.add(UDL); + p4.add(Subject); + p4.add(Type); + p4.add(status); + + // keep left hand labels from growing & shrinking in X-axis + Dimension d = p3.getPreferredSize(); + d.height = p4.getPreferredSize().height; + p3.setMaximumSize(d); + + // panel containing label & textfield panels + JPanel p2 = new JPanel(); + p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS)); + p2.add(Box.createRigidArea(new Dimension(5,0))); + p2.add(p3); + p2.add(p4); + p2.add(Box.createRigidArea(new Dimension(3,0))); + + // top-level panel + JPanel ptop = new JPanel(); + ptop.setLayout(new BoxLayout(ptop, BoxLayout.Y_AXIS)); + ptop.add(Box.createRigidArea(new Dimension(0,3))); + ptop.add(p2); + ptop.add(Box.createRigidArea(new Dimension(0,5))); + ptop.add(p1); + ptop.add(Box.createRigidArea(new Dimension(0,5))); + + return ptop; + } + + + + + +// public boolean isUserDictionary() { +// return isUserDictionary; +// } +// +// public void setUserDictionary(boolean userDictionary) { +// isUserDictionary = userDictionary; +// } +// +// public INameProvider getUserDictionary() { +// return userDictionary; +// } +// +// +// public INameProvider getFileDictionary() { +// return fileDictionary; +// } + + /** + * Attempt to switch dictionaries. If using dictionary from file, + * switch to user-selected dictionary if it exists, or vice versa. + * + * @return <code>true</code> if dictionary was switched, else <code>false</code> + */ + public boolean switchDictionary() { + // if switching from user-selected dictionary file to embedded file .. + if (isUserDictionary) { + switch (eventSource) { + case FILE: + if (fileDictionary != null) { + currentDictionary = fileDictionary; + NameProvider.setProvider(currentDictionary); + eventTreePanel.refreshDescription(); + eventTreePanel.getEventInfoPanel().setDictionary("from file"); + isUserDictionary = false; + return true; + } + break; + case CMSG: + if (cmsgDictionary != null) { + currentDictionary = cmsgDictionary; + NameProvider.setProvider(currentDictionary); + eventTreePanel.refreshDescription(); + eventTreePanel.getEventInfoPanel().setDictionary("from cMsg message"); + isUserDictionary = false; + return true; + } + } + } + // if switching from embedded file to user-selected dictionary file .. + else { + if (userDictionary != null) { + currentDictionary = userDictionary; + NameProvider.setProvider(currentDictionary); + eventTreePanel.refreshDescription(); + eventTreePanel.getEventInfoPanel().setDictionary(dictionaryFilePath); + isUserDictionary = true; + return true; + } + } + return false; + } + +// /** +// * Get the filepath of file whose data is currently loaded. +// * @return the filepath of file whose data is currently loaded. +// */ +// public String getDataFilePath() { +// return dataFilePath; +// } + +// /** +// * Get the filepath of dictionary file whose data is currently loaded.[truncated at 1000 lines; 242 more skipped]
diff -N cMsgHandler.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ cMsgHandler.java 8 Mar 2012 21:05:14 -0000 1.1 @@ -0,0 +1,391 @@
+package org.jlab.coda.jevio.graphics; + +import org.jlab.coda.cMsg.*; +import org.jlab.coda.jevio.*; + +import java.nio.IntBuffer; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.nio.ByteOrder; +import java.nio.ByteBuffer; +import java.io.IOException; + +/** + * This class handles all cMsg communications using a singleton pattern. + * It is expecting each message to contain bytes corresponding to an evio + * event in the byteArray field. It also looks for an xml format dictionary + * in a String payload item called "dictionary" (case sensitive). + * The endianness of the byte array is set in cmsg by the setByteArrayEndian + * method and, of course, must be set by the sender. + * + * @author timmer + * @date Oct 19, 2009 + */ +public class cMsgHandler { + + /** Handle all cmsg communications with this object. */ + private cMsg cmsg; + + /** Uniform Domain Locator (UDL) used to specify cmsg server to connect to. */ + private String udl; + + /** Subject to subscribe to for receiving evio event filled messages. */ + private String subject; + + /** Type to subscribe to for receiving evio event filled messages. */ + private String type; + + /** Handle cmsg subscription with this object. */ + private cMsgSubscriptionHandle handle; + + /** Callback to run when receiving a message. */ + private myCallback callback; + + /** Queue of received EvioEvent objects (parsed cMsg messages). */ + private BlockingQueue<EvioEvent> eventQueue; + + /** Last message to be retrieved from the queue after a new connection is made. */ + private cMsgMessage lastRetrievedMessage; + + /** Last xml string to be loaded as a dictionary from a message. */ + private String lastDictionaryLoaded; + + /** + * This class defines the callback to be run when a message matching + * our subscription arrives. + */ + private class myCallback extends cMsgCallbackAdapter { + /** + * Callback method definition. + * @param msg message received from cmsg server + * @param userObject object passed as an argument which was set when the client + * orginally subscribed to a subject and type of message. + */ + public void callback(cMsgMessage msg, Object userObject) { + // check to see if message may contain evio event (there is a byte array) + byte[] data = msg.getByteArray(); + if (data == null) return; + + // decode messages into events & store on the Q if there is room, + // else it disappears + extractEvents(msg); + } + } + + /** + * Singleton handler. + */ + private static cMsgHandler handler = new cMsgHandler(); + + /** + * Returns the handler <code>cMsgHandler</code> object. + * + * @return the singleton cmsg handler. + */ + public static cMsgHandler getInstance() { + return handler; + } + + /** + * Constructor. + */ + private cMsgHandler() { + // create cmsg callback to handle incoming messages + callback = new myCallback(); + // create list to hold 1000 incoming events + eventQueue = new LinkedBlockingQueue<EvioEvent>(1000); + } + + /** + * Get current subscription's subject. + * @return current subscription's subject. + */ + public String getSubject() { + return subject; + } + + /** + * Get current subscription's type. + * @return current subscription's type. + */ + public String getType() { + return type; + } + + /** + * Disconnect from the cmsg server that is currently connected to. + * + * @throws cMsgException + */ + public void disconnect() { + if (cmsg == null || !cmsg.isConnected()) { + return; + } + + try { + cmsg.disconnect(); + } + catch (cMsgException e) { + // if this fails it's disconnected anyway + } + + // each new connection means resubscribing + handle = null; + subject = null; + type = null; + + return; + } + + /** + * Connect to the specified cmsg server. + * + * @param udl UDL used to specify a cmsg server to connect to + * @throws cMsgException + */ + public void connect(String udl) throws cMsgException { + if (cmsg == null) { + // must create a unique cmsg client name + String name = "evioViewer_" + System.currentTimeMillis(); + String descr = "evio event viewer"; + cmsg = new cMsg(udl, name, descr); + // store udl + this.udl = udl; + } + + // if we're already connected ... + if (cmsg.isConnected()) { + // if to same server, just return + if (udl.equals(this.udl)) { + return; + } + // otherwise disconnect from old server, before reconnecting to new one + else { + cmsg.disconnect(); + } + } + + // if using new udl, recreate cmsg object + if (!udl.equals(this.udl)) { + String name = "evioViewer_" + System.currentTimeMillis(); + String descr = "evio event viewer"; + cmsg = new cMsg(udl, name, descr); + // store udl + this.udl = udl; + } + + // connect to cmsg server + cmsg.connect(); + + // allow receipt of messages + cmsg.start(); + + // each new connection means resubscribing + subject = null; + type = null; + + return; + } + + /** + * Subscribe to the given subject and type. + * If no connection to a cmsg server exists, nothing is done. + * If an identical subscription already exists nothing is done. + * If an older subscription exists, it is replaced by the new one. + * + * @param subject subject to subscribe to. + * @param type type to subscribe to. + * @return <code>true</code> if the subscription exists or was made, else <code>false</code>. + * @throws cMsgException + */ + public boolean subscribe(String subject, String type) throws cMsgException { + // can't subscribe without connection or with null args + if (cmsg == null || !cmsg.isConnected() || + subject == null || type == null || + subject.length() < 1 || type.length() < 1) { + handle = null; + return false; + } + // already subscribed to this subject & type + else if (subject.equals(this.subject) && type.equals(this.type)) { + return true; + } + + // only want 1 subscription at a time for receiving evio messages + if (handle != null) { + try { + cmsg.unsubscribe(handle); + } + catch (cMsgException e) { } + } + + handle = cmsg.subscribe(subject, type, callback, null); + this.subject = subject; + this.type = type; + + return true; + } + + /** + * Take a cMsg message and extract any evio events in it + * and place it on the event queue. If there is no room on + * the queue, the event disappears. + * + * @return evio event from the queue or null if none. + */ + private void extractEvents(cMsgMessage msg) { + try { + EvioEvent ev; + ByteBuffer buf = ByteBuffer.wrap(msg.getByteArray()); +// ByteOrder byteOrder = ByteOrder.BIG_ENDIAN; +// if (msg.getByteArrayEndian() == cMsgConstants.endianLittle) { +//System.out.println("cMsgHandler: set byte order to little endian"); +// byteOrder = ByteOrder.LITTLE_ENDIAN; +// } +// buf.order(byteOrder); +// System.out.println("extract Events:"); +// IntBuffer ibuf = buf.asIntBuffer(); +// for (int i=0; i < 8; i++) { +// System.out.println(" Buf(" + i + ") = " + Integer.toHexString(ibuf.get(i))); +// } + EvioReader reader = new EvioReader(buf); + String dictionary = reader.getDictionaryXML(); + + while ( (ev = reader.parseNextEvent()) != null) { + ev.setDictionaryXML(dictionary); +//System.out.println("EV:\n" + ev.toXML()); + eventQueue.offer(ev); + } + } + catch (IOException e) { + // data in wrong format so try next msg + } + catch (EvioException e) { + // data in wrong format so try next msg + } + } + + /** + * Get next evio event from the queue. If there is no properly formatted message + * on the queue and one does not appear within .1 seconds, null is returned. + * + * @return evio event from the queue or null if none. + */ + public EvioEvent getNextEvent() { + try { + return eventQueue.poll(100, TimeUnit.MILLISECONDS); + } + catch (InterruptedException e) { } + return null; + } + + /** + * Clear the entire message and event queues - all events. + */ + public void clearQueue() { + eventQueue.clear(); + } + + /** + * Clear the specified number of events in the queue. + * @param numberToDelete number of queue events to delete + */ + public void clearQueue(int numberToDelete) { //TODO check changes + if (numberToDelete <= eventQueue.size()) { + for (int i=0; i < numberToDelete; i++) { + eventQueue.poll(); + } + return; + } + + eventQueue.clear(); + } + + /** + * Get the size (number of messages) of the event queue. + * + * @return size of the event queue. + */ + public int getQueueSize() { + return eventQueue.size(); + } + + /** + * Get the last dictionary in xml string form to be loaded from a message. + * @return the last dictionary in xml string form to be loaded from a message. + */ + public String getDictionaryString() { + return lastDictionaryLoaded; + } + + /** + * Set the dictionary to the one contained in the last retrieved event/message (if there is one). + * + * @return <code>true</code> if the dictionary was set, else <code>false</code>. + */ + public boolean setDictionary() { //TODO: change this way of setting dictionaries, to 1 w/ each event + return setDictionary(lastRetrievedMessage); + } + + /** + * Given a message, set the dictionary to one contained in the message if there is one. + * + * @param msg message which may contain an xml dictionary + * @return <code>true</code> if the dictionary was set, else <code>false</code>. + */ + public boolean setDictionary(cMsgMessage msg) { //TODO: change this way of setting dictionaries + if (msg == null) { + return false; + } + + // look for dictionary info in message payload + cMsgPayloadItem payloadItem = msg.getPayloadItem("dictionary"); + if (payloadItem == null) { + return false; + } + + try { + String dictionary = payloadItem.getString(); + NameProvider.setProvider(NameProviderFactory.createNameProvider(dictionary)); + lastDictionaryLoaded = dictionary; + return true; + } + catch (cMsgException e) { + } + + return false; + } + + /** + * Get the byte order of the last retrieved event/message ( + * either {@link ByteOrder#BIG_ENDIAN} or {@link ByteOrder#LITTLE_ENDIAN}). + * NOTE: sender of the event is responsible for correctly setting the + * byte order. + * + * @return byte order of event in message, + * {@link ByteOrder#BIG_ENDIAN} or {@link ByteOrder#LITTLE_ENDIAN}. + */ + public ByteOrder getByteOrder() { + return getByteOrder(lastRetrievedMessage); + } + + /** + * Given a message, get the byte order of the contained event ( + * either {@link ByteOrder#BIG_ENDIAN} or {@link ByteOrder#LITTLE_ENDIAN}). + * NOTE: sender of the event is responsible for correctly setting the + * byte order. + * + * @param msg message with contained event + * @return byte order of event in message, + * {@link ByteOrder#BIG_ENDIAN} or {@link ByteOrder#LITTLE_ENDIAN}. + */ + public ByteOrder getByteOrder(cMsgMessage msg) { + int endian = msg.getByteArrayEndian(); + if (endian == cMsgConstants.endianLittle) { + return ByteOrder.LITTLE_ENDIAN; + } + return ByteOrder.BIG_ENDIAN; + } + +}
diff -N cMsgEventProducer.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ cMsgEventProducer.java 8 Mar 2012 21:05:14 -0000 1.1 @@ -0,0 +1,323 @@
+package org.jlab.coda.jevio.test; + +import org.jlab.coda.cMsg.*; +import org.jlab.coda.jevio.*; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.io.IOException; + +/** + * Class for testing EventTreeFrame graphics application with events + * coming in cMsg messages. + */ +public class cMsgEventProducer { + private String subject = "evio"; + private String type = "anything"; + private String name = "producer"; + private String description = "java event producer"; + private String UDL = "cMsg://localhost/cMsg/myNameSpace"; + + private int delay, count = 50000; + private boolean debug; + + + /** Constructor. */ + cMsgEventProducer(String[] args) { + decodeCommandLine(args); + } + + + /** + * Method to decode the command line used to start this application. + * @param args command line arguments + */ + private void decodeCommandLine(String[] args) { + + // loop over all args + for (int i = 0; i < args.length; i++) { + + if (args[i].equalsIgnoreCase("-h")) { + usage(); + System.exit(-1); + } + else if (args[i].equalsIgnoreCase("-n")) { + name = args[i + 1]; + i++; + } + else if (args[i].equalsIgnoreCase("-d")) { + description = args[i + 1]; + i++; + } + else if (args[i].equalsIgnoreCase("-u")) { + UDL= args[i + 1]; + i++; + } + else if (args[i].equalsIgnoreCase("-s")) { + subject = args[i + 1]; + i++; + } + else if (args[i].equalsIgnoreCase("-t")) { + type = args[i + 1]; + i++; + } + else if (args[i].equalsIgnoreCase("-c")) { + count = Integer.parseInt(args[i + 1]); + if (count < 1) + System.exit(-1); + i++; + } + else if (args[i].equalsIgnoreCase("-delay")) { + delay = Integer.parseInt(args[i + 1]); + i++; + } + else if (args[i].equalsIgnoreCase("-debug")) { + debug = true; + } + else { + usage(); + System.exit(-1); + } + } + + return; + } + + + /** Method to print out correct program command line usage. */ + private static void usage() { + System.out.println("\nUsage:\n\n" + + " java cMsgProducer\n" + + " [-n <name>] set client name\n"+ + " [-d <description>] set description of client\n" + + " [-u <UDL>] set UDL to connect to cMsg\n" + + " [-s <subject>] set subject of sent messages\n" + + " [-t <type>] set type of sent messages\n" + + " [-c <count>] set # of messages to send before printing output\n" + + " [-delay <time>] set time in millisec between sending of each message\n" + + " [-debug] turn on printout\n" + + " [-h] print this help\n"); + } + + + /** + * Run as a stand-alone application. + */ + public static void main(String[] args) { + try { + cMsgEventProducer producer = new cMsgEventProducer(args); + producer.run(); + } + catch (cMsgException e) { + System.out.println(e.toString()); + System.exit(-1); + } + } + + + /** + * Method to convert a double to a string with a specified number of decimal places. + * + * @param d double to convert to a string + * @param places number of decimal places + * @return string representation of the double + */ + private static String doubleToString(double d, int places) { + if (places < 0) places = 0; + + double factor = Math.pow(10,places); + String s = "" + (double) (Math.round(d * factor)) / factor; + + if (places == 0) { + return s.substring(0, s.length()-2); + } + + while (s.length() - s.indexOf(".") < places+1) { + s += "0"; + } + + return s; + } + + /** + * This class defines the callback to be run when a message matching + * our subscription arrives. + */ + class myCallback extends cMsgCallbackAdapter { + public void callback(cMsgMessage msg, Object userObject) { + // keep track of how many messages we receive + //count++; + + System.out.println("Received msg: "); + System.out.println(msg.toString(true, false, true)); + } + } + + + + /** + * This method is executed as a thread. + */ + public void run() throws cMsgException { + + if (debug) { + System.out.println("Running cMsg producer sending to:\n" + + " subject = " + subject + + "\n type = " + type + + "\n UDL = " + UDL); + } + + // connect to cMsg server + cMsg coda = new cMsg(UDL, name, description); + try { + coda.connect(); + } + catch (cMsgException e) { + e.printStackTrace(); + return; + } + + // create a message + cMsgMessage msg = new cMsgMessage(); + msg.setSubject(subject); + msg.setType(type); + + //-------------------------------------- + // create an array of simple evio events + //-------------------------------------- + + ByteBuffer myBuf = ByteBuffer.allocate(10000); + myBuf.order(ByteOrder.LITTLE_ENDIAN); + + // xml dictionary + // xml dictionary + String dictionary = + "<xmlDict>\n" + + " <xmldumpDictEntry name=\"bank\" tag=\"1\" num=\"1\"/>\n" + + " <xmldumpDictEntry name=\"bank of short banks\" tag=\"2\" num=\"2\"/>\n" + + " <xmldumpDictEntry name=\"shorts pad0\" tag=\"3\" num=\"3\"/>\n" + + " <xmldumpDictEntry name=\"shorts pad2\" tag=\"4\" num=\"4\"/>\n" + + " <xmldumpDictEntry name=\"bank of char banks\" tag=\"5\" num=\"5\"/>\n" + + " <xmldumpDictEntry name=\"chars pad0\" tag=\"6\" num=\"6\"/>\n" + + " <xmldumpDictEntry name=\"chars pad3\" tag=\"7\" num=\"7\"/>\n" + + " <xmldumpDictEntry name=\"chars pad2\" tag=\"8\" num=\"8\"/>\n" + + " <xmldumpDictEntry name=\"chars pad1\" tag=\"9\" num=\"9\"/>\n" + + "</xmlDict>"; + + // use just a bunch of zeros for data + byte[] byteData1 = new byte[] {1,2,3,4}; + byte[] byteData2 = new byte[] {1,2,3,4,5}; + byte[] byteData3 = new byte[] {1,2,3,4,5,6}; + byte[] byteData4 = new byte[] {1,2,3,4,5,6,7}; + short[] shortData1 = new short[] {11,22}; + short[] shortData2 = new short[] {11,22,33}; + + try { + EventWriter eventWriterNew = new EventWriter(myBuf, 100, 3, dictionary, null); + + // event - bank of banks + EventBuilder eventBuilder2 = new EventBuilder(1, DataType.BANK, 1); + EvioEvent eventShort = eventBuilder2.getEvent(); + + // bank of short banks + EvioBank bankBanks = new EvioBank(2, DataType.BANK, 2); + + // 3 shorts + EvioBank shortBank1 = new EvioBank(3, DataType.SHORT16, 3); + shortBank1.appendShortData(shortData1); + eventBuilder2.addChild(bankBanks, shortBank1); + + EvioBank shortBank2 = new EvioBank(4, DataType.SHORT16, 4); + shortBank2.appendShortData(shortData2); + eventBuilder2.addChild(bankBanks, shortBank2); + + eventBuilder2.addChild(eventShort, bankBanks); + eventWriterNew.writeEvent(eventShort); + + + + // each event is a trivial event containing an array of ints - all zeros + EventBuilder eventBuilder = new EventBuilder(5, DataType.BANK, 5); + EvioEvent event = eventBuilder.getEvent(); + + // event 1 + EvioBank charBank1 = new EvioBank(6, DataType.CHAR8, 6); + charBank1.appendByteData(byteData1); + eventBuilder.addChild(event, charBank1); + + // event 2 + EvioBank charBank2 = new EvioBank(7, DataType.CHAR8, 7); + charBank2.appendByteData(byteData2); + eventBuilder.addChild(event, charBank2); + + // event 3 + EvioBank charBank3 = new EvioBank(8, DataType.CHAR8, 8); + charBank3.appendByteData(byteData3); + eventBuilder.addChild(event, charBank3); + + // event 4 + EvioBank charBank4 = new EvioBank(9, DataType.CHAR8, 9); + charBank4.appendByteData(byteData4); + eventBuilder.addChild(event, charBank4); + + eventWriterNew.writeEvent(event); + + // all done writing + eventWriterNew.close(); + myBuf.flip(); + } + catch (IOException e) { + e.printStackTrace(); + } + catch (EvioException e) { + e.printStackTrace(); + } + + + // Add event buffer as byte array + msg.setByteArray(myBuf.array(), 0, myBuf.limit()); + + + // variables to track message rate + double freq=0., freqAvg=0.; + long t1, t2, deltaT, totalT=0, totalC=0; + + // Ignore the first N values found for freq in order + // to get better avg statistics. Since the JIT compiler in java + // takes some time to analyze & compile code, freq may initially be low. + long ignore=0; + + while (true) { + t1 = System.currentTimeMillis(); + for (int i = 0; i < count; i++) { + System.out.println("SEND MSG"); + coda.send(msg); + coda.flush(0); + + // delay between messages sent + if (delay != 0) { + try {Thread.sleep(delay);} + catch (InterruptedException e) {} + } + } + t2 = System.currentTimeMillis(); + + if (ignore == 0) { + deltaT = t2 - t1; // millisec + freq = (double) count / deltaT * 1000; + totalT += deltaT; + totalC += count; + freqAvg = (double) totalC / totalT * 1000; + + if (debug) { + System.out.println(doubleToString(freq, 1) + " Hz, Avg = " + + doubleToString(freqAvg, 1) + " Hz"); + } + } + else { + ignore--; + } + } + } + +}
Use REPLY-ALL to reply to list
To unsubscribe from the LCD-CVS list, click the following link:
https://listserv.slac.stanford.edu/cgi-bin/wa?SUBED1=LCD-CVS&A=1