Module org.refcodes.p2p
Package org.refcodes.p2p
Class AbstractPeer<LOCATOR,HEADER extends P2PHeader<LOCATOR>,TAIL extends P2PTail<LOCATOR>,MSG extends P2PMessage<LOCATOR,HEADER,?,TAIL>,CONSUMER extends P2PMessageConsumer<MSG,PEER>,PEER extends Peer<LOCATOR,HEADER,TAIL,MSG,ROUTER>,ROUTER extends PeerRouter<LOCATOR,HEADER,TAIL,MSG>>
java.lang.Object
org.refcodes.p2p.AbstractPeer<LOCATOR,HEADER,TAIL,MSG,CONSUMER,PEER,ROUTER>
- Type Parameters:
ROUTER
- ThePeerRouter
type to use when routingP2PMessage
instances.LOCATOR
- Defines the type of the locators identifying a peer.HEADER
- TheP2PHeader
defines the static attributes addressed for the target of theP2PMessage
(a header might be signed as it is not modified during dispatch).TAIL
- TheP2PTail
describes the dynamic attributes required during dispatch of aP2PMessage
(a tail is modified during dispatch by having the visited hops appended).MSG
- The (sub-)type of theP2PMessage
being processed by the accordingPeer
(sub-)type.
- All Implemented Interfaces:
org.refcodes.mixin.LocatorAccessor<LOCATOR>
,Peer<LOCATOR,
,HEADER, TAIL, MSG, ROUTER> PeerRouter<LOCATOR,
,HEADER, TAIL, MSG> Terminal<LOCATOR>
public abstract class AbstractPeer<LOCATOR,HEADER extends P2PHeader<LOCATOR>,TAIL extends P2PTail<LOCATOR>,MSG extends P2PMessage<LOCATOR,HEADER,?,TAIL>,CONSUMER extends P2PMessageConsumer<MSG,PEER>,PEER extends Peer<LOCATOR,HEADER,TAIL,MSG,ROUTER>,ROUTER extends PeerRouter<LOCATOR,HEADER,TAIL,MSG>>
extends Object
implements Peer<LOCATOR,HEADER,TAIL,MSG,ROUTER>
The
AbstractPeer
class implements the Peer
interface and is a
concrete implementation of a physical Peer.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.refcodes.mixin.LocatorAccessor
org.refcodes.mixin.LocatorAccessor.LocatorBuilder<T extends Object,
B extends org.refcodes.mixin.LocatorAccessor.LocatorBuilder<T, B>>, org.refcodes.mixin.LocatorAccessor.LocatorMutator<T extends Object>, org.refcodes.mixin.LocatorAccessor.LocatorProperty<T extends Object> -
Field Summary
-
Constructor Summary
ConstructorDescriptionAbstractPeer
(LOCATOR aLocator, CONSUMER aMessageConsumer) Constructs thePeer
instance with the given initial state. -
Method Summary
Modifier and TypeMethodDescriptionboolean
addPeerRouter
(ROUTER aPeerRouter) Adds aPeerRouter
to the list of the knownPeerRouter
instances.int
getHopCount
(LOCATOR aDestination, LOCATOR[] aTrail) Determines the (nearest) hop count to the given peer.Retrieves all known (attached)PeerRouter
instances.boolean
removePeerRouter
(ROUTER aPeerRouter) Removes aPeerRouter
from the list of the knownPeerRouter
instances.void
sendMessage
(MSG aMessage) Passes a message to the given peer.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.refcodes.p2p.Peer
sendMessage
Methods inherited from interface org.refcodes.p2p.PeerRouter
getHopCount
-
Field Details
-
_locator
-
_peerRouter
-
_messageConsumer
-
-
Constructor Details
-
AbstractPeer
Constructs thePeer
instance with the given initial state. TheP2PMessageConsumer
is your business logic to be invoked upon receivel of a message.- Parameters:
aLocator
- The LOCATOR of thePeer
being constructed. Messages targeted to thisPeer
will be addressed to the given LOCATOR.aMessageConsumer
- The consumer (being the functionalP2PMessageConsumer
interface) of theP2PMessage
instances targeted at thisPeer
instance.
-
-
Method Details
-
getLocator
- Specified by:
getLocator
in interfaceorg.refcodes.mixin.LocatorAccessor<LOCATOR>
-
getHopCount
Determines the (nearest) hop count to the given peer.- Specified by:
getHopCount
in interfacePeerRouter<LOCATOR,
HEADER extends P2PHeader<LOCATOR>, TAIL extends P2PTail<LOCATOR>, MSG extends P2PMessage<LOCATOR, HEADER, ?, TAIL>> - Parameters:
aDestination
- The unique ID of the peer for which to get the hop count.aTrail
- The trail of peers already passed (in order to avoid turning circles when determining the hop count).- Returns:
- The according hop count or -1 if the targeted peer is unreachable.
- Throws:
IOException
- thrown in case I/O problems occurred while accessing the mesh.
-
peerRouters
Retrieves all known (attached)PeerRouter
instances.- Specified by:
peerRouters
in interfacePeer<LOCATOR,
HEADER extends P2PHeader<LOCATOR>, TAIL extends P2PTail<LOCATOR>, MSG extends P2PMessage<LOCATOR, HEADER, ?, TAIL>, CONSUMER extends P2PMessageConsumer<MSG, PEER>> - Returns:
- The known (attached)
PeerRouter
instances.
-
addPeerRouter
Adds aPeerRouter
to the list of the knownPeerRouter
instances.- Specified by:
addPeerRouter
in interfacePeer<LOCATOR,
HEADER extends P2PHeader<LOCATOR>, TAIL extends P2PTail<LOCATOR>, MSG extends P2PMessage<LOCATOR, HEADER, ?, TAIL>, CONSUMER extends P2PMessageConsumer<MSG, PEER>> - Parameters:
aPeerRouter
- ThePeerRouter
being added.- Returns:
- False in case this
PeerRouter
has already been added.
-
removePeerRouter
Removes aPeerRouter
from the list of the knownPeerRouter
instances.- Specified by:
removePeerRouter
in interfacePeer<LOCATOR,
HEADER extends P2PHeader<LOCATOR>, TAIL extends P2PTail<LOCATOR>, MSG extends P2PMessage<LOCATOR, HEADER, ?, TAIL>, CONSUMER extends P2PMessageConsumer<MSG, PEER>> - Parameters:
aPeerRouter
- ThePeerRouter
to be removed.- Returns:
- False in case this
PeerRouter
has not been added (or already been removed).
-
sendMessage
Passes a message to the given peer.- Specified by:
sendMessage
in interfacePeerRouter<LOCATOR,
HEADER extends P2PHeader<LOCATOR>, TAIL extends P2PTail<LOCATOR>, MSG extends P2PMessage<LOCATOR, HEADER, ?, TAIL>> - Parameters:
aMessage
- The message to be passed.- Throws:
NoSuchDestinationException
- thrown in case there is none such destination peer.IOException
- thrown in case I/O problems occurred while accessing the mesh.
-
toString
-