当前位置: 首页>>代码示例>>Java>>正文


Java AttributeKey类代码示例

本文整理汇总了Java中io.netty.util.AttributeKey的典型用法代码示例。如果您正苦于以下问题:Java AttributeKey类的具体用法?Java AttributeKey怎么用?Java AttributeKey使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


AttributeKey类属于io.netty.util包,在下文中一共展示了AttributeKey类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: sendReq

import io.netty.util.AttributeKey; //导入依赖的package包/类
private void sendReq(String btyimei) {
    Battery battery = batteryService.fetchBtyByIMEI(btyimei);
    if (battery == null) {
        logger.error("电池不存在, " + btyimei);
        return;
    }
    boolean hasConn = false;
    ChannelGroup channelGroup = SamBtyDataHandler.getChannels();
    for (Channel c : channelGroup) {
        String imei = (String) c.attr(AttributeKey.valueOf("IMEI")).get();
        logger.info("已经连接的imei:" + imei);
        if (imei != null && imei.equals(battery.getImei())) {
            c.writeAndFlush("tellme" + imei + "\n");
            hasConn = true;
        }

    }
    if (!hasConn) {
        logger.error("未获取到长连接, " + btyimei);
    }
}
 
开发者ID:booleguo,项目名称:sam-elle,代码行数:22,代码来源:UserBtyInfoController.java

示例2: chat

import io.netty.util.AttributeKey; //导入依赖的package包/类
@Override
public boolean chat(String deviceImei, String chatType) {
    Battery battery = batteryService.fetchBtyByIMEI(deviceImei);
    if (battery == null) {
        logger.error("数据库中设备不存在, 设备Imei卡号:{}", deviceImei);
        return false;
    }
    boolean hasConn = false;
    ChannelGroup channelGroup = SamBtyDataHandler.getChannels();
    for (Channel c : channelGroup) {
        String imei = (String) c.attr(AttributeKey.valueOf("IMEI")).get();
        logger.info("已经连接设备的imei:{}", imei);
        if (imei != null && imei.equals(battery.getImei())) {
            String msg = chatType + imei + "\n";
            c.writeAndFlush(msg);
            hasConn = true;
        }

    }
    if (!hasConn) {
        logger.error("未获取到长连接, 设备Imei卡号:{}", deviceImei);
    }

    return hasConn;

}
 
开发者ID:booleguo,项目名称:sam-elle,代码行数:27,代码来源:DeviceChatServiceImpl.java

示例3: clientToProxyRequest

import io.netty.util.AttributeKey; //导入依赖的package包/类
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (httpObject instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) httpObject;

        if (ProxyUtils.isCONNECT(httpRequest)) {
            Attribute<String> hostname = ctx.attr(AttributeKey.<String>valueOf(HttpsAwareFiltersAdapter.HOST_ATTRIBUTE_NAME));
            String hostAndPort = httpRequest.getUri();

            // CONNECT requests contain the port, even when using the default port. a sensible default is to remove the
            // default port, since in most cases it is not explicitly specified and its presence (in a HAR file, for example)
            // would be unexpected.
            String hostNoDefaultPort = BrowserMobHttpUtil.removeMatchingPort(hostAndPort, 443);
            hostname.set(hostNoDefaultPort);
        }
    }

    return null;
}
 
开发者ID:misakuo,项目名称:Dream-Catcher,代码行数:20,代码来源:HttpsHostCaptureFilter.java

示例4: onIdeled

import io.netty.util.AttributeKey; //导入依赖的package包/类
private void onIdeled(Channel channel) {

        Long lastTime = (Long) channel.attr(AttributeKey.valueOf("heart")).get();


        if (lastTime != null && System.currentTimeMillis() - lastTime >= 30 * 1000) {

            LOG.info("wait for heart timeout close the channel {}", channel);
            channel.attr(AttributeKey.valueOf("heart")).set(null);
            channel.close();
        } else {
            LOG.info("send heart .. {}", channel);
            if (lastTime == null) channel.attr(AttributeKey.valueOf("heart")).set(System.currentTimeMillis());
            channel.writeAndFlush(heartMsg.msg());
        }
    }
 
开发者ID:jayqqaa12,项目名称:jbase,代码行数:17,代码来源:NettyHeartHandler.java

示例5: attr

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * Allow to specify an initial attribute of the newly created {@link Channel}.  If the {@code value} is
 * {@code null}, the attribute of the specified {@code key} is removed.
 */
