Skip to content

Commit

Permalink
Reorganized and refactored the io-hotmoka-node-remote module
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Dec 29, 2023
1 parent 2312557 commit 6abbeb9
Show file tree
Hide file tree
Showing 41 changed files with 556 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import io.hotmoka.node.local.api.LocalNodeConfigBuilder;

/**
* The configuration of a node.
* The configuration of a local node.
*
* @param <C> the concrete type of the configuration
* @param <B> the concrete type of the builder
Expand Down Expand Up @@ -153,19 +153,22 @@ public String toToml() {

@Override
public boolean equals(Object other) {
if (other != null && getClass() == other.getClass()) {
var otherConfig = (LocalNodeConfigImpl<?,?>) other;
if (other instanceof LocalNodeConfigImpl<?,?> otherConfig && getClass() == other.getClass())
return dir.equals(otherConfig.dir) &&
maxPollingAttempts == otherConfig.maxPollingAttempts &&
pollingDelay == otherConfig.pollingDelay &&
requestCacheSize == otherConfig.requestCacheSize &&
responseCacheSize == otherConfig.responseCacheSize &&
maxGasPerViewTransaction.equals(otherConfig.maxGasPerViewTransaction);
}
else
return false;
}

@Override
public int hashCode() {
return dir.hashCode() ^ Long.hashCode(maxPollingAttempts) ^ maxGasPerViewTransaction.hashCode();
}

@Override
public String toString() {
return toToml();
Expand Down
9 changes: 7 additions & 2 deletions io-hotmoka-remote/pom.xml → io-hotmoka-node-remote/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>io-hotmoka-remote</artifactId>
<artifactId>io-hotmoka-node-remote</artifactId>
<packaging>jar</packaging>
<name>io-hotmoka-remote</name>
<name>io-hotmoka-node-remote</name>
<version>${hotmoka.version}</version>

<dependencies>
Expand All @@ -36,6 +36,11 @@
<artifactId>io-hotmoka-ws-client</artifactId>
<version>${hotmoka.version}</version>
</dependency>
<dependency>
<groupId>io.hotmoka</groupId>
<artifactId>toml4j</artifactId>
<version>0.7.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2021 Dinu Berinde and Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.remote;

import io.hotmoka.annotations.ThreadSafe;
import io.hotmoka.node.api.Node;

/**
* A node that forwards its calls to a remote network service.
*/
@ThreadSafe
public interface RemoteNode extends Node {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2023 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.remote;

import io.hotmoka.annotations.Immutable;

/**
* The configuration of a node that forwards all its calls to a remote network service.
*/
@Immutable
public interface RemoteNodeConfig {

/**
* Yields the URL of the remote service, without the protocol. This defaults
* to {@code localhost:8080}.
*
* @return the URL
*/
String getURL();

/**
* Determines if web sockets should be used for the connection. This defaults to false.
*
* @return true if and only if web sockets should be used for the connection
*/
boolean usesWebSockets();

/**
* Yields a TOML representation of this configuration.
*
* @return the TOML representation, as a string
*/
String toToml();

@Override
boolean equals(Object other);

@Override
String toString();

/**
* Yields a builder initialized with the information in this object.
*
* @return the builder
*/
RemoteNodeConfigBuilder toBuilder();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2023 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.remote;

/**
* The builder of a configuration object.
*/
public interface RemoteNodeConfigBuilder {

/**
* Specifies the URL of the remote service, without the protocol.
* The default is {@code localhost:8080}.
*
* @param url the url
* @return this same builder
*/
RemoteNodeConfigBuilder setURL(String url);

/**
* Sets the use of websockets.
*
* @param usesWebSockets true if and only if websockets should be used
* instead of http connections. This defaults to false
* @return this same builder
*/
RemoteNodeConfigBuilder usesWebSockets(boolean usesWebSockets);

/**
* Builds the configuration from this builder.
*
* @return the configuration
*/
RemoteNodeConfig build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2023 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.hotmoka.remote;

import java.io.FileNotFoundException;
import java.nio.file.Path;

import com.moandjiezana.toml.Toml;

import io.hotmoka.remote.internal.RemoteNodeConfigImpl.RemoteNodeConfigBuilderImpl;

/**
* Providers of configuration object builders for a remote node.
*/
public abstract class RemoteNodeConfigBuilders {

private RemoteNodeConfigBuilders() {}

/**
* Creates a builder containing default data.
*
* @return the builder
*/
public static RemoteNodeConfigBuilder defaults() {
return new RemoteNodeConfigBuilderImpl();
}

/**
* Creates a builder from the given TOML configuration file.
* The resulting builder will contain the information in the file,
* and use defaults for the data not contained in the file.
*
* @param path the path to the TOML file
* @return the builder
* @throws FileNotFoundException if {@code path} cannot be found
*/
public static RemoteNodeConfigBuilder load(Path path) throws FileNotFoundException {
return new RemoteNodeConfigBuilderImpl(path);
}

/**
* Creates a builder by reading the properties of the given TOML file and
* setting them for the corresponding fields of the builder.
*
* @param toml the file
* @return the builder
*/
public static RemoteNodeConfigBuilder from(Toml toml) {
return new RemoteNodeConfigBuilderImpl(toml);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 Dinu Berinde and Fausto Spoto
Copyright 2023 Fausto Spoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,25 +19,26 @@
import java.io.IOException;

import io.hotmoka.annotations.ThreadSafe;
import io.hotmoka.node.api.Node;
import io.hotmoka.remote.internal.http.HTTPRemoteNodeImpl;
import io.hotmoka.remote.internal.websockets.WebSocketsRemoteNodeImpl;

/**
* A node that forwards its calls to a remote network service.
* Providers of nodes that forward their calls to a remote network service.
*/
@ThreadSafe
public interface RemoteNode extends Node {
public abstract class RemoteNodes {

/**
private RemoteNodes() {}

/**
* Yields a remote node with the given configuration.
*
* @param config the configuration
* @return the remote node
* @throws IOException
*/
static RemoteNode of(RemoteNodeConfig config) throws IOException {
public static RemoteNode of(RemoteNodeConfig config) throws IOException {
// there are two implementations: for websockets or for http connections
return config.webSockets ? new WebSocketsRemoteNodeImpl(config) : new HTTPRemoteNodeImpl(config);
return config.usesWebSockets() ? new WebSocketsRemoteNodeImpl(config) : new HTTPRemoteNodeImpl(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public abstract class AbstractRemoteNode extends AbstractNode implements RemoteN
protected AbstractRemoteNode(RemoteNodeConfig config) throws IOException {
this.config = config;
try {
this.webSocketClient = new WebSocketClient("ws://" + config.url + "/node");
this.webSocketClient = new WebSocketClient("ws://" + config.getURL() + "/node");
}
catch (WebSocketException e) {
throw new IOException(e);
Expand Down
Loading

0 comments on commit 6abbeb9

Please sign in to comment.