use of com.cas.sim.tis.message.LoginMessage in project TeachingInSimulation by ScOrPiOzzy.
the class LoginController method processLogin.
@FXML
public void processLogin() {
errorMessage.setText("");
// 0、验证登录信息的完整性
if (StringUtils.isEmpty(userId.getText())) {
setErrorMsg(resources.getString("login.account.notnull"));
return;
}
if (StringUtils.isEmpty(password.getText())) {
setErrorMsg(resources.getString("login.password.notnull"));
return;
}
String address = "";
int port = 0;
Properties prop = new Properties();
try {
prop.load(new FileInputStream("application.properties"));
address = prop.getProperty("server.base.address");
port = Integer.parseInt(prop.getProperty("server.base.port"));
} catch (Exception e) {
LOG.error("文件application.properties读取失败", e);
throw new RuntimeException(e);
}
boolean success = SocketUtil.INSTENCE.connect(address, port);
if (success) {
loginBtn.setDisable(true);
// 注册消息及消息处理类
LoginMessageHandler loginMessageHandler = new LoginMessageHandler();
ClientMessageListener.INSTENCE.registerMessageHandler(LoginMessage.class, loginMessageHandler);
loginMessageHandler.setLoginUIController(this);
ClientMessageListener.INSTENCE.registerMessageHandler(SerializerRegistrationsMessage.class, new SerializerRegistrationsMessageHandler());
ClientMessageListener.INSTENCE.registerMessageHandler(ExamMessage.class, new ExamMessageHandler());
SocketUtil.INSTENCE.start();
// 3、项服务器发送登录消息
LoginMessage msg = new LoginMessage();
msg.setUserCode(userId.getText());
msg.setUserPwd(password.getText());
SocketUtil.INSTENCE.send(msg);
} else {
loginBtn.setDisable(false);
setErrorMsg(resources.getString("server.connect.failure"));
}
}
use of com.cas.sim.tis.message.LoginMessage in project TeachingInSimulation by ScOrPiOzzy.
the class LoginController method failure.
public void failure(LoginMessage m) {
String messageKey = m.getResult().getMsg();
if (LoginResult.DUPLICATE == m.getResult()) {
AlertUtil.showConfirm(resources.getString(messageKey), resp -> {
if (ButtonType.YES == resp) {
loginBtn.setDisable(true);
LoginMessage msg = new LoginMessage();
msg.setUserCode(userId.getText());
msg.setUserPwd(password.getText());
msg.setFocus(true);
SocketUtil.INSTENCE.send(msg);
}
});
} else {
setErrorMsg(resources.getString(messageKey));
}
loginBtn.setDisable(false);
}
use of com.cas.sim.tis.message.LoginMessage in project TeachingInSimulation by ScOrPiOzzy.
the class LoginMessageHandler method execute.
@Override
public void execute(HostedConnection source, LoginMessage m) throws Exception {
// 接收客户端的请求信息
String code = m.getUserCode();
String passwd = m.getUserPwd();
if (code == null || passwd == null) {
return;
}
// 验证用户信息
// 准备一个消息用作服务器的响应消息
LoginMessage respMsg = (LoginMessage) m;
try {
final User user = userService.login(code, passwd);
List<HostedConnection> clients = serverConfig.getClients();
// 进一步验证
if (clients.size() >= serverConfig.getMaxLogin()) {
// 告诉这个用户,当前用户已满,
respMsg.setResult(LoginResult.MAX_SIZE);
source.send(respMsg);
LOG.warn("客户端数量已经达到上限:{}", clients.size());
return;
}
// 检查用户是否已经登录了
HostedConnection existConn = clients.stream().filter(c -> user.getId().equals(c.getAttribute(Session.KEY_LOGIN_ID.name()))).findAny().orElse(null);
if (existConn == null) {
source.setAttribute(Session.KEY_LOGIN_ID.name(), user.getId());
source.setAttribute(Session.KEY_LOGIN_CLASSID.name(), user.getClassId());
source.setAttribute(Session.KEY_LOGIN_ACCOUNT.name(), user.getCode());
clients.add(source);
LOG.info("客户端登录成功,当前客户端数量{}", clients.size());
// 用户成功连接
respMsg.setResult(LoginResult.SUCCESS);
respMsg.setUserId(user.getId());
respMsg.setUserType(user.getRole());
source.send(respMsg);
} else if (m.isFocus()) {
DisconnectMessage disconnect = new DisconnectMessage();
disconnect.setReason(DisconnectMessage.KICK);
disconnect.setType(DisconnectMessage.KICK);
existConn.send(disconnect);
source.setAttribute(Session.KEY_LOGIN_ID.name(), user.getId());
source.setAttribute(Session.KEY_LOGIN_CLASSID.name(), user.getClassId());
source.setAttribute(Session.KEY_LOGIN_ACCOUNT.name(), user.getCode());
clients.add(source);
LOG.info("客户端登录成功,当前客户端数量{}", clients.size());
// 用户成功连接
respMsg.setResult(LoginResult.SUCCESS);
respMsg.setUserId(user.getId());
respMsg.setUserType(user.getRole());
source.send(respMsg);
} else {
// 用户已经登录了
respMsg.setResult(LoginResult.DUPLICATE);
source.send(respMsg);
}
} catch (ServiceException e) {
// 登录失败:原因是登录信息错误
respMsg.setResult(LoginResult.FAILURE);
source.send(respMsg);
throw e;
} catch (Exception e) {
// 登录失败:服务器出现异常
respMsg.setResult(LoginResult.SERVER_EXCE);
source.send(respMsg);
throw e;
}
}
Aggregations