io.netty.channel.ChannelId源码实例Demo

类io.netty.channel.ChannelId源码实例Demo

下面列出了io.netty.channel.ChannelId 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: bitchat   文件: AbstractSessionManager.java
@Override
public void removeSession(ChannelId channelId) {
    Assert.notNull(channelId, "channelId can not be null");
    Collection<Session> sessions = allSession();
    if (CollectionUtil.isEmpty(sessions)) {
        return;
    }
    Iterator<Session> iterator = sessions.iterator();
    while (iterator.hasNext()) {
        Session session = iterator.next();
        if (session.channelId() == channelId) {
            iterator.remove();
            log.info("remove a session, session={}, channelId={}", session, channelId);
            break;
        }
    }
}
 
源代码2 项目: lannister   文件: Sessions.java
protected void remove(Session session) {
	if (session == null) { return; }

	synchronized (this) {
		try {
			if (session.cleanSession()) { // [MQTT-3.1.2-5]
				sessions.remove(session.clientId());
			}

			ChannelId channelId = session.channelId();
			if (channelId == null) { return; }

			clientIds.remove(channelId);
			ctxs.remove(session.clientId());
		}
		finally {
			if (logger.isDebugEnabled()) {
				logger.debug("session removed [clientId={}, sessionsSize={}, clientIdsSize={}, ctxsSize={}]",
						session.clientId(), sessions.size(), clientIds.size(), ctxs.size());
			}
		}
	}
}
 
源代码3 项目: lannister   文件: ConnectReceiverTest.java
private MqttConnAckMessage executeNormalChannelRead0(String clientId, boolean cleanSession, ChannelId channelId)
		throws Exception {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false,
			10);
	MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, true, true, true, 0, true,
			cleanSession, 60);
	MqttConnectPayload payload = new MqttConnectPayload(clientId, "willtopic", "willmessage", "username",
			"password");

	MqttConnectMessage msg = new MqttConnectMessage(fixedHeader, variableHeader, payload);

	ChannelId cid = channelId == null ? TestUtil.newChannelId(clientId, false) : channelId;

	EmbeddedChannel channel = new EmbeddedChannel(cid, new ConnectReceiver());

	channel.writeInbound(msg);

	return channel.readOutbound();
}
 
源代码4 项目: reactor-netty   文件: HttpClientTest.java
private ChannelId[] doTestConnectionIdleTime(ConnectionProvider provider) throws Exception {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .wiretap(true)
			          .handle((req, res) -> res.sendString(Mono.just("hello")))
			          .bindNow();

	Flux<ChannelId> id = createHttpClientForContextWithAddress(provider)
	                       .get()
	                       .uri("/")
	                       .responseConnection((res, conn) -> Mono.just(conn.channel().id())
	                                                              .delayUntil(ch -> conn.inbound().receive()));

	ChannelId id1 = id.blockLast(Duration.ofSeconds(30));
	Thread.sleep(30);
	ChannelId id2 = id.blockLast(Duration.ofSeconds(30));

	assertThat(id1).isNotNull();
	assertThat(id2).isNotNull();

	provider.dispose();
	return new ChannelId[] {id1, id2};
}
 
源代码5 项目: reactor-netty   文件: HttpClientTest.java
private ChannelId[] doTestConnectionLifeTime(ConnectionProvider provider) throws Exception {
	disposableServer =
			HttpServer.create()
			          .port(0)
			          .handle((req, resp) ->
			              resp.sendObject(ByteBufFlux.fromString(Mono.delay(Duration.ofMillis(30))
			                                                         .map(Objects::toString))))
			          .wiretap(true)
			          .bindNow();

	Flux<ChannelId> id = createHttpClientForContextWithAddress(provider)
	                       .get()
	                       .uri("/")
	                       .responseConnection((res, conn) -> Mono.just(conn.channel().id())
	                                                              .delayUntil(ch -> conn.inbound().receive()));

	ChannelId id1 = id.blockLast(Duration.ofSeconds(30));
	Thread.sleep(10);
	ChannelId id2 = id.blockLast(Duration.ofSeconds(30));

	assertThat(id1).isNotNull();
	assertThat(id2).isNotNull();

	provider.dispose();
	return new ChannelId[] {id1, id2};
}
 
