Package guiserver

Source Code of guiserver.GuiServer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/*
* GuiServer.java
*
* Created on Nov 24, 2009, 12:50:52 AM
*/
package guiserver;

import communicator.Cluster;
import communicator.Communicator;
import communicator.Message;
import communicator.MessageListener;
import communicator.MessageSendException;
import communicator.Node;
import communicator.NodeCommunicatorServer;
import communicator.NodeGroup;
import communicator.RemoteNode;
import configuration.ClusterConfiguration;
import configuration.Configuration;
import configuration.CurrentNodeConfiguration;
import configuration.NodeConfiguration;
import configuration.NodeGroupConfiguration;
import communicator.RemoteNodeImpl;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import helpers.*;
import java.awt.Component;
import java.awt.Rectangle;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;

/**
*
* @author Artur
*/
public class GuiServer extends javax.swing.JFrame {

    /** Creates new form GuiServer */
    public GuiServer() {
        try {
            initComponents();

    

            loggingLevel.removeAllItems();
//
//     //        loggingLevel.addItem(Level.ALL);
////             loggingLevel.addItem(Level.FINE);
//  //           loggingLevel.addItem(Level.FINER);
//    //         loggingLevel.addItem(Level.FINEST);
//             loggingLevel.addItem(Level.SEVERE);
//             loggingLevel.addItem(Level.WARNING);
//             loggingLevel.addItem(Level.OFF);
//             loggingLevel.addItem(Level.INFO);
//             loggingLevel.addItem(Level.CONFIG);

            // config
            Handler h = new TextBoxLogHandler(logTextArea);
            SimpleFormatter formatter = new SimpleFormatter();
            h.setFormatter(formatter);

            Logger.getLogger("").addHandler(h);
            Logger.getLogger("").setLevel(Level.CONFIG);


            Communicator.getCommunicator();
            initConfigTree(Configuration.getCurrentConfiguration());

            List<Node> nodes;
            try {
                nodes = Communicator.getCommunicator().getNodes();
            } catch (Exception ex) {
                ExceptionsHelper.logException(ex);
                return;
            }

            DefaultTableModel modelS = (DefaultTableModel) messagesLog.getModel();
            modelS.removeRow(0);

            final Component comp = this;

            Communicator.getCommunicator().addMessageListener(
                    new MessageListener() {

                        public void messageReceived(Node sender, final Message message) {
                            Logger.getLogger(GuiServer.class.getName()).log(Level.INFO, "Message handled:" + message.toString());

                            String mess = message.toString();

                            DefaultTableModel model = (DefaultTableModel) messagesLog.getModel();
                            model.addRow(new Object[]{sender.getNodeId(), mess});

                            messagesLog.invalidate();
                            messagesLog.changeSelection(model.getRowCount() - 1, 1, false, false);


                        }
                    });

        } catch (Exception ex) {
            ExceptionsHelper.logException(ex);
        }


        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {

            public void run() {

                while (true) {
                    fillNodesTable();
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        // Logger.getLogger(GuiServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }

    /** 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();
        logTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        configjTree = new javax.swing.JTree();
        startButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        nodeTable = new javax.swing.JTable();
        loggingLevel = new javax.swing.JComboBox();
        messagesLogS = new javax.swing.JScrollPane();
        messagesLog = new javax.swing.JTable();

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        logTextArea.setColumns(20);
        logTextArea.setRows(5);
        logTextArea.setName("logTextArea"); // NOI18N
        jScrollPane1.setViewportView(logTextArea);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        configjTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        configjTree.setName("configjTree"); // NOI18N
        jScrollPane2.setViewportView(configjTree);

        startButton.setText("Start server");
        startButton.setName("startButton"); // NOI18N
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        stopButton.setText("Stop server");
        stopButton.setName("stopButton"); // NOI18N
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        nodeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Node ID", "Node Status", "Node Address"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        nodeTable.setName("nodeTable"); // NOI18N
        jScrollPane3.setViewportView(nodeTable);

        loggingLevel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        loggingLevel.setName("loggingLevel"); // NOI18N
        loggingLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loggingLevelChanged(evt);
            }
        });

        messagesLogS.setName("messagesLogS"); // NOI18N

        messagesLog.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null}
            },
            new String [] {
                "Sender", "Message"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        messagesLog.setName("messagesLog"); // NOI18N
        messagesLogS.setViewportView(messagesLog);
        messagesLog.getColumnModel().getColumn(0).setMinWidth(100);
        messagesLog.getColumnModel().getColumn(0).setPreferredWidth(150);
        messagesLog.getColumnModel().getColumn(0).setMaxWidth(150);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(startButton)
                        .addGap(18, 18, 18)
                        .addComponent(stopButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 385, Short.MAX_VALUE)
                        .addComponent(loggingLevel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(messagesLogS, javax.swing.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(messagesLogS, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stopButton)
                    .addComponent(startButton)
                    .addComponent(loggingLevel, 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.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        //if (server == null) {
        try {
            runCommunicatorServer();
            //  }
        } catch (Exception ex) {
            ExceptionsHelper.logException(ex);
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
        try {
            Communicator.getCommunicator().stopCommunicatorServer();
            Logger.getLogger(GuiServer.class.getName()).log(Level.INFO, "Server terminated.");
        } catch (Exception ex) {
            ExceptionsHelper.logException(ex);
        }
    }//GEN-LAST:event_stopButtonActionPerformed

    private void loggingLevelChanged(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loggingLevelChanged
        if (loggingLevel.getItemCount() == 0) {
            loggingLevel.removeAllItems();

            loggingLevel.addItem(Level.ALL);
            loggingLevel.addItem(Level.FINE);
            loggingLevel.addItem(Level.FINER);
            loggingLevel.addItem(Level.FINEST);
            loggingLevel.addItem(Level.CONFIG);
            loggingLevel.addItem(Level.INFO);
            loggingLevel.addItem(Level.WARNING);
            loggingLevel.addItem(Level.SEVERE);

            loggingLevel.addItem(Level.OFF);
            loggingLevel.setSelectedItem("INFO");

        }
        String s = loggingLevel.getSelectedItem().toString();
        Logger.getLogger("").setLevel(Level.parse(s));
    }//GEN-LAST:event_loggingLevelChanged

    public void runCommunicatorServer() throws RemoteException, Exception {


        int rmiPort =
                Configuration.getCurrentConfiguration().getCurrentNodeConfiguration().getRmiRepositoryPort();

        Communicator.getCommunicator().initializeCommunicatorServer(rmiPort);


    }

    private void fillNodesTable() {
        List<Node> nodes;
        try {
            nodes = Communicator.getCommunicator().getNodes();
        } catch (Exception ex) {
            ExceptionsHelper.logException(ex);
            return;
        }
        final DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Node Id");
        model.addColumn("Node status");
        model.addColumn("Node Address");
        for (Node n : nodes) {
            model.addRow(new Object[]{n.getNodeId(), n.getNodeStatus(), n.getConfig().getRmiRegistryAddress()});
        }

        SwingUtilities.invokeLater(
                new Runnable() {

                    public void run() {
                        try {
                            nodeTable.setModel(model);
                        } catch (Exception ex) {
                            ExceptionsHelper.logException(ex);
                        }
                    }
                });

    }

    private void initConfigTree(Configuration c) {

        DefaultMutableTreeNode root =
                new DefaultMutableTreeNode("Configuration");
        CurrentNodeConfiguration curNodeConfig =
                c.getCurrentNodeConfiguration();
        DefaultMutableTreeNode currentNodeConfig =
                new DefaultMutableTreeNode("<html><b>RMI Repository Port:</b>"
                + curNodeConfig.getRmiRepositoryPort()
                + "</html>");

        DefaultMutableTreeNode currentNodeConfig2 =
                new DefaultMutableTreeNode("<html><b>DB Connection String:</b>\""
                + curNodeConfig.getDbUrl()
                + "\"</html>");

        DefaultMutableTreeNode thisNodeConfig =
                new DefaultMutableTreeNode("This node configuration");
        root.add(thisNodeConfig);
        thisNodeConfig.add(currentNodeConfig);
        thisNodeConfig.add(currentNodeConfig2);
        DefaultTreeModel model = new DefaultTreeModel(root);


        DefaultMutableTreeNode nodesGroupRoot =
                new DefaultMutableTreeNode("Nodes group ");


        root.add(nodesGroupRoot);

        ClusterConfiguration clusterConfig = c.getClusterConfiguration();

        int i = 1;
        for (NodeGroupConfiguration ngc :
                clusterConfig.getNodeGroupsConfigurations()) {

            DefaultMutableTreeNode currentNodeGroupConfig =
                    new DefaultMutableTreeNode("Nodes group " + i);

            for (NodeConfiguration nodec : ngc.getNodesConfigurations()) {

                DefaultMutableTreeNode nodeConfig =
                        new DefaultMutableTreeNode("<html><b>RMI Repository Address:</b> \""
                        + nodec.getRmiRegistryAddress()
                        + "\"</html>");
                currentNodeGroupConfig.add(nodeConfig);
            }
            i++;
            nodesGroupRoot.add(currentNodeGroupConfig);
        }
        configjTree.setModel(model);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GuiServer().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree configjTree;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea logTextArea;
    private javax.swing.JComboBox loggingLevel;
    private javax.swing.JTable messagesLog;
    private javax.swing.JScrollPane messagesLogS;
    private javax.swing.JTable nodeTable;
    private javax.swing.JButton startButton;
    private javax.swing.JButton stopButton;
    // End of variables declaration//GEN-END:variables
    public NodeCommunicatorServer server = null;
    private ExecutorService execService = Executors.newSingleThreadExecutor();
}
TOP

Related Classes of guiserver.GuiServer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.