private void joinLeave(MessageContext context, String args) {
   if (args.isEmpty()) {
     return;
   }
   args = args.toLowerCase();
   String[] split = args.split(" ");
   String cid = split[0];
   JoinLeave target = JoinLeave.BOTH;
   if (split.length != 1) {
     String jlStr = split[1].toLowerCase();
     for (JoinLeave joinLeave : JoinLeave.values()) {
       if (joinLeave.name().toLowerCase().startsWith(jlStr)) {
         target = joinLeave;
         break;
       }
     }
   }
   if ("default".equals(cid)) {
     cid = context.getServer().getId();
   } else if ("this".equals(cid)) {
     cid = context.getChannel().getId();
   }
   Channel channel = apiClient.getChannelById(cid);
   if (channel != NO_CHANNEL) {
     if (target == JoinLeave.JOIN) {
       bot.getEventListener().joinMessageRedirect.put(context.getServer().getId(), cid);
     }
     if (target == JoinLeave.LEAVE) {
       bot.getEventListener().leaveMessageRedirect.put(context.getServer().getId(), cid);
     }
     if (target == JoinLeave.BOTH) {
       bot.getEventListener().joinMessageRedirect.put(context.getServer().getId(), cid);
       bot.getEventListener().leaveMessageRedirect.put(context.getServer().getId(), cid);
     }
     apiClient.sendMessage(
         loc.localize("commands.mod.jl.response", channel.getName(), channel.getId()),
         context.getChannel());
   } else {
     apiClient.sendMessage(loc.localize("commands.mod.jl.response.invalid"), context.getChannel());
   }
 }
Example #2
0
  public BrokerHost addChannel(Channel channel) {
    lock.lock();
    try {
      // add the channel and return the broker host
      Manageable manageable;
      if (channel.getDirection() == Direction.OUT) {
        BrokerHost host = brokerHosts.get(brokerIndex);
        List<Channel> producerChannels = brokerHostToProducerChannelMap.get(host);
        BlockingQueue<MessageContext> channelOutQueue = producerQueues.get(host);

        if (!producers.containsKey(host)) {
          manageable =
              transport.registerProducer(
                  host, prefix, channel.getProperties(), producerQueues.get(host));
          producers.put(host, manageable);

          manageable.start();
        }

        // now register the channel with the brokers map
        // check weather you have a sender consumer for this host
        channel.setOutQueue(channelOutQueue);
        producerChannels.add(channel);
        producerChannels.add(new Channel("", Direction.OUT));

        LOG.info(
            "Registering channel {} with group {} and host {}",
            channel.getName(),
            name,
            host.toString());
        incrementProducerIndex();

        return host;

      } else if (channel.getDirection() == Direction.IN) {
        BrokerHost host = brokerHosts.get(brokerIndex);
        List<Channel> consumerChannels = brokerHostToConsumerChannelMap.get(host);
        if (!consumers.containsKey(host)) {
          BlockingQueue<MessageContext> channelInQueue = consumerQueues.get(host);
          manageable =
              transport.registerConsumer(host, prefix, channel.getProperties(), channelInQueue);
          consumers.put(host, manageable);

          ConsumingWorker worker;
          if (channel.isGrouped()) {
            worker = new ConsumingWorker(consumerChannels, channelInQueue);
          } else {
            worker = new ConsumingWorker(consumerChannels, channelInQueue, true);
          }
          Thread thread = new Thread(worker);
          thread.start();

          manageable.start();

          consumingWorkers.put(host, worker);
        }

        // now register the channel with the brokers map
        // check weather you have a sender consumer for this host
        consumerChannels.add(channel);

        LOG.info(
            "Registering channel {} with group {} and host {}",
            channel.getName(),
            name,
            host.toString());
        incrementConsumerIndex();

        return host;
      }
    } finally {
      lock.unlock();
    }
    return null;
  }