源代码6 项目: iot-dc   文件: ChannelManagerHandler.java
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    LOGGER.info("new channel coming! ----> {}", ctx.channel());
    ChannelId channelId = ctx.channel().id();
    RTUChannelInfo channelInfo = GlobalInfo.CHANNEL_INFO_MAP.getOrDefault(channelId, RTUChannelInfo.build("unknownSN", channelId));
    GlobalInfo.CHANNEL_INFO_MAP.put(channelId, channelInfo);
    ctx.fireChannelRegistered();
}
 
源代码7 项目: iot-dc   文件: ChannelManagerHandler.java
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    LOGGER.info("channel out! ----> {}", ctx.channel());
    ChannelId channelId = ctx.channel().id();
    RTUChannelInfo channelInfo = GlobalInfo.CHANNEL_INFO_MAP.remove(channelId);
    GlobalInfo.SN_CHANNEL_INFO_MAP.remove(channelInfo.getSn());
    LOGGER.info("remove channel: {}", channelInfo);
    ctx.fireChannelUnregistered();
}
 
源代码8 项目: iot-dc   文件: ChannelManagerHandler.java
/**
 * 补全 链路信息:根据 channelId 获取 channelInfo 并写入 sn 和 物联网信息
 *
 * @param ctx
 * @param sn
 * @return
 */
public static void setRTUChannelInfo(ChannelHandlerContext ctx, String sn) {
    ChannelId channelId = ctx.channel().id();
    IotInfo iot = GlobalInfo.iotMapper.get(sn);
    GlobalInfo.CHANNEL_INFO_MAP.get(channelId)
            .setSn(sn).setIotInfo(iot).setChannel(ctx.channel());

    RTUChannelInfo channelInfo = GlobalInfo.SN_CHANNEL_INFO_MAP.getOrDefault(sn, RTUChannelInfo.build(sn, channelId));
    channelInfo.setIotInfo(iot).setChannel(ctx.channel());
    GlobalInfo.SN_CHANNEL_INFO_MAP.put(sn, channelInfo);
    LOGGER.info("sn: {} in the house.", sn);
}
 
源代码9 项目: netty-4.1.22   文件: Http2StreamChannelId.java
@Override
public int compareTo(ChannelId o) {
    if (o instanceof Http2StreamChannelId) {
        Http2StreamChannelId otherId = (Http2StreamChannelId) o;
        int res = parentId.compareTo(otherId.parentId);
        if (res == 0) {
            return id - otherId.id;
        } else {
            return res;
        }
    }
    return parentId.compareTo(o);
}
 
源代码10 项目: netty-4.1.22   文件: EmbeddedChannelId.java
@Override
public int compareTo(final ChannelId o) {
    if (o instanceof EmbeddedChannelId) {
        return 0;
    }

    return asLongText().compareTo(o.asLongText());
}
 
源代码11 项目: netty-4.1.22   文件: DefaultChannelGroup.java
@Override
public Channel find(ChannelId id) {
    Channel c = nonServerChannels.get(id);
    if (c != null) {
        return c;
    } else {
        return serverChannels.get(id);
    }
}
 
源代码12 项目: netty-4.1.22   文件: DefaultChannelGroup.java
@Override
public boolean add(Channel channel) {
    ConcurrentMap<ChannelId, Channel> map =
        channel instanceof ServerChannel? serverChannels : nonServerChannels;

    boolean added = map.putIfAbsent(channel.id(), channel) == null;
    if (added) {
        channel.closeFuture().addListener(remover);
    }

    if (stayClosed && closed) {

        // First add channel, than check if closed.
        // Seems inefficient at first, but this way a volatile
        // gives us enough synchronization to be thread-safe.
        //
        // If true: Close right away.
        // (Might be closed a second time by ChannelGroup.close(), but this is ok)
        //
        // If false: Channel will definitely be closed by the ChannelGroup.
        // (Because closed=true always happens-before ChannelGroup.close())
        //
        // See https://github.com/netty/netty/issues/4020
        channel.close();
    }

    return added;
}
 
源代码13 项目: netty-4.1.22   文件: CustomChannelId.java
@Override
public int compareTo(final ChannelId o) {
    if (o instanceof CustomChannelId) {
        return MathUtil.compare(id, ((CustomChannelId) o).id);
    }

    return asLongText().compareTo(o.asLongText());
}
 
