public void onSuccess(Void v) { // To make sure DISCONNECT has been flushed out to the socket onRefillCalled = false; refiller = new Runnable() { public void run() { if(onRefillCalled) { stop.run(); } } }; if(transport != null){ transport.flush(); } } public void onFailure(Throwable value) {
public void resume() { suspendChanges.incrementAndGet(); if( suspendCount.decrementAndGet() == 0 && this.transport!=null ) { this.transport.resumeRead(); if(this.heartBeatMonitor!=null){ this.heartBeatMonitor.resumeRead(); } } }
transport = new TcpTransport(); } else if( SslTransport.protocol(scheme)!=null ) { SslTransport ssl = new SslTransport(); if( mqtt.sslContext == null ) { mqtt.sslContext = SSLContext.getDefault(); ssl.setSSLContext(mqtt.sslContext); transport = ssl; } else { transport.setBlockingExecutor(mqtt.blockingExecutor); transport.setDispatchQueue(queue); transport.setProtocolCodec(new MQTTProtocolCodec()); tcp.setMaxReadRate(mqtt.maxReadRate); tcp.setMaxWriteRate(mqtt.maxWriteRate); tcp.setReceiveBufferSize(mqtt.receiveBufferSize); tcp.setSendBufferSize(mqtt.sendBufferSize); tcp.setTrafficClass(mqtt.trafficClass); tcp.setUseLocalHost(mqtt.useLocalHost); tcp.connecting(mqtt.host, mqtt.localAddress); transport.setTransportListener(new DefaultTransportListener(){ @Override public void onTransportConnected() { transport.start(NOOP);
public void onSessionEstablished(Transport transport) { this.transport = transport; if( suspendCount.get() > 0 ) { this.transport.suspendRead(); this.transport.setTransportListener(new DefaultTransportListener() { @Override public void onTransportCommand(Object command) { pingedAt = 0; if(mqtt.getKeepAlive()>0) { heartBeatMonitor = new HeartBeatMonitor(); heartBeatMonitor.setWriteInterval((mqtt.getKeepAlive() * 1000) / 2); heartBeatMonitor.setTransport(this.transport); heartBeatMonitor.suspendRead(); // to match the suspended state of the transport. heartBeatMonitor.setOnKeepAlive(new Task() { @Override public void run() { heartBeatMonitor.start();
public void onSuccess(final Transport transport) { transport.setTransportListener(new DefaultTransportListener() { @Override public void onTransportFailure(IOException error) { transport.resumeRead(); if( mqtt.connect.clientId() == null ) { String id = hex(transport.getLocalAddress())+Long.toHexString(System.currentTimeMillis()/1000); if(id.length() > 23) { id = id.substring(0,23); boolean accepted = transport.offer(encoded); mqtt.tracer.onSend(encoded); mqtt.tracer.debug("Logging in");
public void run() { if(!executed) { executed = true; requests.remove(requestId); if(heartBeatMonitor!=null) { heartBeatMonitor.stop(); heartBeatMonitor = null; } transport.stop(new Task() { @Override public void run() { listener.onDisconnected(); if (onComplete != null) { onComplete.onSuccess(null); } } }); } } };
@Override public void onTransportFailure(IOException error) { mqtt.tracer.debug("Transport failure: %s", error); transport.stop(NOOP); onFailure(error); }
public void suspend() { suspendChanges.incrementAndGet(); if( suspendCount.incrementAndGet() == 1 && this.transport!=null ) { this.transport.suspendRead(); if(this.heartBeatMonitor!=null){ this.heartBeatMonitor.suspendRead(); } } }
private void send(Request request) { if( failure !=null ) { if( request.cb!=null ) { request.cb.onFailure(failure); } } else { // Put the request in the map before sending it over the wire. if(request.id!=0) { this.requests.put(request.id, request); } if( overflow.isEmpty() && transport!=null && transport.offer(request.frame) ) { mqtt.tracer.onSend(request.frame); if(request.id==0) { if( request.cb!=null ) { ((Callback<Void>)request.cb).onSuccess(null); } } } else { // Remove it from the request. this.requests.remove(request.id); overflow.addLast(request); } } }
public boolean full() { queue.assertExecuting(); return this.transport.full(); }
protected final void handleSocket(SocketChannel socket) throws Exception { TcpTransport transport = createTransport(); transport.connected(socket); listener.onAccept(transport); }
void onCanceled() { trace("CONNECTED.onCanceled"); CANCELING state = new CANCELING(); socketState = state; state.add(createDisconnectTask()); state.onCanceled(); } Task createDisconnectTask() {
public boolean full() { return codec==null || codec.full() || !socketState.is(CONNECTED.class) || getServiceState() != STARTED; }
public void setTransport(Transport transport) { this.transport = transport; this.readChannel = transport.getReadChannel(); this.writeChannel = transport.getWriteChannel(); initNext(); }
/** * Kills the connection without a graceful disconnect. * @param onComplete */ public void kill(final Callback<Void> onComplete) { if( disconnected ) { if(onComplete!=null){ onComplete.onSuccess(null); } return; } disconnected = true; if(heartBeatMonitor!=null) { heartBeatMonitor.stop(); heartBeatMonitor = null; } transport.stop(new Task() { @Override public void run() { listener.onDisconnected(); if (onComplete != null) { onComplete.onSuccess(null); } } }); }
mqtt.tracer.debug("MQTT login rejected"); transport.stop(NOOP); cb.onFailure(new MQTTException("Could not connect: " + connack.code(), connack)); mqtt.tracer.debug("Received unexpected MQTT frame: %d", response.messageType()); transport.stop(NOOP); cb.onFailure(new IOException("Could not connect. Received unexpected command: " + response.messageType())); transport.stop(NOOP); cb.onFailure(e);
if( this.transport.offer(request.frame) ) { mqtt.tracer.onSend(request.frame); overflow.removeFirst();
void handleSessionFailure(Throwable error) { // Socket failure, should we try to reconnect? if( !disconnected && (mqtt.reconnectAttemptsMax<0 || reconnects < mqtt.reconnectAttemptsMax ) ) { mqtt.tracer.debug("Reconnecting transport"); // Cleanup the previous transport. if(heartBeatMonitor!=null) { heartBeatMonitor.stop(); heartBeatMonitor = null; } final Transport t = transport; transport = null; if(t!=null) { t.stop(new Task() { @Override public void run() { listener.onDisconnected(); reconnect(); } }); } else { reconnect(); } } else { // nope. handleFatalFailure(error); } }
if(CallbackConnection.this.transport.offer(encoded)) { mqtt.tracer.onSend(encoded); final long now = System.currentTimeMillis();