Class NodeBase
- java.lang.Object
-
- cz.cvut.fel.sit.omo.foodchain.node.NodeBase
-
- All Implemented Interfaces:
ChannelObserver
- Direct Known Subclasses:
Customer,Distributor,Farmer,Processor,Seller
public class NodeBase extends java.lang.Object implements ChannelObserver
Base class for all nodes
-
-
Field Summary
Fields Modifier and Type Field Description private doubleacceptableDiscountprotected NodeTypeallowedPurchaserprotected NodeTypeallowedSellerprotected static java.util.logging.LoggerLOGGERprivate doublemarginprivate javax.money.MonetaryAmountmoneyprotected NodeTypenodeTypeprivate java.util.List<RefusedRequirement>refusedRequirementsprivate java.util.List<Transaction>refusedTransactionsprotected Storagestorageprivate java.util.Map<ChannelBase,ChannelSubscriptionType>subscribedChannels
-
Method Summary
Modifier and Type Method Description java.lang.Stringaccept(ReportTextVisitor visitor)private voidacceptRequirement(Requirement req)Accepts requirement and creates response.voidaddMoney(javax.money.MonetaryAmount m)Adds amount to current assetsvoidcreateRequirement(TransactionType tType, ProductType pType, java.lang.String pName, javax.money.MonetaryAmount price)Creates requirement based on data provided.voiddoAction(ProductOperationType action)Calls specific ProductOperation on each product in stock (Propagated to its storage).voiddoAction(ProductOperationType action, java.lang.String name)Proceed specific type of action on provided product if its possible.doublegetAcceptableDiscount()NodeTypegetAllowedPurchaser()NodeTypegetAllowedSeller()doublegetMargin()javax.money.MonetaryAmountgetMoney()NodeTypegetNodeType()java.util.List<RefusedRequirement>getRefusedRequirements()java.util.List<Transaction>getRefusedTransactions()StoragegetStorage()java.util.Map<ChannelBase,ChannelSubscriptionType>getSubscribedChannels()private voidreceiveTransaction(FinancialTransaction transaction)Receives (validates) financial transaction.private voidreceiveTransaction(ProductTransaction transaction)Receives (validates) product transaction and sends financial transaction as response.private voidreceiveTransaction(Transaction transaction)Receives (processes) transaction.private voidreceiveTransactions()Receives all transactions for this node.private voidrefuseTransaction(Transaction transaction)Adds transaction to refused transactions.private booleanrequirementCheck(Requirement req)Chacks if product is available or if node has enough moneyprivate booleansenderPurchaserCheck(Requirement req)Checks if requirement creator is an allowed purchaser or allowed seller.private voidsendTransaction(Product product, NodeBase node)Sends product transaction to node through a channel based on its type.private voidsendTransaction(javax.money.MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction)Sends financial transaction to node through a channel based on previous transaction type.voidsubscribeToChannel(ChannelBase channel, ChannelSubscriptionType type)Adds channel to subscribed channels with correct type.voidsubtractMoney(javax.money.MonetaryAmount m)Subtracts amount from current assets.voidtick()Receives transactions and passes tick to storage.private booleantransactionSubscriptionCheck(Requirement requirement, ChannelBase channel)Checks if requirement type corresponds with channel subscription type.voidunsubscribeFromChannel(ChannelBase channel)Removes channel from subscribed channels.voidupdate(Requirement requirement, ChannelBase channel)Chacks incomming requirement.private booleanvalidateSignature(Transaction transaction)Validates signature on transaction.
-
-
-
Field Detail
-
LOGGER
protected static final java.util.logging.Logger LOGGER
-
subscribedChannels
private final java.util.Map<ChannelBase,ChannelSubscriptionType> subscribedChannels
-
refusedTransactions
private final java.util.List<Transaction> refusedTransactions
-
refusedRequirements
private final java.util.List<RefusedRequirement> refusedRequirements
-
money
private javax.money.MonetaryAmount money
-
margin
private final double margin
-
acceptableDiscount
private final double acceptableDiscount
-
storage
protected final Storage storage
-
nodeType
protected NodeType nodeType
-
allowedSeller
protected NodeType allowedSeller
-
allowedPurchaser
protected NodeType allowedPurchaser
-
-
Method Detail
-
sendTransaction
private void sendTransaction(Product product, NodeBase node)
Sends product transaction to node through a channel based on its type.- Parameters:
product- product to be sentnode- receiver node
-
sendTransaction
private void sendTransaction(javax.money.MonetaryAmount amount, NodeBase node, ProductTransaction prevTransaction)Sends financial transaction to node through a channel based on previous transaction type.- Parameters:
amount- amount to be sentnode- receiver nodeprevTransaction- previous product transaction
-
receiveTransaction
private void receiveTransaction(Transaction transaction)
Receives (processes) transaction.- Parameters:
transaction- transaction to be received
-
receiveTransaction
private void receiveTransaction(ProductTransaction transaction)
Receives (validates) product transaction and sends financial transaction as response.- Parameters:
transaction- transaction to be received
-
receiveTransaction
private void receiveTransaction(FinancialTransaction transaction)
Receives (validates) financial transaction.- Parameters:
transaction- transaction to be received
-
refuseTransaction
private void refuseTransaction(Transaction transaction)
Adds transaction to refused transactions.- Parameters:
transaction-
-
receiveTransactions
private void receiveTransactions()
Receives all transactions for this node.
-
createRequirement
public void createRequirement(TransactionType tType, ProductType pType, java.lang.String pName, javax.money.MonetaryAmount price) throws javax.naming.InsufficientResourcesException
Creates requirement based on data provided.- Parameters:
tType- transaction typepType- product typepName- product nameprice- product price- Throws:
javax.naming.InsufficientResourcesException- if product is unavailable
-
acceptRequirement
private void acceptRequirement(Requirement req) throws javax.naming.InsufficientResourcesException
Accepts requirement and creates response.- Parameters:
req- requirement to be accepted- Throws:
javax.naming.InsufficientResourcesException- if product is unavailable
-
subscribeToChannel
public void subscribeToChannel(ChannelBase channel, ChannelSubscriptionType type)
Adds channel to subscribed channels with correct type.- Parameters:
channel- channel to be subscribedtype- type of subscription
-
unsubscribeFromChannel
public void unsubscribeFromChannel(ChannelBase channel)
Removes channel from subscribed channels.- Parameters:
channel-
-
doAction
public void doAction(ProductOperationType action, java.lang.String name) throws java.lang.IllegalCallerException, javax.naming.InsufficientResourcesException
Proceed specific type of action on provided product if its possible. (Propagated to its storage).- Parameters:
action- type of product operation to doname- product to proceed- Throws:
java.lang.IllegalCallerExceptionjavax.naming.InsufficientResourcesException
-
doAction
public void doAction(ProductOperationType action)
Calls specific ProductOperation on each product in stock (Propagated to its storage).- Parameters:
action- ProductOperationType
-
getStorage
public Storage getStorage()
-
getNodeType
public NodeType getNodeType()
-
getMoney
public javax.money.MonetaryAmount getMoney()
-
getSubscribedChannels
public java.util.Map<ChannelBase,ChannelSubscriptionType> getSubscribedChannels()
-
getRefusedTransactions
public java.util.List<Transaction> getRefusedTransactions()
-
getRefusedRequirements
public java.util.List<RefusedRequirement> getRefusedRequirements()
-
getAllowedSeller
public NodeType getAllowedSeller()
-
getAllowedPurchaser
public NodeType getAllowedPurchaser()
-
getMargin
public double getMargin()
-
getAcceptableDiscount
public double getAcceptableDiscount()
-
addMoney
public void addMoney(javax.money.MonetaryAmount m)
Adds amount to current assets- Parameters:
m- amount to be added
-
subtractMoney
public void subtractMoney(javax.money.MonetaryAmount m)
Subtracts amount from current assets.- Parameters:
m- amount to be subtracted
-
validateSignature
private boolean validateSignature(Transaction transaction)
Validates signature on transaction.- Parameters:
transaction- transaction to be validated- Returns:
- signature is valid
-
senderPurchaserCheck
private boolean senderPurchaserCheck(Requirement req)
Checks if requirement creator is an allowed purchaser or allowed seller.- Parameters:
req- requirement- Returns:
- creator is valid
-
transactionSubscriptionCheck
private boolean transactionSubscriptionCheck(Requirement requirement, ChannelBase channel)
Checks if requirement type corresponds with channel subscription type.- Parameters:
requirement- requirementchannel- channel- Returns:
- requirement type corresponds with channel subscription type
-
requirementCheck
private boolean requirementCheck(Requirement req)
Chacks if product is available or if node has enough money- Parameters:
req- requirement- Returns:
- product is available or node has enough money
-
update
public void update(Requirement requirement, ChannelBase channel)
Chacks incomming requirement.- Specified by:
updatein interfaceChannelObserver- Parameters:
requirement- requirementchannel- channel
-
tick
public void tick()
Receives transactions and passes tick to storage.
-
accept
public java.lang.String accept(ReportTextVisitor visitor)
-
-