本文整理汇总了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);
}
}
示例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;
}
示例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;
}
示例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());
}
}
示例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;
}
示例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());
}
示例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());
}
示例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;
}
示例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;
}
示例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();
}
}
示例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;
}
示例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);
}
示例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);
}
}
示例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();
}
示例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());
}