public synchronized void handlePacket(Connection c, Packet p) {
    Address address = c.getAddress();
    System.out.println(p.toString());

    // silently ignore packets from a connection if we haven't received a version packet

    if (!c.hasReceivedVersion() && p.packetType() != PacketType.VERSION) {
      return;
    }

    switch (p.packetType()) {
      case VERSION:
        VersionPacket v = (VersionPacket) p;
        long ourVersion = ProtocolVersion.version(),
            theirVersion = v.getVersion(),
            negotiatedVersion;
        negotiatedVersion = theirVersion < ourVersion ? theirVersion : ourVersion;
        c.setVersion(negotiatedVersion);
        c.hasReceivedVersion(true);
        addressBook.justSeen(address);
        if (negotiatedVersion >= 209) {
          Packet verack = c.createPacket(PacketType.VERACK);
          c.sendPacket(verack);
        }
        break;
      case VERACK:
        c.hasRecievedVerack(true);
        addressBook.justSeen(address);
        break;
      case HEADERS:
        // primitive headers function
        HeadersPacket h = (HeadersPacket) p;
        if (h.headers().size() == 0) {
          break;
        }
        for (Block header : h.headers()) {
          try {
            blockChain.addBlock(header);

          } catch (InvalidBlockException e) {
            // TODO actually handle
            e.printStackTrace();
            continue;
          } catch (OrphanBlockException e) {
            // TODO actually handle
            e.printStackTrace();
            continue;
          } catch (BlockExistsException e) {
            // TODO actually handle
            e.printStackTrace();
            continue;
          }
        }
        GetHeadersPacket gh = (GetHeadersPacket) c.createPacket(PacketType.GETHEADERS);
        gh.startHashes().add(blockChain.topBlock().hash());
        c.sendPacket(gh);
    }
  }
 public synchronized void getInitialHeaders() {
   for (Connection c : connections) {
     GetHeadersPacket gh = (GetHeadersPacket) c.createPacket(PacketType.GETHEADERS);
     gh.startHashes().add(blockChain.topBlock().hash());
     c.sendPacket(gh);
   }
 }
Example #3
0
  /**
   * Sends a roster to userID. All the entries of the roster will be sent to the target user.
   *
   * @param roster the roster to send
   * @param targetUserID the user that will receive the roster entries
   */
  public void send(Roster roster, String targetUserID) {
    // Create a new message to send the roster
    Message msg = new Message(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange(roster);
    msg.addExtension(rosterExchange);

    // Send the message that contains the roster
    con.sendPacket(msg);
  }
 public synchronized Connection connectTo(Address a) throws IOException {
   addressBook.justTried(a);
   Connection c = new Connection(this, a);
   connections.add(c);
   addressBook.justConnected(a);
   VersionPacket p = (VersionPacket) c.createPacket(PacketType.VERSION);
   p.setRemoteAddress(c.getAddress());
   p.setFromAddress(localAddress);
   c.sendPacket(p);
   c.connect();
   return c;
 }
Example #5
0
  /**
   * Sends a roster group to userID. All the entries of the group will be sent to the target user.
   *
   * @param rosterGroup the roster group to send
   * @param targetUserID the user that will receive the roster entries
   */
  public void send(RosterGroup rosterGroup, String targetUserID) {
    // Create a new message to send the roster
    Message msg = new Message(targetUserID);
    // Create a RosterExchange Package and add it to the message
    RosterExchange rosterExchange = new RosterExchange();
    for (RosterEntry entry : rosterGroup.getEntries()) {
      rosterExchange.addRosterEntry(entry);
    }
    msg.addExtension(rosterExchange);

    // Send the message that contains the roster
    con.sendPacket(msg);
  }
Example #6
0
  /**
   * Returns the private data specified by the given element name and namespace. Each chunk of
   * private data is uniquely identified by an element name and namespace pair.
   *
   * <p>If a PrivateDataProvider is registered for the specified element name/namespace pair then
   * that provider will determine the specific object type that is returned. If no provider is
   * registered, a {@link DefaultPrivateData} instance will be returned.
   *
   * @param elementName the element name.
   * @param namespace the namespace.
   * @return the private data.
   * @throws XMPPException if an error occurs getting the private data.
   */
  public PrivateData getPrivateData(final String elementName, final String namespace)
      throws XMPPException {
    // Create an IQ packet to get the private data.
    IQ privateDataGet =
        new IQ() {
          public String getChildElementXML() {
            StringBuilder buf = new StringBuilder();
            buf.append("<query xmlns=\"jabber:iq:private\">");
            buf.append("<")
                .append(elementName)
                .append(" xmlns=\"")
                .append(namespace)
                .append("\"/>");
            buf.append("</query>");
            return buf.toString();
          }
        };
    privateDataGet.setType(IQ.Type.GET);
    // Address the packet to the other account if user has been set.
    if (user != null) {
      privateDataGet.setTo(user);
    }

    // Setup a listener for the reply to the set operation.
    String packetID = privateDataGet.getPacketID();
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(packetID));

    // Send the private data.
    connection.sendPacket(privateDataGet);

    // Wait up to five seconds for a response from the server.
    IQ response = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    // Stop queuing results
    collector.cancel();
    if (response == null) {
      throw new XMPPException("No response from the server.");
    }
    // If the server replied with an error, throw an exception.
    else if (response.getType() == IQ.Type.ERROR) {
      throw new XMPPException(response.getError());
    }
    return ((PrivateDataResult) response).getPrivateData();
  }