org.ocap.resource.package.html Maven / Gradle / Ivy
The Resource Management API allows a monitor application to refuse a reservation
of limited resources unconditionally and to resolve a resource reservation
contention after negotiation. The monitor application can implement a subclass
of the org.dvb.application.AppsDatabaseFilter
class to refuse a reservation, and a concrete class that implements the org.ocap.resource.ResourceContentionHandler
interface to resolve a contention. See Section 19 Resource Management for more
details.
Example of Monitor Application
This sample code shows how the monitor application implements this package.
The class ResourceHandler is one of the classes of the monitor application. It
prevents an application that has an organization ID of REJECTED_ORGANIZATION
from reserving a section filter resource and assigns priority based upon
expressed need, allowing higher priority for resource reservation to an
application that has an organization ID of PRIORITIZED_ORGANIZATION.
import org.ocap.resource.*;
import org.dvb.application.*;
public class ResourceHandler extends AppsDatabaseFilter
implements ResourceContentionHandler, ResourceContentionHandler2
{
private static final int REJECTED_ORGANIZATION = 0xABCD;
private static final int PRIORITIZED_ORGANIZATION = 0x1234;
/*
* This is Constructor.
* Set a ResourceFilter and a ResourceContentionManager for a resource
* handling when constructing.
*/
public ResourceHandler() {
ResourceContentionManager rcManager = ResourceContentionManager.getInstance();
rcManager.setResourceFilter(this, "org.davic.mpeg.sections.SectionFilterGroup");
rcManager.setResourceContentionHandler(this);
}
/*
* Check if the application is allowed to reserve a resource or not.
*/
public boolean accept(AppID appid) {
return appid.getOID() != REJECTED_ORGANIZATION;
}
/*
* Resolve a resource contention.
*/
public ResourceUsage[] resolveResourceContention(
ResourceUsage[] newRequests,
ResourceUsage[] currentReservations)
{
// Copy new requests and current reservations into single array
ResourceUsage[] result =
new ResourceUsage[newRequests.length + currentReservations.length];
System.arraycopy(newRequests, 0,
result, 0,
newRequests.length);
System.arraycopy(currentReservations, 0,
result, newRequests.length,
currentReservations.length);
// Use bubble sort to order all requests by priority descending
for(int i = 0; i < result.length; i++)
{
for(int j = 0; j < result.length - 1; i++)
{
if (mapPriority(result[j]) < mapPriority(result[j+1]))
{
ResourceUsage temp = result[j];
result[j] = result[j+1];
result[j+1] = temp;
}
}
}
// Return array contain all new requests and current reservations
// ordered by priority descending.
return result;
}
/*
* Resolve a resource contention.
*/
public ResourceUsage[] resolveResourceContention(
ResourceUsage newRequest,
ResourceUsage[] currentReservations)
{
return resolveResourceContention(new ResourceUsage[] { newRequest },
currentReservations);
}
/*
* Map given ResourceUsage to desired priority.
*/
private static int mapPriority(ResourceUsage ru) {
if ( ru == null )
return -1;
int p = ru.getResourcePriority();
if (ru.getAppID().getOID() == PRIORITIZED_ORGANIZATION)
return p;
if (p >= ResourcePriority.PRIORITY_UNKNOWN
&& p < ResourcePriority.PRIORITY_MSO_FIRST)
return p;
return ResourcePriority.PRIORITY_UNKNOWN;
}
public void resourceContentionWarning(ResourceUsage newRequest, ResourceUsage[] currentReservations)
{
// Ignored for this example
}
}