cz.zeno.miner.ui.MinerUI.java Source code

Java tutorial

Introduction

Here is the source code for cz.zeno.miner.ui.MinerUI.java

Source

package cz.zeno.miner.ui;

//Copyright 2014 Zeno Futurista (zenofuturista@gmail.com)

//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

import cz.zeno.miner.FpgaSerial;
import cz.zeno.miner.Utils;
import cz.zeno.miner.stratum.StratumClient;
import cz.zeno.miner.interfaces.Appender;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.DefaultCaret;
import org.apache.commons.codec.DecoderException;

/**
 *
 * @author zeno
 */
public class MinerUI extends javax.swing.JFrame implements Appender {

    /**
     * Creates new form MinerUI
     */
    public MinerUI() {
        //init GUI components
        initComponents();
        Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        p.translate(-this.getWidth() / 2, -this.getHeight() / 2);
        setLocation(p);

        for (String s : Utils.getAvailableSerialPorts())
            device.addItem(s);
        //output, where status text is appended, should always show newest messages.. (shift automatically its caret)
        //        DefaultCaret caret = (DefaultCaret)output.getCaret();  
        //        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        start = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        log = new javax.swing.JTextArea();
        server = new javax.swing.JTextField();
        workerName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        password = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        serverPort = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        clear = new javax.swing.JButton();
        device = new javax.swing.JComboBox();
        scrypt = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        start.setText("Start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        jLabel1.setText("Stratum server:");

        log.setColumns(20);
        log.setFont(new java.awt.Font("DejaVu Sans Mono", 0, 12)); // NOI18N
        log.setRows(5);
        jScrollPane1.setViewportView(log);

        jLabel2.setText("Worker name:");

        jLabel3.setText("Password:");

        jLabel4.setText("Serial port:");

        jLabel5.setText("Stratum port:");

        clear.setText("Clear log");
        clear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearActionPerformed(evt);
            }
        });

        scrypt.setText("Scrypt");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(device, javax.swing.GroupLayout.PREFERRED_SIZE, 134,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18).addComponent(scrypt)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(clear)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(start))
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 111,
                                                        Short.MAX_VALUE)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(server).addComponent(serverPort).addComponent(workerName,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 819, Short.MAX_VALUE)
                                        .addComponent(password))))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1).addComponent(server, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5).addComponent(serverPort,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2).addComponent(workerName,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(password,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(start).addComponent(jLabel4).addComponent(clear)
                                .addComponent(device, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(scrypt))
                        .addContainerGap()));

        pack();
    }// </editor-fold>                        

    StratumClient stratumClient = null;
    FpgaSerial serial = null;

    private void startActionPerformed(java.awt.event.ActionEvent evt) {
        //this is only very simple miner app
        //it instantiates new stratum client (internally Server interface)
        //Appender interface is passed to its constructor - could be command line only, I am lazy to do it, so for now it is this UI...
        //after that, you register your miner/worker (Worker interface)
        //and finally start stratum client
        //this could be done in different order, but this is only proof of concept, so I will leave it for now as is.

        try {
            if (stratumClient == null) {
                stratumClient = new StratumClient(this, server.getText(), Integer.parseInt(serverPort.getText()),
                        workerName.getText(), password.getText(), scrypt.isSelected());
                serial = new FpgaSerial(device.getSelectedItem().toString(), stratumClient, this);
                stratumClient.registerWorker(serial);

                stratumClient.start();

                start.setText("Stop");
            } else {
                stratumClient.unregisterWorker(serial);

                serial.stop();
                serial = null;

                stratumClient.close();
                stratumClient.join();

                stratumClient = null;

                start.setText("Start");
            }
        } catch (IOException | InterruptedException | DecoderException ex) {
            Logger.getLogger(MinerUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void clearActionPerformed(java.awt.event.ActionEvent evt) {
        //clear log
        log.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Metal look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Metal".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MinerUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MinerUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MinerUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MinerUI.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MinerUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton clear;
    private javax.swing.JComboBox device;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private static javax.swing.JTextArea log;
    private javax.swing.JTextField password;
    private javax.swing.JCheckBox scrypt;
    private javax.swing.JTextField server;
    private javax.swing.JTextField serverPort;
    private javax.swing.JButton start;
    private javax.swing.JTextField workerName;
    // End of variables declaration                   

    @Override
    public void append(String message) {
        //append message to output
        log.append(message);
        log.setCaretPosition(log.getDocument().getLength());
    }

}