예제 #1
0
  /**
   * Remove all references to the channel. 1) the map of a channel (socket) to its interests. 2) the
   * map of interests to its channels.
   *
   * <p>As each interest is removed inform all channels having a similar interest.
   *
   * @param sourceCtx
   */
  public void unregister(final ChannelHandlerContext sourceCtx) {
    final SocketAddress key = sourceCtx.channel().remoteAddress();
    majorLogger.info("unregister channel {}", key);
    final Interest.ChannelInterestSet channelInterestSet = this.channelInterestMap.remove(key);
    if (channelInterestSet == null) {
      majorLogger.warn(
          "nothing to unregister in interest map: {}", this.channelInterestMap.keySet());
      return;
    }
    final String trackingGuid = UUID.randomUUID().toString();
    majorLogger.warn("no interested channel set");

    for (final Interest interest : channelInterestSet.getInterestList()) {
      if (!this.interestedChannelMap.containsKey(interest)) {
        continue;
      }

      final Interest.InterestedChannelSet interestedChannelSet =
          this.interestedChannelMap.get(interest);
      interestedChannelSet.removeItem(sourceCtx, interest);
      majorLogger.info("unregistered interest {}", interest);
      // this.interestedChannelMap.put(interest, interestedChannelSet);

      // an implicit DISINTEREST message will be sent to all interested channels

      final MetaLinkMsg.Edit.Builder editBuilder =
          MetaLinkMsg.Edit.newBuilder()
              .addMode(MetaLinkMsg.Edit.EditMode.DISINTEREST)
              .setEditMode(MetaLinkMsg.Edit.EditMode.DISINTEREST)
              .setGuid(trackingGuid)
              .setSequence(sequenceNumber.incrementAndGet())
              .addAllTopic(interest.getTopic())
              .addOrigin(sourceCtx.channel().remoteAddress().toString())
              .addOrigin(METALINK_BRIDGE_NAME);
      final MetaLinkMsg.Edit disinterestMsg = editBuilder.build();

      majorLogger.trace("expressing disinterest to all channels except source");
      for (final Map.Entry<SocketAddress, Interest.ChannelInterestSet> entry :
          this.channelInterestMap.entrySet()) {
        final ChannelHandlerContext ctx = entry.getValue().context;
        if (isSameChannel(ctx, sourceCtx)) {
          continue;
        }
        majorLogger.trace("disinterest {} to {}", trackingGuid, ctx.channel().remoteAddress());
        ctx.write(disinterestMsg);
        ctx.flush();
        detailLogger.info("message sent:\n{}", disinterestMsg);
      }
    }
  }