Search in sources :

Example 1 with ServiceException

use of com.cas.sim.tis.services.exception.ServiceException in project TeachingInSimulation by ScOrPiOzzy.

the class AbstractService method findBy.

@Override
@Nullable
public T findBy(String fieldName, Object value) throws TooManyResultsException {
    try {
        T model = modelClass.newInstance();
        Field field = modelClass.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(model, value);
        return mapper.selectOne(model);
    } catch (Exception e) {
        throw new ServiceException(e.getMessage(), e);
    }
}
Also used : Field(java.lang.reflect.Field) ServiceException(com.cas.sim.tis.services.exception.ServiceException) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServiceException(com.cas.sim.tis.services.exception.ServiceException) Nullable(javax.annotation.Nullable)

Example 2 with ServiceException

use of com.cas.sim.tis.services.exception.ServiceException 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)

Example 3 with ServiceException

use of com.cas.sim.tis.services.exception.ServiceException in project TeachingInSimulation by ScOrPiOzzy.

the class UserServiceImpl method login.

@Override
public User login(String usercode, String password) {
    Condition condition = new Condition(User.class);
    Criteria criteria = condition.createCriteria();
    criteria.andEqualTo("code", usercode);
    criteria.andEqualTo("password", password);
    criteria.andEqualTo("del", 0);
    List<User> user = null;
    try {
        user = mapper.selectByCondition(condition);
    } catch (Exception e) {
        throw new ServerException("服务器异常", e);
    }
    if (user.size() == 1) {
        return user.get(0);
    } else if (user.size() == 0) {
        throw new ServiceException("用户名或密码错误!");
    } else {
        throw new TooManyResultsException();
    }
}
Also used : Condition(tk.mybatis.mapper.entity.Condition) User(com.cas.sim.tis.entity.User) ServerException(com.cas.sim.tis.services.exception.ServerException) ServiceException(com.cas.sim.tis.services.exception.ServiceException) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) Criteria(tk.mybatis.mapper.entity.Example.Criteria) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServiceException(com.cas.sim.tis.services.exception.ServiceException) ServerException(com.cas.sim.tis.services.exception.ServerException)

Aggregations

ServiceException (com.cas.sim.tis.services.exception.ServiceException)3 User (com.cas.sim.tis.entity.User)2 TooManyResultsException (org.apache.ibatis.exceptions.TooManyResultsException)2 LoginMessage (com.cas.sim.tis.message.LoginMessage)1 ServerException (com.cas.sim.tis.services.exception.ServerException)1 HostedConnection (com.jme3.network.HostedConnection)1 DisconnectMessage (com.jme3.network.message.DisconnectMessage)1 Field (java.lang.reflect.Field)1 Nullable (javax.annotation.Nullable)1 Condition (tk.mybatis.mapper.entity.Condition)1 Criteria (tk.mybatis.mapper.entity.Example.Criteria)1