Example #1
0
  public static void callListeners(Message message) {
    synchronized (listeners) {
      if (!listeners.containsKey(message.getChannel())
          || !listeners.get(message.getChannel()).containsKey(message.getPacket())) {
        return;
      }

      if (!packets.containsKey(message.getChannel())) {
        return;
      }

      // Build up that message
      if (!packets.get(message.getChannel()).containsKey(message.getPacket())) {
        return;
      }

      Class packetClass = packets.get(message.getChannel()).get(message.getPacket());

      try {
        net.cubespace.ComuCator.API.Message.Message message1 =
            (net.cubespace.ComuCator.API.Message.Message) packetClass.newInstance();
        message1.read(new DataInputStream(new ByteArrayInputStream(message.getMessage())));

        for (ListenerMethod listenerMethod :
            listeners.get(message.getChannel()).get(message.getPacket())) {
          listenerMethod.getMethod().invoke(listenerMethod.getPacketListener(), message1);
        }
      } catch (Throwable throwable) {
        Logger.warn("Could not execute Listeners", throwable);
      }
    }
  }
Example #2
0
  public static void registerPacket(Class packet) {
    if (!net.cubespace.ComuCator.API.Message.Message.class.isAssignableFrom(packet)) {
      Logger.warn("This packet does not implement the Message Interface - " + packet.getName());
      return;
    }

    // Create the Packet once
    try {
      net.cubespace.ComuCator.API.Message.Message message =
          (net.cubespace.ComuCator.API.Message.Message) packet.newInstance();

      // Find the Channel to which this Packet belongs
      String channel;
      if (packet.isAnnotationPresent(Channel.class)) {
        channel = ((Channel) packet.getAnnotation(Channel.class)).value();
      } else {
        channel = message.getChannel();
      }

      // If there is no channel for this Packet => Outta here
      if (channel == null) {
        Logger.warn(
            "The packet has no Channel annotation and did gave null back on getChannel() - "
                + packet.getName());
        return;
      }

      Long key = StringCode.getStringCode(channel);
      Long classKey = StringCode.getStringCode(packet.getName());

      synchronized (packets) {
        // Check if Channel exists
        if (packets.containsKey(key)) {
          packets.get(key).put(classKey, packet);
        } else {
          LinkedHashMap<Long, Class> classes = new LinkedHashMap<>();
          classes.put(classKey, packet);

          packets.put(key, classes);
          ChannelKeyCache.addToCache(channel);
        }

        ClassKeyCache.addToCache(packet);
      }
    } catch (Exception e) {
      Logger.warn(
          "The packet could not be build. Make sure it has a default Constructor - "
              + packet.getName());
    }
  }
Example #3
0
  public static void removePacket(Class packet) {
    if (!net.cubespace.ComuCator.API.Message.Message.class.isAssignableFrom(packet)) {
      Logger.warn("This packet does not implement the Message Interface - " + packet.getName());
      return;
    }

    // Create the Packet once
    try {
      net.cubespace.ComuCator.API.Message.Message message =
          (net.cubespace.ComuCator.API.Message.Message) packet.newInstance();

      // Find the Channel to which this Packet belongs
      String channel;
      if (packet.isAnnotationPresent(Channel.class)) {
        channel = ((Channel) packet.getAnnotation(Channel.class)).value();
      } else {
        channel = message.getChannel();
      }

      // If there is no channel for this Packet => Outta here
      if (channel == null) {
        Logger.warn(
            "The packet has no Channel annotation and did gave null back on getChannel() - "
                + packet.getName());
        return;
      }

      Long key = StringCode.getStringCode(channel);
      Long classKey = StringCode.getStringCode(packet.getName());

      synchronized (packets) {
        // Check if Channel exists
        if (packets.containsKey(key)) {
          packets.get(key).remove(classKey);
          ClassKeyCache.removeFromCache(packet);
        }

        if (packets.get(key).size() == 0) {
          ChannelKeyCache.removeFromCache(channel);
        }
      }

      synchronized (listeners) {
        if (listeners.containsKey(key)) {
          listeners.get(key).remove(classKey);

          PacketRegister packetRegister = new PacketRegister();
          packetRegister.setMode((byte) 1);
          packetRegister.setChannel(key);
          packetRegister.setPacket(classKey);

          P2PServers.broadCastToAll(packetRegister);
          P2PServers.removePacket(key, classKey);
        }
      }
    } catch (Exception e) {
      Logger.warn(
          "The packet could not be build. Make sure it has a default Constructor - "
              + packet.getName());
    }
  }