⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 relayserver.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) {            LOG.info("Stopped " + publicAddress);        }            }        /*     * Methods inherited from MessageSender     */        /**     * {@inheritDoc}     */    public EndpointAddress getPublicAddress() {        return publicAddress;    }        /**     * {@inheritDoc}     */    public boolean isConnectionOriented() {        return true;    }        /**     * {@inheritDoc}     */    public boolean allowsRouting() {        return true;    }        /**     * {@inheritDoc}     */    public Object transportControl(Object operation, Object Value) {        return null;    }        /**     * {@inheritDoc}     */    public Messenger getMessenger(EndpointAddress destAddr, Object hintIgnored) {        Messenger messenger = null;                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("getMessenger for dest " + destAddr);        }                if (!RelayTransport.protocolName.equals(destAddr.getProtocolName())) {            if (Logging.SHOW_WARNING && LOG.isLoggable(Level.WARNING)) {                LOG.warning("could not make messenger for protocol :" + destAddr.getProtocolName());            }                        return null;        }                // check if we have a queue for this client        RelayServerClient handler = getClient(destAddr.getProtocolAddress());                if (handler != null) {            messenger = handler.getMessenger(publicAddress, destAddr, false);        }                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("messenger for " + destAddr.getProtocolAddress() + " is " + messenger);        }                return messenger;    }    /**     * {@inheritDoc}     */    @Deprecated    public boolean ping(EndpointAddress addr) {                synchronized (relayedClients) {            return (null != relayedClients.get(addr.getProtocolAddress()));        }    }        /*     * Methods inherited from MessageTransport     */        /**     * {@inheritDoc}     */    public String getProtocolName() {        return RelayTransport.protocolName;    }        /**     * {@inheritDoc}     */    public EndpointService getEndpointService() {        return endpointService;    }        /**     * {@inheritDoc}     */    public boolean messengerReady(MessengerEvent event) {        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("messengerReady");        }                Messenger newMessenger = event.getMessenger();        Object source = event.getSource();        EndpointAddress connectionAddress = event.getConnectionAddress();                // Sanity check, this should not happen        if (newMessenger == null || source == null || connectionAddress == null) {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("there was not a messenger or not enough information");            }                        return false;        }                // We do not grab just any messenger; that would replace the        // existing one and then we could have a fight between the        // front channel and the back channel from the same peer.        // We only grab back-channel messengers that where explicitly        // directed to the relay.        if (!serviceName.equals(connectionAddress.getServiceName())) {            return false;        }                // make sure that it is not a higher level messenger        if (source instanceof MessageSender && !((MessageSender) source).allowsRouting()) {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("this is a higher level messenger");            }                        return false;        }                // make sure that this is not one of our own.        if (source == this || newMessenger instanceof RelayServerClient.RelayMessenger) {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("this is a relay messenger");            }                        return false;        }                // make sure that the messenger matches a possible client address        EndpointAddress destAddr = newMessenger.getLogicalDestinationAddress();                if (destAddr == null || !"jxta".equals(destAddr.getProtocolName())) {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("LogicalDestinationAddress is not a \"jxta\" protocol");            }                        return false;        }                // check if we have a queue for this client        // In that case, we just give it the handler and be done.        // We must not process the lease request that comes with a messenger        // for an existing client. If we did, we would reply with a lease        // response. Some connections can carry only one message and then        // close. In that case, the client has to re-establish the connection        // every time we respond. So, if we repond to all incoming connections        // we're going nowhere. In some cases, the client realy wants a        // response because it believes it is an initial connection while        // we still have it from a previous session. In that case, the client        // must try to send an explicit lease renewal message. (To which        // we do respond).                String clientPeerId = destAddr.getProtocolAddress();        RelayServerClient handler = getClient(clientPeerId);                if (handler != null) {            return handler.addMessenger(newMessenger);        }                // Non-existent client. We want to process the        // connection request and respond.        // handleRequest may do whatever, but we always keep the        // messenger. It was meant for us anyway.        handleRequest(newMessenger, connectionAddress);        return true;    }    protected void handleRequest(Messenger messenger, EndpointAddress connectionAddress) {                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("handleRequest from messenger");        }                // In this case, the request comes within the messenger's destination.        String request = connectionAddress.getServiceParameter();                // make sure that the messenger shows a client logical address        EndpointAddress clientAddr = messenger.getLogicalDestinationAddress();                if (clientAddr == null || !"jxta".equals(clientAddr.getProtocolName())) {            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("LogicalDestinationAddress is not a \"jxta\" protocol");            }                        return;        }                String clientPeerId = clientAddr.getProtocolAddress();                handleRequest(request, clientPeerId, messenger);    }        protected void handleRequest(Message message, EndpointAddress dstAddr) {                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("handleRequest from message");        }                String request = RelayTransport.getString(message, RelayTransport.REQUEST_ELEMENT);        String clientPeerId = dstAddr.getServiceParameter();                handleRequest(request, clientPeerId, null);    }        void handleRequest(String request, String clientPeerId, Messenger messenger) {        // This request may come along with a messenger (if it is a renewal        // post-disconnection or an initial lease request).                if (request == null) {            return;        }                request = request.toLowerCase();                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("request = " + request);        }                // only process the request if a client peer id was sent        if (clientPeerId == null) {            return;        }                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {            LOG.fine("clientPeerId = " + clientPeerId);        }                // The only valid anonymous request is a request to obtain a real pid.        if ((clientPeerId.equals("unknown-unknown")) && (!request.startsWith(RelayTransport.PID_REQUEST))) {	            return;        }                Message responseMessage = null;                RelayServerClient closingHandler = null;        boolean rawMessenger = false;        boolean closeMessenger = false;                // Figure out which request it is        if (request.startsWith(RelayTransport.CONNECT_REQUEST)) {            // Connect Request            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.fine("connect clientPeerId = " + clientPeerId);            }                        long requestedLease = maxLeaseDuration;            boolean returnRelayAdv = false;            boolean returnOtherRelayAdv = false;            boolean flushQueue = false;                        String requestedLeaseString = null;                        // check if a lease value was specified            int startIdx = request.indexOf(',');                        if (startIdx != -1) {                // find the end of the lease value                int endIdx = request.indexOf(',', startIdx + 1);                                if (endIdx == -1) {                    requestedLeaseString = request.substring(startIdx + 1);                } else {                    requestedLeaseString = request.substring(startIdx + 1, endIdx);                    String flags = request.substring(endIdx + 1);                                        if (flags.endsWith("true")) {                        returnRelayAdv = true;                    } else if (flags.endsWith("other")) {                        // This is an addition to the protocol. Newer                        // clients will always set that in connection requests                        // when not setting true. Only older clients use to                        // set nothing at all.                        returnOtherRelayAdv = true;                    }                    // Only two flag positions for now                    // The inserted first position is another extention.                    // Only newer clients use it. Older servers will not                    // notice it because they only check how the request ends.                    // So, new clients are also compatible with old servers.                    if (flags.startsWith("flush")) {                        flushQueue = true;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -