org.ourgrid.peer.community.nof
Class AccountingManager

java.lang.Object
  extended by org.ourgrid.peer.community.nof.AccountingManager

public class AccountingManager
extends java.lang.Object

Stores the reputation of all known peers. It coordinates the peer's strategies for calculating and maintaining his knowledge about the other peer's reputations. Each peer maintains a map with a balance for each other known peer calculated as a function of the past bilateral interactions between this and each other known peer.

Each peer accounts both for each favor that it makes and each favor that is made to him.


Field Summary
static double DEFAULT_UNKNOWN_PEER_BALANCE
          The default value of the reputation of an unknown peer.
static double FACTOR
          This factor is a timer scale.
 
Method Summary
protected  double calculateNewBalance(ObjectID peerID, double usedTime, boolean isProviding)
          Calculates the new balance based on the use of a given GuM.
 double consumerAccount(double usedTime, double currentProviderBalance)
           Calculates the new balance given the used time of a GridMachine and the current balance of a provider peer.
 double finishAccounting(GumID gumID, long requestID, boolean isProviding)
           
 double finishAccounting(GumID gumID, ObjectID peerID, boolean isProviding)
           
static AccountingManager getInstance()
          Recover the unique instance of this entity.
 java.util.Map<ObjectID,java.lang.Double> getKnownPeerBalances()
           Gets a Map containing all the known peer balances.
 java.util.List<java.util.Map.Entry<ObjectID,java.lang.Double>> getKnownPeers()
           
 double getPeerBalance(ObjectID peerID)
           Gets the balance of a peer represented by its ID.
 ObjectID getPeerIdForRequest(long requestID)
          Gets the peer's id of the peer that make the request identified by the given requestID
protected  boolean isAKnownPeer(ObjectID peerID)
           Verifies if the peer is a known one.
 void loadRankingFromFile(java.lang.String fileName)
           Loads the ranking from a file.
 double providerAccount(double usedTime, double currentConsumerBalance)
           Calculates the new balance given the grid machine used time and the current balance of a consumer peer.
 void registerPeerIdForRequest(long requestID, ObjectID peerID)
          Associates an requestID with the peer's id of the peer that has made that request.
 void removeRequest(long requestID)
          Removed a request from the
static void reset()
          Reseting the know instance of AccountManager.
 void saveRankingInFile(java.lang.String fileName)
           Saves the current ranking to a file.
 void startAccounting(GumID gumID, long requestID)
           
 void startAccounting(GumID gumID, ObjectID peerID)
           Informs, with accounting purpose, that the usage of a gum has started.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_UNKNOWN_PEER_BALANCE

public static final double DEFAULT_UNKNOWN_PEER_BALANCE
The default value of the reputation of an unknown peer.

See Also:
Constant Field Values

FACTOR

public static final double FACTOR
This factor is a timer scale.

See Also:
Constant Field Values
Method Detail

startAccounting

public void startAccounting(GumID gumID,
                            long requestID)

startAccounting

public void startAccounting(GumID gumID,
                            ObjectID peerID)

Informs, with accounting purpose, that the usage of a gum has started. Note that it is used both when providing or receiving a machine.

Parameters:
gumID - The internet name of GridMachine just being used.
peerID - The peer's ID that is receiving or providing the machine.

finishAccounting

public double finishAccounting(GumID gumID,
                               long requestID,
                               boolean isProviding)

finishAccounting

public double finishAccounting(GumID gumID,
                               ObjectID peerID,
                               boolean isProviding)

calculateNewBalance

protected double calculateNewBalance(ObjectID peerID,
                                     double usedTime,
                                     boolean isProviding)
Calculates the new balance based on the use of a given GuM.

Parameters:
usedTime - The used time of this GuM.
peerID - The peer that is using or providind this GuM.
isProviding - If the peer is providing the GuM.
Returns:
The new balance based on the use of a given GuM.

getPeerBalance

public double getPeerBalance(ObjectID peerID)

Gets the balance of a peer represented by its ID.

Parameters:
peerID - The ID of peer whose balance is wished.
Returns:
The balance of the peer reprented by its ID

getKnownPeers

public java.util.List<java.util.Map.Entry<ObjectID,java.lang.Double>> getKnownPeers()

isAKnownPeer

protected boolean isAKnownPeer(ObjectID peerID)

Verifies if the peer is a known one.

Parameters:
peerID - The peer is
Returns:
true if the peer is known, false otherwise.

getKnownPeerBalances

public java.util.Map<ObjectID,java.lang.Double> getKnownPeerBalances()

Gets a Map containing all the known peer balances.

Returns:
A Map containing all the known peer balances.

saveRankingInFile

public void saveRankingInFile(java.lang.String fileName)
                       throws java.io.IOException

Saves the current ranking to a file. Currently, this file is written as a Properties file.

Parameters:
fileName - The name of the file to write into.
Throws:
java.io.IOException - If any problem happens while writing the file.

loadRankingFromFile

public void loadRankingFromFile(java.lang.String fileName)

Loads the ranking from a file.

Parameters:
fileName - The name of the file to read from.

getPeerIdForRequest

public ObjectID getPeerIdForRequest(long requestID)
Gets the peer's id of the peer that make the request identified by the given requestID

Parameters:
requestID - The request ID.
Returns:
the peerID or null if the request isn't associate with any peerID.

registerPeerIdForRequest

public void registerPeerIdForRequest(long requestID,
                                     ObjectID peerID)
Associates an requestID with the peer's id of the peer that has made that request. If already exists an peerID associate with the given requestId, nothing is done.

Parameters:
requestID - The unique identifier for the request.
peerID - the unique identifier for the peer that makes the request.

removeRequest

public void removeRequest(long requestID)
Removed a request from the

Parameters:
requestID -

getInstance

public static AccountingManager getInstance()
Recover the unique instance of this entity.

Returns:
Single AccountingManager instance.

providerAccount

public double providerAccount(double usedTime,
                              double currentConsumerBalance)
                       throws java.lang.IllegalArgumentException

Calculates the new balance given the grid machine used time and the current balance of a consumer peer. Only positives balance are allowed. That is, if the new consumer balance will not be lower than 0 (zero). The new balance is obtained from currentConsumerBalance - usedTime / SimpleAccontant.FACTOR.

Parameters:
usedTime - The time a GridMachine was used.
currentConsumerBalance - The current balance of a peer
Returns:
The new balance of the peer.
Throws:
java.lang.IllegalArgumentException - Thrown when the given used time is negative.

consumerAccount

public double consumerAccount(double usedTime,
                              double currentProviderBalance)
                       throws java.lang.IllegalArgumentException

Calculates the new balance given the used time of a GridMachine and the current balance of a provider peer. The new balance is obtained from currentConsumerBalance + usedTime / SimpleAccontant.FACTOR.

Parameters:
usedTime - The time a GridMachine was used.
currentProviderBalance - The current balance of a peer
Returns:
The new balance of the peer.
Throws:
java.lang.IllegalArgumentException - Thrown when the given used time is not positive.

reset

public static void reset()
Reseting the know instance of AccountManager.