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);
}
}
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;
}
}
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();
}
}
Aggregations