源代码14 项目: bitchat   文件: DefaultChannelManager.java
@Override
public ChannelWrapper getChannelWrapper(ChannelId channelId) {
    Assert.notNull(channelId, "channelId can not be null");
    if (channels.isEmpty()) {
        return null;
    }
    Channel channel = channels.find(channelId);
    return wrapChannel(channel);
}
 
源代码15 项目: bitchat   文件: DefaultChannelListener.java
@Override
public void channelInactive(Channel channel) {
    ChannelId channelId = channel.id();
    channelManager.removeChannel(channelId);
    sessionManager.removeSession(channelId);
    SessionHelper.markOffline(channel);
    log.info("Remove an inactive Channel={}", channel);
}
 
源代码16 项目: bitchat   文件: AbstractSessionManager.java
@Override
public void bound(Session session, ChannelId channelId, long userId) {
    Assert.notNull(session, "session can not be null");
    Assert.notNull(channelId, "channelId can not be null");
    session.bound(channelId, userId);
    sessionMap.putIfAbsent(session.sessionId(), session);
    log.info("bound a new session, session={}, channelId={}", session, channelId);
}
 
源代码17 项目: bitchat   文件: DefaultSession.java
@Override
public void bound(ChannelId channelId, long userId) {
    if (bounded.compareAndSet(false, true)) {
        ChannelWrapper channelWrapper = ChannelHelper.getChannelWrapper(channelId);
        Assert.notNull(channelWrapper, "channelId does not exists");
        this.channelId = channelId;
        this.userId = userId;
        this.channel = channelWrapper.getChannel();
        this.channelType = channelWrapper.getChannelType();
    }
}
 
源代码18 项目: bitchat   文件: DefaultSession.java
@Override
public ChannelId channelId() {
    if (!bounded.get()) {
        throw new IllegalStateException("Not bounded yet, Please call bound first");
    }
    return channelId;
}
 
源代码19 项目: iot-dc   文件: ChannelManagerHandler.java
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    LOGGER.info("new channel coming! ----> {}", ctx.channel());
    ChannelId channelId = ctx.channel().id();
    RTUChannelInfo channelInfo = GlobalInfo.CHANNEL_INFO_MAP.getOrDefault(channelId, RTUChannelInfo.build("unknownSN", channelId));
    GlobalInfo.CHANNEL_INFO_MAP.put(channelId, channelInfo);
    ctx.fireChannelRegistered();
}
 
源代码20 项目: iot-dc   文件: ChannelManagerHandler.java
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    LOGGER.info("channel out! ----> {}", ctx.channel());
    ChannelId channelId = ctx.channel().id();
    RTUChannelInfo channelInfo = GlobalInfo.CHANNEL_INFO_MAP.remove(channelId);
    GlobalInfo.SN_CHANNEL_INFO_MAP.remove(channelInfo.getSn());
    LOGGER.info("remove channel: {}", channelInfo);
    ctx.fireChannelUnregistered();
}
 
源代码21 项目: iot-dc   文件: ChannelManagerHandler.java
/**
 * 补全 链路信息:根据 channelId 获取 channelInfo 并写入 sn 和 物联网信息
 *
 * @param ctx
 * @param sn
 * @return
 */
public static void setRTUChannelInfo(ChannelHandlerContext ctx, String sn) {
    ChannelId channelId = ctx.channel().id();
    IotInfo iot = GlobalInfo.iotMapper.get(sn);
    GlobalInfo.CHANNEL_INFO_MAP.get(channelId)
            .setSn(sn).setIotInfo(iot).setChannel(ctx.channel());

    RTUChannelInfo channelInfo = GlobalInfo.SN_CHANNEL_INFO_MAP.getOrDefault(sn, RTUChannelInfo.build(sn, channelId));
    channelInfo.setIotInfo(iot).setChannel(ctx.channel());
    GlobalInfo.SN_CHANNEL_INFO_MAP.put(sn, channelInfo);
    LOGGER.info("sn: {} in the house.", sn);
}
 
源代码22 项目: redant   文件: SessionHelper.java
/**
 * 获取单例
 * @return
 */
