Changed connection direction

Added gui elements to be able to select method and server
This commit is contained in:
ebner 2013-04-15 12:59:13 +02:00
parent f5f9b6ba19
commit b3d3ea7b6c
2 changed files with 176 additions and 44 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>ch.psi</groupId> <groupId>ch.psi</groupId>
<artifactId>ch.psi.imagej.zeromq</artifactId> <artifactId>ch.psi.imagej.zeromq</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.3-SNAPSHOT</version>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -1,4 +1,5 @@
package ch.psi.imagej.zeromq; package ch.psi.imagej.zeromq;
// EPICS_AD_Viewer.java // EPICS_AD_Viewer.java
// Original authors // Original authors
// Tim Madden, APS // Tim Madden, APS
@ -11,12 +12,12 @@ import ij.plugin.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.awt.event.*; import java.awt.event.*;
import java.util.concurrent.Semaphore;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.swing.*; import javax.swing.*;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.border.*;
import org.jeromq.ZMQ; import org.jeromq.ZMQ;
@ -34,15 +35,29 @@ public class ZeroMQViewer implements PlugIn {
private int numImageUpdates; private int numImageUpdates;
private JFrame frame; private JFrame frame;
private JTextField fpsText; // private JLabel fpsText;
private boolean isPluginRunning; private volatile boolean isPluginRunning;
private volatile boolean collect;
private Timer timer; private Timer timer;
private ZMQ.Context context; private ZMQ.Context context;
private ZMQ.Socket socket; private ZMQ.Socket socket;
private JPanel panel_1;
private JLabel lblNewLabel;
private JLabel labelFrameRate;
private JTextField textHostname;
private JLabel lblHostname;
private JLabel lblPort;
private JTextField textPort;
private JButton btnStart;
private Semaphore semaphore = new Semaphore(1);
private JLabel lblMethod;
private JComboBox comboBoxMethod;
public void run(String arg) { public void run(String arg) {
IJ.showStatus("Running ZeroMQ Viewer"); IJ.showStatus("Running ZeroMQ Viewer");
try { try {
@ -50,6 +65,7 @@ public class ZeroMQViewer implements PlugIn {
prevTime = System.currentTimeMillis(); prevTime = System.currentTimeMillis();
numImageUpdates = 0; numImageUpdates = 0;
semaphore.acquire(); // block semaphore
javax.swing.SwingUtilities.invokeLater(new Runnable() { javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
@ -57,11 +73,34 @@ public class ZeroMQViewer implements PlugIn {
} }
}); });
context = ZMQ.context(1);
socket = context.socket(ZMQ.PULL);
socket.bind("tcp://*:8080");
while (isPluginRunning) { while (isPluginRunning) {
semaphore.acquire();
collect=true;
try{
if(img!=null){
img.close();
img=null;
}
String hostname = textHostname.getText();
int port = Integer.parseInt(textPort.getText());
String method = (String) comboBoxMethod.getSelectedItem();
context = ZMQ.context(1);
if(method.equals("PULL")){
socket = context.socket(ZMQ.PULL);
}
else if(method.equals("SUB")){
socket = context.socket(ZMQ.SUB);
}
else{
logger.severe("Method not supported");
collect=false;
}
socket.connect("tcp://"+hostname+":"+port);
while(collect){
byte[] message = socket.recv(); byte[] message = socket.recv();
byte[] content = null; byte[] content = null;
if (socket.hasReceiveMore()) { if (socket.hasReceiveMore()) {
@ -71,10 +110,16 @@ public class ZeroMQViewer implements PlugIn {
updateImage(content); updateImage(content);
} }
timer.stop();
socket.close(); socket.close();
context.term(); context.term();
}
catch(Exception e){
logger.log(Level.SEVERE, "",e);
btnStart.setText("Start");
}
}
timer.stop();
img.close(); img.close();
frame.setVisible(false); frame.setVisible(false);
@ -89,7 +134,7 @@ public class ZeroMQViewer implements PlugIn {
public void updateImage(byte[] content) { public void updateImage(byte[] content) {
try { try {
if(img==null){ if (img == null) {
// TODO eventually use ByteProcessor or BinaryProcessor // TODO eventually use ByteProcessor or BinaryProcessor
// BinaryProcessor p = new ij.process.BinaryProcessor(new // BinaryProcessor p = new ij.process.BinaryProcessor(new
// ByteProcessor(imageSizeX, imageSizeY)); // ByteProcessor(imageSizeX, imageSizeY));
@ -112,46 +157,133 @@ public class ZeroMQViewer implements PlugIn {
/** /**
* Create the GUI and show it. For thread safety, this method should be * Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread. * invoked from the event-dispatching thread.
*
* @wbp.parser.entryPoint
*/ */
public void createAndShowGUI() { public void createAndShowGUI() {
fpsText = new JTextField(6);
fpsText.setEditable(false);
fpsText.setHorizontalAlignment(JTextField.CENTER);
frame = new JFrame("ZeroMQ_Viewer Plugin"); frame = new JFrame("ZeroMQ_Viewer Plugin");
JPanel panel = new JPanel(new BorderLayout()); panel_1 = new JPanel();
panel.setLayout(new GridBagLayout()); GridBagConstraints gbc_panel_1 = new GridBagConstraints();
panel.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5))); gbc_panel_1.fill = GridBagConstraints.BOTH;
frame.getContentPane().add(BorderLayout.CENTER, panel); gbc_panel_1.gridx = 0;
GridBagConstraints c = new GridBagConstraints(); gbc_panel_1.gridy = 1;
// Add extra space around each component to avoid clutter frame.getContentPane().add(panel_1, BorderLayout.CENTER);
c.insets = new Insets(2, 2, 2, 2); GridBagLayout gbl_panel_1 = new GridBagLayout();
gbl_panel_1.columnWidths = new int[] { 0, 0, 0 };
gbl_panel_1.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
gbl_panel_1.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
panel_1.setLayout(gbl_panel_1);
// Top row lblHostname = new JLabel("Hostname");
// Anchor all components CENTER GridBagConstraints gbc_lblHostname = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER; gbc_lblHostname.insets = new Insets(0, 0, 5, 5);
c.gridx = 0; gbc_lblHostname.anchor = GridBagConstraints.EAST;
c.gridy = 0; gbc_lblHostname.gridx = 0;
panel.add(new JLabel("Frames/s"), c); gbc_lblHostname.gridy = 0;
panel_1.add(lblHostname, gbc_lblHostname);
// Middle row textHostname = new JTextField();
// These widgets should be centered GridBagConstraints gbc_textHostname = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER; gbc_textHostname.insets = new Insets(0, 0, 5, 0);
c.gridy = 1; gbc_textHostname.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0; gbc_textHostname.gridx = 1;
panel.add(fpsText, c); gbc_textHostname.gridy = 0;
panel_1.add(textHostname, gbc_textHostname);
textHostname.setColumns(10);
lblPort = new JLabel("Port");
GridBagConstraints gbc_lblPort = new GridBagConstraints();
gbc_lblPort.anchor = GridBagConstraints.EAST;
gbc_lblPort.insets = new Insets(0, 0, 5, 5);
gbc_lblPort.gridx = 0;
gbc_lblPort.gridy = 1;
panel_1.add(lblPort, gbc_lblPort);
textPort = new JTextField();
GridBagConstraints gbc_textPort = new GridBagConstraints();
gbc_textPort.insets = new Insets(0, 0, 5, 0);
gbc_textPort.fill = GridBagConstraints.HORIZONTAL;
gbc_textPort.gridx = 1;
gbc_textPort.gridy = 1;
panel_1.add(textPort, gbc_textPort);
textPort.setColumns(10);
lblMethod = new JLabel("Method");
GridBagConstraints gbc_lblMethod = new GridBagConstraints();
gbc_lblMethod.anchor = GridBagConstraints.EAST;
gbc_lblMethod.insets = new Insets(0, 0, 5, 5);
gbc_lblMethod.gridx = 0;
gbc_lblMethod.gridy = 2;
panel_1.add(lblMethod, gbc_lblMethod);
comboBoxMethod = new JComboBox();
comboBoxMethod.setModel(new DefaultComboBoxModel(new String[] {"PULL", "SUB"}));
comboBoxMethod.setSelectedIndex(0);
GridBagConstraints gbc_comboBoxMethod = new GridBagConstraints();
gbc_comboBoxMethod.insets = new Insets(0, 0, 5, 0);
gbc_comboBoxMethod.fill = GridBagConstraints.HORIZONTAL;
gbc_comboBoxMethod.gridx = 1;
gbc_comboBoxMethod.gridy = 2;
panel_1.add(comboBoxMethod, gbc_comboBoxMethod);
lblNewLabel = new JLabel("Frame/s");
GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 3;
panel_1.add(lblNewLabel, gbc_lblNewLabel);
labelFrameRate = new JLabel("0.0");
GridBagConstraints gbc_labelFrameRate = new GridBagConstraints();
gbc_labelFrameRate.anchor = GridBagConstraints.EAST;
gbc_labelFrameRate.insets = new Insets(0, 0, 5, 0);
gbc_labelFrameRate.gridx = 1;
gbc_labelFrameRate.gridy = 3;
panel_1.add(labelFrameRate, gbc_labelFrameRate);
btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btnStart.getText().equals("Start")){
// Start data acquisition
semaphore.release();
btnStart.setText("Stop");
}
else{
// Stop data acquisition
collect = false;
try{
socket.notifyAll();
}
catch(Exception ex){
// This exception can savely be ignored (somewhat most of the time an exception is expected)
}
btnStart.setText("Start");
}
}
});
GridBagConstraints gbc_btnStart = new GridBagConstraints();
gbc_btnStart.anchor = GridBagConstraints.WEST;
gbc_btnStart.gridx = 1;
gbc_btnStart.gridy = 4;
panel_1.add(btnStart, gbc_btnStart);
// Display the window. // Display the window.
frame.pack(); frame.pack();
frame.addWindowListener(new FrameExitListener()); frame.addWindowListener(new FrameExitListener());
frame.setVisible(true); frame.setVisible(true);
// Update frame rate
int timerDelay = 2000; // 2 seconds int timerDelay = 2000; // 2 seconds
timer = new Timer(timerDelay, new ActionListener() { timer = new Timer(timerDelay, new ActionListener() {
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
double fps = 1000. * numImageUpdates / (double) (time - prevTime); double fps = 1000. * numImageUpdates / (double) (time - prevTime);
fpsText.setText(String.format("%.1f", fps)); labelFrameRate.setText(String.format("%.1f", fps));
prevTime = time; prevTime = time;
numImageUpdates = 0; numImageUpdates = 0;
} }