Question
Implement the following in my java program which is written under the requirements: When a new member joins, the server will check if it is
Implement the following in my java program which is written under the requirements:
When a new member joins, the server will check if it is the first member. If it is, the member will be informed and become the coordinator. If not, the member will request details of existing members from the server and receive everyone's IDs, IP addresses, and ports, including the current group coordinator.
The new member will then contact everyone through the server to let them know that they can update their set of members to include the new member.
If some members do not respond, the new member will inform the server, and the server will inform other members to update their local list of existing members.
If the coordinator does not respond, any new member will become the coordinator. The coordinator will maintain the state of group members by checking periodically how many of them are not responding and inform active members about it so they can update their state of members.
Implement a messaging system for members to send private or broadcast messages to every other member through the server.
Print out messages sent to and by the members.
Allow the program to run either automatically or manually. In the former case, simulate the task automatically without accepting any input parameters from users. In the latter case, the program will require user input to simulate the task.
My java program code:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MemberServer {
private JFrame frame;
private JTextField idField;
private JTextField ipField;
private JTextField portField;
private JLabel statusLabel;
private static ServerSocket serverSocket;
private boolean isRunning;
// Frame 1
public MemberServer() {
frame = new JFrame("Member Server");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(null);
JLabel idLabel = new JLabel("ID:");
idLabel.setBounds(10, 10, 100, 25);
panel.add(idLabel);
idField = new JTextField();
idField.setBounds(120, 10, 165, 25);
panel.add(idField);
JLabel ipLabel = new JLabel("IP Address:");
ipLabel.setBounds(10, 40, 100, 25);
panel.add(ipLabel);
ipField = new JTextField();
ipField.setBounds(120, 40, 165, 25);
panel.add(ipField);
JLabel portLabel = new JLabel("Port:");
portLabel.setBounds(10, 70, 100, 25);
panel.add(portLabel);
portField = new JTextField();
portField.setBounds(120, 70, 165, 25);
panel.add(portField);
// Start Button
JButton startButton = new JButton("Start Server");
startButton.setBounds(10, 100, 125, 25);
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
startServer();
Frame2 frame = new Frame2();
frame.setVisible(true);
frame.setSize(500, 200);
frame.setResizable(false);
}
});
panel.add(startButton);
// Quit Button
JButton quitButton = new JButton("Quit");
quitButton.setBounds(290, 100, 125, 25);
;
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(quitButton);
// Save Button
JButton saveButton = new JButton("Save");
saveButton.setBounds(150, 100, 125, 25);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveData();
}
});
panel.add(saveButton);
statusLabel = new JLabel();
statusLabel.setBounds(10, 140, 275, 25);
panel.add(statusLabel);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(500, 200);
frame.setResizable(false);
}
// Saving data
private void saveData() {
// Write code here to save data to a file or database
JOptionPane.showMessageDialog(this, "Data saved successfully.", "Success", JOptionPane.INFORMATION_MESSAGE);
}
// Frame 2
public class Frame2 extends JFrame {
public Frame2() {
setTitle("Member Server P2");
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
private void startServer() {
String id = idField.getText();
String ip = ipField.getText();
int port = Integer.parseInt(portField.getText());
if (id.isEmpty() || ip.isEmpty() || port <= 0) {
statusLabel.setText("Please enter a valid ID, IP address, and port.");
return;
}
String uniqueId = UUID.randomUUID().toString();
statusLabel.setText("Server started with ID: " + uniqueId);
isRunning = true;
new Thread(new Runnable() {
@Override
public void run() {
try {
serverSocket = new ServerSocket(port);
while (isRunning) {
Socket socket = serverSocket.accept();
// process incoming connection from member
}
} catch (IOException e) {
statusLabel.setText("Error starting server: " + e.getMessage());
}
}
}).start();
}
public static void main(String[] args) {
new MemberServer();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started