Package transport

Class Transport

  • All Implemented Interfaces:
    java.lang.Runnable
    Direct Known Subclasses:
    Receiver, Sender

    public abstract class Transport
    extends java.lang.Object
    implements java.lang.Runnable
    This is the base interface of the transport layer.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int END_OF_STREAM
      value (length) returned by receive operations when reaching the end of stream.
      static int MAX_PACKET_SIZE
      maximum size of the packets sent through the network-level primitive operation.
    • Constructor Summary

      Constructors 
      Constructor Description
      Transport()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void allowDisconnect()
      allows the application to close the connection The transport layer should call this method to signal that it has no pending communications, and therefore that it is okay to close the connection.
      protected void blockDisconnect()
      blocks the application before shutting down the network-level connection.
      void cancelTimeout​(TimeoutAction a)
      removes a scheduled timeout action.
      void connect​(int source_port, java.net.InetAddress dest_address, int dest_port)
      establishes the network-level connection to the remote endpoint.
      void disconnect()
      closes the network-level connection to the remote endpoint.
      void run()  
      void setErrorPercentage​(double error_percent)
      sets the probability of dropping outgoing packets.
      void setTimeout​(long delay, TimeoutAction a)
      schedules the execution of a timeout action after a given delay.
      protected abstract void unreliableReceive​(byte[] buffer, int offset, int length)
      handles packets received from the network; your transport layer must provide an implementation of this method.
      protected void unreliableSend​(byte[] data, int offset, int length)
      sends a packet using the unreliable network-level primitive.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Transport

        public Transport()
    • Method Detail

      • connect

        public final void connect​(int source_port,
                                  java.net.InetAddress dest_address,
                                  int dest_port)
                           throws java.lang.IllegalArgumentException,
                                  java.net.SocketException
        establishes the network-level connection to the remote endpoint. Actually, this method does not create any connection in the sense of a connection-oriented protocol. Rather, it sets the destination address and port for the other end-point of the network-transport-application stack. Every subsequent call to unreliableSend will use the given destination address. This operation also allocates the necessary system resources to implement the unreliableSend operation. In particular, it creates a DatagramSocket and binds it to the given local and remote addresses.
        Parameters:
        source_port - port number for the local endpoint of the connection
        dest_address - IP address of the remote endpoint of the connection
        dest_port - port number for the remote endpoint of the connection
        Throws:
        java.lang.IllegalArgumentException
        java.net.SocketException
        See Also:
        disconnect(), unreliableSend(byte [] data, int offset, int length)
      • allowDisconnect

        protected final void allowDisconnect()
        allows the application to close the connection The transport layer should call this method to signal that it has no pending communications, and therefore that it is okay to close the connection.
        See Also:
        allowDisconnect()
      • blockDisconnect

        protected final void blockDisconnect()
        blocks the application before shutting down the network-level connection. After this method is called, an application calling disconnect() will block until allowDisconnect() is called. This method can be used by the transport layer to prevent the application from shutting down the connection while the transport layer is still waiting for acknowledgements or other transmissions.
        See Also:
        allowDisconnect()
      • disconnect

        public final void disconnect()
                              throws java.lang.InterruptedException
        closes the network-level connection to the remote endpoint. Closes the DatagramSocket previously created with connect. The effect of this is also to terminate the execution of any transport-level sender or receiver. Notice that this method does not normally invoke the transport-level Sender.close() method, nor does automatically wait for the termination of the closing procedures. In other words, the execution of the (disconnect()) method may shutdown the whole communication channel without a clean closing of the transport-level connection. To prevent this situation, the transport layer can blocked the disconnection (i.e., keep it on hold) by calling blockDisconnect(), for example to wait for acknowledgements from the other endpoint. Once the diconnection is safe, the transport layer can allow the disconnection with allowDisconnect().
        Throws:
        java.lang.InterruptedException
        See Also:
        connect(int, InetAddress, int), blockDisconnect(), allowDisconnect(), Sender.close()
      • unreliableReceive

        protected abstract void unreliableReceive​(byte[] buffer,
                                                  int offset,
                                                  int length)
        handles packets received from the network; your transport layer must provide an implementation of this method. this method is called whenever the transport layer receives a packet from the network layer. The data received from the network consists of length bytes starting at position offset in the buffer array of bytes.
        Parameters:
        buffer - byte array containing the received packet
        offset - position of the received packet within the given byte array
        length - number of bytes received
      • unreliableSend

        protected final void unreliableSend​(byte[] data,
                                            int offset,
                                            int length)
        sends a packet using the unreliable network-level primitive. Notice that this primitive can only send at most MAX_PACKET_SIZE bytes. If the given length is greater than MAX_PACKET_SIZE, only at most MAX_PACKET_SIZE are sent, and the rest are silently ignored.
        See Also:
        MAX_PACKET_SIZE, unreliableReceive(byte [], int, int)
      • setErrorPercentage

        public final void setErrorPercentage​(double error_percent)
        sets the probability of dropping outgoing packets. Notice that in addition to these self-induced errors, the network might naturally drop (or duplicate, or reorder) packets.
        Parameters:
        error_percent - error probability (%)
      • setTimeout

        public final void setTimeout​(long delay,
                                     TimeoutAction a)
        schedules the execution of a timeout action after a given delay.
        Parameters:
        delay - duration of the timeout in milliseconds
        a - timeout response
      • run

        public final void run()
        Specified by:
        run in interface java.lang.Runnable