org.apache.mina.core.write.DefaultWriteRequest Java Examples
The following examples show how to use
org.apache.mina.core.write.DefaultWriteRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StartSessionHandshake.java From neoscada with Eclipse Public License 1.0 | 6 votes |
@Override public void postApply ( final HandshakeContext context, final Map<String, String> acceptedProperties ) throws Exception { if ( isEnabled ( acceptedProperties ) ) { if ( context.isClientMode () ) { // the feature is enabled and we are on the client side, tell the server we are ready logger.debug ( "Sending session start" ); context.getNextFilter ().filterWrite ( context.getSession (), new DefaultWriteRequest ( new StartMessage () ) ); } } else { if ( !context.isClientMode () ) { // the feature is disabled and we are on the server side, we are ready then // otherwise we need to wait for a response from the client // yet we only mark the session as open, it well actually be opened by the MessageChannelFilter for us logger.debug ( "Marking session directly as opened since we are not enabled" ); MessageChannelFilter.markOpened ( context.getSession () ); } } }
Example #2
Source File: KeepAliveFilter.java From neoscada with Eclipse Public License 1.0 | 6 votes |
@Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { try { if (messageFactory.isRequest(session, message)) { Object pongMessage = messageFactory.getResponse(session, message); if (pongMessage != null) { nextFilter.filterWrite(session, new DefaultWriteRequest(pongMessage)); } } if (messageFactory.isResponse(session, message)) { resetStatus(session); } } finally { if (!isKeepAliveMessage(session, message)) { nextFilter.messageReceived(session, message); } } }
Example #3
Source File: BufferedWriteFilter.java From neoscada with Eclipse Public License 1.0 | 6 votes |
/** * Writes <code>data</code> {@link IoBuffer} to the <code>buf</code> * {@link IoBuffer} which buffers write requests for the * <code>session</code> {@ link IoSession} until buffer is full * or manually flushed. * * @param session the session where buffer will be written * @param data the data to buffer * @param buf the buffer where data will be temporarily written */ private void write(IoSession session, IoBuffer data, IoBuffer buf) { try { int len = data.remaining(); if (len >= buf.capacity()) { /* * If the request length exceeds the size of the output buffer, * flush the output buffer and then write the data directly. */ NextFilter nextFilter = session.getFilterChain().getNextFilter(this); internalFlush(nextFilter, session, buf); nextFilter.filterWrite(session, new DefaultWriteRequest(data)); return; } if (len > (buf.limit() - buf.position())) { internalFlush(session.getFilterChain().getNextFilter(this), session, buf); } synchronized (buf) { buf.put(data); } } catch (Throwable e) { session.getFilterChain().fireExceptionCaught(e); } }
Example #4
Source File: NetManager.java From jane with GNU Lesser General Public License v3.0 | 6 votes |
/** * 发送对象的底层入口. 可带监听器,并返回WriteFuture */ public static WriteFuture write(IoSession session, Object obj, IoFutureListener<?> listener) { if (session.isClosing() || obj == null) return null; IoFilterChain ifc = session.getFilterChain(); WriteFuture wf = new DefaultWriteFuture(session); if (listener != null) wf.addListener(listener); DefaultWriteRequest dwr = new DefaultWriteRequest(obj, wf); synchronized (session) { ifc.fireFilterWrite(dwr); } return wf; }
Example #5
Source File: MessageChannelFilter.java From neoscada with Eclipse Public License 1.0 | 5 votes |
@Override public void sessionOpened ( final NextFilter nextFilter, final IoSession session ) throws Exception { final ProtocolConfiguration configuration = ProtocolConfiguration.fromSession ( session ); // init timeout for handshake // FIXME: this is not a real timeout but a read idle timeout only session.getConfig ().setReaderIdleTime ( (int)Math.ceil ( configuration.getHandshakeTimeout () / 1000.0 ) ); if ( this.clientMode ) { nextFilter.filterWrite ( session, new DefaultWriteRequest ( createHelloMessage ( session, nextFilter ) ) ); } }
Example #6
Source File: AbstractProxyLogicHandler.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * Writes data to the proxy server. * * @param nextFilter the next filter * @param data Data buffer to be written. */ protected WriteFuture writeData(final NextFilter nextFilter, final IoBuffer data) { // write net data ProxyHandshakeIoBuffer writeBuffer = new ProxyHandshakeIoBuffer(data); LOGGER.debug(" session write: {}", writeBuffer); WriteFuture writeFuture = new DefaultWriteFuture(getSession()); getProxyFilter().writeData(nextFilter, getSession(), new DefaultWriteRequest(writeBuffer, writeFuture), true); return writeFuture; }
Example #7
Source File: AbstractStreamWriteFilter.java From neoscada with Eclipse Public License 1.0 | 5 votes |
@Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { // If we're already processing a stream we need to queue the WriteRequest. if (session.getAttribute(CURRENT_STREAM) != null) { Queue<WriteRequest> queue = getWriteRequestQueue(session); if (queue == null) { queue = new ConcurrentLinkedQueue<WriteRequest>(); session.setAttribute(WRITE_REQUEST_QUEUE, queue); } queue.add(writeRequest); return; } Object message = writeRequest.getMessage(); if (getMessageClass().isInstance(message)) { T stream = getMessageClass().cast(message); IoBuffer buffer = getNextBuffer(stream); if (buffer == null) { // End of stream reached. writeRequest.getFuture().setWritten(); nextFilter.messageSent(session, writeRequest); } else { session.setAttribute(CURRENT_STREAM, message); session.setAttribute(CURRENT_WRITE_REQUEST, writeRequest); nextFilter.filterWrite(session, new DefaultWriteRequest(buffer)); } } else { nextFilter.filterWrite(session, writeRequest); } }
Example #8
Source File: AbstractStreamWriteFilter.java From neoscada with Eclipse Public License 1.0 | 5 votes |
@Override public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { T stream = getMessageClass().cast(session.getAttribute(CURRENT_STREAM)); if (stream == null) { nextFilter.messageSent(session, writeRequest); } else { IoBuffer buffer = getNextBuffer(stream); if (buffer == null) { // End of stream reached. session.removeAttribute(CURRENT_STREAM); WriteRequest currentWriteRequest = (WriteRequest) session.removeAttribute(CURRENT_WRITE_REQUEST); // Write queued WriteRequests. Queue<WriteRequest> queue = removeWriteRequestQueue(session); if (queue != null) { WriteRequest wr = queue.poll(); while (wr != null) { filterWrite(nextFilter, session, wr); wr = queue.poll(); } } currentWriteRequest.getFuture().setWritten(); nextFilter.messageSent(session, currentWriteRequest); } else { nextFilter.filterWrite(session, new DefaultWriteRequest(buffer)); } } }
Example #9
Source File: KeepAliveFilter.java From neoscada with Eclipse Public License 1.0 | 5 votes |
@Override public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception { if (status == interestedIdleStatus) { if (!session.containsAttribute(WAITING_FOR_RESPONSE)) { Object pingMessage = messageFactory.getRequest(session); if (pingMessage != null) { nextFilter.filterWrite(session, new DefaultWriteRequest(pingMessage)); // If policy is OFF, there's no need to wait for // the response. if (getRequestTimeoutHandler() != KeepAliveRequestTimeoutHandler.DEAF_SPEAKER) { markStatus(session); if (interestedIdleStatus == IdleStatus.BOTH_IDLE) { session.setAttribute(IGNORE_READER_IDLE_ONCE); } } else { resetStatus(session); } } } else { handlePingTimeout(session); } } else if (status == IdleStatus.READER_IDLE) { if (session.removeAttribute(IGNORE_READER_IDLE_ONCE) == null) { if (session.containsAttribute(WAITING_FOR_RESPONSE)) { handlePingTimeout(session); } } } if (forwardEvent) { nextFilter.sessionIdle(session, status); } }
Example #10
Source File: BufferedWriteFilter.java From neoscada with Eclipse Public License 1.0 | 5 votes |
/** * Internal method that actually flushes the buffered data. * * @param nextFilter the {@link NextFilter} of this filter * @param session the session where buffer will be written * @param buf the data to write * @throws Exception if a write operation fails */ private void internalFlush(NextFilter nextFilter, IoSession session, IoBuffer buf) throws Exception { IoBuffer tmp = null; synchronized (buf) { buf.flip(); tmp = buf.duplicate(); buf.clear(); } logger.debug("Flushing buffer: {}", tmp); nextFilter.filterWrite(session, new DefaultWriteRequest(tmp)); }
Example #11
Source File: NioSession.java From jane with GNU Lesser General Public License v3.0 | 5 votes |
@Override public WriteFuture write(Object message) { if (message == null) throw new IllegalArgumentException("trying to write a null message: not allowed"); // If the session has been closed or is closing, we can't either send a message to the remote side. // We generate a future containing an exception. if (isClosing() || !isConnected()) return DefaultWriteFuture.newNotWrittenFuture(this, new WriteToClosedSessionException(null)); try { if ((message instanceof IoBuffer) && !((IoBuffer)message).hasRemaining()) { // Nothing to write: probably an error in the user code throw new IllegalArgumentException("message is empty, forgot to call flip()?"); } else if (message instanceof FileChannel) { FileChannel fileChannel = (FileChannel)message; message = new DefaultFileRegion(fileChannel, 0, fileChannel.size()); } } catch (IOException e) { ExceptionMonitor.getInstance().exceptionCaught(e); return DefaultWriteFuture.newNotWrittenFuture(this, e); } // Now, we can write the message. WriteFuture writeFuture = new DefaultWriteFuture(this); WriteRequest writeRequest = new DefaultWriteRequest(message, writeFuture); filterChain.fireFilterWrite(writeRequest); return writeFuture; }
Example #12
Source File: SslHandler.java From jane with GNU Lesser General Public License v3.0 | 5 votes |
WriteFuture writeNetBuffer(NextFilter nextFilter, boolean needFuture) throws Exception { // Check if any net data needed to be writen if (outNetBuffer == null || !outNetBuffer.hasRemaining()) return null; // no; bail out // set flag that we are writing encrypted data (used in SSLFilter.filterWrite()) writingEncryptedData = true; // write net data WriteFuture writeFuture = (needFuture ? new DefaultWriteFuture(session) : null); try { IoBuffer writeBuffer = fetchOutNetBuffer(); sslFilter.filterWrite(nextFilter, session, writeFuture != null ? new DefaultWriteRequest(writeBuffer, writeFuture) : writeBuffer); // loop while more writes required to complete handshake while (handshakeStatus == HandshakeStatus.NEED_WRAP && !isInboundDone()) { try { handshake(nextFilter); } catch (SSLException ssle) { SSLException newSsle = new SSLHandshakeException("SSL handshake failed"); newSsle.initCause(ssle); throw newSsle; } IoBuffer currentOutNetBuffer = fetchOutNetBuffer(); if (currentOutNetBuffer != null && currentOutNetBuffer.hasRemaining()) { writeFuture = (needFuture ? new DefaultWriteFuture(session) : null); sslFilter.filterWrite(nextFilter, session, writeFuture != null ? new DefaultWriteRequest(currentOutNetBuffer, writeFuture) : currentOutNetBuffer); } } } finally { writingEncryptedData = false; } return writeFuture; }
Example #13
Source File: MessageChannelFilter.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public void messageReceived ( final NextFilter nextFilter, final IoSession session, final Object message ) throws Exception { logger.debug ( "messageReceived - message: {}, session: {}", message, session ); //$NON-NLS-1$ final boolean opened = isOpened ( session ); if ( message instanceof HelloMessage && !opened && !this.clientMode ) { // received hello when we need it final Object reply = createSession ( session, nextFilter, (HelloMessage)message ); logger.info ( "Reply to session creation: {}", reply ); //$NON-NLS-1$ if ( reply instanceof AcceptMessage ) { this.acceptedProperties = Collections.unmodifiableMap ( ( (AcceptMessage)reply ).getProperties () ); // configure the session, enable filters configureSession ( session, nextFilter, this.acceptedProperties ); /* * send reply - this must be the first message after the configureSession call * so that filter can reset their initial flag. The first message must still be * un-filtered since the other side needs to adapt its filter chain first. However * this must be an atomic operation since sending and changing the filter in two * separate steps could mean that we already receive the reply to our ACCEPT * before we had a chance to change our filter chain. */ nextFilter.filterWrite ( session, new DefaultWriteRequest ( reply ) ); // post configure the session postConfigureSession ( session, nextFilter, this.acceptedProperties ); if ( isOpened ( session ) ) { // if the session is marked "open" by the StartSessionHandshake, then notify the open state immediately startSession ( session, nextFilter ); } } else { // send reply nextFilter.filterWrite ( session, new DefaultWriteRequest ( reply ) ); // close - after message was sent session.close ( false ); } dumpFilterChain ( session ); logger.debug ( "Done handling HelloMessage" ); } else if ( message instanceof AcceptMessage && !opened && this.clientMode ) { this.acceptedProperties = Collections.unmodifiableMap ( ( (AcceptMessage)message ).getProperties () ); configureSession ( session, nextFilter, this.acceptedProperties ); postConfigureSession ( session, nextFilter, this.acceptedProperties ); markOpened ( session ); startSession ( session, nextFilter ); dumpFilterChain ( session ); } else if ( message instanceof CloseMessage ) { // remote will close session.setAttribute ( "closeReason.message", ( (CloseMessage)message ).getMessage () ); //$NON-NLS-1$ session.setAttribute ( "closeReason.code", ( (CloseMessage)message ).getCode () ); //$NON-NLS-1$ session.close ( true ); } else if ( message instanceof DataMessage && opened ) { nextFilter.messageReceived ( session, message ); } else if ( message instanceof StartMessage && !opened ) { logger.debug ( "Starting session" ); markOpened ( session ); startSession ( session, nextFilter ); dumpFilterChain ( session ); } else { throw new IllegalStateException ( String.format ( "(opened: %s) Message type: %s", opened, message.getClass () ) ); //$NON-NLS-1$ } }
Example #14
Source File: HttpCodec.java From jane with GNU Lesser General Public License v3.0 | 4 votes |
public static void write(NextFilter next, WriteRequest writeRequest, IoBuffer buf) throws Exception { WriteFuture wf = writeRequest.writeRequestFuture(); next.filterWrite(wf == DefaultWriteRequest.UNUSED_FUTURE ? buf : new DefaultWriteRequest(buf, wf)); }