@SuppressWarnings("unchecked")
public <T> B attr(AttributeKey<T> key, T value) {
    if (key == null) {
        throw new NullPointerException("key");
    }
    if (value == null) {
        synchronized (attrs) {
            attrs.remove(key);
        }
    } else {
        synchronized (attrs) {
            attrs.put(key, value);
        }
    }
    return (B) this;
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:21,代码来源:AbstractBootstrap.java

示例6: testGetSetString

import io.netty.util.AttributeKey; //导入依赖的package包/类
@Test
public void testGetSetString() {
    AttributeKey<String> key = AttributeKey.valueOf("Nothing");
    Attribute<String> one = map.attr(key);

    assertSame(one, map.attr(key));

    one.setIfAbsent("Whoohoo");
    assertSame("Whoohoo", one.get());

    one.setIfAbsent("What");
    assertNotSame("What", one.get());

    one.remove();
    assertNull(one.get());
}
 
开发者ID:line,项目名称:armeria,代码行数:17,代码来源:DefaultAttributeMapTest.java

示例7: testGetSetInt

import io.netty.util.AttributeKey; //导入依赖的package包/类
@Test
public void testGetSetInt() {
    AttributeKey<Integer> key = AttributeKey.valueOf("Nada");
    Attribute<Integer> one = map.attr(key);

    assertSame(one, map.attr(key));

    one.setIfAbsent(3653);
    assertEquals(Integer.valueOf(3653), one.get());

    one.setIfAbsent(1);
    assertNotSame(1, one.get());

    one.remove();
    assertNull(one.get());
}
 
开发者ID:line,项目名称:armeria,代码行数:17,代码来源:DefaultAttributeMapTest.java

示例8: attr

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * Allow to specify an initial attribute of the newly created {@link Channel}.  If the {@code value} is
 * {@code null}, the attribute of the specified {@code key} is removed.
 */
   /* 对this.attrs进行负值。
    * 如果对应的key的value为null,则从attrs删除该属性
    * 否则直接put
    */
public <T> B attr(AttributeKey<T> key, T value) {
    if (key == null) {
        throw new NullPointerException("key");
    }
    if (value == null) {
        synchronized (attrs) {
            attrs.remove(key);
        }
    } else {
        synchronized (attrs) {
            attrs.put(key, value);
        }
    }

    @SuppressWarnings("unchecked")
    B b = (B) this;
    return b;
}
 
开发者ID:kyle-liu,项目名称:netty4study,代码行数:27,代码来源:AbstractBootstrap.java

示例9: attr

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * Allow to specify an initial attribute of the newly created {@link Channel}.  If the {@code value} is
 * {@code null}, the attribute of the specified {@code key} is removed.
 */
public <T> B attr(AttributeKey<T> key, T value) {
    if (key == null) {
        throw new NullPointerException("key");
    }
    if (value == null) {
        synchronized (attrs) {
            attrs.remove(key);
        }
    } else {
        synchronized (attrs) {
            attrs.put(key, value);
        }
    }

    @SuppressWarnings("unchecked")
    B b = (B) this;
    return b;
}
 
开发者ID:nathanchen,项目名称:netty-netty-5.0.0.Alpha1,代码行数:23,代码来源:AbstractBootstrap.java

示例10: NPCNetworkManager

import io.netty.util.AttributeKey; //导入依赖的package包/类
public NPCNetworkManager() {
    super(EnumProtocolDirection.SERVERBOUND);

    try {
        Field channel = NetworkManager.class.getDeclaredField("i");
        Field address = NetworkManager.class.getDeclaredField("j");

        channel.setAccessible(true);
        address.setAccessible(true);

        Channel parent = new NPCChannel(null);
        try {
            Field protocolVersion = NetworkManager.class.getDeclaredField("protocolVersion");
            parent.attr(((AttributeKey<Integer>) protocolVersion.get(null))).set(5);
        } catch(NoSuchFieldException ignored) { // This server isn't spigot, we're good.
        }
        channel.set(this, parent);
        address.set(this, new SocketAddress() {
            private static final long serialVersionUID = 6994835504305404545L;
        });
    } catch(Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:lenis0012,项目名称:NPCFactory,代码行数:25,代码来源:NPCNetworkManager.java

示例11: mockChannelHandlerContext

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * Mock Nettys ChannelHandlerContext with the minimal functions.
 * 
 * @param buf
 *            The buffer to use for decoding
 * @param m2
 *            The message reference to store the result
 * @return The mocked ChannelHandlerContext
 */
@SuppressWarnings("unchecked")
private ChannelHandlerContext mockChannelHandlerContext(final ByteBuf buf,
        final AtomicReference<Message2> m2) {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ByteBufAllocator alloc = mock(ByteBufAllocator.class);
    when(ctx.alloc()).thenReturn(alloc);
    when(alloc.ioBuffer()).thenReturn(buf);
    DatagramChannel dc = mock(DatagramChannel.class);
    when(ctx.channel()).thenReturn(dc);
    when(ctx.writeAndFlush(any(), any(ChannelPromise.class))).thenReturn(null);

    Attribute<InetSocketAddress> attr = mock(Attribute.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(attr);

    when(ctx.fireChannelRead(any())).then(new Answer<Void>() {
        @Override
        public Void answer(final InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            m2.set((Message2) args[0]);
            return null;
        }
    });

    return ctx;
}
 
开发者ID:maxatp,项目名称:tomp2p_5,代码行数:35,代码来源:TestMessage.java

示例12: channelInactive

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * 连接被关闭
 * @param ctx
 * @throws Exception
 */
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    AttributeKey<Session> attrKeySession = AttributeKey.valueOf("Session");
    Attribute<Session> attrSession = ctx.attr(attrKeySession);
    Session session = attrSession.get();

    logger.warn("client diconnect!");
    super.channelInactive(ctx);
}
 
开发者ID:ninelook,项目名称:wecard-server,代码行数:16,代码来源:NettyServerHandler.java

示例13: decodeAndHuntMessage

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * 「内部使用」将 TCP 帧解码并转换为消息对象,而后传递至 Server
 *
 * @param head     TCP 帧头
 * @param subMsgId TCP 子帧功能位
 * @param data     TCP 子帧数据体
 * @param channel  该消息帧的容器 Channel
 */
public void decodeAndHuntMessage(FrameMajorHeader head, int subMsgId, byte[] data, Channel channel) {
    BaseMsg msg = msgProcessor.decode(head, subMsgId, data);
    if (msg == null) {
        logger.warn("帧解析出错");
        return;
    }
    logger.info("收到正常消息对象:「" + msg.getMsgDetail() + "」");
    switch (msg.getJointMsgFlag()) {
        case JointMsgType.HeartBeat:
            Attribute<Integer> key = channel.attr(AttributeKey.valueOf("ID"));
            MsgReplyNormal replyHeartbeat = MsgCodecReplyNormal.createByBaseMsg(MsgCodecHeartbeat.create(key.get()));
            logger.info("已生成并发送正常回复消息对象:「" + replyHeartbeat.getMsgDetail() + "」");
            sendMessageToTcp(replyHeartbeat);
            break;
        default:
            //剩下的均为正常需回复消息
            server.huntMessage(msg);
            MsgReplyNormal replyNormal = MsgCodecReplyNormal.createByBaseMsg(msg);
            if (replyNormal == null) {
                logger.warn("生成正常回复消息对象出错");
                return;
            }
            logger.info("已生成并发送正常回复消息对象:「" + replyNormal.getMsgDetail() + "」");
            sendMessageToTcp(replyNormal);
    }
}
 
开发者ID:bitkylin,项目名称:ClusterDeviceControlPlatform,代码行数:35,代码来源:TcpPresenter.java

示例14: activeChannel

import io.netty.util.AttributeKey; //导入依赖的package包/类
/**
 * 保存已激活的 Channel
 *
 * @param channel 已激活的 Channel
 */
public void activeChannel(Channel channel) {
    Attribute<Integer> key = channel.attr(AttributeKey.valueOf("ID"));
    int id = key.get();

    CHANNEL_ARRAY.set(id, channel);
    SENDING_MESSAGE_QUEUE.get(id).clear();
}
 
开发者ID:bitkylin,项目名称:ClusterDeviceControlPlatform,代码行数:13,代码来源:TcpRepository.java

示例15: channelActive

import io.netty.util.AttributeKey; //导入依赖的package包/类
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);
    Attribute<Integer> key = ctx.channel().attr(AttributeKey.valueOf("ID"));
    logger.info("Channel「" + key.get() + "」建立连接成功");
    tcpPresenter.channelActive(ctx.channel());
}
 
开发者ID:bitkylin,项目名称:ClusterDeviceControlPlatform,代码行数:8,代码来源:ConfigHandler.java


注:本文中的io.netty.util.AttributeKey类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。