From 2448ba173bfb85d71ae1b89141b7da0d711dceee Mon Sep 17 00:00:00 2001 From: CodyNeeraj Date: Thu, 15 Apr 2021 18:09:54 +0530 Subject: [PATCH] Partially implemented Server Working --- src/main/Client.java | 358 ++++++++++++++++++++++++++++++++ src/main/ClientHandler.java | 190 ----------------- src/main/Message.java | 75 +++++++ src/main/SocketConnection.java | 10 + src/main/SynchList.java | 35 ++++ src/main/Transaction.java | 120 +++++++++++ src/main/serverChatConsole.java | 15 +- src/main/serverStartFrame.form | 25 +-- src/main/serverStartFrame.java | 93 +++++++-- 9 files changed, 694 insertions(+), 227 deletions(-) create mode 100644 src/main/Client.java delete mode 100644 src/main/ClientHandler.java create mode 100644 src/main/Message.java create mode 100644 src/main/SocketConnection.java create mode 100644 src/main/SynchList.java create mode 100644 src/main/Transaction.java diff --git a/src/main/Client.java b/src/main/Client.java new file mode 100644 index 0000000..b51c243 --- /dev/null +++ b/src/main/Client.java @@ -0,0 +1,358 @@ +package main; + +import javax.swing.*; +import javax.swing.border.Border; +import javax.swing.border.LineBorder; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.io.*; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Random; +import java.util.Set; + +public class Client implements SocketConnection, ActionListener, ItemListener +{ + + protected Socket socket; + + protected ObjectInputStream inputStream; + + protected ObjectOutputStream outputStream; + + protected String CurrentToWho = "Everyone"; + + protected static ArrayList users_lists; + private JComboBox display_users; + + private JPanel switchPanels; // a panel that uses CardLayout + + ///Swing UI + private JFrame window; + + private JPanel cover; + private JPanel main; + private JTextField host; + private JTextField port; + private JTextField clientName; + private JButton run; + private Color clientColour; + private JLabel labels_cover[]; + + private JTextArea server; + private JTextField input; + private JScrollPane scrollBar; + + public void runPanel () + { + + window = new JFrame("Client"); + labels_cover = new JLabel[6]; + switchPanels = new JPanel(new CardLayout()); + + cover = new JPanel(); + cover.setLayout(null); // Setting to layout to null, so it becomes absolute position + cover.setSize(300, 400); + + clientName = new JTextField(); + clientName.setBounds(60, 60, 400, 40); + clientName.addActionListener(this); + labels_cover[0] = new JLabel("Please enter your name?"); + labels_cover[0].setBounds(60, 30, 300, 40); + cover.add(clientName); + cover.add(labels_cover[0]); + + host = new JTextField(); + host.setBounds(60, 120, 400, 40); + host.setText("localhost"); + host.addActionListener(this); + labels_cover[1] = new JLabel("Please enter your host?"); + labels_cover[1].setBounds(60, 90, 300, 40); + cover.add(labels_cover[1]); + cover.add(host); + + port = new JTextField(); + port.setBounds(60, 180, 400, 40); + port.addActionListener(this); + port.setText("5000"); + labels_cover[2] = new JLabel("Please enter your port number?"); + labels_cover[2].setBounds(60, 150, 300, 40); + cover.add(labels_cover[2]); + cover.add(port); + + // Button for the user to leave the chat system + run = new JButton("Run Client"); + run.setBounds(200, 250, 100, 50); + run.addActionListener(this); + cover.add(run); + + switchPanels.add(cover, "cover"); + + main = new JPanel(); + main.setLayout(null); // Setting to layout to null, so it becomes absolute position + main.setSize(300, 400); + + mainInterface(); + window.setSize(500, 350); + + switchPanels.add(main, "main"); + window.add(switchPanels); + + window.setResizable(false); // size remain the same, not changeable + window.setSize(500, 350); // size for the client + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.setVisible(true); // display frame + + } + + public void mainInterface () + { + + window.setTitle(this.clientName.getText()); + main.setBackground(clientColour); + + labels_cover[3] = new JLabel("Display Messages:"); + labels_cover[3].setForeground(Color.white); + labels_cover[3].setBounds(30, -5, 300, 40); + main.add(labels_cover[3]); + + Border thinBorder = LineBorder.createBlackLineBorder(); + server = new JTextArea(); + server.setEditable(false); + server.setBorder(thinBorder); + scrollBar = new JScrollPane(server); + scrollBar.setBounds(20, 30, 450, 150); + + main.add(scrollBar); + + users_lists = new ArrayList<>(); + users_lists.add("Everyone"); + + display_users = new JComboBox(users_lists.toArray()); + display_users.setBounds(20, 200, 200, 50); + + display_users.addItemListener(this); + + labels_cover[4] = new JLabel("Direct message to"); + labels_cover[4].setForeground(Color.white); + labels_cover[4].setBounds(30, 180, 300, 40); + main.add(labels_cover[4]); + main.add(display_users); + + labels_cover[5] = new JLabel("Enter message here"); + labels_cover[5].setForeground(Color.white); + labels_cover[5].setBounds(30, 250, 300, 40); + main.add(labels_cover[5]); + + input = new JTextField(); + input.setBounds(20, 280, 450, 30); + input.addActionListener(this); + main.add(input); + + } + + class serverReader extends Thread + { + + @Override + public void run () + { + try + { + Message p; + String message = ""; + while ((p = (Message) inputStream.readObject()) != null) + { + + if (p.getToWho().equals("Everyone")) + { + + message = message + p.toString() + "\n"; + server.setText(message); + + } + if (p.getToWho().equals("Update")) + { + + updateOnlineUsers(p.getOnlineUsers()); + message = message + p.toString() + "\n"; + server.setText(message); + + } + + if (p.getToWho().toLowerCase().equals(clientName.getText().toLowerCase())) + { + message = message + p.toString() + "\n"; + server.setText(message); + } + + } + + } + catch (Exception e) + { + } + + } + } + + public Client () + { + + try + { + + clientColour = randomColors(); + runPanel(); + + int port_number = Integer.parseInt(port.getText()); + + socket = new Socket(host.getText(), port_number); + + // sends output to the socket + outputStream = new ObjectOutputStream(socket.getOutputStream()); + + // takes input from the sockets + inputStream = new ObjectInputStream(socket.getInputStream()); + + new serverReader().start(); + + } + catch (UnknownHostException u) + { + System.out.println(u); + } + catch (IOException i) + { + System.out.println(i); + } + } + + @Override + public void communicate () + { + + try + { + String message = input.getText(); + outputStream.writeObject(new Message(this.clientName.getText(), message, CurrentToWho)); + outputStream.flush(); + + } + catch (IOException i) + { + System.out.println("Error " + i); + } + + input.setText(""); + } + + public void joinUser () + { + + try + { + outputStream.writeObject(new Message("@join", clientName.getText(), "Update")); + outputStream.flush(); + + } + catch (IOException i) + { + System.out.println("Error " + i); + } + + input.setText(""); + } + + public void updateOnlineUsers (ArrayList updated_users) + { + + //remove duplicates + Set set = new HashSet<>(updated_users); + users_lists.clear(); + users_lists.addAll(set); + + System.out.print(users_lists); + DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(users_lists.toArray()); + display_users.setModel(defaultComboBoxModel); + } + + @Override + public void closeConnections () + { + try + { + inputStream.close(); + outputStream.close(); + this.socket.close(); + } + catch (IOException i) + { + System.out.println(i); + } + } + + public Color randomColors () + { + Random randomGenerator = new Random(); + int red = randomGenerator.nextInt(256); + int green = randomGenerator.nextInt(256); + int blue = randomGenerator.nextInt(256); + + return new Color(red, green, blue); + } + + @Override + public void actionPerformed (ActionEvent e) + { + + CardLayout changePages = (CardLayout) (switchPanels.getLayout()); + + if (e.getSource() == run && port.getText().length() < 1 && clientName.getText().length() < 1) + { + + JOptionPane.showMessageDialog(null, "All forms must be filled!"); + + } + if (e.getSource() == run && port.getText().length() > 0 && clientName.getText().length() > 0) + { + + changePages.show(switchPanels, "main"); + window.setSize(500, 350); + joinUser(); + + } + + if (!input.getText().equals("")) + { + // user message input + communicate(); + + } + + } + + @Override + public void itemStateChanged (ItemEvent e) + { + + if (e.getStateChange() == ItemEvent.SELECTED) + { + + CurrentToWho = (String) e.getItem(); + } + + } + + public static void main (String[] args) + { + + new Client(); + } + +} diff --git a/src/main/ClientHandler.java b/src/main/ClientHandler.java deleted file mode 100644 index 9f4c10c..0000000 --- a/src/main/ClientHandler.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * 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 main; - -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintStream; -import java.net.Socket; - -/** - * - * @author Neeraj - */ -// For every client's connection we call this class -public class ClientHandler extends Thread -{ - - private String clientName; - private DataInputStream din; - private PrintStream pr; - private Socket clientSocket; - private final ClientHandler[] threads; - private BufferedReader br; - private int MAXConnections; - private DataOutputStream dos; - - public ClientHandler (Socket clientSocket, ClientHandler[] threads) - { - this.clientSocket = clientSocket; - this.threads = threads; - MAXConnections = threads.length; - } - - @Override - public void run () - { - int maxClientsCount = this.MAXConnections; - ClientHandler[] threads = this.threads; - - try - { - /* - * Create input and output streams for this client. - */ - din = new DataInputStream(clientSocket.getInputStream()); - pr = new PrintStream(clientSocket.getOutputStream()); - br = new BufferedReader(new InputStreamReader(din)); - String name; - while (true) - { - pr.println("Enter your name please"); - name = br.readLine().trim(); - // name = din.readUTF().trim(); - if (name.indexOf('@') == -1) - { - pr.println("Thank god the nam doesn't contains @ sign"); - break; - } - else - { - pr.println("The name should not contain '@' character."); - } - } - - /* Welcome the new the client. */ - pr.println("Welcome " - + name - + " to our chat room.\nTo leave enter /quit in a new line."); - - synchronized (this) - { - for (int i = 0; i < maxClientsCount; i++) - { - if (threads[i] != null && threads[i] == this) - { - clientName = "@" + name; - break; - } - } - for (int i = 0; i < maxClientsCount; i++) - { - if (threads[i] != null && threads[i] != this) - { - threads[i].pr.println( - "*** A new user " - + name - + " entered the chat room !!! ***"); - } - } - } - /* Start the conversation. */ - while (true) - { - String line = din.readUTF(); - if (line.startsWith("/quit")) - { - break; - } - /* If the message is private sent it to the given client. */ - if (line.startsWith("@")) - { - String[] words = line.split("\\s", 2); - if (words.length > 1 && words[1] != null) - { - words[1] = words[1].trim(); - if (!words[1].isEmpty()) - { - synchronized (this) - { - for (int i = 0; i < maxClientsCount; i++) - { - if (threads[i] != null && threads[i] != this - && threads[i].clientName != null - && threads[i].clientName.equals(words[0])) - { - threads[i].pr.println("<" + name + "> " + words[1]); - /* - * Echo this message to let the client know the private - * message was sent. - */ - this.pr.println(">" + name + "> " + words[1]); - break; - } - } - } - } - } - } - else - { - /* The message is public, broadcast it to all other clients. */ - synchronized (this) - { - for (int i = 0; i < maxClientsCount; i++) - { - if (threads[i] != null && threads[i].clientName != null) - { - threads[i].pr.println("<" + name + "> " + line); - } - } - } - } - } - synchronized (this) - { - for (int i = 0; i < maxClientsCount; i++) - { - if (threads[i] != null && threads[i] != this - && threads[i].clientName != null) - { - threads[i].pr.println("*** The user " + name - + " is leaving the chat room !!! ***"); - } - } - } - pr.println("*** Bye " + name + " ***"); - - /* - * Clean up. Set the current thread variable to null so that a new client - * could be accepted by the server. - */ - synchronized (this) - { - for (int i = 0; i < maxClientsCount; i++) - { - if (threads[i] == this) - { - threads[i] = null; - } - } - } - /* - * Close the output stream, close the input stream, close the socket. - */ - din.close(); - pr.close(); - br.close(); - clientSocket.close(); - } - catch (IOException e) - { - } - } -} diff --git a/src/main/Message.java b/src/main/Message.java new file mode 100644 index 0000000..4a39852 --- /dev/null +++ b/src/main/Message.java @@ -0,0 +1,75 @@ +package main; + +import java.io.Serializable; +import java.util.ArrayList; + +public class Message implements Serializable +{ + + private String name; + private String message; + private String toWho; + // list to collect online users name + public ArrayList onlineUsers;// = new ArrayList<>(); + + public Message (String _name, String _message, String _toWho) + { + this.name = _name; + this.message = _message; + this.toWho = _toWho; + } + + public String getName () + { + return name; + } + + public void setName (String name) + { + this.name = name; + } + + public String getMessage () + { + return message; + } + + public void setMessage (String message) + { + this.message = message; + } + + public String getToWho () + { + return toWho; + } + + public void setToWho (String toWho) + { + this.toWho = toWho; + } + + public void addOnlineUser (String s) + { + this.onlineUsers.add(s); + } + + public ArrayList getOnlineUsers () + { + return this.onlineUsers; + } + + @SuppressWarnings("unchecked") + public void setOnlineUsers (ArrayList l) + { + //Initialising new ArrayList for this class + onlineUsers = new ArrayList<>(); + onlineUsers.addAll(l); + } + + @Override + public String toString () + { + return this.name + ": " + this.message; + } +} diff --git a/src/main/SocketConnection.java b/src/main/SocketConnection.java new file mode 100644 index 0000000..3c18ab3 --- /dev/null +++ b/src/main/SocketConnection.java @@ -0,0 +1,10 @@ +package main; + +public interface SocketConnection +{ + + void communicate (); + + void closeConnections (); + +} diff --git a/src/main/SynchList.java b/src/main/SynchList.java new file mode 100644 index 0000000..2f42eff --- /dev/null +++ b/src/main/SynchList.java @@ -0,0 +1,35 @@ +package main; + +import java.io.ObjectOutputStream; +import java.util.ArrayList; + +public class SynchList +{ + + private ArrayList list; + + SynchList () + { + list = new ArrayList<>(); + } + + synchronized ObjectOutputStream get (int i) + { + return list.get(i); + } + + synchronized void add (ObjectOutputStream o) + { + list.add(o); + } + + synchronized int size () + { + return list.size(); + } + + synchronized void remove (ObjectOutputStream o) + { + list.remove(o); + } +} diff --git a/src/main/Transaction.java b/src/main/Transaction.java new file mode 100644 index 0000000..3f95af8 --- /dev/null +++ b/src/main/Transaction.java @@ -0,0 +1,120 @@ +package main; + +//import org.omg.PortableInterceptor.SYSTEM_EXCEPTION; +//import sun.tools.asm.CatchData; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.net.Socket; +import java.io.IOException; +import java.util.ArrayList; + +class Transaction extends Thread implements SocketConnection +{ + + private ObjectInputStream inputStream; + private ObjectOutputStream outputStream; + private Socket socket; + private SynchList outputs; + private int n; + private Message message; + protected ArrayList online_users; + + public Transaction (int n, SynchList outputs, ArrayList online_users, Socket socket) throws Exception + { + this.outputStream = new ObjectOutputStream(socket.getOutputStream());//starting new outputstream + this.inputStream = new ObjectInputStream(socket.getInputStream()); //starting new inputstream + this.socket = socket; + this.outputs = outputs;//Synchlist + this.online_users = online_users;//Old Arraylist referencing + this.n = n; //Synchlist size + this.outputs.add(outputStream); //after assigning the referrnce, adding outsteam objects into Synchlists + } + + @Override + public void run () + { + this.communicate(); + } + + @Override + public void communicate () + { + try + { + while (true) + { + // deserializing the object, reading the content from the + // clients + message = (Message) inputStream.readObject(); + + if (message.getName().equals("@join")) + { + String newUser = message.getMessage(); + online_users.add(newUser); + message.setOnlineUsers(online_users); + message.setMessage(newUser + " has joined the conversation!"); + message.addOnlineUser(newUser); + message.setName("Server"); + } + + for (int j = 0; j < outputs.size(); j++) + { + // serialise the object + outputs.get(j).writeObject(message); + outputs.get(j).flush(); + } + System.out.println(message.toString()); + } + } + catch (IOException | ClassNotFoundException e) + { + this.outputs.remove(outputStream); + } + + clientLeft(message); + + } + + public void clientLeft (Message m) + { + online_users.remove(m.getName()); + m.setMessage(m.getName() + " has left the conversation!"); + m.setToWho("Update"); + m.setName("Server"); + message = m; + System.out.println(message + " has left the conversation!"); + + try + { + for (int j = 0; j < outputs.size(); j++) + { + // serialise the object + outputs.get(j).writeObject(message); + outputs.get(j).flush(); + } + } + catch (IOException e) + { + System.out.println("Error " + e); + this.outputs.remove(outputStream); + } + + } + + //override close method + @Override + public void closeConnections () + { + try + { + inputStream.close(); + outputStream.close(); + socket.close(); + } + catch (IOException i) + { + System.out.println(i); + } + } + +} diff --git a/src/main/serverChatConsole.java b/src/main/serverChatConsole.java index bce5684..c26c358 100644 --- a/src/main/serverChatConsole.java +++ b/src/main/serverChatConsole.java @@ -34,7 +34,7 @@ public class serverChatConsole extends javax.swing.JFrame private static Socket soc; private static final int MAXConnections = 10; private int port; - private static final ClientHandler threads[] = new ClientHandler[MAXConnections]; + // private static final ClientHandler threads[] = new ClientHandler[MAXConnections]; private long start; private long end; private DateTimeFormatter pattern; @@ -43,7 +43,7 @@ public class serverChatConsole extends javax.swing.JFrame private StringBuilder builder; private String selectedFilePath; private File selectedFile; - static CopyOnWriteArrayList ar = new CopyOnWriteArrayList<>(); + // static CopyOnWriteArrayList ar = new CopyOnWriteArrayList<>(); private DataInputStream dis = null; private DataOutputStream dos = null; private serverStartFrame st; @@ -852,11 +852,11 @@ public static void main (String args[]) int i; for (i = 0; i < MAXConnections; i++) { - if (threads[i] == null) - { - (threads[i] = new ClientHandler(soc, threads)).start(); - break; - } +// if (threads[i] == null) +// { +// (threads[i] = new ClientHandler(soc, threads)).start(); +// break; +// } } if (i == MAXConnections) { @@ -917,4 +917,5 @@ public static void main (String args[]) private javax.swing.JButton slctFilePathBtn; private javax.swing.JButton stopServerBtn; // End of variables declaration//GEN-END:variables + } diff --git a/src/main/serverStartFrame.form b/src/main/serverStartFrame.form index 2c0d1c6..d7fd4c6 100644 --- a/src/main/serverStartFrame.form +++ b/src/main/serverStartFrame.form @@ -27,6 +27,9 @@ + + + @@ -73,18 +76,14 @@ - + + + + - - - - - - - - - + + @@ -109,13 +108,11 @@ - - - - + + diff --git a/src/main/serverStartFrame.java b/src/main/serverStartFrame.java index bfd8866..787917d 100644 --- a/src/main/serverStartFrame.java +++ b/src/main/serverStartFrame.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.BindException; import java.net.ServerSocket; +import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; @@ -14,10 +15,13 @@ public class serverStartFrame extends javax.swing.JFrame { private static final long serialVersionUID = 1L; + private ServerSocket ss; private boolean isAlreadyEntered; - //private String portToParse; - private int port = 0; + private int port = 0; + private SynchList outputs; + private ArrayList online_users; + private Transaction transactions; public serverStartFrame () { @@ -72,6 +76,13 @@ private void initComponents() setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Crpyton Server"); setResizable(false); + addWindowStateListener(new java.awt.event.WindowStateListener() + { + public void windowStateChanged(java.awt.event.WindowEvent evt) + { + formWindowStateChanged(evt); + } + }); jLabel1.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N jLabel1.setText("Select IP"); @@ -194,17 +205,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel3)) .addComponent(passField))))) - .addContainerGap()) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 555, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() - .addGap(0, 0, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() - .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 555, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(0, 0, Short.MAX_VALUE)) - .addGroup(layout.createSequentialGroup() - .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap()) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(4, 4, 4) .addComponent(jLabel7) .addGap(18, 18, 18) @@ -225,9 +233,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(activeClientList, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGroup(layout.createSequentialGroup() - .addComponent(serverStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(20, Short.MAX_VALUE)))))))) + .addComponent(serverStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addContainerGap(20, Short.MAX_VALUE)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) @@ -289,6 +296,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) setLocationRelativeTo(null); }// //GEN-END:initComponents + @SuppressWarnings("empty-statement") private void serverStartActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_serverStartActionPerformed {//GEN-HEADEREND:event_serverStartActionPerformed boolean isBindException, isIOException, isIllegalArgument; @@ -340,14 +348,54 @@ private void serverStartActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIR } if (!isBindException && !isIOException && !isIllegalArgument) { + //enabling btn for use now refreshBtn.setEnabled(true); + + //updating fields for useful information loc_Ip_Port.setText(new IpFetcher().loc_Ip() + " : " + port); public_Ip_Port.setText(new IpFetcher().pub_Ip() + " : " + port); - System.out.println("Success, the port entered was " + port); - //here to declare the new coming ports + //Updating status field for Active participants info + try + { + serverStatus.setText("Connected Clients : " + Integer.toString(online_users.size())); + } + catch (NullPointerException e) + { + serverStatus.setText("Connected Clients : " + 0); + } + + outputs = new SynchList(); + online_users = new ArrayList<>(); + online_users.add("Everyone"); + System.out.println("Success, the Server is now listening on the port " + port + "....."); +// try +// { +// Thread.sleep(10000); +// } +// catch (InterruptedException ex) +// { +// System.out.println("error happened in calling Sleep method before Main initialisation"); +// Logger.getLogger(serverStartFrame.class.getName()).log(Level.SEVERE, null, ex); +// } + + for (int x = 0; x <= 999999999; x++); + + while (true) + { + try + { + //Invoking txns class by passing needed values + transactions = new Transaction(outputs.size(), outputs, online_users, ss.accept()); + } + catch (Exception ex) + { + Logger.getLogger(serverStartFrame.class.getName()).log(Level.SEVERE, null, ex); + } + transactions.start(); + System.out.println("A new client has joined..."); + } - //new serverChatConsole(ss, port).setVisible(true); } } }//GEN-LAST:event_serverStartActionPerformed @@ -409,6 +457,18 @@ private void refreshBtnActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRS } }//GEN-LAST:event_refreshBtnActionPerformed + private void formWindowStateChanged(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowStateChanged + {//GEN-HEADEREND:event_formWindowStateChanged + try + { + serverStatus.setText("Connected Clients : " + Integer.toString(online_users.size())); + } + catch (NullPointerException e) + { + serverStatus.setText("Connected Clients : " + 0); + } + }//GEN-LAST:event_formWindowStateChanged + /** * @param args the command line arguments */ @@ -446,4 +506,5 @@ public static void main (String args[]) private javax.swing.JButton serverStart; private javax.swing.JLabel serverStatus; // End of variables declaration//GEN-END:variables + }