if (!ses.accepted()) { proceedMessageReceived(ses, msg); Integer magic = ses.meta(MAGIC_META_KEY); byte[] magicBuf = ses.meta(MAGIC_BUF_KEY); ses.addMeta(MAGIC_META_KEY, cnt + magicRead); ses.addMeta(MAGIC_BUF_KEY, magicBuf); ses.removeMeta(MAGIC_BUF_KEY); ses.addMeta(MAGIC_META_KEY, U.IGNITE_HEADER.length); ses.close(); "Ignite port?) [rmtAddr=" + ses.remoteAddress() + ", locAddr=" + ses.localAddress() + ']');
/** * Sends a response to be encoded and sent to the Redis client. * * @param ses NIO session. * @param res Response. * @return NIO send future. */ private GridNioFuture<?> sendResponse(GridNioSession ses, GridRedisMessage res) { return ses.send(res); } }
/** {@inheritDoc} */ @Override public List<String> getConnections() { Collection<? extends GridNioSession> sessions = srv.sessions(); List<String> res = new ArrayList<>(sessions.size()); for (GridNioSession ses : sessions) { ClientListenerConnectionContext connCtx = ses.meta(CONN_CTX_META_KEY); if (connCtx == null || ses.closeTime() != 0) continue; // Skip non-initialized or closed session. String desc = clientConnectionDescription(ses, connCtx); res.add(desc); } return res; }
/** {@inheritDoc} */ @Override public long getIdleTime() { long now = U.currentTimeMillis(); // Session can be used for receiving and sending. return Math.min(Math.min(now - ses.lastReceiveTime(), now - ses.lastSendScheduleTime()), now - ses.lastSendTime()); }
/** {@inheritDoc} */ @Override public byte[] decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException { ClientListenerNioServerBuffer nioBuf = ses.meta(BUF_META_KEY); // Decode for a given session is called per one thread, so there should not be any concurrency issues. // However, we make some additional checks. if (nioBuf == null) { nioBuf = new ClientListenerNioServerBuffer(); ClientListenerNioServerBuffer old = ses.addMeta(BUF_META_KEY, nioBuf); assert old == null; } boolean checkHandshake = ses.meta(ClientListenerNioListener.CONN_CTX_HANDSHAKE_PASSED) == null; return nioBuf.read(buf, checkHandshake); }
client.forwardMessage(routerMsg, routerMsg.destinationId(), ses.<Byte>meta(MARSHALLER_ID.ordinal())) .listen(new GridClientFutureListener() { @Override public void onDone(GridClientFuture fut) { ses.send(makeFailureResponse(e, clientId, reqId)); ", supported=" + SUPP_VERS + ']'); ses.close(); "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']'); ses.close(); ses.addMeta(MARSHALLER_ID.ordinal(), marshId); ses.addMeta(MARSHALLER.ordinal(), marsh); ses.send(GridClientHandshakeResponse.OK); ses.send(GridClientPingPacket.PING_MESSAGE); else throw new IllegalArgumentException("Unsupported input message: " + msg);
/** * Closes the session by timeout (i.e. inactivity within the configured period of time). * * @param ses Session, that was inactive. */ @Override public void onSessionIdleTimeout(GridNioSession ses) { ses.close(); } }
redisLsnr.onMessage(ses, (GridRedisMessage)msg); else if (msg instanceof GridClientPingPacket) ses.send(msg); else if (msg instanceof GridClientHandshakeRequest) { GridClientHandshakeRequest hs = (GridClientHandshakeRequest)msg; ", supported=" + SUPP_VERS + ']'); ses.close(); U.error(log, "Marshaller is not initialized.", e); ses.close(); "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']'); ses.close(); ses.addMeta(MARSHALLER.ordinal(), marsh); ses.send(GridClientHandshakeResponse.OK);
U.warn(log, "Unexpected client request (will close session): " + ses.remoteAddress()); ses.close(); connCtx.initializeFromHandshake(ver, reader); ses.addMeta(CONN_CTX_META_KEY, connCtx); ses.addMeta(CONN_CTX_HANDSHAKE_PASSED, true); U.warn(log, "Error during handshake [rmtAddr=" + ses.remoteAddress() + ", msg=" + e.getMessage() + ']'); ses.send(writer.array());
@Override public void onConnected(GridNioSession ses) { HadoopProcessDescriptor desc = ses.meta(PROCESS_META); assert desc != null : "Received connected notification without finished handshake: " + ses; }
", opCode=" + Integer.toHexString(req.operationCode()) + ']'); ses.close(); ses.close(); IgniteInternalFuture<GridRestResponse> lastFut = ses.removeMeta(LAST_FUT.ordinal()); ses.addMeta(LAST_FUT.ordinal(), f);
MessageReader reader = ses.meta(READER_META_KEY); ses.addMeta(READER_META_KEY, reader = readerFactory.reader(ses, msgFactory)); Message msg = ses.removeMeta(MSG_META_KEY); ses.addMeta(MSG_META_KEY, msg);
ses.addMeta(HANDSHAKE_FINISH_META, fin);
/** {@inheritDoc} */ @Override public void onSessionOpened(final GridNioSession ses) throws IgniteCheckedException { if (ses.accepted()) { if (log.isDebugEnabled()) log.debug("Accepted connection, initiating handshake: " + ses); // Server initiates handshake. ses.send(locIdMsg).listen(new CI1<IgniteInternalFuture<?>>() { @Override public void apply(IgniteInternalFuture<?> fut) { try { // Make sure there were no errors. fut.get(); } catch (IgniteCheckedException e) { log.warning("Failed to send handshake message, will close session: " + ses, e); ses.close(); } } }); } }
/** * @throws Exception If test failed. */ @Test public void testAsyncSendReceive() throws Exception { CountDownLatch latch = new CountDownLatch(10); NioListener lsnr = new NioListener(latch); GridNioServer<?> srvr1 = startServer(new BufferedParser(false), lsnr); GridNioServer<?> srvr2 = startServer(new BufferedParser(false), lsnr); GridNioSession ses = null; try { SocketChannel ch = SocketChannel.open(new InetSocketAddress(U.getLocalHost(), srvr2.port())); GridNioFuture<GridNioSession> fut = srvr1.createSession(ch, null, false, null); ses = fut.get(); for (int i = 0; i < 5; i++) { ses.send(createMessageWithSize()); ses.send(createMessageWithSize()); } assert latch.await(30, SECONDS); assertEquals("Unexpected message count", 10, lsnr.getMessageCount()); } finally { if (ses != null) ses.close(); srvr1.stop(); srvr2.stop(); } }
/** {@inheritDoc} */ @Override public void onMessage(final GridNioSession ses, final GridRedisMessage msg) { if (handlers.get(msg.command()) == null) { U.warn(log, "Cannot find the corresponding command (session will be closed) [ses=" + ses + ", command=" + msg.command().name() + ']'); ses.close(); return; } else { String cacheName = ses.meta(CONN_CTX_META_KEY); if (cacheName != null) msg.cacheName(cacheName); IgniteInternalFuture<GridRedisMessage> f = handlers.get(msg.command()).handleAsync(ses, msg); f.listen(new CIX1<IgniteInternalFuture<GridRedisMessage>>() { @Override public void applyx(IgniteInternalFuture<GridRedisMessage> f) throws IgniteCheckedException { GridRedisMessage res = f.get(); sendResponse(ses, res); } }); } }
/** {@inheritDoc} */ @Override public boolean sendMessage(@Nullable UUID nodeId, Message msg, IgniteInClosure<IgniteException> c) throws IgniteCheckedException { try { // Node ID is never provided in asynchronous send mode. assert nodeId == null; ses.sendNoFuture(msg, c); } catch (IgniteCheckedException e) { if (log.isDebugEnabled()) log.debug("Failed to send message [client=" + this + ", err=" + e + ']'); if (e.getCause() instanceof IOException) { ses.close(); return true; } else throw new IgniteCheckedException("Failed to send message [client=" + this + ']', e); } return false; }
ParserState state = ses.removeMeta(PARSER_STATE.ordinal()); ses.addMeta(PARSER_STATE.ordinal(), state);