FileDocCategorySizeDatePackage
Receiver.javaAPI DocphoneME MR2 API (J2ME)3262Wed May 02 18:00:02 BST 2007com.sun.midp.links

Receiver

public class Receiver extends Thread
A thread that receives a message on a given link.

Fields Summary
public static final long
TIMEOUT
Link
link
LinkMessage
msg
boolean
done
Throwable
exception
Constructors Summary
Receiver(Link newlink, long sleeptime)
Constructs a new Receiver that starts a thread that calls receive() on newlink. After starting the thread, sleeps the indicated number of milliseconds to give the new thread a chance to run and call receive() before letting the caller's thread continue.



                                                  
        
        link = newlink;
        done = false;
        exception = null;
        start();

        if (sleeptime > 0) {
            Utils.sleep(sleeptime);
        }
    
Receiver(Link newlink)
Constructs a new Receiver, providing a default sleep time.

        this(newlink, 50L);
    
Methods Summary
public voidawait()
Waits until the thread finishes or until a timeout has expired.

        long timeout = System.currentTimeMillis() + TIMEOUT;
        synchronized (this) {
            try {
                while (System.currentTimeMillis() < timeout && !done) {
                    wait(TIMEOUT);
                }
            } catch (InterruptedException ignore) { }
        }
    
public voidcompleted(LinkMessage msg, java.lang.Throwable thr)
A completion callback. Called after the thread returns from the receive() call. The msg parameter contains the received message, or null if there was an exception was thrown. The thr parameter contains any Throwable caught, or null there was none. This is intended to be overridden by a subclass. The default implementation does nothing.

    
public voidrun()
Receives a message and notifies when done, capturing any exceptions.

        LinkMessage lm;
        try {
            msg = link.receive();
        } catch (Throwable t) {
            exception = t;
        } finally {
            synchronized (this) {
                done = true;
                notifyAll();
            }
        }

        completed(msg, exception);