/** * Create new streaming session with the peer. * * @param peer Address of streaming peer * @param connecting Actual connecting address * @param factory is used for establishing connection */ public StreamSession(InetAddress peer, InetAddress connecting, StreamConnectionFactory factory, int index) { this.peer = peer; this.connecting = connecting; this.index = index; this.factory = factory; this.handler = new ConnectionHandler(this); this.metrics = StreamingMetrics.get(connecting); }
public ListenableFuture<?> close() { logger.debug("[Stream #{}] Closing stream connection handler on {}", session.planId(), session.peer); ListenableFuture<?> inClosed = closeIncoming(); ListenableFuture<?> outClosed = closeOutgoing(); return Futures.allAsList(inClosed, outClosed); }
private void attachConnection(InetAddress from, int sessionIndex, IncomingStreamingConnection connection, boolean isForOutgoing, int version) throws IOException { StreamSession session = coordinator.getOrCreateSessionById(from, sessionIndex, connection.socket.getInetAddress()); session.init(this); session.handler.initiateOnReceivingSide(connection, isForOutgoing, version); }
/**l * Call back for handling exception during streaming. * * @param e thrown exception */ public void onError(Throwable e) { logError(e); // send session failure message if (handler.isOutgoingConnected()) handler.sendMessage(new SessionFailedMessage()); // fail session closeSession(State.FAILED); }
/** * Enqueue messages to be sent. * * @param messages messages to send */ public void sendMessages(Collection<? extends StreamMessage> messages) { for (StreamMessage message : messages) sendMessage(message); }
/** * Check if session is completed on receiving {@code StreamMessage.Type.COMPLETE} message. */ public synchronized void complete() { if (state == State.WAIT_COMPLETE) { if (!completeSent) { handler.sendMessage(new CompleteMessage()); completeSent = true; } closeSession(State.COMPLETE); } else { state(State.WAIT_COMPLETE); handler.closeIncoming(); } }
private boolean maybeCompleted() { boolean completed = receivers.isEmpty() && transfers.isEmpty(); if (completed) { if (state == State.WAIT_COMPLETE) { if (!completeSent) { handler.sendMessage(new CompleteMessage()); completeSent = true; } closeSession(State.COMPLETE); } else { // notify peer that this session is completed handler.sendMessage(new CompleteMessage()); completeSent = true; state(State.WAIT_COMPLETE); handler.closeOutgoing(); } } return completed; }
private void startStreamingFiles() { streamResult.handleSessionPrepared(this); state(State.STREAMING); for (StreamTransferTask task : transfers.values()) { Collection<OutgoingFileMessage> messages = task.getFileMessages(); if (messages.size() > 0) handler.sendMessages(messages); else taskCompleted(task); // there is no file to send } }
private synchronized void closeSession(State finalState) { if (isAborted.compareAndSet(false, true)) { state(finalState); if (finalState == State.FAILED) { for (StreamTask task : Iterables.concat(receivers.values(), transfers.values())) task.abort(); } if (keepAliveFuture != null) { logger.debug("[Stream #{}] Finishing keep-alive task.", planId()); keepAliveFuture.cancel(false); keepAliveFuture = null; } // Note that we shouldn't block on this close because this method is called on the handler // incoming thread (so we would deadlock). handler.close(); streamResult.handleSessionComplete(this); } }
public void start() { if (requests.isEmpty() && transfers.isEmpty()) { logger.info("[Stream #{}] Session does not have any tasks.", planId()); closeSession(State.COMPLETE); return; } try { logger.info("[Stream #{}] Starting streaming to {}{}", planId(), peer, peer.equals(connecting) ? "" : " through " + connecting); handler.initiate(); onInitializationComplete(); } catch (Exception e) { JVMStabilityInspector.inspectThrowable(e); onError(e); } }
/** * Enqueue messages to be sent. * * @param messages messages to send */ public void sendMessages(Collection<? extends StreamMessage> messages) { for (StreamMessage message : messages) sendMessage(message); }
/**l * Call back for handling exception during streaming. * * @param e thrown exception */ public void onError(Throwable e) { logError(e); // send session failure message if (handler.isOutgoingConnected()) handler.sendMessage(new SessionFailedMessage()); // fail session closeSession(State.FAILED); }
/** * Check if session is completed on receiving {@code StreamMessage.Type.COMPLETE} message. */ public synchronized void complete() { if (state == State.WAIT_COMPLETE) { if (!completeSent) { handler.sendMessage(new CompleteMessage()); completeSent = true; } closeSession(State.COMPLETE); } else { state(State.WAIT_COMPLETE); handler.closeIncoming(); } }
private boolean maybeCompleted() { boolean completed = receivers.isEmpty() && transfers.isEmpty(); if (completed) { if (state == State.WAIT_COMPLETE) { if (!completeSent) { handler.sendMessage(new CompleteMessage()); completeSent = true; } closeSession(State.COMPLETE); } else { // notify peer that this session is completed handler.sendMessage(new CompleteMessage()); completeSent = true; state(State.WAIT_COMPLETE); handler.closeOutgoing(); } } return completed; }
private void startStreamingFiles() { streamResult.handleSessionPrepared(this); state(State.STREAMING); for (StreamTransferTask task : transfers.values()) { Collection<OutgoingFileMessage> messages = task.getFileMessages(); if (messages.size() > 0) handler.sendMessages(messages); else taskCompleted(task); // there is no file to send } }
private synchronized void closeSession(State finalState) { if (isAborted.compareAndSet(false, true)) { state(finalState); if (finalState == State.FAILED) { for (StreamTask task : Iterables.concat(receivers.values(), transfers.values())) task.abort(); } if (keepAliveFuture != null) { logger.debug("[Stream #{}] Finishing keep-alive task.", planId()); keepAliveFuture.cancel(false); keepAliveFuture = null; } // Note that we shouldn't block on this close because this method is called on the handler // incoming thread (so we would deadlock). handler.close(); streamResult.handleSessionComplete(this); } }
public void start() { if (requests.isEmpty() && transfers.isEmpty()) { logger.info("[Stream #{}] Session does not have any tasks.", planId()); closeSession(State.COMPLETE); return; } try { logger.info("[Stream #{}] Starting streaming to {}{}", planId(), peer, peer.equals(connecting) ? "" : " through " + connecting); handler.initiate(); onInitializationComplete(); } catch (Exception e) { JVMStabilityInspector.inspectThrowable(e); onError(e); } }
/** * Enqueue messages to be sent. * * @param messages messages to send */ public void sendMessages(Collection<? extends StreamMessage> messages) { for (StreamMessage message : messages) sendMessage(message); }
public ListenableFuture<?> close() { logger.debug("[Stream #{}] Closing stream connection handler on {}", session.planId(), session.peer); ListenableFuture<?> inClosed = closeIncoming(); ListenableFuture<?> outClosed = closeOutgoing(); return Futures.allAsList(inClosed, outClosed); }
/**l * Call back for handling exception during streaming. * * @param e thrown exception */ public void onError(Throwable e) { logError(e); // send session failure message if (handler.isOutgoingConnected()) handler.sendMessage(new SessionFailedMessage()); // fail session closeSession(State.FAILED); }