public static SessionHelper instange(){
    synchronized (SessionHelper.class) {
        if (manager == null) {
            manager = new SessionHelper();
            if (manager.sessionMap == null) {
                // 需要线程安全的Map
                manager.sessionMap = new ConcurrentHashMap<ChannelId,HttpSession>();
            }
        }
    }
    return manager;
}
 
源代码23 项目: redant   文件: SessionHelper.java
/**
 * 清除过期的session
 * 需要在定时器中执行该方法
 */
public void clearExpireSession(){
    Iterator<Map.Entry<ChannelId,HttpSession>> iterator = manager.sessionMap.entrySet().iterator();
    while(iterator.hasNext()){
        Map.Entry<ChannelId,HttpSession> sessionEntry = iterator.next();
        if(sessionEntry.getValue()==null || sessionEntry.getValue().isExpire()){
            iterator.remove();
        }
    }
}
 
源代码24 项目: redant   文件: HttpSession.java
public HttpSession(ChannelId id,ChannelHandlerContext context,Long createTime,Long expireTime){
    this.id = id;
    this.context = context;
    this.createTime = createTime;
    this.expireTime = expireTime;
    assertSessionMapNotNull();
}
 
源代码25 项目: esjc   文件: SubscriptionManager.java
public void purgeSubscribedAndDropped(ChannelId connectionId) {
    List<SubscriptionItem> subscriptionsToRemove = new ArrayList<>();

    activeSubscriptions.values().stream()
        .filter(s -> s.isSubscribed && s.connectionId.equals(connectionId))
        .forEach(s -> {
            s.operation.connectionClosed();
            subscriptionsToRemove.add(s);
        });

    subscriptionsToRemove.forEach(s -> activeSubscriptions.remove(s.correlationId));
}
 
源代码26 项目: lannister   文件: Session.java
@JsonSerialize(using = ChannelIdSerializer.class)
@JsonProperty
public ChannelId channelId() {
	ChannelHandlerContext ctx = NEXUS.channelHandlerContext(clientId);
	if (ctx == null) { return null; }

	return ctx.channel().id();
}
 
源代码27 项目: reactor-netty   文件: HttpClientTest.java
@Test
public void testConnectionIdleTimeFixedPool() throws Exception {
	ConnectionProvider provider =
			ConnectionProvider.builder("testConnectionIdleTimeFixedPool")
			                  .maxConnections(1)
			                  .pendingAcquireTimeout(Duration.ofMillis(100))
			                  .maxIdleTime(Duration.ofMillis(10))
			                  .build();
	ChannelId[] ids = doTestConnectionIdleTime(provider);
	assertThat(ids[0]).isNotEqualTo(ids[1]);
}
 
源代码28 项目: reactor-netty   文件: HttpClientTest.java
@Test
public void testConnectionIdleTimeElasticPool() throws Exception {
	ConnectionProvider provider =
			ConnectionProvider.builder("testConnectionIdleTimeElasticPool")
			                  .maxConnections(Integer.MAX_VALUE)
			                  .maxIdleTime(Duration.ofMillis(10))
			                  .build();
	ChannelId[] ids = doTestConnectionIdleTime(provider);
	assertThat(ids[0]).isNotEqualTo(ids[1]);
}
 
源代码29 项目: reactor-netty   文件: HttpClientTest.java
@Test
public void testConnectionNoIdleTimeFixedPool() throws Exception {
	ConnectionProvider provider =
			ConnectionProvider.builder("testConnectionNoIdleTimeFixedPool")
			                  .maxConnections(1)
			                  .pendingAcquireTimeout(Duration.ofMillis(100))
			                  .build();
	ChannelId[] ids = doTestConnectionIdleTime(provider);
	assertThat(ids[0]).isEqualTo(ids[1]);
}
 
源代码30 项目: reactor-netty   文件: HttpClientTest.java
@Test
public void testConnectionNoIdleTimeElasticPool() throws Exception {
	ConnectionProvider provider =
			ConnectionProvider.create("testConnectionNoIdleTimeElasticPool", Integer.MAX_VALUE);
	ChannelId[] ids = doTestConnectionIdleTime(provider);
	assertThat(ids[0]).isEqualTo(ids[1]);
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法