Exemple #1
0
  byte[] messageToBuffer(Message msg) throws Exception {
    ObjectOutputStream out;
    // BufferedOutputStream bos;

    out_stream.reset();
    // bos=new BufferedOutputStream(out_stream);
    out_stream.write(Version.version_id, 0, Version.version_id.length); // write the version
    // bos.write(Version.version_id, 0, Version.version_id.length); // write the version
    out = new ObjectOutputStream(out_stream);
    // out=new ObjectOutputStream(bos);
    msg.writeExternal(out);
    out.flush(); // needed if out buffers its output to out_stream
    return out_stream.toByteArray();
  }
 /**
  * Checks for the next item.
  *
  * @return result of check
  * @throws IOException I/O exception
  */
 public boolean more() throws IOException {
   if (cache == null) {
     out.write(4);
     send(id);
     cache = new ArrayList<byte[]>();
     final ByteArrayOutputStream os = new ByteArrayOutputStream();
     while (in.read() > 0) {
       receive(in, os);
       cache.add(os.toByteArray());
       os.reset();
     }
     if (!ok()) throw new IOException(receive());
     pos = 0;
   }
   if (pos < cache.size()) return true;
   cache = null;
   return false;
 }
Exemple #3
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) {
      }
    }
  }