Class DrasylNode
- java.lang.Object
-
- org.drasyl.node.DrasylNode
-
- Direct Known Subclasses:
BehavioralDrasylNode
public abstract class DrasylNode extends Object
Represents a node in the drasyl Overlay Network. Applications that want to run on drasyl must implement this class.Example usage:
DrasylNode node = new DrasylNode() { @Override public void onEvent(Event event) { // handle incoming events (messages) here System.out.println("Event received: " + event); } }; node.start(); // wait till NodeOnlineEvent has been received // send message to another node node.send("0229041b273dd5ee1c2bef2d77ae17dbd00d2f0a2e939e22d42ef1c4bf05147ea9", "Hello World"); // shutdown node node.shutdown();
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
DrasylNode()
Creates a new drasyl Node.protected
DrasylNode(Identity identity, io.netty.bootstrap.ServerBootstrap bootstrap, io.netty.channel.ChannelFuture channelFuture)
protected
DrasylNode(DrasylConfig config)
Creates a new drasyl Node with the givenconfig
.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static Identity
generateIdentity(DrasylConfig config)
Generates an identity or uses the already generated identity from the givenconfig
.Identity
identity()
Returns theIdentity
of this node.abstract void
onEvent(Event event)
Sendsevent
to the application and tells it information about the local node, other peers, connections or incoming messages.io.netty.channel.ChannelPipeline
pipeline()
Returns theChannelPipeline
to allow users to add own handlers.CompletionStage<io.netty.channel.Channel>
resolve(String address)
Creates a future containing aChannel
for communication withaddress
.CompletionStage<io.netty.channel.Channel>
resolve(DrasylAddress address)
Creates a future containing aChannel
for communication withaddress
.CompletionStage<Void>
send(String recipient, Object payload)
Sends the content ofpayload
to the identityrecipient
.CompletionStage<Void>
send(DrasylAddress recipient, Object payload)
Sends the content ofpayload
to the identityrecipient
.CompletionStage<Void>
shutdown()
Shut the drasyl node down.CompletionStage<Void>
start()
Start the drasyl node.
-
-
-
Field Detail
-
identity
protected final Identity identity
-
bootstrap
protected final io.netty.bootstrap.ServerBootstrap bootstrap
-
-
Constructor Detail
-
DrasylNode
protected DrasylNode(Identity identity, io.netty.bootstrap.ServerBootstrap bootstrap, io.netty.channel.ChannelFuture channelFuture)
-
DrasylNode
protected DrasylNode(DrasylConfig config) throws DrasylException
Creates a new drasyl Node with the givenconfig
. The node is only being created, it neither connects to the overlay network, nor can send or receive messages. To do this you have to callstart()
.Note: This is a blocking method, because when a node is started for the first time, its identity must be created. This can take up to a minute because of the proof of work.
- Parameters:
config
- custom configuration used for this node- Throws:
NullPointerException
- ifconfig
isnull
DrasylException
- if identity could not be loaded or created
-
DrasylNode
protected DrasylNode() throws DrasylException
Creates a new drasyl Node. The node is only being created, it neither connects to the Overlay Network, nor can send or receive messages. To do this you have to callstart()
.Note: This is a blocking method, because when a node is started for the first time, its identity must be created. This can take up to a minute because of the proof of work.
- Throws:
DrasylException
- if identity could not be loaded or createdDrasylConfigException
- if config is invalid
-
-
Method Detail
-
generateIdentity
public static Identity generateIdentity(DrasylConfig config) throws DrasylException
Generates an identity or uses the already generated identity from the givenconfig
.- Parameters:
config
- custom configuration used for this identity- Returns:
- generated or already present identity
- Throws:
DrasylException
-
onEvent
public abstract void onEvent(@NonNull Event event)
Sendsevent
to the application and tells it information about the local node, other peers, connections or incoming messages.- Parameters:
event
- the event
-
send
@NonNull public CompletionStage<Void> send(@NonNull String recipient, @Nullable Object payload)
Sends the content ofpayload
to the identityrecipient
. Returns a failed future with aIllegalStateException
if the message could not be sent to the recipient or a super peer. Important: Just because the future did not fail does not automatically mean that the message could be delivered. Delivery confirmations must be implemented by the application.Note: It is possible that the passed object cannot be serialized. In this case it is not sent and the future is fulfilled with an exception. Serializable objects can be added on start via the
DrasylConfig
.- Parameters:
recipient
- the recipient of a message as compressed public keypayload
- the payload of a message- Returns:
- a completion stage if the message was successfully processed, otherwise an exceptionally completion stage
- Since:
- 0.1.3
- See Also:
MessageSerializer
-
send
@NonNull public CompletionStage<Void> send(@NonNull DrasylAddress recipient, @Nullable Object payload)
Sends the content ofpayload
to the identityrecipient
. Returns a failed future with aIllegalStateException
if the message could not be sent to the recipient or a super peer. Important: Just because the future did not fail does not automatically mean that the message could be delivered. Delivery confirmations must be implemented by the application.Note: It is possible that the passed object cannot be serialized. In this case it is not sent and the future is fulfilled with an exception. Serializable objects can be added on start via the
DrasylConfig
.- Parameters:
recipient
- the recipient of a messagepayload
- the payload of a message- Returns:
- a completion stage if the message was successfully processed, otherwise an exceptionally completion stage
- Since:
- 0.1.3
- See Also:
MessageSerializer
-
resolve
@NonNull public CompletionStage<io.netty.channel.Channel> resolve(@NonNull DrasylAddress address)
Creates a future containing aChannel
for communication withaddress
.Note: be aware that the returned channel can be closed on inactivity according to
DrasylConfig.getChannelInactivityTimeout()
. A closed channel can no longer be used. However, a new channel can be created via this method.- Parameters:
address
- peer address used forChannel
creation- Returns:
- future containing
Channel
foraddress
on completion
-
resolve
@NonNull public CompletionStage<io.netty.channel.Channel> resolve(@NonNull String address)
Creates a future containing aChannel
for communication withaddress
.Note: be aware that the returned channel can be closed on inactivity according to
DrasylConfig.getChannelInactivityTimeout()
. A closed channel can no longer be used. However, a new channel can be created via this method.- Parameters:
address
- peer address used forChannel
creation- Returns:
- future containing
Channel
foraddress
on completion
-
shutdown
@NonNull public CompletionStage<Void> shutdown()
Shut the drasyl node down.If there is a connection to a Super Peer, our node will deregister from that Super Peer.
If the local server has been started, it will now be stopped.
This method does not stop the shared threads. To kill the shared threads, you have to call the
DrasylNodeSharedEventLoopGroupHolder.shutdown()
method.- Returns:
- this method returns a future, which complements if all shutdown steps have been completed.
-
start
@NonNull public CompletionStage<Void> start()
Start the drasyl node.First, the identity of the node is loaded. If none exists, a new one is generated.
If activated, a local server is started. This allows other nodes to discover our node.
If a super peer has been configured, a client is started which connects to this super peer. Our node uses the Super Peer to discover and communicate with other nodes.
- Returns:
- this method returns a future, which complements if all components necessary for the operation have been started.
-
pipeline
@Nullable public io.netty.channel.ChannelPipeline pipeline()
Returns theChannelPipeline
to allow users to add own handlers.- Returns:
- the pipeline
-
-