/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package granolasdr; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; /** * * @author matt */ public class SignalPlay extends javax.swing.JFrame { private boolean show = true; /** * Creates new form SignalPlay */ public SignalPlay() { initComponents(); jProgressBar1.setVisible(false); } private void OpenActionPerformed() throws IOException { show = false; File file = null; int returnVal = jFileChooser1.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = jFileChooser1.getSelectedFile(); System.out.println("File is " + file); } else { file = null; System.out.println("File access cancelled by user."); } if (file != null) { TCPServer server = new TCPServer(file); Thread serverThread = new Thread(server); serverThread.setName("Server Thread"); serverThread.setDaemon(true); serverThread.start(); } } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { jFileChooser1 = new javax.swing.JFileChooser(); selectButton = new javax.swing.JButton(); testButton = new javax.swing.JButton(); jProgressBar1 = new javax.swing.JProgressBar(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); selectButton.setText("Select File"); selectButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { selectButtonActionPerformed(evt); } }); testButton.setText("Test Data"); testButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { testButtonActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(selectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(testButton))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(57, 57, 57) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(selectButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(testButton)) .addGap(18, 18, 18) .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(32, 32, 32)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed selectButton.setEnabled(false); testButton.setEnabled(false); try { OpenActionPerformed(); } catch (IOException ex) { Logger.getLogger(SignalPlay.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_selectButtonActionPerformed private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testButtonActionPerformed Thread create = new Thread() { @Override public void run() { try { jProgressBar1.setVisible(true); jProgressBar1.setStringPainted(true); jProgressBar1.setString("Generating"); selectButton.setEnabled(false); testButton.setEnabled(false); File file = File.createTempFile("rtldata", ".dat"); // I do not delete on exit so temp file will remain. Add delete on exit here generateTestData(file); TCPServer server = new TCPServer(file); Thread serverThread = new Thread(server); serverThread.setName("Server Thread"); serverThread.setDaemon(true); serverThread.start(); } catch (IOException ex) { Logger.getLogger(SignalPlay.class.getName()).log(Level.SEVERE, null, ex); } } }; create.setDaemon(true); create.setName("Create thread"); create.start(); }//GEN-LAST:event_testButtonActionPerformed private void generateTestData(File file) { FileOutputStream fout = null; try { // Generate a chirp signal int N = 1048576 * 10; byte[] EXP = new byte[2 * N]; double inv = 1.0; for (int cnt = 0; cnt < N; cnt++) { jProgressBar1.setValue((int) (100.0 * (double) cnt / (double) N)); double phase = -Math.PI * cnt * cnt / N; double i = Math.cos(phase); double q = Math.sin(phase); EXP[2 * cnt] = (byte) (i * 120 * inv); EXP[2 * cnt + 1] = (byte) (q * 120.0 * inv); inv = -inv; } jProgressBar1.setValue(0); jProgressBar1.setString("Ready"); fout = new FileOutputStream(file); fout.write(EXP); fout.flush(); fout.close(); } catch (IOException ex) { Logger.getLogger(SignalPlay.class.getName()).log(Level.SEVERE, null, ex); } finally { try { fout.close(); } catch (IOException ex) { Logger.getLogger(SignalPlay.class.getName()).log(Level.SEVERE, null, ex); } } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus 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 ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(SignalPlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(SignalPlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(SignalPlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(SignalPlay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new SignalPlay().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JFileChooser jFileChooser1; private javax.swing.JProgressBar jProgressBar1; private javax.swing.JButton selectButton; private javax.swing.JButton testButton; // End of variables declaration//GEN-END:variables }