- Forward


Customizing UDP Sockets
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • UDP Sockets:
    • Are very versatile
    • But there are times when applications programmers need to develop specializations
  • Examples:
    • To handle the requirements of a firewall
    • To handle encryption
Possible Approaches
Back SMYC Forward
  • Extension:
    • Override some methods in the existing class and inherit others
  • Decorator Pattern:
    • The decorator implements the same interface as the decorated
    • The decorator delegates to methods in the decorated class
  • Simple Delegation:
    • The decorator delegates some method calls but doesn't implement the same interface as the decorated
An Example
Back SMYC Forward
  • The Type of Application:
    • "Similar" datagrams are transmitted over time
    • When a later datagram becomes available, earlier datagrams "expire"
  • Requirements:
    • We are willing to "lose" some packets
    • We we would like to increase the reliability of UDP somewhat
  • One Solution:
    • Re-send any datagrams that have not yet expired if they are "lost"
High Level Design Considerations
Back SMYC Forward
  • Some Issues:
    • Java uses a DatagramSocket to both send and receive but our sender and receiver will need different capabilities
    • Many methods in the DatagramSocket class are inappropriate for this application
  • Conclusion:
    • Use delegation
Low Level Design Alternatives for the Transmitter
Back SMYC Forward
  • Alternative T1:
    1. Start a Timer
    2. Call receive()
    3. Call send() in the Timer "callback"
  • Evaluation:
    • Since receive() blocks the method above will block
    • Expand
Low Level Designs for the Transmitter (cont.)
Back SMYC Forward
  • Alternative T2:
    1. Call receive() in an ackThread
    2. Start a Timer
    3. Call send() in the Timer "callback"
  • Evaluation:
    • The method above will return before the actual sending occurs
    • Expand
Low Level Designs for the Transmitter (cont.)
Back SMYC Forward
  • Alternative T3:
    1. Call receive() in an ackThread
    2. Call send()
    3. Start a Timer
    4. Call send() for re-sends in the Timer "callback"
  • Evaluation:
    • Difficult to coordinate the timeout and the Timer
    • Expand
Low Level Designs for the Transmitter (cont.)
Back SMYC Forward
  • Alternative T4:
    1. Call setSoTimeout()
    2. Call receive() in an ackThread
    3. Call send()
    4. Call send() for re-sends in the catch for the SocketTimeoutException
  • Evaluation:
    • Looks good
    • Expand
Multithreading in the Transmitter
Back SMYC Forward
  • Options:
    • Use one thread for all datagrams
    • Use a different thread for each datagram
  • Evaluation:
    • It's instructive to use a different thread for each datagram
    • It's instructive to use a thread pool (i.e., a ExecutorService )
The Transmitter
Back SMYC Forward
javaexamples/esdsocket/ESDSendingSocket.java
 
Low Level Designs for the Receiver (cont.)
Back SMYC Forward
  • Alternative R1:
    1. Call receive()
    2. Call send() after receive() returns
  • Evaluation:
    • From the caller's perspective, there is a slight delay after receive() stops blocking
    • Expand
Low Level Design Alternatives for the Receiver
Back SMYC Forward
  • Alternative R2:
    1. Call receive()
    2. Call send() in an ackThread after receive() returns
  • Evaluation:
    • Seems more complicated than is needed
    • Expand
The Receiver
Back SMYC Forward
javaexamples/esdsocket/ESDReceivingSocket.java
 
Using a Factory
Back SMYC Forward

Appropriate when the Transmitter/Receiver are the Same

custom_datagram_sockets_java
There's Always More to Learn
Back -