private CarbonManager(XMPPConnection connection) {
   super(connection);
   ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
   sdm.addFeature(CarbonExtension.NAMESPACE);
 }
  /**
   * Creates a new ServiceDiscoveryManager for a given Connection. This means that the service
   * manager will respond to any service discovery request that the connection may receive.
   *
   * @param connection the connection to which a ServiceDiscoveryManager is going to be created.
   */
  private ServiceDiscoveryManager(Connection connection) {
    this.connection = new WeakReference<Connection>(connection);

    // Register the new instance and associate it with the connection
    instances.put(connection, this);

    addFeature(DiscoverInfo.NAMESPACE);
    addFeature(DiscoverItems.NAMESPACE);

    // Listen for disco#items requests and answer with an empty result
    PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
    PacketListener packetListener =
        new PacketListener() {
          public void processPacket(Packet packet) {
            Connection connection = ServiceDiscoveryManager.this.connection.get();
            if (connection == null) return;
            DiscoverItems discoverItems = (DiscoverItems) packet;
            // Send back the items defined in the client if the request is of type GET
            if (discoverItems != null && discoverItems.getType() == IQ.Type.GET) {
              DiscoverItems response = new DiscoverItems();
              response.setType(IQ.Type.RESULT);
              response.setTo(discoverItems.getFrom());
              response.setPacketID(discoverItems.getPacketID());
              response.setNode(discoverItems.getNode());

              // Add the defined items related to the requested node. Look for
              // the NodeInformationProvider associated with the requested node.
              NodeInformationProvider nodeInformationProvider =
                  getNodeInformationProvider(discoverItems.getNode());
              if (nodeInformationProvider != null) {
                // Specified node was found, add node items
                response.addItems(nodeInformationProvider.getNodeItems());
                // Add packet extensions
                response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
              } else if (discoverItems.getNode() != null) {
                // Return <item-not-found/> error since client doesn't contain
                // the specified node
                response.setType(IQ.Type.ERROR);
                response.setError(new XMPPError(XMPPError.Condition.item_not_found));
              }
              connection.sendPacket(response);
            }
          }
        };
    connection.addPacketListener(packetListener, packetFilter);

    // Listen for disco#info requests and answer the client's supported features
    // To add a new feature as supported use the #addFeature message
    packetFilter = new PacketTypeFilter(DiscoverInfo.class);
    packetListener =
        new PacketListener() {
          public void processPacket(Packet packet) {
            Connection connection = ServiceDiscoveryManager.this.connection.get();
            if (connection == null) return;
            DiscoverInfo discoverInfo = (DiscoverInfo) packet;
            // Answer the client's supported features if the request is of the GET type
            if (discoverInfo != null && discoverInfo.getType() == IQ.Type.GET) {
              DiscoverInfo response = new DiscoverInfo();
              response.setType(IQ.Type.RESULT);
              response.setTo(discoverInfo.getFrom());
              response.setPacketID(discoverInfo.getPacketID());
              response.setNode(discoverInfo.getNode());
              // Add the client's identity and features only if "node" is null
              // and if the request was not send to a node. If Entity Caps are
              // enabled the client's identity and features are may also added
              // if the right node is chosen
              if (discoverInfo.getNode() == null) {
                addDiscoverInfoTo(response);
              } else {
                // Disco#info was sent to a node. Check if we have information of the
                // specified node
                NodeInformationProvider nodeInformationProvider =
                    getNodeInformationProvider(discoverInfo.getNode());
                if (nodeInformationProvider != null) {
                  // Node was found. Add node features
                  response.addFeatures(nodeInformationProvider.getNodeFeatures());
                  // Add node identities
                  response.addIdentities(nodeInformationProvider.getNodeIdentities());
                  // Add packet extensions
                  response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
                } else {
                  // Return <item-not-found/> error since specified node was not found
                  response.setType(IQ.Type.ERROR);
                  response.setError(new XMPPError(XMPPError.Condition.item_not_found));
                }
              }
              connection.sendPacket(response);
            }
          }
        };
    connection.addPacketListener(packetListener, packetFilter);
  }
  public static void initialize(final XMPPConnection theConnection, final History h) {
    final String jid = XmppStringUtils.parseBareJid(theConnection.getUser());
    // Hash fetch service
    theConnection.registerIQRequestHandler(
        new IQRequestHandler() {

          @Override
          public IQ handleIQRequest(IQ iqRequest) {
            HistorySyncQuery query = (HistorySyncQuery) iqRequest;
            if (query.getType() != IQ.Type.get) {
              throw new Error();
            }
            if (!XmppStringUtils.parseBareJid(iqRequest.getFrom()).equals(jid)) {
              return error(query);
            }

            HistorySyncHashes response = query.reply(h);
            return response;
          }

          @Override
          public Mode getMode() {
            return Mode.async;
          }

          @Override
          public Type getType() {
            return Type.get;
          }

          @Override
          public String getElement() {
            return "query";
          }

          @Override
          public String getNamespace() {
            return "http://jlenet.de/histsync";
          }
        });

    // sync service
    theConnection.registerIQRequestHandler(
        new IQRequestHandler() {
          @Override
          public IQ handleIQRequest(IQ iqRequest) {
            HistorySyncSet sync = (HistorySyncSet) iqRequest;
            if (!XmppStringUtils.parseBareJid(iqRequest.getFrom()).equals(jid)) {
              return error(sync);
            }

            if (Debug.ENABLED) {
              System.out.println("sync pack");
            }
            HistoryLeafNode hln =
                (HistoryLeafNode) h.getAnyBlock(sync.getHour() * History.BASE, History.LEVELS);
            if (Debug.ENABLED) {
              System.out.println("Have: " + hln.getMessages().size());
              System.out.println("Got: " + sync.getMessages().size());
            }
            TreeSet<HistoryEntry> forMe = new TreeSet<HistoryEntry>();
            TreeSet<HistoryEntry> forOther = new TreeSet<HistoryEntry>();
            Iterator<HistoryEntry> have = hln.getMessages().iterator();
            Iterator<HistoryEntry> got = sync.getMessages().iterator();
            HistoryEntry currentGot = null;
            HistoryEntry currentHave = null;
            while (have.hasNext() || got.hasNext() || currentHave != null || currentGot != null) {
              if (currentGot == null && got.hasNext()) {
                currentGot = got.next();
              }
              if (currentHave == null && have.hasNext()) {
                currentHave = have.next();
              }
              if (currentHave == null && currentGot == null) {
                // Should never happen;
                System.out.println("this should never happen");
                break;
              }
              if (currentGot == null
                  || (currentHave != null && currentHave.compareTo(currentGot) < 0)) {
                // current Have is alone
                forOther.add(currentHave);
                currentHave = null;
              } else if (currentHave == null || currentHave.compareTo(currentGot) > 0) {
                // current Got is alone
                forMe.add(currentGot);
                currentGot = null;
              } else {
                currentHave = null;
                currentGot = null;
              }
            }
            hln.getMessages().addAll(forMe);
            h.modified(sync.getHour() * History.BASE);
            // Construct response
            HistorySyncSet hss =
                new HistorySyncSet(
                    sync.getHour(), History.beautifyChecksum(hln.getChecksum()), false);
            hss.setMessages(forOther);
            hss.setType(IQ.Type.result);
            hss.setTo(sync.getFrom());
            hss.setStanzaId(sync.getStanzaId());
            h.store();

            if (Debug.ENABLED) {
              System.out.println("now Have: " + hln.getMessages().size());
              System.out.println("adding: " + forMe.size());
              System.out.println("for other: " + forOther.size());
            }
            return hss;
          }

          @Override
          public Mode getMode() {
            return Mode.async;
          }

          @Override
          public Type getType() {
            return Type.set;
          }

          @Override
          public String getElement() {
            return "syncSet";
          }

          @Override
          public String getNamespace() {
            return "http://jlenet.de/histsync#syncSet";
          }
        });
    theConnection.registerIQRequestHandler(
        new IQRequestHandler() {

          @Override
          public IQ handleIQRequest(IQ iqRequest) {
            HistorySyncSet sync = (HistorySyncSet) iqRequest;
            if (!XmppStringUtils.parseBareJid(iqRequest.getFrom()).equals(jid)) {
              return error(sync);
            }
            HistoryLeafNode hln =
                (HistoryLeafNode) h.getAnyBlock(sync.getHour() * History.BASE, History.LEVELS);
            if (Debug.ENABLED) {
              System.out.println("Have: " + hln.getMessages().size());
              System.out.println("Got: " + sync.getMessages().size());
            }

            hln.getMessages().addAll(sync.getMessages());
            h.modified(sync.getHour() * History.BASE);
            h.store();

            byte[] myChecksum = hln.getChecksum();
            String status;
            if (!Arrays.equals(myChecksum, History.parseChecksum(sync.getChecksum()))) {
              status = "success";
            } else {
              status = "mismatch";
            }
            HistorySyncUpdateResponse hss = new HistorySyncUpdateResponse(status);
            hss.setType(IQ.Type.result);
            hss.setTo(sync.getFrom());
            hss.setStanzaId(sync.getStanzaId());
            if (Debug.ENABLED) {
              System.out.println("update was: " + status);
            }
            if (Debug.ENABLED) {
              System.out.println("now Have: " + hln.getMessages().size());
            }
            return hss;
          }

          @Override
          public Type getType() {
            return Type.set;
          }

          @Override
          public Mode getMode() {
            return Mode.async;
          }

          @Override
          public String getElement() {
            return "syncUpdate";
          }

          @Override
          public String getNamespace() {
            return "http://jlenet.de/histsync#syncUpdate";
          }
        });

    ProviderManager.addIQProvider(
        "query", "http://jlenet.de/histsync", new HistorySyncQueryProvider());
    ProviderManager.addIQProvider(
        "hashes", "http://jlenet.de/histsync#hashes", new HistorySyncResponseProvider());

    HistorySyncSetProvider setProvider = new HistorySyncSetProvider();
    ProviderManager.addIQProvider("syncSet", "http://jlenet.de/histsync#syncSet", setProvider);
    ProviderManager.addIQProvider(
        "syncUpdate", "http://jlenet.de/histsync#syncUpdate", setProvider);
    ProviderManager.addIQProvider(
        "syncStatus",
        "http://jlenet.de/histsync#syncStatus",
        new HistorySyncUpdateResponse.Provider());

    ServiceDiscoveryManager manager = ServiceDiscoveryManager.getInstanceFor(theConnection);
    manager.addFeature("http://jlenet.de/histsync#disco");
  }
