Search in sources :

Example 6 with HostedConnection

use of com.jme3.network.HostedConnection 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 7 with HostedConnection

use of com.jme3.network.HostedConnection in project TeachingInSimulation by ScOrPiOzzy.

the class LibraryPublishServiceImpl method publishLibraryToClass.

@Override
public Integer publishLibraryToClass(LibraryPublish publish) {
    // 记录考核发布记录
    saveUseGeneratedKeys(publish);
    // 广播开始考核
    ExamMessage message = new ExamMessage();
    message.setPid(publish.getId());
    message.setType(ExamMessage.EXAM_START);
    // 筛选当前登陆人员
    List<HostedConnection> collection = new ArrayList<>();
    for (HostedConnection hostedConnection : serverConfig.getClients()) {
        if (publish.getClassId().equals(hostedConnection.getAttribute(Session.KEY_LOGIN_CLASSID.name()))) {
            collection.add(hostedConnection);
        }
    }
    serverConfig.getServer().broadcast(Filters.in(collection), message);
    return publish.getId();
}
Also used : ArrayList(java.util.ArrayList) HostedConnection(com.jme3.network.HostedConnection) ExamMessage(com.cas.sim.tis.message.ExamMessage)

Example 8 with HostedConnection

use of com.jme3.network.HostedConnection in project TeachingInSimulation by ScOrPiOzzy.

the class LinkMsgHandler method execute.

@Override
public void execute(HostedConnection source, Message m) {
    LinkerMsg reqMsg = (LinkerMsg) m;
    String myLeader = source.getAttribute(GROUP_LEADER_ID);
    if (Util.isEmpty(myLeader)) {
        throw new RuntimeException("在线连线状态下参数:GROUP_LEADER_ID不可能为空!");
    }
    String userid = source.getAttribute(PERSONAL_ID);
    Collection<HostedConnection> conns = source.getServer().getConnections();
    String leaderid = null;
    String loginid = null;
    for (HostedConnection conn : conns) {
        leaderid = conn.getAttribute(GROUP_LEADER_ID);
        loginid = conn.getAttribute(PERSONAL_ID);
        if (!myLeader.equals(leaderid) || userid.equals(loginid)) {
            continue;
        }
        LinkerMsg respMsg = reqMsg;
        conn.send(respMsg);
    }
}
Also used : HostedConnection(com.jme3.network.HostedConnection) LinkerMsg(com.cas.circuit.msg.LinkerMsg)

Aggregations

HostedConnection (com.jme3.network.HostedConnection)7 Server (com.jme3.network.Server)2 ArrayList (java.util.ArrayList)2 LinkerMsg (com.cas.circuit.msg.LinkerMsg)1 LibraryPublish (com.cas.sim.tis.entity.LibraryPublish)1 User (com.cas.sim.tis.entity.User)1 ExamMessage (com.cas.sim.tis.message.ExamMessage)1 LoginMessage (com.cas.sim.tis.message.LoginMessage)1 ServerHandler (com.cas.sim.tis.message.handler.ServerHandler)1 ServiceException (com.cas.sim.tis.services.exception.ServiceException)1 ConnectionListener (com.jme3.network.ConnectionListener)1 Message (com.jme3.network.Message)1 ClientRegistrationMessage (com.jme3.network.message.ClientRegistrationMessage)1 DisconnectMessage (com.jme3.network.message.DisconnectMessage)1 SessionDataDelegator (com.jme3.network.util.SessionDataDelegator)1 IOException (java.io.IOException)1 FtpServer (org.apache.ftpserver.FtpServer)1 Bean (org.springframework.context.annotation.Bean)1