Search in sources :

Example 1 with HostedConnection

use of com.jme3.network.HostedConnection in project jmonkeyengine by jMonkeyEngine.

the class RpcHostedService method onInitialize.

/**
     *  Used internally to setup the message delegator that will
     *  handle HostedConnection specific messages and forward them
     *  to that connection's RpcConnection.
     */
@Override
protected void onInitialize(HostedServiceManager serviceManager) {
    Server server = serviceManager.getServer();
    // A general listener for forwarding the messages
    // to the client-specific handler
    this.delegator = new SessionDataDelegator(RpcConnection.class, ATTRIBUTE_NAME, true);
    server.addMessageListener(delegator, delegator.getMessageTypes());
    if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "Registered delegator for message types:{0}", Arrays.asList(delegator.getMessageTypes()));
    }
}
Also used : Server(com.jme3.network.Server) SessionDataDelegator(com.jme3.network.util.SessionDataDelegator)

Example 2 with HostedConnection

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

the class ServerConfig method createDataServer.

@Bean
public Server createDataServer() throws IOException {
    server = Network.createServer(SystemInfo.APP_NAME, SystemInfo.APP_VERSION, port, -1);
    LOG.info("主服务器地址:{}", InetAddress.getLocalHost());
    LOG.info("主服务器监听在{}端口上", port);
    server.addConnectionListener(new ConnectionListener() {

        @Override
        public void connectionAdded(Server server, HostedConnection conn) {
            LOG.info("用户{}已连接", conn.getAddress());
        }

        @Override
        public void connectionRemoved(Server server, HostedConnection conn) {
            boolean success = clients.remove(conn);
            if (success) {
                String account = conn.getAttribute(Session.KEY_LOGIN_ACCOUNT.name());
                LOG.info("用户{}已断开连接, 当前客户端数量{}", account, clients.size());
            }
        }
    });
    server.addMessageListener(new MessageListener<HostedConnection>() {

        @Override
        public void messageReceived(HostedConnection client, Message m) {
            LOG.info("收到客户端{}的消息{}", client.getAddress(), m.getClass().getName());
            ServerHandler<Message> handler = messageHandlerClass.get(m.getClass());
            if (handler != null) {
                try {
                    handler.execute(client, m);
                    LOG.debug("消息处理成功");
                } catch (Exception e) {
                    LOG.warn("消息处理失败", e);
                }
            } else {
                LOG.error("无法处理消息{},缺少相应的处理类,Eg:public class {} implements ServerHandler\\{\\}", m.getClass(), m.getClass().getSimpleName());
            }
        }
    });
    return server;
}
Also used : Server(com.jme3.network.Server) FtpServer(org.apache.ftpserver.FtpServer) Message(com.jme3.network.Message) HostedConnection(com.jme3.network.HostedConnection) ConnectionListener(com.jme3.network.ConnectionListener) ServerHandler(com.cas.sim.tis.message.handler.ServerHandler) IOException(java.io.IOException) Bean(org.springframework.context.annotation.Bean)

Example 3 with HostedConnection

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

the class ExamMessageHandler method execute.

@Override
public void execute(HostedConnection source, ExamMessage m) throws Exception {
    if (ExamMessage.EXAM_OVER == m.getType()) {
        // 获得发布记录对象
        LibraryPublish publish = libraryPublishService.findById(m.getPid());
        // 更新发布记录状态
        publish.setState(true);
        libraryPublishService.update(publish);
        // 通知当前考试学生考试结束
        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), m);
    }
}
Also used : ArrayList(java.util.ArrayList) HostedConnection(com.jme3.network.HostedConnection) LibraryPublish(com.cas.sim.tis.entity.LibraryPublish)

Example 4 with HostedConnection

use of com.jme3.network.HostedConnection in project jmonkeyengine by jMonkeyEngine.

the class KernelAdapter method dispatch.

/**
     *  Note on threading for those writing their own server 
     *  or adapter implementations.  The rule that a single connection be 
     *  processed by only one thread at a time is more about ensuring that
     *  the messages are delivered in the order that they are received
     *  than for any user-code safety.  99% of the time the user code should
     *  be writing for multithreaded access anyway.
     *
     *  <p>The issue with the messages is that if a an implementation is
     *  using a general thread pool then it would be possible for a 
     *  naive implementation to have one thread grab an Envelope from
     *  connection 1's and another grab the next Envelope.  Since an Envelope
     *  may contain several messages, delivering the second thread's messages
     *  before or during the first's would be really confusing and hard
     *  to code for in user code.</p>
     *
     *  <p>And that's why this note is here.  DefaultServer does a rudimentary
     *  per-connection locking but it couldn't possibly guard against
     *  out of order Envelope processing.</p>    
     */
protected void dispatch(Endpoint p, Message m) {
    // here.
    if (m instanceof ClientRegistrationMessage) {
        server.registerClient(this, p, (ClientRegistrationMessage) m);
        return;
    }
    try {
        HostedConnection source = getConnection(p);
        if (source == null) {
            if (reliable) {
                // If it's a reliable connection then it's slightly more
                // concerning but this can happen all the time for a UDP endpoint.
                log.log(Level.WARNING, "Recieved message from unconnected endpoint:" + p + "  message:" + m);
            }
            return;
        }
        messageDispatcher.messageReceived(source, m);
    } catch (Exception e) {
        reportError(p, m, e);
    }
}
Also used : HostedConnection(com.jme3.network.HostedConnection) ClientRegistrationMessage(com.jme3.network.message.ClientRegistrationMessage)

Example 5 with HostedConnection

use of com.jme3.network.HostedConnection in project jmonkeyengine by jMonkeyEngine.

the class RmiHostedService method shareGlobal.

/**
     *  Shares a server-wide object associated with the specified name over the specified
     *  channel.  All connections with RMI hosting started will have access to this shared 
     *  object as soon as they connect and they will all share the same instance.  It is up 
     *  to the shared object to handle any multithreading that might be required.
     *  All network communcation associated with the shared object will be done over
     *  the specified channel. 
     */
public <T> void shareGlobal(byte channel, String name, T object, Class<? super T> type) {
    GlobalShare share = new GlobalShare(channel, object, type);
    GlobalShare existing = globalShares.put(name, share);
    if (existing != null) {
    // Shouldn't need to do anything actually.
    }
    // Go through all of the children
    for (HostedConnection conn : getServer().getConnections()) {
        RmiRegistry child = getRmiRegistry(conn);
        if (child == null) {
            continue;
        }
        child.share(channel, name, object, type);
    }
}
Also used : HostedConnection(com.jme3.network.HostedConnection)

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