Search in sources :

Example 1 with Channel

use of org.apache.storm.shade.io.netty.channel.Channel in project storm by apache.

the class PacemakerClientHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) {
    // register the newly established channel
    Channel channel = ctx.channel();
    LOG.info("Connection established from {} to {}", channel.localAddress(), channel.remoteAddress());
    client.channelReady(channel);
}
Also used : Channel(org.apache.storm.shade.io.netty.channel.Channel)

Example 2 with Channel

use of org.apache.storm.shade.io.netty.channel.Channel in project storm by apache.

the class PacemakerClient method send.

public HBMessage send(HBMessage m) throws PacemakerConnectionException, InterruptedException {
    LOG.debug("Sending pacemaker message to {}: {}", host, m);
    int next = availableMessageSlots.take();
    synchronized (m) {
        m.set_message_id(next);
        messages[next] = m;
        LOG.debug("Put message in slot: {} for {}", Integer.toString(next), host);
        int retry = maxRetries;
        while (true) {
            try {
                waitUntilReady();
                Channel channel = channelRef.get();
                if (channel != null) {
                    channel.writeAndFlush(m, channel.voidPromise());
                    m.wait(1000);
                }
                if (messages[next] != m && messages[next] != null) {
                    // messages[next] == null can happen if we lost the connection and subsequently reconnected or timed out.
                    HBMessage ret = messages[next];
                    messages[next] = null;
                    LOG.debug("Got Response: {}", ret);
                    return ret;
                }
            } catch (PacemakerConnectionException e) {
                if (retry <= 0) {
                    throw e;
                }
                LOG.error("Error attempting to write to a channel to host {} - {}", host, e.getMessage());
            }
            if (retry <= 0) {
                throw new PacemakerConnectionException("couldn't get response after " + maxRetries + " attempts.");
            }
            retry--;
            LOG.warn("Not getting response or getting null response. Making {} more attempts for {}.", retry, host);
        }
    }
}
Also used : NioSocketChannel(org.apache.storm.shade.io.netty.channel.socket.nio.NioSocketChannel) Channel(org.apache.storm.shade.io.netty.channel.Channel) HBMessage(org.apache.storm.generated.HBMessage)

Example 3 with Channel

use of org.apache.storm.shade.io.netty.channel.Channel in project storm by apache.

the class KerberosSaslServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null) {
        return;
    }
    Channel channel = ctx.channel();
    if (msg instanceof SaslMessageToken) {
        // client).
        try {
            LOG.debug("Got SaslMessageToken!");
            KerberosSaslNettyServer saslNettyServer = channel.attr(KerberosSaslNettyServerState.KERBOROS_SASL_NETTY_SERVER).get();
            if (saslNettyServer == null) {
                LOG.debug("No saslNettyServer for {}  yet; creating now, with topology token: ", channel);
                try {
                    saslNettyServer = new KerberosSaslNettyServer(topoConf, jaasSection, authorizedUsers);
                    channel.attr(KerberosSaslNettyServerState.KERBOROS_SASL_NETTY_SERVER).set(saslNettyServer);
                } catch (RuntimeException ioe) {
                    LOG.error("Error occurred while creating saslNettyServer on server {} for client {}", channel.localAddress(), channel.remoteAddress());
                    throw ioe;
                }
            } else {
                LOG.debug("Found existing saslNettyServer on server: {} for client {}", channel.localAddress(), channel.remoteAddress());
            }
            byte[] responseBytes = saslNettyServer.response(((SaslMessageToken) msg).getSaslToken());
            SaslMessageToken saslTokenMessageRequest = new SaslMessageToken(responseBytes);
            if (saslTokenMessageRequest.getSaslToken() == null) {
                channel.writeAndFlush(ControlMessage.SASL_COMPLETE_REQUEST, channel.voidPromise());
            } else {
                // Send response to client.
                channel.writeAndFlush(saslTokenMessageRequest, channel.voidPromise());
            }
            if (saslNettyServer.isComplete()) {
                // If authentication of client is complete, we will also send a
                // SASL-Complete message to the client.
                LOG.info("SASL authentication is complete for client with username: {}", saslNettyServer.getUserName());
                channel.writeAndFlush(ControlMessage.SASL_COMPLETE_REQUEST, channel.voidPromise());
                LOG.debug("Removing SaslServerHandler from pipeline since SASL authentication is complete.");
                ctx.pipeline().remove(this);
                server.authenticated(channel);
            }
        } catch (Exception ex) {
            LOG.error("Failed to handle SaslMessageToken: ", ex);
            throw ex;
        }
    } else {
        // Client should not be sending other-than-SASL messages before
        // SaslServerHandler has removed itself from the pipeline. Such
        // non-SASL requests will be denied by the Authorize channel handler
        // (the next handler upstream in the server pipeline) if SASL
        // authentication has not completed.
        LOG.warn("Sending upstream an unexpected non-SASL message : {}", msg);
        ctx.fireChannelRead(msg);
    }
}
Also used : Channel(org.apache.storm.shade.io.netty.channel.Channel) IOException(java.io.IOException)

Example 4 with Channel

use of org.apache.storm.shade.io.netty.channel.Channel in project storm by apache.

