|
Lightweight Bartering Grid | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectlbg.protocol.data.GridDataManager
public class GridDataManager
Handles GridData. The data manager (hereafter GDM) can host and retrieve
GridData files, and cache them for later use.
It is a merge of the previous separate Peer- and ResourceDataManager,
grouped for convenience and ease of use.
A lot of work was done to make this a normal object instead of a
singleton (a choice that was initially lead by the use of Azureus
as BitTorrent engine). It is now much more convenient to use but could be
subject to some documented side effects.
Related logger: lbg.data
Warnings:
store(GridData, boolean)
Concept of Working Set
,
Warning about use of user.home in conjunction with this class
Field Summary | |
---|---|
static int |
HOST_FTP
Download a GridData and host it on an FTP Server. |
static int |
HOST_TORRENT_SEED
Download a TorrentGridData and act as a new seed. |
static int |
HOST_TORRENT_TRACK
Download a GridData and seed & track it as a TorrentGridData. |
static int |
INFINITE_CAPACITY
Denotes that the GDM cache has an infinite capacity. |
static int |
NUM_SHARE_OPTIONS
Number (counting 1) of elements in a valid GridData download options array. |
static int |
SHARED_FTP
Index of the FTPGridData in the GridData download options array. |
static int |
SHARED_INLINE
Index of the InlineGridData in the GridData download options array. |
static int |
SHARED_TORRENT
Index of the TorrentGridData in the GridData download options array. |
Constructor Summary | |
---|---|
GridDataManager(java.lang.String hostname,
java.lang.String cacheDir,
int btDataPort,
int ftpDataPort,
java.lang.String trackerHost,
int trackerPort,
int capacity,
int sharePreferences,
java.lang.String statsDirectory)
Creates a GridDataManager. |
Method Summary | |
---|---|
boolean |
checkWorkingSet(GridData[] workingSet)
Checks if the passed GridData's would fit in the Working Set. |
GridData |
extract(GridData[] data)
Selects the preferred download option out of an array of possibilities. |
GridData[] |
extract(GridData[][] datas)
Extracts an array of data downloading options. |
java.io.File |
getFile(GridData data)
Store data in cache and get the associated File. Data saved this way is never considered as local. |
java.io.File |
getFile(GridData[] data)
Convenience method to automate extraction of data when downloading |
protected Metadata<java.lang.String> |
getMetadata(java.lang.String id)
|
int |
getSeedCount(java.lang.String dataID,
boolean countPeers)
Get the number of seeds participating in a BitTorrent download for a particular file |
boolean |
hasDefaultPolicy()
Detects whether the manager was configured with a default policy or not. |
static byte[] |
hash(java.io.File f)
Hashes a file using the default algorithm ("SHA-256"). |
static byte[] |
hash(java.io.File f,
java.lang.String algorithm)
Hashes a file using the supplied algorithm. |
static java.lang.String |
hashToString(byte[] hash)
Returns a human-readable representation of a hash. |
GridData[] |
host(GridData data,
boolean isLocal)
Hosts given GridData with default sharing policy. |
GridData[] |
host(GridData data,
boolean isLocal,
int prefs)
Hosts given GridData. |
protected boolean |
isCached(java.lang.String id)
|
void |
restartFTPServer()
Restart the FTP server, forcing reload of the configuration. |
void |
seed(GridData[][] datasOptions)
Ensures that all given data's are being seeded. |
GridDataManager |
setFTPConfig(int port)
Sets the FTP server configuration. If a server is already running, it will need to be restarted for the changes to take effect. |
GridDataManager |
setFTPConfig(int port,
java.lang.String user,
java.lang.String password)
Sets the FTP server configuration. If a server is already running, it will need to be restarted for the changes to take effect. |
boolean |
setWorkingSet(GridData[] workingSet)
Sets the working set. |
boolean |
setWorkingSet(GridData[][] workingSet)
Sets the working set. |
java.lang.String |
toString()
|
void |
waitFor(GridData[] requirements)
Blockingly wait for all required data's to be present in cache. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int INFINITE_CAPACITY
public static final int HOST_TORRENT_SEED
host(GridData, boolean, int)
,
Constant Field Valuespublic static final int HOST_TORRENT_TRACK
HOST_TORRENT_SEED
,
host(GridData, boolean, int)
,
Constant Field Valuespublic static final int HOST_FTP
host(GridData, boolean, int)
,
Constant Field Valuespublic static final int SHARED_TORRENT
public static final int SHARED_FTP
public static final int SHARED_INLINE
public static final int NUM_SHARE_OPTIONS
Constructor Detail |
---|
public GridDataManager(java.lang.String hostname, java.lang.String cacheDir, int btDataPort, int ftpDataPort, java.lang.String trackerHost, int trackerPort, int capacity, int sharePreferences, java.lang.String statsDirectory) throws GridException
hostname
- Hostname of the server or client owning this manager.cacheDir
- Absolute path to cache directory, without
trailing path separator (see java.io.File.separatorChar). The directory
must be created and writable.btDataPort
- The port for TCP and UDP BitTorrent transfers. It
won't be taken into account if an Azureus core was already startedftpDataPort
- Port for FTP data connections. Needed for
FTPGridData downloads.trackerHost
- The BitTorrent tracker host (i.e. the local host).
Set to null for no tracking capabilities.trackerPort
- The desired BitTorrent tracker port. Will be ignored
if trackerHHost is null.capacity
- The number of supplying files this manager can handle.
INFINITE_CAPACITY for no upper bound. See class description for details.sharePreferences
- Flags for setting how data given to host() will
be shared. 0 if you don't plan to host anything.statsDirectory
- Where the cache hit stats should be saved. Set to
null If you don't want to keep track of the stats.
GridException
- If the parameters are set incorrectly.PeerDataConfiguration.buildRcvPolicy(String)
Method Detail |
---|
public static byte[] hash(java.io.File f) throws GridException
f
- The file to hash
GridException
- If hashing cannot be completedGridData.HASHING_ALGORITHM
public static byte[] hash(java.io.File f, java.lang.String algorithm) throws GridException
f
- The file to hashalgorithm
- The desired hashing algorithm, as per
MessageDigest
GridException
- If hashing cannot be completedhash(File)
public static java.lang.String hashToString(byte[] hash)
hash
- The hash to print
public java.lang.String toString()
toString
in class java.lang.Object
public boolean hasDefaultPolicy()
protected boolean isCached(java.lang.String id)
protected Metadata<java.lang.String> getMetadata(java.lang.String id)
public boolean checkWorkingSet(GridData[] workingSet)
workingSet
- The candidate working set
public boolean setWorkingSet(GridData[][] workingSet)
workingSet
- The new working set (an array of download options)
public boolean setWorkingSet(GridData[] workingSet)
A Working Set is a set of GridData's that should stay in cache. When data has to be popped from cache, only those not in the WS will be candidate.
This call is not blocking, data will be transferred in the background.
workingSet
- The new working set (an array of different data's).
Set to null to remove working set management.
public void waitFor(GridData[] requirements) throws GridException
The required data has to be in the current working set. Else, we can't be sure it will ever be downloaded. If the data was already known, the submitted one's local file copy is set.
This method doesn't actually download the data.
requirements
- The data's to wait for
GridException
- if the active working set isn't included in the
previously given working set.setWorkingSet(GridData[])
public GridData[] extract(GridData[][] datas)
datas
- An array of arrays of options for downloading GridData'spublic GridData extract(GridData[] data)
public GridData[] host(GridData data, boolean isLocal, int prefs) throws GridException
Hosting some GridData is saving it locally and rendering it available for other peers / resources using different protocols depending on the flags.
data
- The GridData to host.isLocal
- Whether the data is local to the GDM owner (true)
or from an extern consumer (supplying: false).
The cache policy for local data is to leave data in cache as long as
it's not explicitly deleted. Cache for supplying data is generally
limited.prefs
- Flags for setting how data given to host() should be
shared (overrides the default policy if any).
GridException
- if hosting failed (could be at numerous places
depending on the flags)HOST_TORRENT_SEED
,
HOST_TORRENT_TRACK
,
HOST_FTP
,
SHARED_TORRENT
,
SHARED_FTP
,
extract(GridData[])
public GridData[] host(GridData data, boolean isLocal) throws GridException
GridException
- if the manager doesn't have a default policyhost(GridData, boolean, int)
,
extract(GridData[])
public void seed(GridData[][] datasOptions) throws GridException
datasOptions
- An array of arrays of data transfer options for
data's. In other words, there are datasOptions.length data's to check.
GridException
public int getSeedCount(java.lang.String dataID, boolean countPeers)
dataID
- The identifier of the data to check forcountPeers
- true if the BitTorrent Peers should also be
counted. These could have no interesting pieces right now, but could
become valuable sharers anyway.
public java.io.File getFile(GridData data) throws GridException
host(GridData, boolean)
.
data
- The data to store
GridException
- if the data wasn't successfully downloaded and
stored.public java.io.File getFile(GridData[] data) throws GridException
data
- An array of dowload options for a GridData (not an
array of independent GridData's)
GridException
public void restartFTPServer() throws GridException
GridException
public GridDataManager setFTPConfig(int port) throws GridException
port
- FTP Requests port
GridException
- if the parameters are invalidrestartFTPServer()
public GridDataManager setFTPConfig(int port, java.lang.String user, java.lang.String password) throws GridException
port
- FTP Requests portuser
- User namepassword
- Associated password
GridException
- if the parameters are invalidrestartFTPServer()
|
Lightweight Bartering Grid | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright (c) 2005-2008, Cyril Briquet, parts Xavier Dalem.