package client; import java.io.File; import java.util.ArrayList; import javax.swing.DefaultListModel; public class ClientUI extends javax.swing.JFrame { /** * Creates new form ClientUI */ public ClientUI() { initComponents(); } /** * 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() { jScrollPane1 = new javax.swing.JScrollPane(); jTextPane1 = new javax.swing.JTextPane(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); txtUrl = new javax.swing.JTextField(); txtPath = new javax.swing.JTextField(); jScrollPane2 = new javax.swing.JScrollPane(); lstSentList = new javax.swing.JList(); jLabel3 = new javax.swing.JLabel(); txtFrequency = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); cmdToggleButton = new javax.swing.JToggleButton(); cmdClear = new javax.swing.JButton(); lblFilesToSend = new javax.swing.JLabel(); jScrollPane1.setViewportView(jTextPane1); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Client"); jLabel1.setText("URL to send Get "); jLabel2.setText("Text file path"); txtUrl.setText("http://localhost:9000/process/"); txtUrl.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtUrlActionPerformed(evt); } }); txtPath.setText("D:\\texts"); txtPath.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtPathActionPerformed(evt); } }); jScrollPane2.setViewportView(lstSentList); jLabel3.setText("Frequency"); txtFrequency.setText("2"); txtFrequency.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtFrequencyActionPerformed(evt); } }); jLabel4.setText("messages per second"); jLabel5.setText("Sent messges"); jButton1.setText("Exit"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); cmdToggleButton.setText("Start"); cmdToggleButton.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { cmdToggleButtonStateChanged(evt); } }); cmdToggleButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdToggleButtonActionPerformed(evt); } }); cmdClear.setText("Clear"); cmdClear.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdClearActionPerformed(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() .addContainerGap(41, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel3) .addComponent(jLabel5)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txtPath) .addComponent(txtUrl, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(txtFrequency, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(cmdToggleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(63, 63, 63) .addComponent(cmdClear, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 8, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addComponent(lblFilesToSend, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)) .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(txtUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(txtPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(txtFrequency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(cmdToggleButton) .addComponent(cmdClear)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(lblFilesToSend)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void txtUrlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUrlActionPerformed this.getURL = txtUrl.getText(); requester.setGetURL(getURL); }//GEN-LAST:event_txtUrlActionPerformed private void txtFrequencyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtFrequencyActionPerformed this.frequency = Double.parseDouble(txtFrequency.getText()); requester.setDelay((int) (1000.0 / frequency)); // delay in mili seconds }//GEN-LAST:event_txtFrequencyActionPerformed private void txtPathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPathActionPerformed this.path = txtPath.getText(); requester.setTxtPath(path); }//GEN-LAST:event_txtPathActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed System.exit(0); }//GEN-LAST:event_jButton1ActionPerformed private void cmdToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdToggleButtonActionPerformed if (!isSending) { setVariables(); loadLists(); requester.filesList = filesList; requester.isSending = true; this.isSending = true; new Thread(requester).start(); sendigChangedUpdateUI(); } else if (isSending) { requester.isSending = false; this.isSending = false; } }//GEN-LAST:event_cmdToggleButtonActionPerformed private void cmdToggleButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_cmdToggleButtonStateChanged if (cmdToggleButton.isSelected()) { cmdToggleButton.setText("Stop"); } else { cmdToggleButton.setText("Start"); } }//GEN-LAST:event_cmdToggleButtonStateChanged private void cmdClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdClearActionPerformed clearList(); }//GEN-LAST:event_cmdClearActionPerformed /** * @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(ClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ClientUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ClientUI.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 ClientUI().setVisible(true); } }); } private String getURL; private double frequency; private String path; boolean isSending = false; private int totalNoOfFiles=0; Requester requester = new Requester(); ArrayList<File> filesList = new ArrayList<File>(); DefaultListModel sentModel = new DefaultListModel(); // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cmdClear; private javax.swing.JToggleButton cmdToggleButton; private javax.swing.JButton jButton1; 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 javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextPane jTextPane1; private javax.swing.JLabel lblFilesToSend; private javax.swing.JList lstSentList; private javax.swing.JTextField txtFrequency; private javax.swing.JTextField txtPath; private javax.swing.JTextField txtUrl; // End of variables declaration//GEN-END:variables private void loadLists() { path = txtPath.getText(); File txtFolder = new File(path); File[] tempListOfFiles = txtFolder.listFiles(); for (File f : tempListOfFiles) { if (getExtension(f.getName()).equalsIgnoreCase("txt")) { filesList.add(f); } } totalNoOfFiles=filesList.size(); } private void setVariables() { this.path = txtPath.getText(); requester.setTxtPath(path); this.frequency = Integer.parseInt(txtFrequency.getText()); requester.setDelay((int) (1000.0 / frequency)); this.getURL = txtUrl.getText(); requester.setGetURL(getURL); requester.ui = this; } private static String getExtension(String fileName) { String extension = ""; int i = fileName.lastIndexOf('.'); int p = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\')); if (i > p) { extension = fileName.substring(i + 1); } return extension; } public void updateList(String sentMessage) { sentModel.addElement(sentMessage); lstSentList.setModel(sentModel); } public void updateNoOfFiles(int sentFiles) { lblFilesToSend.setText(sentFiles + " out of " + totalNoOfFiles + " messages sent"); } public void sendigChangedUpdateUI() { cmdToggleButton.setSelected(isSending); } public void clearList() { sentModel.removeAllElements(); } }