Exemple #1
0
 @Override
 public boolean equals(Object o) {
   if (this == o) return true;
   // TODO: why not allow equality to non-HashIndex indices?
   if (!(o instanceof HashIndex)) return false;
   HashIndex hashIndex = (HashIndex) o;
   return indexes.equals(hashIndex.indexes) && objects.equals(hashIndex.objects);
 }
Exemple #2
0
  /** Method to keep the connection alive with the name server */
  public void run() {
    boolean connected = false;
    int ticket = 0;
    int serial = -1;
    int time = 1000;

    DatagramPacket inPack = new DatagramPacket(new byte[1024], 1024);
    DatagramPacket outPack = new DatagramPacket(new byte[1024], 1024);

    ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
    DataInputStream inData;
    DataOutputStream outData = new DataOutputStream(outBuf);

    while (running) {
      if (!connected) { // Thoust ought Register thine self
        try {
          outBuf.reset();
          outData.writeByte(0);
          outData.writeUTF(userName);
          outData.writeInt(portNum);
          outData.flush();
          outPack.setData(outBuf.toByteArray());
          nameServer.send(outPack);
        } catch (IOException e) {
          System.err.println("LeetActive: " + e);
        }
      } else { // Thoust ought Renew thine self
        try {
          outBuf.reset();
          outData.writeByte(2);
          outData.writeInt(ticket);
          outData.flush();
          outPack.setData(outBuf.toByteArray());
          nameServer.send(outPack);
        } catch (IOException e) {
          System.err.println(e);
        }
      }

      // Now we will receive a packet...
      try {
        nameServer.receive(inPack);
        inData = new DataInputStream(new ByteArrayInputStream(inPack.getData()));

        byte type = inData.readByte();

        if (type == 1) { // Twas a ticket packet
          try {
            ticket = inData.readInt();
            if (ticket > -1) { // Make sure its not evil
              connected = true;
            } else {
              connected = false;
            }
            time = inData.readInt();
          } catch (IOException e) {
            System.err.println(e);
          }
        }

        if (type == 5) { // Twas an update packet
          try {
            int s = inData.readInt();
            if (s > serial) { // Make sure its not old
              serial = s;
              int size = inData.readInt();
              ArrayList newList = new ArrayList(size);

              for (int x = 0; x < size; x++) {
                newList.add(
                    new String(
                        "" + inData.readUTF() + "@" + inData.readUTF() + ":" + inData.readInt()));
              }

              if (!newList.equals(hostList)) {
                hostList = newList;
                updated = true;
              }
            }
          } catch (IOException e) {
            System.err.println(e);
          }
        }
      } catch (SocketTimeoutException e) { // Server hates you
        connected = false;
        System.err.println(e);
      } catch (IOException e) {
        System.err.println(e);
      }

      try { // Take a nap
        sleep(time / 4);
      } catch (InterruptedException e) {
      }
    }
  }