本文整理匯總了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());
}