the class SaslStormServerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null) {
        return;
    }
    Channel channel = ctx.channel();
    if (msg instanceof ControlMessage && msg == ControlMessage.SASL_TOKEN_MESSAGE_REQUEST) {
        // initialize server-side SASL functionality, if we haven't yet
        // (in which case we are looking at the first SASL message from the
        // client).
        SaslNettyServer saslNettyServer = channel.attr(SaslNettyServerState.SASL_NETTY_SERVER).get();
        if (saslNettyServer == null) {
            LOG.debug("No saslNettyServer for " + channel + " yet; creating now, with topology token: " + topologyName);
            try {
                saslNettyServer = new SaslNettyServer(topologyName, token);
                LOG.debug("SaslNettyServer for " + channel + "created with topology token: " + topologyName);
            } catch (IOException e) {
                LOG.error("Error occurred while creating saslNettyServer on server " + channel.localAddress() + " for client " + channel.remoteAddress());
                throw new IllegalStateException("Failed to set SaslNettyServerState.SASL_NETTY_SERVER");
            }
            channel.attr(SaslNettyServerState.SASL_NETTY_SERVER).set(saslNettyServer);
        } else {
            LOG.debug("Found existing saslNettyServer on server:" + channel.localAddress() + " for client " + channel.remoteAddress());
        }
        LOG.debug("processToken:  With nettyServer: " + saslNettyServer + " and token length: " + token.length);
        SaslMessageToken saslTokenMessageRequest;
        saslTokenMessageRequest = new SaslMessageToken(saslNettyServer.response(new byte[0]));
        // Send response to client.
        channel.writeAndFlush(saslTokenMessageRequest, channel.voidPromise());
        // to be done for SASL_TOKEN_MESSAGE_REQUEST requests.
        return;
    }
    if (msg instanceof SaslMessageToken) {
        // initialize server-side SASL functionality, if we haven't yet
        // (in which case we are looking at the first SASL message from the
        // client).
        SaslNettyServer saslNettyServer = channel.attr(SaslNettyServerState.SASL_NETTY_SERVER).get();
        if (saslNettyServer == null) {
            throw new Exception("saslNettyServer was unexpectedly " + "null for channel: " + channel);
        }
        SaslMessageToken saslTokenMessageRequest = new SaslMessageToken(saslNettyServer.response(((SaslMessageToken) msg).getSaslToken()));
        // Send response to client.
        channel.writeAndFlush(saslTokenMessageRequest, channel.voidPromise());
        if (saslNettyServer.isComplete()) {
            // If authentication of client is complete, we will also send a
            // SASL-Complete message to the client.
            LOG.debug("SASL authentication is complete for client with " + "username: " + saslNettyServer.getUserName());
            channel.writeAndFlush(ControlMessage.SASL_COMPLETE_REQUEST, channel.voidPromise());
            LOG.debug("Removing SaslServerHandler from pipeline since SASL " + "authentication is complete.");
            ctx.pipeline().remove(this);
            server.authenticated(channel);
        }
    } else {
        // Client should not be sending other-than-SASL messages before
        // SaslServerHandler has removed itself from the pipeline. Such
        // non-SASL requests will be denied by the Authorize channel handler
        // (the next handler upstream in the server pipeline) if SASL
        // authentication has not completed.
        LOG.warn("Sending upstream an unexpected non-SASL message :  " + msg);
        ctx.fireChannelRead(msg);
    }
}
Also used : Channel(org.apache.storm.shade.io.netty.channel.Channel) IOException(java.io.IOException) IOException(java.io.IOException)

Example 5 with Channel

use of org.apache.storm.shade.io.netty.channel.Channel in project storm by apache.

the class KerberosSaslClientHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) {
    // register the newly established channel
    Channel channel = ctx.channel();
    LOG.info("Connection established from {} to {}", channel.localAddress(), channel.remoteAddress());
    try {
        KerberosSaslNettyClient saslNettyClient = channel.attr(KerberosSaslNettyClientState.KERBEROS_SASL_NETTY_CLIENT).get();
        if (saslNettyClient == null) {
            LOG.debug("Creating saslNettyClient now for channel: {}", channel);
            saslNettyClient = new KerberosSaslNettyClient(topoConf, jaasSection, host);
            channel.attr(KerberosSaslNettyClientState.KERBEROS_SASL_NETTY_CLIENT).set(saslNettyClient);
        }
        LOG.debug("Going to initiate Kerberos negotiations.");
        byte[] initialChallenge = saslNettyClient.saslResponse(new SaslMessageToken(new byte[0]));
        LOG.debug("Sending initial challenge: {}", initialChallenge);
        channel.writeAndFlush(new SaslMessageToken(initialChallenge), channel.voidPromise());
    } catch (Exception e) {
        LOG.error("Failed to authenticate with server due to error: ", e);
    }
}
Also used : Channel(org.apache.storm.shade.io.netty.channel.Channel) IOException(java.io.IOException)

Aggregations

Channel (org.apache.storm.shade.io.netty.channel.Channel)13 IOException (java.io.IOException)8 NioSocketChannel (org.apache.storm.shade.io.netty.channel.socket.nio.NioSocketChannel)5 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 HBMessage (org.apache.storm.generated.HBMessage)1 TaskMessage (org.apache.storm.messaging.TaskMessage)1