Search in sources :

Example 1 with LoginMessage

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"));
    }
}
Also used : SerializerRegistrationsMessageHandler(com.cas.sim.tis.message.handler.SerializerRegistrationsMessageHandler) LoginMessage(com.cas.sim.tis.message.LoginMessage) ExamMessageHandler(com.cas.sim.tis.message.handler.ExamMessageHandler) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) LoginMessageHandler(com.cas.sim.tis.message.handler.LoginMessageHandler) FXML(javafx.fxml.FXML)

Example 2 with LoginMessage

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);
}
Also used : LoginMessage(com.cas.sim.tis.message.LoginMessage)

Example 3 with LoginMessage

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;
    }
}
Also used : User(com.cas.sim.tis.entity.User) ServiceException(com.cas.sim.tis.services.exception.ServiceException) DisconnectMessage(com.jme3.network.message.DisconnectMessage) LoginMessage(com.cas.sim.tis.message.LoginMessage) HostedConnection(com.jme3.network.HostedConnection) ServiceException(com.cas.sim.tis.services.exception.ServiceException)

Aggregations

LoginMessage (com.cas.sim.tis.message.LoginMessage)3 User (com.cas.sim.tis.entity.User)1 ExamMessageHandler (com.cas.sim.tis.message.handler.ExamMessageHandler)1 LoginMessageHandler (com.cas.sim.tis.message.handler.LoginMessageHandler)1 SerializerRegistrationsMessageHandler (com.cas.sim.tis.message.handler.SerializerRegistrationsMessageHandler)1 ServiceException (com.cas.sim.tis.services.exception.ServiceException)1 HostedConnection (com.jme3.network.HostedConnection)1 DisconnectMessage (com.jme3.network.message.DisconnectMessage)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 FXML (javafx.fxml.FXML)1