Пример #4
0
 private CarbonManager(Connection connection) {
   ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
   sdm.addFeature(CarbonExtension.NAMESPACE);
   weakRefConnection = new WeakReference<Connection>(connection);
   instances.put(connection, this);
 }
Пример #5
0
  /**
   * Creates a new ServiceDiscoveryManager for a given XMPPConnection. This means that the service
   * manager will respond to any service discovery request that the connection may receive.
   *
   * @param connection the connection to which a ServiceDiscoveryManager is going to be created.
   */
  private ServiceDiscoveryManager(XMPPConnection connection) {
    super(connection);

    addFeature(DiscoverInfo.NAMESPACE);
    addFeature(DiscoverItems.NAMESPACE);

    // Listen for disco#items requests and answer with an empty result
    PacketListener packetListener =
        new PacketListener() {
          public void processPacket(Packet packet) throws NotConnectedException {
            DiscoverItems discoverItems = (DiscoverItems) packet;
            DiscoverItems response = new DiscoverItems();
            response.setType(IQ.Type.result);
            response.setTo(discoverItems.getFrom());
            response.setPacketID(discoverItems.getPacketID());
            response.setNode(discoverItems.getNode());

            // Add the defined items related to the requested node. Look for
            // the NodeInformationProvider associated with the requested node.
            NodeInformationProvider nodeInformationProvider =
                getNodeInformationProvider(discoverItems.getNode());
            if (nodeInformationProvider != null) {
              // Specified node was found, add node items
              response.addItems(nodeInformationProvider.getNodeItems());
              // Add packet extensions
              response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
            } else if (discoverItems.getNode() != null) {
              // Return <item-not-found/> error since client doesn't contain
              // the specified node
              response.setType(IQ.Type.error);
              response.setError(new XMPPError(XMPPError.Condition.item_not_found));
            }
            connection().sendPacket(response);
          }
        };
    connection.addPacketListener(packetListener, GET_DISCOVER_ITEMS);

    // Listen for disco#info requests and answer the client's supported features
    // To add a new feature as supported use the #addFeature message
    packetListener =
        new PacketListener() {
          public void processPacket(Packet packet) throws NotConnectedException {
            DiscoverInfo discoverInfo = (DiscoverInfo) packet;
            // Answer the client's supported features if the request is of the GET type
            DiscoverInfo response = new DiscoverInfo();
            response.setType(IQ.Type.result);
            response.setTo(discoverInfo.getFrom());
            response.setPacketID(discoverInfo.getPacketID());
            response.setNode(discoverInfo.getNode());
            // Add the client's identity and features only if "node" is null
            // and if the request was not send to a node. If Entity Caps are
            // enabled the client's identity and features are may also added
            // if the right node is chosen
            if (discoverInfo.getNode() == null) {
              addDiscoverInfoTo(response);
            } else {
              // Disco#info was sent to a node. Check if we have information of the
              // specified node
              NodeInformationProvider nodeInformationProvider =
                  getNodeInformationProvider(discoverInfo.getNode());
              if (nodeInformationProvider != null) {
                // Node was found. Add node features
                response.addFeatures(nodeInformationProvider.getNodeFeatures());
                // Add node identities
                response.addIdentities(nodeInformationProvider.getNodeIdentities());
                // Add packet extensions
                response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
              } else {
                // Return <item-not-found/> error since specified node was not found
                response.setType(IQ.Type.error);
                response.setError(new XMPPError(XMPPError.Condition.item_not_found));
              }
            }
            connection().sendPacket(response);
          }
        };
    connection.addPacketListener(packetListener, GET_DISCOVER_INFO);
  }