📄 bayeuxclient.java
字号:
} /* ------------------------------------------------------------ */ /* * (non-Javadoc) * * @see dojox.cometd.Client#publish(java.lang.String, java.lang.Object, * java.lang.String) */ public void publish(String toChannel, Object data, String msgId) { if (!isRunning() || _disconnecting) throw new IllegalStateException("Not running"); MessageImpl msg = _msgPool.newMessage(); msg.put(Bayeux.CHANNEL_FIELD,toChannel); msg.put(Bayeux.DATA_FIELD,data); if (msgId != null) msg.put(Bayeux.ID_FIELD,msgId); publish(msg); msg.decRef(); } /* ------------------------------------------------------------ */ /* * (non-Javadoc) * * @see dojox.cometd.Client#subscribe(java.lang.String) */ public void subscribe(String toChannel) { if (!isRunning() || _disconnecting) throw new IllegalStateException("Not running"); MessageImpl msg = _msgPool.newMessage(); msg.put(Bayeux.CHANNEL_FIELD,Bayeux.META_SUBSCRIBE); msg.put(Bayeux.SUBSCRIPTION_FIELD,toChannel); publish(msg); msg.decRef(); } /* ------------------------------------------------------------ */ /* * (non-Javadoc) * * @see dojox.cometd.Client#unsubscribe(java.lang.String) */ public void unsubscribe(String toChannel) { if (!isRunning() || _disconnecting) throw new IllegalStateException("Not running"); MessageImpl msg = _msgPool.newMessage(); msg.put(Bayeux.CHANNEL_FIELD,Bayeux.META_UNSUBSCRIBE); msg.put(Bayeux.SUBSCRIPTION_FIELD,toChannel); publish(msg); msg.decRef(); } /* ------------------------------------------------------------ */ /** * Disconnect this client. * @deprecated use {@link #disconnect()} */ public void remove() { disconnect(); } /* ------------------------------------------------------------ */ /** * Disconnect this client. */ public void disconnect() { if (!isRunning() || _disconnecting) throw new IllegalStateException("Not running"); MessageImpl msg = _msgPool.newMessage(); msg.put(Bayeux.CHANNEL_FIELD,Bayeux.META_DISCONNECT); synchronized (_outQ) { _outQ.add(msg); _disconnecting = true; if (_batch == 0 && _initialized && _push == null) { _push = new Publish(); try { send(_push); } catch (IOException e) { Log.warn(e.toString()); Log.debug(e); send(_push,true); } } _initialized = false; } } /* ------------------------------------------------------------ */ /** * @deprecated */ public void setListener(org.cometd.Listener listener) { synchronized (_inQ) { if (_listener != null) removeListener(_listener); _listener = listener; if (_listener != null) addListener(_listener); } } /* ------------------------------------------------------------ */ /* * (non-Javadoc) Removes all available messages from the inbound queue. If a * listener is set then messages are not queued. * * @see dojox.cometd.Client#takeMessages() */ public List<Message> takeMessages() { final LinkedList<Message> list; synchronized (_inQ) { list = new LinkedList<Message>(_inQ); _inQ.clear(); } for (Message m : list) if (m instanceof MessageImpl) ((MessageImpl)m).decRef(); return list; } /* ------------------------------------------------------------ */ /* * (non-Javadoc) * * @see dojox.cometd.Client#endBatch() */ public void endBatch() { synchronized (_outQ) { if (--_batch <= 0) { _batch = 0; if ((_initialized || _disconnecting) && _push == null && _outQ.size() > 0) { _push = new Publish(); try { send(_push); } catch (IOException e) { metaPublishFail(e,((Publish)_push).getOutboundMessages()); } } } } } /* ------------------------------------------------------------ */ /* * (non-Javadoc) * * @see dojox.cometd.Client#startBatch() */ public void startBatch() { if (!isRunning()) throw new IllegalStateException("Not running"); synchronized (_outQ) { _batch++; } } /* ------------------------------------------------------------ */ /** * Customize an Exchange. Called when an exchange is about to be sent to * allow Cookies and Credentials to be customized. Default implementation * sets any cookies */ protected void customize(HttpExchange exchange) { StringBuilder buf = null; for (Cookie cookie : _cookies.values()) { if (buf == null) buf = new StringBuilder(); else buf.append("; "); buf.append(cookie.getName()); // TODO quotes buf.append("="); buf.append(cookie.getValue()); // TODO quotes } if (buf != null) exchange.addRequestHeader(HttpHeaders.COOKIE,buf.toString()); if (_scheme!=null) exchange.setScheme(_scheme); } /* ------------------------------------------------------------ */ public void setCookie(Cookie cookie) { _cookies.put(cookie.getName(),cookie); } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** * The base class for all bayeux exchanges. */ protected class Exchange extends ContentExchange { Message[] _responses; int _connectFailures; int _backoffRetries = 0; String _json; /* ------------------------------------------------------------ */ Exchange(String info) { setMethod("POST"); setScheme(HttpSchemes.HTTP_BUFFER); setAddress(_cometdAddress); setURI(_path + "/" + info); setRequestContentType(_formEncoded?"application/x-www-form-urlencoded;charset=utf-8":"text/json;charset=utf-8"); } /* ------------------------------------------------------------ */ public int getBackoffRetries() { return _backoffRetries; } /* ------------------------------------------------------------ */ public void incBackoffRetries() { ++_backoffRetries; } /* ------------------------------------------------------------ */ protected void setMessage(String message) { message=extendOut(message); setJson(message); } /* ------------------------------------------------------------ */ protected void setJson(String json) { try { _json = json; if (_formEncoded) setRequestContent(new ByteArrayBuffer("message=" + URLEncoder.encode(_json,"utf-8"))); else setRequestContent(new ByteArrayBuffer(_json,"utf-8")); } catch (Exception e) { Log.ignore(e); setRequestContent(new ByteArrayBuffer(_json)); } } /* ------------------------------------------------------------ */ protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException { super.onResponseStatus(version,status,reason); } /* ------------------------------------------------------------ */ protected void onResponseHeader(Buffer name, Buffer value) throws IOException { super.onResponseHeader(name,value); if (!isRunning()) return; if (HttpHeaders.CACHE.getOrdinal(name) == HttpHeaders.SET_COOKIE_ORDINAL) { String cname = null; String cvalue = null; QuotedStringTokenizer tok = new QuotedStringTokenizer(value.toString(),"=;",false,false); tok.setSingle(false); if (tok.hasMoreElements()) cname = tok.nextToken(); if (tok.hasMoreElements()) cvalue = tok.nextToken(); Cookie cookie = new Cookie(cname,cvalue); while (tok.hasMoreTokens()) { String token = tok.nextToken(); if ("Version".equalsIgnoreCase(token)) cookie.setVersion(Integer.parseInt(tok.nextToken())); else if ("Comment".equalsIgnoreCase(token)) cookie.setComment(tok.nextToken()); else if ("Path".equalsIgnoreCase(token)) cookie.setPath(tok.nextToken()); else if ("Domain".equalsIgnoreCase(token)) cookie.setDomain(tok.nextToken()); else if ("Expires".equalsIgnoreCase(token)) { tok.nextToken(); // TODO } else if ("Max-Age".equalsIgnoreCase(token)) { tok.nextToken(); // TODO } else if ("Secure".equalsIgnoreCase(token)) cookie.setSecure(true); } BayeuxClient.this.setCookie(cookie); } } /* ------------------------------------------------------------ */ protected void onResponseComplete() throws IOException { if (!isRunning()) return; super.onResponseComplete(); if (getResponseStatus() == 200) { String content = getResponseContent(); // TODO if (content == null || content.length() == 0) throw new IllegalStateException(); _responses = _msgPool.parse(content); if (_responses!=null) for (int i=0;i<_responses.length;i++) extendIn(_responses[i]); } } /* ------------------------------------------------------------ */ protected void resend(boolean backoff) { if (!isRunning()) return; final boolean disconnecting; synchronized (_outQ) { disconnecting=_disconnecting; } if (disconnecting) { try{stop();}catch(Exception e){Log.ignore(e);} return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -