コード例 #1
0
  private boolean sendAck(Packet p, DatagramPacket dp) {

    // Respond
    // System.out.println("RT: Sending ack for packet w/ seqnum: " + p.getSeqNum());
    Packet hsp = new Packet(p.getSeqNum());
    hsp.assembleHandshakePacket();
    packet =
        new DatagramPacket(hsp.getPacketData(), hsp.getPacketSize(), dp.getAddress(), dp.getPort());

    try {
      socket.send(packet);
    } catch (Exception e) {
      System.out.println("RT: Could not send ack packet:\n" + e.getMessage());
      return false;
    }

    return true;
  }
コード例 #2
0
  private int processHandshake(Packet p, InetAddress ip, int port) throws Exception {
    // System.out.println("Got handshake");
    // Get connection information
    this.numMessages = p.getNumMessages();
    this.totalDataSize = p.getTotalDataLength();
    this.seqNum = p.getSeqNum();

    // Respond
    // System.out.println("RT: Sending handshake response");
    Packet hsp = new Packet(seqNum);
    hsp.assembleHandshakePacket();
    packet = new DatagramPacket(hsp.getPacketData(), hsp.getPacketSize(), ip, port);
    socket.send(packet);

    // System.out.println("RT: Done with handshake:\n\tStarting seqnum: "+seqNum+"\n\tTotal datalen:
    // "+totalDataSize);
    return 1; // FIXME p.getNumMessages();
  }
コード例 #3
0
  private MyMessage getMessage() throws Exception {
    int dataRecieved = 0;
    String m = new String();
    String t;

    // FIXME needs to get multiple packets and messages
    // System.out.println("RT: Now waiting for message data");

    while (dataRecieved < totalDataSize) {
      // Get message data
      response = new byte[65000];
      DatagramPacket packet = new DatagramPacket(response, response.length);
      socket.receive(packet);
      Packet p = new Packet(packet.getData());
      // System.out.println("RT: Got message with seqnum: "+p.getSeqNum());

      // If we have a handshake packet something broke
      if (p.isHandshake()) {
        return null;
      }

      // If next in-order packet grab data and is not corrupt
      // XXX
      if (p.getSeqNum() == seqNum + 1 && p.checkChecksum()) {
        // Get the message ( part? )
        // System.out.println("RT: Is correct seqnum");
        t = p.getMessages().get(0).getMessageContents();
        // System.out.println("RT: Got message packet: " + t);
        m += t;

        // Send ack
        sendAck(p, packet);

        // Increment counters
        dataRecieved += t.length();
        seqNum++;
      } else {
        System.out.println("RT: Is NOT correct seqnum");
      }
    }
    MyMessage result = new MyMessage();
    result.setMessageContents(m);
    return result;
  }