Example usage for io.netty.util Attribute setIfAbsent

List of usage examples for io.netty.util Attribute setIfAbsent

Introduction

In this page you can find the example usage for io.netty.util Attribute setIfAbsent.

Prototype

T setIfAbsent(T value);

Source Link

Document

Atomically sets to the given value if this Attribute 's value is null .

Usage

From source file:org.jupiter.registry.DefaultRegistry.java

License:Apache License

private static boolean attachSubscribeEventOnChannel(RegisterMeta.ServiceMeta serviceMeta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta.ServiceMeta>> attr = channel.attr(C_SUBSCRIBE_KEY);
    ConcurrentSet<RegisterMeta.ServiceMeta> serviceMetaSet = attr.get();
    if (serviceMetaSet == null) {
        ConcurrentSet<RegisterMeta.ServiceMeta> newServiceMetaSet = new ConcurrentSet<>();
        serviceMetaSet = attr.setIfAbsent(newServiceMetaSet);
        if (serviceMetaSet == null) {
            serviceMetaSet = newServiceMetaSet;
        }//from   ww w.j a va  2 s  . com
    }

    return serviceMetaSet.add(serviceMeta);
}

From source file:org.jupiter.registry.DefaultRegistryServer.java

License:Apache License

private static boolean attachSubscribeEventOnChannel(RegisterMeta.ServiceMeta serviceMeta, Channel channel) {
    Attribute<ConcurrentSet<RegisterMeta.ServiceMeta>> attr = channel.attr(S_SUBSCRIBE_KEY);
    ConcurrentSet<RegisterMeta.ServiceMeta> serviceMetaSet = attr.get();
    if (serviceMetaSet == null) {
        ConcurrentSet<RegisterMeta.ServiceMeta> newServiceMetaSet = new ConcurrentSet<>();
        serviceMetaSet = attr.setIfAbsent(newServiceMetaSet);
        if (serviceMetaSet == null) {
            serviceMetaSet = newServiceMetaSet;
        }/*from  w w w  .j a v a  2s. c o  m*/
    }

    return serviceMetaSet.add(serviceMeta);
}

From source file:org.jupiter.transport.netty.channel.NettyChannel.java

License:Apache License

/**
 * Returns the {@link NettyChannel} for given {@link Channel}, this method never return null.
 *//*from ww w . ja v  a 2  s  . co  m*/
public static NettyChannel attachChannel(Channel channel) {
    Attribute<NettyChannel> attr = channel.attr(NETTY_CHANNEL_KEY);
    NettyChannel nChannel = attr.get();
    if (nChannel == null) {
        NettyChannel newNChannel = new NettyChannel(channel);
        nChannel = attr.setIfAbsent(newNChannel);
        if (nChannel == null) {
            nChannel = newNChannel;
        }
    }
    return nChannel;
}

From source file:org.lanternpowered.server.network.vanilla.message.handler.play.HandlerPlayInChatMessage.java

License:MIT License

@Override
public void handle(NetworkContext context, MessagePlayInChatMessage message) {
    final NetworkSession session = context.getSession();
    final LanternPlayer player = session.getPlayer();
    player.resetIdleTimeoutCounter();/*from   w  w w .j  a v a  2 s.com*/
    final String message0 = message.getMessage();

    // Check for a valid click action callback
    final Matcher matcher = LanternClickActionCallbacks.COMMAND_PATTERN.matcher(message0);
    if (matcher.matches()) {
        final UUID uniqueId = UUID.fromString(matcher.group(1));
        final Optional<Consumer<CommandSource>> callback = LanternClickActionCallbacks.get()
                .getCallbackForUUID(uniqueId);
        if (callback.isPresent()) {
            callback.get().accept(player);
        } else {
            player.sendMessage(
                    error(t("The callback you provided was not valid. Keep in mind that callbacks will expire "
                            + "after 10 minutes, so you might want to consider clicking faster next time!")));
        }
        return;
    }

    String message1 = StringUtils.normalizeSpace(message0);
    if (!isAllowedString(message0)) {
        session.disconnect(t("multiplayer.disconnect.illegal_characters"));
        return;
    }
    if (message1.startsWith("/")) {
        Lantern.getSyncExecutorService()
                .submit(() -> Sponge.getCommandManager().process(player, message1.substring(1)));
    } else {
        final Text nameText = player.get(Keys.DISPLAY_NAME).get();
        final Text rawMessageText = Text.of(message0);
        final GlobalConfig.Chat.Urls urls = Lantern.getGame().getGlobalConfig().getChat().getUrls();
        final Text messageText;
        if (urls.isEnabled() && player.hasPermission(Permissions.Chat.FORMAT_URLS)) {
            messageText = newTextWithLinks(message0, urls.getTemplate(), false);
        } else {
            messageText = rawMessageText;
        }
        final MessageChannel channel = player.getMessageChannel();
        final MessageChannelEvent.Chat event = SpongeEventFactory.createMessageChannelEventChat(
                Cause.of(NamedCause.source(player)), channel, Optional.of(channel),
                new MessageEvent.MessageFormatter(nameText, messageText), rawMessageText, false);
        if (!Sponge.getEventManager().post(event) && !event.isMessageCancelled()) {
            event.getChannel().ifPresent(c -> c.send(player, event.getMessage(), ChatTypes.CHAT));
        }
    }
    final Attribute<ChatData> attr = context.getChannel().attr(CHAT_DATA);
    ChatData chatData = attr.get();
    if (chatData == null) {
        chatData = new ChatData();
        final ChatData chatData1 = attr.setIfAbsent(chatData);
        if (chatData1 != null) {
            chatData = chatData1;
        }
    }
    //noinspection SynchronizationOnLocalVariableOrMethodParameter
    synchronized (chatData) {
        final long currentTime = LanternGame.currentTimeTicks();
        if (chatData.lastChatTime != -1L) {
            chatData.chatThrottle = (int) Math.max(0,
                    chatData.chatThrottle - (currentTime - chatData.lastChatTime));
        }
        chatData.lastChatTime = currentTime;
        chatData.chatThrottle += 20;
        if (chatData.chatThrottle > Lantern.getGame().getGlobalConfig().getChatSpamThreshold()) {
            session.disconnect(t("disconnect.spam"));
        }
    }
}

From source file:org.quartzpowered.network.session.SessionManager.java

License:Open Source License

public Session get(Channel channel) {
    Attribute<Session> attribute = channel.attr(ATTRIBUTE_KEY);
    Session session = attribute.get();//from  w  ww .j av a 2  s  . c o m
    if (session == null) {
        attribute.setIfAbsent(factory.create(channel));
        session = attribute.get();
    }
    return session;
}

From source file:sailfish.remoting.channel.HighPerformanceChannelWriter.java

License:Apache License

private static HighPerformanceChannelWriter getWriter(Channel channel) {
    Attribute<HighPerformanceChannelWriter> attr = channel.attr(ChannelAttrKeys.highPerformanceWriter);
    HighPerformanceChannelWriter writer = attr.get();
    if (null == writer) {
        HighPerformanceChannelWriter old = attr.setIfAbsent(writer = new HighPerformanceChannelWriter(channel));
        if (null != old) {
            writer = old;/*from   ww w .j ava 2s.com*/
        }
    }
    return writer;
}