JXTA

net.jxta.impl.discovery
Class DiscoveryServiceInterface

java.lang.Object
  extended by net.jxta.impl.discovery.DiscoveryServiceInterface
All Implemented Interfaces:
DiscoveryService, Module, Service

public final class DiscoveryServiceInterface
extends Object
implements DiscoveryService

Provides a pure interface object that permits interaction with the actual Discovery Service implementation without giving access to the real object.


Field Summary
 
Fields inherited from interface net.jxta.discovery.DiscoveryService
ADV, DEFAULT_EXPIRATION, DEFAULT_LIFETIME, GROUP, INFINITE_LIFETIME, NO_EXPIRATION, PEER
 
Fields inherited from interface net.jxta.platform.Module
START_AGAIN_PROGRESS, START_AGAIN_STALLED, START_DISABLED, START_OK
 
Constructor Summary
protected DiscoveryServiceInterface(DiscoveryServiceImpl theRealThing)
          Only authorized constructor
 
Method Summary
 void addDiscoveryListener(DiscoveryListener listener)
          Register a Discovery listener.
 void flushAdvertisement(Advertisement adv)
          Removes the specified Advertisement from the cache of locally stored Advertisements.
 void flushAdvertisements(String id, int type)
          Removes the specified Advertisement from the cache of locally stored Advertisements.
 long getAdvExpirationTime(Advertisement adv)
          Returns the maximum duration in milliseconds for which this document will be cached by peers other than the publisher.
 long getAdvExpirationTime(ID id, int type)
          Returns the maximum duration in milliseconds for which this document will be cached by peers other than the publisher.
 long getAdvLifeTime(Advertisement adv)
          Returns the maximum duration in milliseconds for which this document should be kept in local cache.
 long getAdvLifeTime(ID id, int type)
          Returns the maximum duration in milliseconds for which this document should be kept in local cache.
 Advertisement getImplAdvertisement()
          Returns the advertisement for this service.
 Service getInterface()
          Service objects are not manipulated directly to protect usage of the service.
 Enumeration getLocalAdvertisements(int type, String attribute, String value)
          Retrieve locally stored Advertisements.
 int getRemoteAdvertisements(String peer, int type, String attribute, String value, int threshold)
          Discover advertisements from remote peers.
 int getRemoteAdvertisements(String peer, int type, String attribute, String value, int threshold, DiscoveryListener listener)
          Discover advertisements from remote peers.
 void init(PeerGroup pg, ID assignedID, Advertisement impl)
          Initialize the module, passing it its peer group and advertisement.
 void publish(Advertisement adv)
          Publish an Advertisement.
 void publish(Advertisement adv, long lifetime, long expiration)
          Publish an Advertisement.
 void remotePublish(Advertisement adv)
          Publish an Advertisement via propagation to other peers on the network.
 void remotePublish(Advertisement adv, long expiration)
          Publish an Advertisement via propagation to other peers on the network.
 void remotePublish(String peerid, Advertisement adv)
          Publish an Advertisement to another peer on the network.
 void remotePublish(String peerid, Advertisement adv, long expiration)
          Publish an Advertisement to another peer on the network.
 boolean removeDiscoveryListener(DiscoveryListener listener)
          Remove a Discovery listener which was previously registered with getRemoteAdvertisements() or addDiscoveryListener().
 int startApp(String[] arg)
          Complete any remaining initialization of the module.
 void stopApp()
          Stop a module.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiscoveryServiceInterface

protected DiscoveryServiceInterface(DiscoveryServiceImpl theRealThing)
Only authorized constructor

Parameters:
theRealThing - The actual discovery implementation
Method Detail

getInterface

public Service getInterface()
Service objects are not manipulated directly to protect usage of the service. A Service interface is returned to access the service methods.

Specified by:
getInterface in interface Service
Returns:
Service public interface of the service

getImplAdvertisement

public Advertisement getImplAdvertisement()
Returns the advertisement for this service.

Specified by:
getImplAdvertisement in interface Service
Returns:
Advertisement the advertisement. This is always a ModuleImplAdvertisement.

init

public void init(PeerGroup pg,
                 ID assignedID,
                 Advertisement impl)
Initialize the module, passing it its peer group and advertisement.

Note: when subclassing one of the existing PeerGroup implementations (which implement Module), it may not be recommended to overload the init method. See the documentation of the PeerGroup class being subclassed.

