Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/java/client/domain/ClientInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package client.domain;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
Expand Down Expand Up @@ -27,4 +33,23 @@ public String getKey() {
return key;
}

public Document toXmlDocument() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.newDocument();
Element rootElement = document.createElement("ClientInfo");
document.appendChild(rootElement);

Element keyElement = document.createElement("key");
keyElement.appendChild(document.createTextNode(key));
rootElement.appendChild(keyElement);

return document;
} catch (ParserConfigurationException e) {
e.printStackTrace();
return null;
}
}
}
37 changes: 31 additions & 6 deletions src/main/java/client/domain/ClientThread.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package client.domain;


import client.view.ClientMultiGUI;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ClientThread extends Thread {
Socket socket;
ClientMultiGUI clientMultiGUI;
private Socket socket;
private ClientMultiGUI clientMultiGUI;

public ClientThread(Socket socket, ClientMultiGUI clientMultiGUI) {
this.socket = socket;
Expand All @@ -24,13 +29,33 @@ public void run() {
BufferedReader reader = new BufferedReader(inputStreamReader);

while (true) {
String message = reader.readLine();
if (message == null)
String xmlMessage = reader.readLine();
if (xmlMessage == null)
break;
clientMultiGUI.append(message + "\n");

Document messageDocument = parseXML(xmlMessage);
handleMessage(messageDocument);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private Document parseXML(String xmlString) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new java.io.StringReader(xmlString));
return builder.parse(inputSource);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private void handleMessage(Document messageDocument) {
// Handle the received XML message as needed
// For example, you can extract information and update the GUI
// clientMultiGUI.append(message + "\n");
}
}
149 changes: 88 additions & 61 deletions src/main/java/client/view/ClientMultiGUI.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package client.view;


import client.domain.ClientInfo;
import client.domain.ClientThread;
import client.utils.EncryptionUtils;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand All @@ -16,109 +17,131 @@
import static client.utils.OutputMessage.STRING_WELCOME_MESSAGE;

public class ClientMultiGUI extends JFrame implements ActionListener {
private JLabel label;
private JTextField client;
private JTextField clientName, clientPassword;
private JTextField clientServer, clientPort;
private JButton connect;
private JButton disconnect;
private JFrame loginFrame;
private JTextField clientName, clientPassword, clientServer, clientPort;
private JButton connectButton;
private JTextArea client; // 수정된 부분
private JTextArea clientMessage;
private boolean connected;
private PrintWriter writer;
private ClientInfo clientInfo = new ClientInfo();

private final static String ClientName = "메신저 서버";
private final static String Message = "보낼 메시지";
private final static String IPAddress = "서버 주소: ";
private final static String PortNumber = "포트 번호: ";
private final static String UserName = "이름: ";
private final static String PassWord = "비밀번호: ";

public ClientMultiGUI() {
super(ClientName);
initLoginWindow();
}

private void initLoginWindow() {
loginFrame = new JFrame("로그인");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(300, 400);

// 로그인 창의 배경을 노란색으로 설정하는 부분 시작
loginFrame.getContentPane().setBackground(Color.YELLOW);
// 로그인 창의 배경을 노란색으로 설정하는 부분 끝

loginFrame.setLayout(new GridLayout(5, 2, 10, 10));

clientServer = new JTextField("");
clientPort = new JTextField("");
clientName = new JTextField("");
clientPassword = new JTextField("");

connectButton = new JButton("연결");
connectButton.addActionListener(this);

loginFrame.add(new JLabel("서버 주소:"));
loginFrame.add(clientServer);
loginFrame.add(new JLabel("포트 번호:"));
loginFrame.add(clientPort);
loginFrame.add(new JLabel("닉네임:"));
loginFrame.add(clientName);
loginFrame.add(new JLabel("비밀번호:"));
loginFrame.add(clientPassword);
loginFrame.add(new JLabel(""));
loginFrame.add(connectButton);

loginFrame.setVisible(true);
}

private void initChatWindow() {
getContentPane().removeAll();

JPanel northPanel = new JPanel(new GridLayout(3, 1));
JPanel southPanel = new JPanel(new GridLayout(3, 1));
JPanel server = new JPanel(new GridLayout(1, 3, 1, 3));
JPanel Port = new JPanel(new GridLayout(1, 3, 1, 3));

JPanel massage = new JPanel(new GridLayout(1, 1));
clientServer = new JTextField("IpAddress");
clientPort = new JTextField("PortNumber");
massage.add(new JLabel(Message));
server.add(new JLabel(IPAddress));
server.add(clientServer);
Port.add(new JLabel(PortNumber));

Port.add(clientPort);
northPanel.add(server);
northPanel.add(Port);
southPanel.add(massage);

JPanel userAndConnect = new JPanel(new GridLayout(1, 5, 1, 3));

connect = new JButton("연결");
disconnect = new JButton("나가기");
connect.addActionListener(this);
userAndConnect.add(new JLabel(UserName));
clientName = new JTextField("nickname");
userAndConnect.add(clientName);
userAndConnect.add(new JLabel(PassWord));
clientPassword = new JTextField("password");
userAndConnect.add(clientPassword);
userAndConnect.add(connect);
client = new JTextField("");
JPanel southPanel = new JPanel(new GridLayout(3, 1)); // 수정된 부분

// 수정된 부분 시작
clientMessage = new JTextArea(STRING_WELCOME_MESSAGE.getMessage(), 40, 40);
JScrollPane messageScrollPane = new JScrollPane(clientMessage);
clientMessage.setEditable(false);
northPanel.add(messageScrollPane);
// 수정된 부분 끝

// 수정된 부분 시작
client = new JTextArea("");
client.setLineWrap(true);

float hue = 209f;
float saturation = 13f;
float brightness = 96f;

Color customColor = Color.getHSBColor(hue / 360f, saturation / 100f, brightness / 100f);

client.setBackground(customColor);
southPanel.add(client);
southPanel.add(disconnect);
userAndConnect.add(new JLabel(""));
northPanel.add(userAndConnect);
southPanel.add(new JScrollPane(client));
// 수정된 부분 끝

JButton sendButton = new JButton("전송");
sendButton.addActionListener(this);
southPanel.add(sendButton);

add(northPanel, BorderLayout.NORTH);
add(southPanel, BorderLayout.SOUTH);
JButton disconnectButton = new JButton("나가기");
disconnectButton.addActionListener(this);
southPanel.add(disconnectButton);

disconnect.addActionListener(new ActionListener() {
disconnectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

clientMessage = new JTextArea(STRING_WELCOME_MESSAGE.getMessage(), 40, 40);
JPanel centerPanel = new JPanel(new GridLayout(1, 1));
centerPanel.add(new JScrollPane(clientMessage));

clientMessage.setEditable(false);
centerPanel.add(northPanel);
centerPanel.add(southPanel); // 수정된 부분
add(centerPanel, BorderLayout.CENTER);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600, 600);
setVisible(true);
client.requestFocus();

}


public void append(String message) {
clientMessage.append(message);
clientMessage.append(message + "\n");
clientMessage.setCaretPosition(clientMessage.getText().length() - 1);
}


public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if (connected) {
writer.println(client.getText());
writer.flush();
client.setText("");
// 수정된 부분 시작
if (o instanceof JButton && ((JButton) o).getText().equals("전송")) {
String message = client.getText();
append("You: " + message);
writer.println(message);
writer.flush();
client.setText("");
}
// 수정된 부분 끝
return;
}

if (o == connect) {
if (o == connectButton) {
String username = clientName.getText().trim();
String password = clientPassword.getText().trim();
if (username.length() == 0)
Expand Down Expand Up @@ -159,12 +182,16 @@ public void actionPerformed(ActionEvent e) {
receiver.start();

connected = true;
connect.setEnabled(false);
connectButton.setEnabled(false);
clientServer.setEditable(false);
clientPort.setEditable(false);
clientName.setEditable(false);
clientPassword.setEditable(false);
client.addActionListener(this);
initChatWindow();
loginFrame.dispose(); // Close the login window
}
}
}



41 changes: 36 additions & 5 deletions src/main/java/server/domain/ServerInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
import java.net.UnknownHostException;
import java.util.Properties;

import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class ServerInfo {

int port;
String address;
private int port;
private String address;
private String password;
private String key;

{
try {
Expand All @@ -19,9 +25,6 @@ public class ServerInfo {
}
}

String password;
String key;

public ServerInfo() {
loadProperties();
}
Expand Down Expand Up @@ -55,4 +58,32 @@ public String getKey() {
return key;
}

public Document toXmlDocument() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();

// Create root element
org.w3c.dom.Element serverInfoElement = document.createElement("ServerInfo");
document.appendChild(serverInfoElement);

// Add elements to the XML document
appendElement(document, serverInfoElement, "Address", address);
appendElement(document, serverInfoElement, "Port", Integer.toString(port));
appendElement(document, serverInfoElement, "Password", password);
appendElement(document, serverInfoElement, "Key", key);

return document;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private void appendElement(Document document, org.w3c.dom.Element parent, String tagName, String textContent) {
org.w3c.dom.Element element = document.createElement(tagName);
element.appendChild(document.createTextNode(textContent));
parent.appendChild(element);
}
}
5 changes: 5 additions & 0 deletions src/main/java/server/domain/ServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,9 @@ private void sendAll(String message) {
private void addUser(String name) {
serverGUI.updateUserList(name);
}

/*public Document toXmlDocument() {
// Implement this method to convert ServerThread state to XML if needed
return null;
}*/
}
Loading