Cashier.java 3.01 KB
package org.streamparty.remoterfid;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Cashier extends javax.swing.JPanel {

    private static final Logger logger = LoggerFactory.getLogger(Cashier.class);
    private String myName;
    private RemoteRfidClient myRemote;

    public Cashier() {
	super();
	
	JButton close = new JButton("Close");
	close.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent arg0) {
	        System.exit(0); 
	    }
	});

	JPanel paneeli = new JPanel(new GridLayout(0, 1));
	paneeli.add(close);
	paneeli.setVisible(true);
	this.add(paneeli);
	this.setVisible(true);
	
	try {
	    logger.debug("Creating Port selector");
	    new SelectPort(this);
	} catch (java.lang.UnsatisfiedLinkError e) {
	    logger.warn("RXTX library error", e);
	    throw e;
	}

	try {
	    while (true) {
		RfidAction inst = RfidAction.getInstance();
		if (inst != null) {
		    String tag = inst.listenPort();
		    RemoteRfidClient remoteClient;
		    try {
			remoteClient = getRemoteClient();

			logger.info("Found tag {}", tag);
			if (remoteClient != null) {
			    remoteClient.foundTag(myName, tag);
			}
		    } catch (Throwable t) {
			logger.warn("Caught exception while executing remote command!", t);
			myRemote = null;
		    }
		    
		}
		Thread.sleep(1000);

	    }
	} catch (InterruptedException e) {
	    e.printStackTrace();
	}
	
    }

    private RemoteRfidClient getRemoteClient() {
	if (myRemote == null) {
	    try {
		Registry registry = LocateRegistry.getRegistry("streamparty.lan", RemoteRfidClient.RMI_PORT);
		myRemote = (RemoteRfidClient) registry.lookup(RemoteRfidClient.REMOTE_DESCRIPTOR);
		logger.info("Created remote  listener {}",myRemote);
	    } catch (RemoteException e1) {
		myRemote = null;
		e1.printStackTrace();
	    } catch (NotBoundException e) {
		myRemote = null;
		e.printStackTrace();
	    }
	   
	}
	return myRemote;
    }

    public JMenuBar menuBar;
    public JToolBar toolBar;
    /**
	 * 
	 */
    private static final long serialVersionUID = 1197345710866765672L;

    public static void main(String[] vars) {

	logger.debug("Libpath " + System.getProperty("java.library.path"));
	JFrame frame = new JFrame("Stream cashier software");
	frame.setSize(400, 400);
	frame.setLocation(300, 200);
	frame.setVisible(true);
	Cashier cashier = new Cashier();
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setJMenuBar(cashier.menuBar);
	// frame.getContentPane().add(cashier.toolBar);
	frame.getContentPane().add(cashier);
	
	logger.debug("Frame set visible");
	frame.validate();
	return;

    }

    public void setReaderName(String text) {
	myName = text;
    }
}