FIXME: This is meaningless for the interface object; it is there only to satisfy the requirements of the interface that we implement. Ultimately, the API should define two levels of interfaces: one for the real service implementation and one for the interface object. Right now it feels a bit heavy to so that since the only different between the two would be init() and may-be getName().

Specified by:
init in interface Module
Parameters:
pg - The PeerGroup from which this Module can obtain services. If this module is a Service, this is also the PeerGroup of which this module is a service.
assignedID - Identity of Module within group. modules can use it as a the root of their namespace to create names that are unique within the group but predictable by the same module on another peer. This is normally the ModuleClassID which is also the name under which the module is known by other modules. For a group it is the PeerGroupID itself. The parameters of a service, in the Peer configuration, are indexed by the assignedID of that service, and a Service must publish its run-time parameters in the Peer Advertisement under its assigned ID.
impl - The implementation advertisement for this Module. It is permissible to pass null if no implementation advertisement is available. This may happen if the implementation was selected by explicit class name rather than by following an implementation advertisement. Modules are not required to support that style of loading, but if they do, then their documentation should mention it.

startApp

public int startApp(String[] arg)
Complete any remaining initialization of the module. The module should be fully functional after startApp() is completed. That is also the opportunity to supply arbitrary arguments (mostly to applications).

If this module is a PeerGroup service, it may be invoked several times depending on its return value.

This is here for temporary class hierarchy reasons. it is ALWAYS ignored. By definition, the interface object protects the real object's start/stop methods from being called

Specified by:
startApp in interface Module
Parameters:
arg - An array of Strings forming the parameters for this Module.
Returns:
int A status indication which may be one of Module.START_OK, Module.START_AGAIN_PROGRESS, Module.START_AGAIN_STALLED, which indicates partial or complete success, or any other value (negative values are recommended for future compatibility), which indicates failure.

stopApp

public void stopApp()
Stop a module. This may be called any time after init() completes and should not assume that startApp() has been called or completed.

The Module cannot be forced to comply, but in the future we might be able to deny it access to anything after some timeout.

This is here for temporary class hierarchy reasons. it is ALWAYS ignored. By definition, the interface object protects the real object's start/stop methods from being called

This request is currently ignored.

Specified by:
stopApp in interface Module

getRemoteAdvertisements

public int getRemoteAdvertisements(String peer,
                                   int type,
                                   String attribute,
                                   String value,
                                   int threshold)
Discover advertisements from remote peers. This does not normally provide an exhaustive search. Instead it provides a "best efforts" search which will provide a selection of advertisements of matching the search criteria. The selection of advertisements returned may be random or predictable depending upon the network configuration and no particular behaviour should be assumed. In general the narrower the query specified the more exhaustive the responses will be.

Discovery can be performed in two ways :

The scope of advertisements returned can be narrowed by specifying an attribute and value pair. The attribute is a case-sensitive string matching the name of an Advertisement XML tag who's values will be matched by the value. Only a limited number of Advertisement XML fields are indexed. Advertisement.getIndexFields() will return the fields on which you may query for a particular Advertisement type.

The value is a case-insensitive string who's value is matched against the values of attribute fields of Advertisements. The value may be of several forms :

