@Override protected NioSession buildSession(SocketChannel sc) { Queue<WriteMessage> queue = this.buildQueue(); final NioSessionConfig sessionCofig = this.buildSessionConfig(sc, queue); MemcachedTCPSession session = new MemcachedTCPSession(sessionCofig, this.configuration.getSessionReadBufferSize(), this.optimiezer, this.getReadThreadCount(), this.commandFactory); session.setBufferAllocator(this.bufferAllocator); return session; }
case NO_ERROR: done.set(true); log.info("Authentication to " + this.memcachedTCPSession.getRemoteSocketAddress() + " successfully"); break; case AUTH_REQUIRED: log.error( "Authentication failed to " + this.memcachedTCPSession.getRemoteSocketAddress()); log.warn("Reopen connection to " + this.memcachedTCPSession.getRemoteSocketAddress() + ",beacause auth fail"); this.memcachedTCPSession.setAuthFailed(true); this.memcachedTCPSession.close(); done.set(true); break; command = commandFactory.createAuthStepCommand(saslClient.getMechanismName(), latch, response); if (!this.memcachedTCPSession.isClosed()) this.memcachedTCPSession.write(command); else { log.error("Authentication fail,because the connection has been closed"); default: log.error( "Authentication failed to " + this.memcachedTCPSession.getRemoteSocketAddress() + ",response status=" + responseStatus); command = startAuth();
public Object decode(IoBuffer buffer, Session origSession) { MemcachedTCPSession session = (MemcachedTCPSession) origSession; if (session.getCurrentCommand() != null) { return decode0(buffer, session); } else { session.takeCurrentCommand(); if (session.getCurrentCommand() == null) return null; return decode0(buffer, session); } }
protected MemcachedTCPSession createSession(SocketChannel socketChannel, InetSocketAddressWrapper wrapper) { MemcachedTCPSession session = (MemcachedTCPSession) this.buildSession(socketChannel); session.setInetSocketAddressWrapper(wrapper); this.selectorManager.registerSession(session, EventType.ENABLE_READ); session.start(); session.onEvent(EventType.CONNECTED, null); return session; }
private Object decode0(IoBuffer buffer, MemcachedTCPSession session) { if (session.getCurrentCommand().decode(session, buffer.buf())) { final Command command = session.getCurrentCommand(); session.setCurrentCommand(null); return command; } return null; } }
private Command startAuth() throws SaslException { // destroy previous client. destroySaslClient(); this.saslClient = Sasl.createSaslClient(authInfo.getMechanisms(), null, "memcached", memcachedTCPSession.getRemoteSocketAddress().toString(), null, this.authInfo.getCallbackHandler()); byte[] response = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(EMPTY_BYTES) : EMPTY_BYTES; CountDownLatch latch = new CountDownLatch(1); Command command = this.commandFactory.createAuthStartCommand(saslClient.getMechanismName(), latch, response); if (!this.memcachedTCPSession.isClosed()) this.memcachedTCPSession.write(command); else { log.error("Authentication fail,because the connection has been closed"); throw new RuntimeException("Authentication fai,connection has been close"); } return command; }
public int getWeight() { return this.getInetSocketAddressWrapper().getWeight(); }
/** * Check if have to reconnect on session closed */ @Override public final void onSessionClosed(Session session) { this.client.getConnector().removeSession(session); // Clear write queue to release noreply operations. ((AbstractSession) session).clearWriteQueue(); MemcachedTCPSession memcachedSession = (MemcachedTCPSession) session; // destroy memached session memcachedSession.destroy(); if (this.client.getConnector().isStarted() && memcachedSession.isAllowReconnect()) { this.reconnect(memcachedSession); } for (MemcachedClientStateListener listener : this.client.getStateListeners()) { listener.onDisconnected(this.client, session.getRemoteSocketAddress()); } }
/** * Auto reconect to memcached server * * @param session */ protected void reconnect(MemcachedTCPSession session) { if (!this.client.isShutdown()) { // Prevent reconnecting repeatedly synchronized (session) { if (!session.isAllowReconnect()) { return; } session.setAllowReconnect(false); } MemcachedSession memcachedTCPSession = session; InetSocketAddressWrapper inetSocketAddressWrapper = memcachedTCPSession.getInetSocketAddressWrapper(); this.client.getConnector().addToWatingQueue( new ReconnectRequest(inetSocketAddressWrapper, 0, this.client.getHealSessionInterval())); } }
@Override public String toString() { return SystemUtils.getRawAddress(this.getRemoteSocketAddress()) + ":" + this.getRemoteSocketAddress().getPort(); }
@Override protected WriteMessage preprocessWriteMessage(WriteMessage writeMessage) { Command currentCommand = (Command) writeMessage; // Check if IoBuffer is null if (currentCommand.getIoBuffer() == null) { currentCommand.encode(); } if (currentCommand.getStatus() == OperationStatus.SENDING) { /** * optimize commands */ currentCommand = this.optimiezer.optimize(currentCommand, this.writeQueue, this.commandAlreadySent, this.sendBufferSize); } currentCommand.setStatus(OperationStatus.WRITING); if (!currentCommand.isAdded() && (!currentCommand.isNoreply() || this.commandFactory.getProtocol() == Protocol.Binary)) { currentCommand.setAdded(true); this.addCommand(currentCommand); } return currentCommand; }
protected MemcachedTCPSession createSession(SocketChannel socketChannel, InetSocketAddressWrapper wrapper) { MemcachedTCPSession session = (MemcachedTCPSession) this.buildSession(socketChannel); session.setInetSocketAddressWrapper(wrapper); this.selectorManager.registerSession(session, EventType.ENABLE_READ); session.start(); session.onEvent(EventType.CONNECTED, null); return session; }
private Command startAuth() throws SaslException { // destroy previous client. destroySaslClient(); this.saslClient = Sasl.createSaslClient(authInfo.getMechanisms(), null, "memcached", memcachedTCPSession.getRemoteSocketAddress().toString(), null, this.authInfo.getCallbackHandler()); byte[] response = saslClient.hasInitialResponse() ? saslClient.evaluateChallenge(EMPTY_BYTES) : EMPTY_BYTES; CountDownLatch latch = new CountDownLatch(1); Command command = this.commandFactory.createAuthStartCommand(saslClient.getMechanismName(), latch, response); if (!this.memcachedTCPSession.isClosed()) this.memcachedTCPSession.write(command); else { log.error("Authentication fail,because the connection has been closed"); throw new RuntimeException("Authentication fai,connection has been close"); } return command; }
public int getOrder() { return this.getInetSocketAddressWrapper().getOrder(); }
private Object decode0(IoBuffer buffer, MemcachedTCPSession session) { if (session.getCurrentCommand().decode(session, buffer.buf())) { final Command command = session.getCurrentCommand(); session.setCurrentCommand(null); return command; } return null; } }
/** * Check if have to reconnect on session closed */ @Override public final void onSessionClosed(Session session) { this.client.getConnector().removeSession(session); // Clear write queue to release noreply operations. ((AbstractSession) session).clearWriteQueue(); MemcachedTCPSession memcachedSession = (MemcachedTCPSession) session; // destroy memached session memcachedSession.destroy(); if (this.client.getConnector().isStarted() && memcachedSession.isAllowReconnect()) { this.reconnect(memcachedSession); } for (MemcachedClientStateListener listener : this.client.getStateListeners()) { listener.onDisconnected(this.client, session.getRemoteSocketAddress()); } }
/** * Auto reconect to memcached server * * @param session */ protected void reconnect(MemcachedTCPSession session) { if (!this.client.isShutdown()) { // Prevent reconnecting repeatedly synchronized (session) { if (!session.isAllowReconnect()) { return; } session.setAllowReconnect(false); } MemcachedSession memcachedTCPSession = session; InetSocketAddressWrapper inetSocketAddressWrapper = memcachedTCPSession.getInetSocketAddressWrapper(); this.client.getConnector().addToWatingQueue( new ReconnectRequest(inetSocketAddressWrapper, 0, this.client.getHealSessionInterval())); } }
public void onConnect(MemcachedSession session, MemcachedClient client) { MemcachedTCPSession tcpSession = (MemcachedTCPSession) session; Map<String, AuthInfo> authInfoMap = client.getAuthInfoStringMap(); if (authInfoMap != null) { AuthInfo authInfo = authInfoMap.get(AddrUtil.getServerString(tcpSession.getRemoteSocketAddress())); if (authInfo != null) { XMemcachedClient xMemcachedClient = (XMemcachedClient) client; AuthTask task = new AuthTask(authInfo, xMemcachedClient.getCommandFactory(), tcpSession); task.start(); // First time,try to wait if (authInfo.isFirstTime()) { try { task.join(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } } }
@Override protected WriteMessage preprocessWriteMessage(WriteMessage writeMessage) { Command currentCommand = (Command) writeMessage; // Check if IoBuffer is null if (currentCommand.getIoBuffer() == null) { currentCommand.encode(); } if (currentCommand.getStatus() == OperationStatus.SENDING) { /** * optimize commands */ currentCommand = this.optimiezer.optimize(currentCommand, this.writeQueue, this.commandAlreadySent, this.sendBufferSize); } currentCommand.setStatus(OperationStatus.WRITING); if (!currentCommand.isAdded() && (!currentCommand.isNoreply() || this.commandFactory.getProtocol() == Protocol.Binary)) { currentCommand.setAdded(true); this.addCommand(currentCommand); } return currentCommand; }
case NO_ERROR: done.set(true); log.info("Authentication to " + this.memcachedTCPSession.getRemoteSocketAddress() + " successfully"); break; case AUTH_REQUIRED: log.error( "Authentication failed to " + this.memcachedTCPSession.getRemoteSocketAddress()); log.warn("Reopen connection to " + this.memcachedTCPSession.getRemoteSocketAddress() + ",beacause auth fail"); this.memcachedTCPSession.setAuthFailed(true); this.memcachedTCPSession.close(); done.set(true); break; command = commandFactory.createAuthStepCommand(saslClient.getMechanismName(), latch, response); if (!this.memcachedTCPSession.isClosed()) this.memcachedTCPSession.write(command); else { log.error("Authentication fail,because the connection has been closed"); default: log.error( "Authentication failed to " + this.memcachedTCPSession.getRemoteSocketAddress() + ",response status=" + responseStatus); command = startAuth();