Adding a Service at the Node Level
Todd Wright
twright at bbn.com
Wed Feb 11 12:38:17 EST 2004
On Tuesday 10 February 2004 15:40, Graham, John H wrote:
> Hello,
> I'm trying to register a service at the node level. I know to it
> involves getting the serviceBroker. I have been able to add a service at
> the Agent Level but I really need to add this Service to the Node Level.
> Is there any example of this?
A component in the NodeAgent can use the NodeControlService to advertise a
service to all agents. The NodeControlService provides access to the "root"
ServiceBroker.
Components are loaded in the NodeAgent by either specifying them in the
node's ".ini" file, e.g.:
YourNode.ini:
cluster = <usual agent lines here>
plugin = <your component>
or in the node's ".xml" file if you're using XMLNode, e.g.:
YourNode.xml:
<node ..>
<component class="YourComponent" ..>
<agent name="AnExampleAgent">..
A component loaded in a regular agent can not obtain the NodeControlService.
Here's an example use of the NodeControlService to advertise a "FooService"
to all agents:
---------------------------------------------------------------------
import org.cougaar.core.component.BindingSite;
import org.cougaar.core.component.Component;
import org.cougaar.core.component.ServiceBroker;
import org.cougaar.core.component.ServiceProvider;
import org.cougaar.core.node.NodeControlService;
import org.cougaar.util.GenericStateModelAdapter;
public class MyComponent
extends GenericStateModelAdapter
implements Component
{
private ServiceBroker sb;
private ServiceBroker rootsb;
private ServiceProvider sp;
public void setBindingSite(BindingSite bs) {
this.sb = bs.getServiceBroker();
}
public void load() {
super.load();
// get rootsb
NodeControlService ncs = (NodeControlService)
sb.getService(this, NodeControlService.class, null);
if (ncs == null) {
throw new RuntimeException("Unable to obtain NodeControlService");
}
rootsb = ncs.getRootServiceBroker();
sb.releaseService(this, NodeControlService.class, ncs);
// advertise service
sp = new MyServiceProvider();
rootsb.addService(FooService.class, sp);
}
public void unload() {
super.unload();
rootsb.revokeService(FooService.class, sp);
sp = null;
}
private static class MyServiceProvider
implements ServiceProvider {
public Object getService(
ServiceBroker sb, Object requestor, Class serviceClass) {
if (FooService.class.isAssignableFrom(serviceClass)) {
// return service instance here!
return new FooService() { ??? };
} else {
return null;
}
}
public void releaseService(
ServiceBroker sb, Object requestor,
Class serviceClass, Object service) {
// optionally cleanup released service instance here!
}
}
}
---------------------------------------------------------------------
>
> What I have done is have my ServiceProvider is to extend the
> ComponentPlugin.
That will work too. In the example above I used a more basic
"GenericStateModel" subclass, which ComponentPlugin extends.
> From which the ServiceProvider then gets the
> bindingSite's ServiceBroker and registers the service with it. The call I
> use is this:
>
> getBindingSite().getServiceBroker().addService(MyService.class,this);
This is equivalent to:
ServiceBroker sb = getBindingSite().getServiceBroker();
sb.addService(MyService.class,this);
which is very similar to the "rootsb" example shown above.
Also, in the above example the ServiceProvider is implemented as an inner
class. This is just a design choice; it's perfectly fine for your component
to implement the ServiceProvider API itself, i.e.
public class MyComponent .. implements Component, ServiceProvider {..}
Todd
>
> Any help would be greatly appreciated,
>
> John
> _______________________________________________
> Cougaar-developers mailing list
> Cougaar-developers at cougaar.org
> http://cougaar.org/mailman/listinfo/cougaar-developers
More information about the Cougaar-developers
mailing list