Specified by:
getRemoteAdvertisements in interface DiscoveryService
Parameters:
peer - If provided the query will be forwarded to the specified peer. If null then the query will be propagated through the network to peers with matching Advertisements.
type - Discovery type; PEER, GROUP or ADV.
attribute - indexed element name (see advertisement(s) for a list of indexed fields. A null attribute indicates any advertisement of specified type
value - value of attribute to narrow discovery to. Valid values for this parameter are null (don't care), Exact value, or use of wild card(s) (e.g. if a Advertisement defines FooBar , a value of "*bar", "foo*", or "*ooB*", will return the Advertisement
threshold - The maximum number of matching advertisements which be returned by each responding peer. A threshold of 0, and type of PEER has a special behaviour.
Returns:
query ID for this discovery query.

getRemoteAdvertisements

public int getRemoteAdvertisements(String peer,
                                   int type,
                                   String attribute,
                                   String value,
                                   int threshold,
                                   DiscoveryListener listener)
Discover advertisements from remote peers. This does not normally provide an exhaustive search. Instead it provides a "best efforts" search which will provide a selection of advertisements of matching the search criteria. The selection of advertisements returned may be random or predictable depending upon the network configuration and no particular behaviour should be assumed. In general the narrower the query specified the more exhaustive the responses will be.

Discovery can be performed in two ways :

The scope of advertisements returned can be narrowed by specifying an attribute and value pair. The attribute is a case-sensitive string matching the name of an Advertisement XML tag who's values will be matched by the value. Only a limited number of Advertisement XML fields are indexed. Advertisement.getIndexFields() will return the fields on which you may query for a particular Advertisement type.

The value is a case-insensitive string who's value is matched against the values of attribute fields of Advertisements. The value may be of several forms :

Specified by:
getRemoteAdvertisements in interface DiscoveryService
Parameters:
peer - If provided the query will be forwarded to the specified peer. If null then the query will be propagated through the network to peers with matching Advertisements.
type - Discovery type; PEER, GROUP or ADV.
attribute - indexed element name (see Advertisement(s) for a list of indexed fields. A null attribute indicates any advertisement of specified type
value - value of attribute to narrow discovery to. Valid values for this parameter or null (don't care), Exact value, or use of wild card(s) (e.g. if a Advertisement defines FooBar, a value of "*bar", "foo*", or "*ooB*", will return the Advertisement
threshold - The maximum number of matching advertisements which be returned by each responding peer. A threshold of 0, and type of PEER has a special behaviour.
listener - The listener which will be called when advertisement which match this query are discovered or null if no callback is desired.
Returns:
query ID for this discovery query.

getLocalAdvertisements

public Enumeration getLocalAdvertisements(int type,
                                          String attribute,
                                          String value)
                                   throws IOException
Retrieve locally stored Advertisements. This is an exhaustive search of the locally cached results. All valid known matching results will be returned.

Specified by:
getLocalAdvertisements in interface DiscoveryService
Parameters:
type - Discovery type; PEER, GROUP or ADV.
attribute - indexed element name (see Advertisement(s) for a list of indexed fields. null indicates any advertisement of specified type
value - value of attribute to narrow discovery to valid values for this parameter are null (don't care), Exact value, or use of wild card(s) (e.g. if a Advertisement defines FooBar , a value of "*bar", "foo*", or "*ooB*", will return the Advertisement
Returns:
Enumeration of stored advertisements.
Throws:
IOException - Thrown if an error occurs during retrieval.

flushAdvertisement

public void flushAdvertisement(Advertisement adv)
                        throws IOException
Removes the specified Advertisement from the cache of locally stored Advertisements.

Specified by:
flushAdvertisement in interface DiscoveryService
Parameters:
adv - Advertisement to remove.
Throws:
IOException - If there is a problem removing the advertisement.

flushAdvertisements

public void flushAdvertisements(String id,
                                int type)
                         throws IOException
Removes the specified Advertisement from the cache of locally stored Advertisements.

Specified by:
flushAdvertisements in interface DiscoveryService
Parameters:
id - The Advertisement.getID() value of the Advertisement to be removed.
type - Discovery type PEER, GROUP, ADV.
Throws:
IOException - If there is a problem removing the advertisement.

getAdvExpirationTime

public long getAdvExpirationTime(ID id,
                                 int type)
Returns the maximum duration in milliseconds for which this document will be cached by peers other than the publisher. This value is either the stored lifetime or the remaining lifetime of the document, whichever is less.

Specified by:
getAdvExpirationTime in interface DiscoveryService
Parameters:
id - Document ID, Peer ID, or PeerGroup ID
type - Discovery type PEER, GROUP, ADV
Returns:
The number of milliseconds that other peers will be told to retain this Advertisement in their local caches. -1 is returned if the Advertisement is not known or already expired.

getAdvLifeTime

public long getAdvLifeTime(ID id,
                           int type)
Returns the maximum duration in milliseconds for which this document should be kept in local cache.

Specified by:
getAdvLifeTime in interface DiscoveryService
Parameters:
id - Document ID, Peer ID, or PeerGroup ID
type - Discovery type PEER, GROUP, ADV
Returns:
The number of milliseconds this Advertisement will remain in the local cache unless refreshed before that time. -1 is returned if the Advertisement is not known or already expired.

getAdvExpirationTime

public long getAdvExpirationTime(Advertisement adv)
Returns the maximum duration in milliseconds for which this document will be cached by peers other than the publisher. This value is either the stored lifetime or the remaining lifetime of the document, whichever is less.

Specified by:
getAdvExpirationTime in interface DiscoveryService
Parameters:
adv - Advertisement
Returns:
The number of milliseconds that other peers will be told to retain this Advertisement in their local caches. -1 is returned if the Advertisement is not known or already expired.

getAdvLifeTime

public long getAdvLifeTime(Advertisement adv)
Returns the maximum duration in milliseconds for which this document should be kept in local cache.

Specified by:
getAdvLifeTime in interface DiscoveryService
Parameters:
adv - Advertisement
Returns:
The number of milliseconds this Advertisement will remain in the local cache unless refreshed before that time. -1 is returned if the Advertisement is not known or already expired.

publish

public void publish(Advertisement adv)
             throws IOException
Publish an Advertisement. The Advertisement will expire automatically on the local peer after DEFAULT_LIFETIME and will expire on other peers after DEFAULT_EXPIRATION.

When an Advertisement is published, it is stored, and indexed in the peer's local cache. The Advertisement indexes are also shared with Rendezvous peers. Advertisement indexes may not be shared with other peers immediately, but may be updated as part of a periodic process. The Discovery Service currently publishes index updates every 30 seconds.

Specified by:
publish in interface DiscoveryService
Parameters:
adv - The Advertisement to publish.
Throws:
IOException - When an error occurs during Advertisement publication.

publish

public void publish(Advertisement adv,
                    long lifetime,
                    long expiration)
             throws IOException
Publish an Advertisement. The Advertisement will expire automatically after the specified time. A peer that discovers this advertisement will hold it for about expiration or lifetime milliseconds, whichever is smaller.

When an Advertisement is published, it is stored, and indexed in the peer's local cache. The Advertisement indexes are also shared with Rendezvous peers. Advertisement indexes may not be shared with other peers immediately, but may be updated as part of a periodic process. The Discovery Service currently publishes index updates every 30 seconds.

Specified by:
publish in interface DiscoveryService
Parameters:
adv - The Advertisement to publish.
lifetime - Duration in relative milliseconds that this advertisement will exist.
expiration - Duration in relative milliseconds that this advertisement will be cached by other peers.
Throws:
IOException - When an error occurs during Advertisement publication.

remotePublish

public void remotePublish(Advertisement adv)
Publish an Advertisement via propagation to other peers on the network. This does not result in the advertisement being stored locally. The Advertisement will be published with an expiration time of DEFAULT_EXPIRATION.

Specified by:
remotePublish in interface DiscoveryService
Parameters:
adv - Advertisement to publish.

remotePublish

public void remotePublish(Advertisement adv,
                          long expiration)
Publish an Advertisement via propagation to other peers on the network. This does not result in the advertisement being stored locally.

Specified by:
remotePublish in interface DiscoveryService
Parameters:
adv - The Advertisement to publish.
expiration - Duration in relative milliseconds that this Advertisement will be cached by other peers.

remotePublish

public void remotePublish(String peerid,
                          Advertisement adv)
Publish an Advertisement to another peer on the network. This does not result in the advertisement being stored locally. The Advertisement will be published with an expiration time of DEFAULT_EXPIRATION.

Specified by:
remotePublish in interface DiscoveryService
Parameters:
peerid - The ID of a peer, specifying null results in propagation within the group.
adv - The Advertisement to publish.

remotePublish

public void remotePublish(String peerid,
                          Advertisement adv,
                          long expiration)
Publish an Advertisement to another peer on the network. This does not result in the advertisement being stored locally.

Specified by:
remotePublish in interface DiscoveryService
Parameters:
peerid - id of a peer, specifying null results in a propagate within the group
adv - The Advertisement to publish.
expiration - Duration in relative milliseconds that this Advertisement will be cached by other peers.

addDiscoveryListener

public void addDiscoveryListener(DiscoveryListener listener)
Register a Discovery listener. The Discovery listener will be called whenever Advertisement responses are received from remote peers by the Discovery Service.

Specified by:
addDiscoveryListener in interface DiscoveryService
Parameters:
listener - the DiscoveryListener

removeDiscoveryListener

public boolean removeDiscoveryListener(DiscoveryListener listener)
Remove a Discovery listener which was previously registered with getRemoteAdvertisements() or addDiscoveryListener().

Specified by:
removeDiscoveryListener in interface DiscoveryService
Parameters:
listener - The listener to be removed.
Returns:
true if the listener was successfully removed, false otherwise

JXSE