mirror of
https://github.com/tiennm99/BSK-All-in-One-Clinic-Management-System.git
synced 2026-10-15 02:16:12 +00:00
finish everything
This commit is contained in:
1 parent
97769cf0a2
commit
e02f3a4255
10 files changed
+58
-37
No files matched your search
@@ -62,7 +62,9 @@ public class Client {
|
||||
log.info("Connecting to {}:{}", serverAddress, port);
|
||||
Channel channel = bootstrap.connect(serverAddress, port).sync().channel();
|
||||
channel.closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
try {
|
||||
@@ -74,6 +76,6 @@ public class Client {
|
||||
});
|
||||
thread.start();
|
||||
|
||||
UIHandler.INSTANCE.showUI();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package BsK.client;
|
||||
|
||||
import BsK.server.network.entity.Role;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class LocalStorage {
|
||||
public static String username;
|
||||
public static Role role;
|
||||
}
|
||||
@@ -42,27 +42,6 @@ public class ClientHandler extends SimpleChannelInboundHandler<TextWebSocketFram
|
||||
}
|
||||
}
|
||||
|
||||
// @Override
|
||||
// protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
|
||||
// log.debug("Received message: {}", frame.text());
|
||||
// var packet = PacketSerializer.GSON.fromJson(frame.text(), Packet.class);
|
||||
// switch (packet) {
|
||||
// case HandshakeCompleteResponse handshakeCompleteResponse -> {
|
||||
// log.info("Handshake complete");
|
||||
// this.ctx = ctx;
|
||||
// this.frame = frame;
|
||||
// }
|
||||
// case ErrorResponse response -> {
|
||||
// log.error("Received error response: {}", response.getError());
|
||||
// switch (response.getError()) {
|
||||
// case USER_ALREADY_EXISTS -> log.error("User already exists");
|
||||
// default -> log.error("Received error response: {}", response.getError());
|
||||
// }
|
||||
// }
|
||||
// case null, default -> log.warn("Unknown message: {}", frame.text());
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
|
||||
log.debug("Received message: {}", frame.text());
|
||||
@@ -74,6 +53,16 @@ public class ClientHandler extends SimpleChannelInboundHandler<TextWebSocketFram
|
||||
log.info("Handshake complete");
|
||||
ClientHandler.ctx = ctx;
|
||||
ClientHandler.frame = frame;
|
||||
|
||||
var Thread = new Thread(() -> {
|
||||
try {
|
||||
UIHandler.INSTANCE.showUI();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
Thread.start();
|
||||
// When the handshake is complete, the UI is shown
|
||||
return; // No further processing needed for handshake response
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package BsK.client.ui.component.CheckUpPage;
|
||||
|
||||
import BsK.client.LocalStorage;
|
||||
import BsK.client.ui.component.MainFrame;
|
||||
import BsK.client.ui.component.common.RoundedButtonUI;
|
||||
import BsK.client.ui.component.common.RoundedPanel;
|
||||
@@ -96,7 +97,7 @@ public class CheckUpPage extends JPanel {
|
||||
title.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
topbar.add(title, BorderLayout.WEST);
|
||||
|
||||
JLabel userInfo = new JLabel("User Name (Admin)");
|
||||
JLabel userInfo = new JLabel("Welcome, " + LocalStorage.username);
|
||||
userInfo.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
topbar.add(userInfo, BorderLayout.EAST);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package BsK.client.ui.component.DashboardPage;
|
||||
|
||||
import BsK.client.LocalStorage;
|
||||
import BsK.client.ui.component.MainFrame;
|
||||
import BsK.client.ui.component.common.RoundedButtonUI;
|
||||
import BsK.client.ui.component.common.RoundedPanel;
|
||||
@@ -85,7 +86,7 @@ public class DashboardPage extends JPanel {
|
||||
title.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
topbar.add(title, BorderLayout.WEST);
|
||||
|
||||
JLabel userInfo = new JLabel("User Name (Admin)");
|
||||
JLabel userInfo = new JLabel("Welcome, " + LocalStorage.username);
|
||||
userInfo.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
topbar.add(userInfo, BorderLayout.EAST);
|
||||
|
||||
@@ -169,6 +170,7 @@ public class DashboardPage extends JPanel {
|
||||
add(mainContent, BorderLayout.CENTER);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
// JFrame frame = new JFrame("Dashboard");
|
||||
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package BsK.client.ui.component.LoginPage;
|
||||
|
||||
import BsK.client.LocalStorage;
|
||||
import BsK.client.network.handler.ClientHandler;
|
||||
import BsK.client.ui.component.MainFrame;
|
||||
import BsK.common.packet.Packet;
|
||||
@@ -80,7 +81,8 @@ public class LoginPage extends JPanel {
|
||||
// Register listeners for login-related responses
|
||||
ClientHandler.addResponseListener(LoginSuccessResponse.class, response -> {
|
||||
log.info("Login successful, UserId: {}, Role: {}", response.getUserId(), response.getRole());
|
||||
SwingUtilities.invokeLater(() -> mainFrame.showPage("DashboardPage"));
|
||||
LocalStorage.username = loginField.getText();
|
||||
mainFrame.showPage("DashboardPage");
|
||||
});
|
||||
|
||||
ClientHandler.addResponseListener(ErrorResponse.class, response -> {
|
||||
|
||||
@@ -24,18 +24,31 @@ public class MainFrame extends JFrame {
|
||||
|
||||
// Add pages to the main panel
|
||||
mainPanel.add(new LandingPage(this), "LandingPage");
|
||||
mainPanel.add(new LoginPage(this), "LoginPage");
|
||||
mainPanel.add(new RegisterPage(this), "RegisterPage");
|
||||
mainPanel.add(new DashboardPage(this), "DashboardPage");
|
||||
mainPanel.add(new CheckUpPage(this), "CheckUpPage");
|
||||
|
||||
add(mainPanel);
|
||||
}
|
||||
|
||||
// public void showPage(String pageName) {
|
||||
// cardLayout.show(mainPanel, pageName);
|
||||
// }
|
||||
|
||||
public void showPage(String pageName) {
|
||||
cardLayout.show(mainPanel, pageName);
|
||||
try {
|
||||
// Construct the fully qualified class name
|
||||
String className = "BsK.client.ui.component." + pageName + "." + pageName;
|
||||
Class<?> pageClass = Class.forName(className);
|
||||
JPanel newPage = (JPanel) pageClass.getConstructor(MainFrame.class).newInstance(this);
|
||||
|
||||
mainPanel.add(newPage, pageName);
|
||||
mainPanel.remove(0);
|
||||
cardLayout.show(mainPanel, pageName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new IllegalArgumentException("Unable to load page: " + pageName, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
new MainFrame().setVisible(true);
|
||||
|
||||
@@ -19,6 +19,6 @@ public class ErrorResponse implements Packet {
|
||||
UNKNOWN,
|
||||
USERNAME_NOT_FOUND,
|
||||
PASSWORD_INCORRECT,
|
||||
USER_ALREADY_EXISTS,
|
||||
USER_ALREADY_EXISTS,INVALID_CREDENTIALS,
|
||||
}
|
||||
}
|
||||
@@ -16,14 +16,13 @@ public class User {
|
||||
|
||||
int userId;
|
||||
Channel channel;
|
||||
Role role;
|
||||
|
||||
int sessionId;
|
||||
Role role = Role.GUEST;
|
||||
int sessionId;
|
||||
|
||||
public User(Channel channel, int sessionId) {
|
||||
this.channel = channel;
|
||||
this.sessionId = sessionId;
|
||||
Role role = Role.GUEST;
|
||||
|
||||
}
|
||||
|
||||
public void authenticate(String username, String password) {
|
||||
|
||||
@@ -42,9 +42,10 @@ public class ServerHandler extends SimpleChannelInboundHandler<TextWebSocketFram
|
||||
|
||||
if (user.isAuthenticated()) {
|
||||
UserUtil.sendPacket(user.getSessionId(), new LoginSuccessResponse(user.getUserId(), user.getRole()));
|
||||
log.info("Send response to client User {} authenticated, role {}", user.getUserId(), user.getRole());
|
||||
log.info("Send response to client User {} authenticated, role {}, session {}", user.getUserId(), user.getRole(), user.getSessionId());
|
||||
} else {
|
||||
log.info("User {} failed to authenticate", user.getUserId());
|
||||
UserUtil.sendPacket(user.getSessionId(), new ErrorResponse(Error.INVALID_CREDENTIALS));
|
||||
}
|
||||
} else if (packet instanceof RegisterRequest registerRequest) {
|
||||
log.debug(
|
||||
|
||||
Reference in new issue
Block a user