StateListener.java
Upload User: liulanlin
Upload Date: 2017-12-08
Package Size: 1274k
Code Size: 2k
Category:

VOIP program

Development Platform:

Java

  1. package gov.nist.applet.phone.media.transmitter;
  2. import javax.media.*;
  3. public class StateListener implements ControllerListener {
  4. private Integer stateLock = new Integer(0);
  5.     private boolean failed = false;
  6.     public Integer getStateLock() {
  7.         return stateLock;
  8.     }
  9.     public void setFailed() {
  10.         failed = true;
  11.     }
  12. public void controllerUpdate(ControllerEvent ce) {
  13. // If there was an error during configure or
  14. // realize, the processor will be closed
  15. if (ce instanceof ControllerClosedEvent)
  16. setFailed();
  17. // All controller events, send a notification
  18. // to the waiting thread in waitForState method.
  19. if (ce instanceof ControllerEvent) {
  20. synchronized (getStateLock()) {
  21. getStateLock().notifyAll();
  22. }
  23. }
  24. }
  25.     public synchronized boolean waitForState(Processor p, int state) {
  26.         p.addControllerListener(this);
  27.         failed = false;
  28.         // Call the required method on the processor
  29.         if (state == Processor.Configured) {
  30.             p.configure();
  31.         } else if (state == Processor.Realized) {
  32.             p.realize();
  33.         }
  34.         // Wait until we get an event that confirms the
  35.         // success of the method, or a failure event.
  36.         // See StateListener inner class
  37.         while (p.getState() < state && !failed) {
  38.             synchronized (getStateLock()) {
  39.                 try {
  40.                     getStateLock().wait();
  41.                 } catch (InterruptedException ie) {
  42.                     return false;
  43.                 }
  44.             }
  45.         }
  46.         if (failed)
  47.             return false;
  48.         else
  49.             return true;
  50.     }
  51. }