statehelper.java

来自「this is the media streaming project」· Java 代码 · 共 71 行

JAVA
71
字号
/****************************************************************************************/
/* 2001-Spring: Java Network-Programming Term-Project					*/
/* Title: Streaming media generation, capture and store.				*/
/* Team Member: Yumin Yuan(yuany@rpi.edu), Rui Mu(mur@rpi.edu), Yining Hu(huyn@rpi.edu) */
/* StateHelper.java: This class keeps track of the state of media transmission 		*/
/* Complile: javac StateHelper.java							*/
/****************************************************************************************/

import javax.media.*;
public class StateHelper implements ControllerListener{

    protected Integer stateLock = new Integer(0);
    protected boolean failed = false;
    Processor p;
    public StateHelper(Processor _p){
	p=_p;
	p.addControllerListener(this);
    }
    Integer getStateLock() {
    	return stateLock;
    }

    void setFailed() {
	    failed = true;
    }

    public synchronized boolean waitForState(int state) {
	failed = false;

	// Call the required method on the processor
	if (state == Processor.Configured) {
	    p.configure();
	} else if (state == Processor.Realized) {
	    p.realize();
	}

	// Wait until we get an event that confirms the
	// success of the method, or a failure event.
	// See StateListener inner class
	while (p.getState() < state && !failed) {
	    synchronized (getStateLock()) {
		try {
		    getStateLock().wait();
		} catch (InterruptedException ie) {
		    return false;
		}
	    }
	}

	if (failed)
	    return false;
	else
	    return true;
    }

    public void controllerUpdate(ControllerEvent event){
    	  if (event instanceof ControllerClosedEvent)
		setFailed();

	    // All controller events, send a notification
	    // to the waiting thread in waitForState method.
	    if (event instanceof ControllerEvent) {
		synchronized (getStateLock()) {
		    getStateLock().notifyAll();
		}
	    }	
    }
}
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?