org.red5.server.stream
Class SimpleBWControlService

java.lang.Object
  extended by java.util.TimerTask
      extended by org.red5.server.stream.SimpleBWControlService
All Implemented Interfaces:
Runnable, IBWControlService

public class SimpleBWControlService
extends TimerTask
implements IBWControlService

A simple implementation of bandwidth controller. The initial burst, if not specified by user, is half of the property "defaultCapacity".

Following is the reference information for the future optimization on threading: The threads that may access this object concurrently are: * Thread A that makes token request. * Thread B that makes token request. * Thread C that distributes tokens and call the callbacks. (Timer) * Thread D that updates the bw config of a controllable. * Thread E that resets a bucket. * Thread F that unregisters a controllable. The implementation now synchronizes on each context to make sure only one thread is accessing the context object at a time.

Version:
$Id$
Author:
Steven Gong (steven.gong@gmail.com)

Nested Class Summary
protected  class SimpleBWControlService.BWContext
           
protected  class SimpleBWControlService.TokenRequest
           
protected  class SimpleBWControlService.TokenRequestContext
           
protected static class SimpleBWControlService.TokenRequestType
           
 
Field Summary
protected  Map<IBWControllable,SimpleBWControlService.BWContext> contextMap
           
protected  long defaultCapacity
           
protected  long interval
           
protected  Timer tokenDistributor
           
 
Fields inherited from interface org.red5.server.stream.IBWControlService
KEY
 
Constructor Summary
SimpleBWControlService()
           
 
Method Summary
 ITokenBucket getAudioBucket(IBWControlContext context)
          Return the token bucket for audio channel.
 ITokenBucket getDataBucket(IBWControlContext context)
          Return the token bucket for data channel.
 ITokenBucket getVideoBucket(IBWControlContext context)
          Return the token bucket for video channel.
 void init()
           
protected  void invokeCallback(SimpleBWControlService.BWContext context)
           
 IBWControlContext lookupContext(IBWControllable bc)
          Lookup the registry context according to the controllable.
protected  boolean processRequest(SimpleBWControlService.TokenRequest request)
           
 IBWControlContext registerBWControllable(IBWControllable bc)
          Register a bandwidth controllable.
 void resetBuckets(IBWControlContext context)
          Reset all the token buckets for a controllable.
protected  void rollbackRequest(SimpleBWControlService.TokenRequest request)
          Give back the acquired tokens due to failing to accomplish the requested operation or over-charged tokens in the case of best-effort request.
 void run()
           
 void setDefaultCapacity(long capacity)
           
 void setInterval(long interval)
           
 void shutdown()
           
 void unregisterBWControllable(IBWControlContext context)
          Unregister the bandwidth controllable.
 void updateBWConfigure(IBWControlContext context)
          Update the bandwidth configuration of a controllable.
 
Methods inherited from class java.util.TimerTask
cancel, scheduledExecutionTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

contextMap

protected Map<IBWControllable,SimpleBWControlService.BWContext> contextMap

tokenDistributor

protected Timer tokenDistributor

interval

protected long interval

defaultCapacity

protected long defaultCapacity
Constructor Detail

SimpleBWControlService

public SimpleBWControlService()
Method Detail

init

public void init()

shutdown

public void shutdown()

run

public void run()
Specified by:
run in interface Runnable
Specified by:
run in class TimerTask

getAudioBucket

public ITokenBucket getAudioBucket(IBWControlContext context)
Description copied from interface: IBWControlService
Return the token bucket for audio channel.

Specified by:
getAudioBucket in interface IBWControlService
Parameters:
context - The registry context.
Returns:
Token bucket for audio channel.

getVideoBucket

public ITokenBucket getVideoBucket(IBWControlContext context)
Description copied from interface: IBWControlService
Return the token bucket for video channel.

Specified by:
getVideoBucket in interface IBWControlService
Parameters:
context - The registry context.
Returns:
Token bucket for video channel.

getDataBucket

public ITokenBucket getDataBucket(IBWControlContext context)
Description copied from interface: IBWControlService
Return the token bucket for data channel.

Specified by:
getDataBucket in interface IBWControlService
Parameters:
context - The registry context.
Returns:
Token bucket for data channel.

registerBWControllable

public IBWControlContext registerBWControllable(IBWControllable bc)
Description copied from interface: IBWControlService
Register a bandwidth controllable. The necessary resources will be allocated and assigned to the controllable.

Specified by:
registerBWControllable in interface IBWControlService
Parameters:
bc - The bandwidth controllable.
Returns:
The registry context. It's used in the subsequent calls to controller's method.

resetBuckets

public void resetBuckets(IBWControlContext context)
Description copied from interface: IBWControlService
Reset all the token buckets for a controllable. All the callback will be reset and all blocked threads will be woken up.

Specified by:
resetBuckets in interface IBWControlService
Parameters:
context - The registry context.

unregisterBWControllable

public void unregisterBWControllable(IBWControlContext context)
Description copied from interface: IBWControlService
Unregister the bandwidth controllable. The resources that were allocated will be freed.

Specified by:
unregisterBWControllable in interface IBWControlService
Parameters:
context - The registry context.

lookupContext

public IBWControlContext lookupContext(IBWControllable bc)
Description copied from interface: IBWControlService
Lookup the registry context according to the controllable.

Specified by:
lookupContext in interface IBWControlService
Parameters:
bc - The bandwidth controllable.
Returns:
The registry context.

updateBWConfigure

public void updateBWConfigure(IBWControlContext context)
Description copied from interface: IBWControlService
Update the bandwidth configuration of a controllable. Each time when the controllable changes the bandwidth config and wants to make the changes take effect, this method should be called.

Specified by:
updateBWConfigure in interface IBWControlService
Parameters:
context - The registry context.

setInterval

public void setInterval(long interval)

setDefaultCapacity

public void setDefaultCapacity(long capacity)

processRequest

protected boolean processRequest(SimpleBWControlService.TokenRequest request)

invokeCallback

protected void invokeCallback(SimpleBWControlService.BWContext context)

rollbackRequest

protected void rollbackRequest(SimpleBWControlService.TokenRequest request)
Give back the acquired tokens due to failing to accomplish the requested operation or over-charged tokens in the case of best-effort request.

Parameters:
request -


Copyright © 2006-2009 The Red5 Project