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

📄 abstractbayeux.java

📁 jetty SERVER連接資料庫用的軟體
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            if (other==null)            {                for (ClientBayeuxListener l : _clientListeners)                    l.clientAdded((Client)client);                return;            }        }    }    /* ------------------------------------------------------------ */    /* (non-Javadoc)     * @see org.mortbay.cometd.Bx#removeClient(java.lang.String)     */    public Client removeClient(String client_id)    {        ClientImpl client;        synchronized(this)        {            if (client_id==null)                return null;            client = _clients.remove(client_id);        }        if (client!=null)        {            client.unsubscribeAll();            for (ClientBayeuxListener l : _clientListeners)                l.clientRemoved((Client)client);        }        return client;    }    /* ------------------------------------------------------------ */    /**     * @param ms The maximum time in ms to wait between polls before timing out a client     */    public void setMaxInterval(long ms)    {        _maxInterval=ms;    }    /* ------------------------------------------------------------ */    /**     * @param commented the commented to set     */    public void setJSONCommented(boolean commented)    {        if (commented)            _context.log("JSONCommented is deprecated");    }    /* ------------------------------------------------------------ */    /**     * @param logLevel     *            the logLevel: 0=none, 1=info, 2=debug     */    public void setLogLevel(int logLevel)    {        _logLevel=logLevel;    }    /* ------------------------------------------------------------ */    /* (non-Javadoc)     * @see org.mortbay.cometd.Bx#setSecurityPolicy(org.mortbay.cometd.SecurityPolicy)     */    public void setSecurityPolicy(SecurityPolicy securityPolicy)    {        _securityPolicy=securityPolicy;    }    /* ------------------------------------------------------------ */    public void setTimeout(long ms)    {        _timeout = ms;        generateAdvice();    }    /* ------------------------------------------------------------ */    public void setInterval(long ms)    {        _interval = ms;        generateAdvice();    }    /* ------------------------------------------------------------ */    /**     * The time a client should delay between reconnects when multiple     * connections from the same browser are detected. This effectively     * produces traditional polling.     * @param multiFrameInterval the multiFrameInterval to set     */    public void setMultiFrameInterval(int multiFrameInterval)    {        _multiFrameInterval=multiFrameInterval;        generateAdvice();    }    /* ------------------------------------------------------------ */    /**     * @return the multiFrameInterval in milliseconds     */    public int getMultiFrameInterval()    {        return _multiFrameInterval;    }    /* ------------------------------------------------------------ */    void generateAdvice()    {        setAdvice(new JSON.Literal("{\"reconnect\":\"retry\",\"interval\":"+getInterval()+",\"timeout\":"+getTimeout()+"}"));    }    /* ------------------------------------------------------------ */    public void setAdvice(JSON.Literal advice)    {        synchronized(this)        {            _adviceVersion++;            _advice=advice;            _multiFrameAdvice=new JSON.Literal(JSON.toString(multiFrameAdvice(advice)));        }    }    /* ------------------------------------------------------------ */    private Map<String,Object> multiFrameAdvice(JSON.Literal advice)    {        Map<String,Object> a = (Map<String,Object>)JSON.parse(_advice.toString());        a.put("multiple-clients",Boolean.TRUE);        if (_multiFrameInterval>0)        {            a.put("reconnect","retry");            a.put("interval",_multiFrameInterval);        }        else            a.put("reconnect","none");        return a;    }    /* ------------------------------------------------------------ */    public JSON.Literal getAdvice()    {        return _advice;    }    /* ------------------------------------------------------------ */    /**     * @return TRUE if {@link #getCurrentRequest()} will return the current request     */    public boolean isRequestAvailable()    {        return _requestAvailable;    }    /* ------------------------------------------------------------ */    /**     * @param requestAvailable TRUE if {@link #getCurrentRequest()} will return the current request     */    public void setRequestAvailable(boolean requestAvailable)    {        _requestAvailable=requestAvailable;    }    /* ------------------------------------------------------------ */    /**     * @return the current request if {@link #isRequestAvailable()} is true, else null     */    public HttpServletRequest getCurrentRequest()    {        return _request.get();    }    /* ------------------------------------------------------------ */    /**     * @return the current request if {@link #isRequestAvailable()} is true, else null     */    void setCurrentRequest(HttpServletRequest request)    {        _request.set(request);    }    /* ------------------------------------------------------------ */    public Collection<Channel> getChannels()    {        List<Channel> channels = new ArrayList<Channel>();        _root.getChannels(channels);        return channels;    }    /* ------------------------------------------------------------ */    /**     * @return     */    public int getChannelCount()    {        return _root.getChannelCount();    }    /* ------------------------------------------------------------ */    public Collection<Client> getClients()    {        synchronized(this)        {            return new ArrayList<Client>(_clients.values());        }    }    /* ------------------------------------------------------------ */    /**     * @return     */    public int getClientCount()    {        synchronized(this)        {            return _clients.size();        }    }    /* ------------------------------------------------------------ */    public boolean hasClient(String clientId)    {        synchronized(this)        {            if (clientId==null)                return false;            return _clients.containsKey(clientId);        }    }    /* ------------------------------------------------------------ */    public Channel removeChannel(String channelId)    {        Channel channel = getChannel(channelId);        boolean removed = false;        if (channel!=null)            removed = channel.remove();        if (removed)            return channel;        else            return null;    }    /* ------------------------------------------------------------ */    protected void initialize(ServletContext context)    {        synchronized(this)        {            _initialized=true;            _context=context;            try            {                _random=SecureRandom.getInstance("SHA1PRNG");            }            catch (Exception e)            {                context.log("Could not get secure random for ID generation",e);                _random=new Random();            }            _random.setSeed(_random.nextLong()^hashCode()^System.nanoTime()^Runtime.getRuntime().freeMemory());            _channelIdCache=new ConcurrentHashMap<String, ChannelId>();            _root.addChild(new ServiceChannel(Bayeux.SERVICE));        }    }    /* ------------------------------------------------------------ */    long getRandom()    {        long l=_random.nextLong();        return l<0?-l:l;    }    /* ------------------------------------------------------------ */    void clientOnBrowser(String browserId,String clientId)    {        List<String> clients=_browser2client.get(browserId);        if (clients==null)        {            List<String> new_clients=new CopyOnWriteArrayList<String>();            clients=_browser2client.putIfAbsent(browserId,new_clients);            if (clients==null)                clients=new_clients;        }        clients.add(clientId);    }    /* ------------------------------------------------------------ */    void clientOffBrowser(String browserId,String clientId)    {        List<String> clients=_browser2client.get(browserId);                if (clients!=null)            clients.remove(clientId);    }    /* ------------------------------------------------------------ */    List<String> clientsOnBrowser(String browserId)    {        List<String> clients=_browser2client.get(browserId);                if (clients==null)            return Collections.emptyList();        return clients;    }    /* ------------------------------------------------------------ */    public void addListener(BayeuxListener listener)    {        if (listener instanceof ClientBayeuxListener)            _clientListeners.add((ClientBayeuxListener)listener);        else if(listener instanceof ChannelBayeuxListener)            _channelListeners.add((ChannelBayeuxListener)listener);    }    /* ------------------------------------------------------------ */    public int getMaxClientQueue()    {        return _maxClientQueue;    }    /* ------------------------------------------------------------ */    public void setMaxClientQueue(int size)    {        _maxClientQueue=size;    }    /* ------------------------------------------------------------ */    protected Message extendRcv(ClientImpl from, Message message)    {        if (_extensions!=null)        {            for (int i=_extensions.length;message!=null && i-->0;)                message=_extensions[i].rcv(from, message);        }        if (from!=null)        {            Extension[] client_exs = from.getExtensions();            if (client_exs!=null)            {                for (int i=client_exs.length;message!=null && i-->0;)                    message=client_exs[i].rcv(from, message);            }        }        return message;    }    /* ------------------------------------------------------------ */    protected Message extendRcvMeta(ClientImpl from, Message message)    {        if (_extensions!=null)        {            for (int i=_extensions.length;message!=null && i-->0;)                message=_extensions[i].rcvMeta(from, message);        }        if (from!=null)        {            Extension[] client_exs = from.getExtensions();            if (client_exs!=null)            {                for (int i=client_exs.length;message!=null && i-->0;)                    message=client_exs[i].rcvMeta(from, message);            }        }        return message;    }    /* ------------------------------------------------------------ */    protected Message extendSendBayeux(Client from, Message message)    {        if (_extensions!=null)        {            for (int i=0;message!=null && i<_extensions.length;i++)            {                Message m=_extensions[i].send(from, message);            }        }        return message;    }    /* ------------------------------------------------------------ */    public Message extendSendClient(Client from, ClientImpl to, Message message)    {        if (to!=null)        {            Extension[] client_exs = to.getExtensions();            if (client_exs!=null)            {                for (int i=0;message!=null && i<client_exs.length;i++)                    message=client_exs[i].send(from, message);            }        }        return message;    }    /* ------------------------------------------------------------ */    public Message extendSendMeta(ClientImpl from, Message message)    {        if (_extensions!=null)        {            for (int i=0;message!=null && i<_extensions.length;i++)                message=_extensions[i].sendMeta(from, message);        }        if (from!=null)        {            Extension[] client_exs = from.getExtensions();            if (client_exs!=null)            {                for (int i=0;message!=null && i<client_exs.length;i++)                    message=client_exs[i].sendMeta(from, message);            }        }        return message;    }    /* ------------------------------------------------------------ */    /* ------------------------------------------------------------ */    public static class DefaultPolicy implements SecurityPolicy    {        public boolean canHandshake(Message message)        {            return true;        }        public boolean canCreate(Client client, String channel, Message message)        {            return client!=null && !channel.startsWith(Bayeux.META_SLASH);        }        public boolean canSubscribe(Client client, String channel, Message message)        {	    if (client!=null && ("/**".equals(channel) || "/*".equals(channel)))	        return false;            return client!=null && !channel.startsWith(Bayeux.META_SLASH);        }        public boolean canPublish(Client client, String channel, Message message)        {            return client!=null || client==null && Bayeux.META_HANDSHAKE.equals(channel);        }    }    /* ------------------------------------------------------------ */    /* ------------------------------------------------------------ */    protected abstract class Handler    {        abstract void handle(ClientImpl client, Transport transport, Message message) throws IOException;        abstract ChannelId getMetaChannelId();        void unknownClient(Transport transport,String channel) throws IOException        {            MessageImpl reply=newMessage();            reply.put(CHANNEL_FIELD,channel);            reply.put(SUCCESSFUL_FIELD,Boolean.FALSE);            reply.put(ERROR_FIELD,"402::Unknown client");            reply.put("advice",_handshakeAdvice);            transport.send(reply);            reply.decRef();        }                void sendMetaReply(final ClientImpl client,Message reply, final Transport transport) throws IOException        {

⌨️ 快捷键说明

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