Skip to content

Commit

Permalink
Merge pull request #1133 from alvasw/move_transport_config_to_network…
Browse files Browse the repository at this point in the history
…_common_module

Move Transport.Config to network.common module
  • Loading branch information
alvasw authored Aug 20, 2023
2 parents 2b1f1f2 + 8eb63cf commit 37bf450
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 26 deletions.
7 changes: 7 additions & 0 deletions network/common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id 'bisq.java-library'
}

dependencies {
implementation libs.google.guava
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.network.common;

import java.nio.file.Path;

public interface TransportConfig {
Path getDataDir();

int getSocketTimeout();
}
3 changes: 2 additions & 1 deletion network/network/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ dependencies {
implementation 'bisq:security'
implementation 'bisq:persistence'

implementation 'tor:tor'
implementation project(':common')
implementation project(':i2p')
implementation 'tor:tor'

implementation libs.failsafe
implementation libs.google.guava
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bisq.network;

import bisq.common.util.ConfigUtil;
import bisq.network.common.TransportConfig;
import bisq.network.p2p.ServiceNode;
import bisq.network.p2p.node.Address;
import bisq.network.p2p.node.transport.ClearNetTransport;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static NetworkServiceConfig from(Path baseDir, Config config) {
}*/


Map<Transport.Type, Transport.Config> configByTransportType = createConfigByTransportType(config, baseDir);
Map<Transport.Type, TransportConfig> configByTransportType = createConfigByTransportType(config, baseDir);


return new NetworkServiceConfig(baseDir.toAbsolutePath().toString(),
Expand Down Expand Up @@ -119,15 +120,15 @@ private static Map<Transport.Type, Integer> createDefaultNodePortByTransportType
return map;
}

private static Map<Transport.Type, Transport.Config> createConfigByTransportType(Config config, Path baseDir) {
Map<Transport.Type, Transport.Config> map = new HashMap<>();
private static Map<Transport.Type, TransportConfig> createConfigByTransportType(Config config, Path baseDir) {
Map<Transport.Type, TransportConfig> map = new HashMap<>();
map.put(Transport.Type.CLEAR, createTransportConfig(Transport.Type.CLEAR, config, baseDir));
map.put(Transport.Type.TOR, createTransportConfig(Transport.Type.TOR, config, baseDir));
map.put(Transport.Type.I2P, createTransportConfig(Transport.Type.I2P, config, baseDir));
return map;
}

private static Transport.Config createTransportConfig(Transport.Type type, Config config, Path baseDir) {
private static TransportConfig createTransportConfig(Transport.Type type, Config config, Path baseDir) {
Config transportConfig = config.getConfig("configByTransportType." + type.name().toLowerCase());
Path dataDir;
switch (type) {
Expand Down Expand Up @@ -170,7 +171,7 @@ private static Set<Address> getSeedAddresses(Transport.Type transportType, Confi

private final String baseDir;
private final Set<Transport.Type> supportedTransportTypes;
private final Map<Transport.Type, Transport.Config> configByTransportType;
private final Map<Transport.Type, TransportConfig> configByTransportType;
private final ServiceNode.Config serviceNodeConfig;
private final Map<Transport.Type, PeerGroupService.Config> peerGroupServiceConfigByTransport;
private final Map<Transport.Type, Integer> defaultNodePortByTransportType;
Expand All @@ -179,7 +180,7 @@ private static Set<Address> getSeedAddresses(Transport.Type transportType, Confi

public NetworkServiceConfig(String baseDir,
Set<Transport.Type> supportedTransportTypes,
Map<Transport.Type, Transport.Config> configByTransportType,
Map<Transport.Type, TransportConfig> configByTransportType,
ServiceNode.Config serviceNodeConfig,
Map<Transport.Type, PeerGroupService.Config> peerGroupServiceConfigByTransport,
Map<Transport.Type, Integer> defaultNodePortByTransportType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.common.util.CompletableFutureUtils;
import bisq.network.NetworkId;
import bisq.network.NetworkService;
import bisq.network.common.TransportConfig;
import bisq.network.p2p.message.NetworkMessage;
import bisq.network.p2p.node.Address;
import bisq.network.p2p.node.Connection;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class ServiceNodesByTransport {
private final Map<Transport.Type, ServiceNode> map = new ConcurrentHashMap<>();
private final Set<Transport.Type> supportedTransportTypes;

public ServiceNodesByTransport(Map<Transport.Type, Transport.Config> configByTransportType,
public ServiceNodesByTransport(Map<Transport.Type, TransportConfig> configByTransportType,
Set<Transport.Type> supportedTransportTypes,
ServiceNode.Config serviceNodeConfig,
Map<Transport.Type, PeerGroupService.Config> peerGroupServiceConfigByTransport,
Expand All @@ -74,7 +75,7 @@ public ServiceNodesByTransport(Map<Transport.Type, Transport.Config> configByTra
ProofOfWorkService proofOfWorkService) {
this.supportedTransportTypes = supportedTransportTypes;
supportedTransportTypes.forEach(transportType -> {
Transport.Config transportConfig = configByTransportType.get(transportType);
TransportConfig transportConfig = configByTransportType.get(transportType);

Node.Config nodeConfig = new Node.Config(transportType,
supportedTransportTypes,
Expand Down
7 changes: 4 additions & 3 deletions network/network/src/main/java/bisq/network/p2p/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.common.util.NetworkUtils;
import bisq.common.util.StringUtils;
import bisq.network.NetworkService;
import bisq.network.common.TransportConfig;
import bisq.network.p2p.message.NetworkMessage;
import bisq.network.p2p.node.authorization.AuthorizationService;
import bisq.network.p2p.node.authorization.AuthorizationToken;
Expand Down Expand Up @@ -104,13 +105,13 @@ public static final class Config {
private final Transport.Type transportType;
private final Set<Transport.Type> supportedTransportTypes;
private final AuthorizationService authorizationService;
private final Transport.Config transportConfig;
private final TransportConfig transportConfig;
private final int socketTimeout;

public Config(Transport.Type transportType,
Set<Transport.Type> supportedTransportTypes,
AuthorizationService authorizationService,
Transport.Config transportConfig,
TransportConfig transportConfig,
int socketTimeout) {
this.transportType = transportType;
this.supportedTransportTypes = supportedTransportTypes;
Expand Down Expand Up @@ -598,7 +599,7 @@ private void handleException(Throwable exception) {
}
}

private Transport getTransport(Transport.Type transportType, Transport.Config config) {
private Transport getTransport(Transport.Type transportType, TransportConfig config) {
switch (transportType) {
case TOR:
return new TorTransport(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bisq.network.p2p.node.transport;

import bisq.network.common.TransportConfig;
import bisq.network.p2p.node.Address;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -22,7 +23,7 @@ public class ClearNetTransport implements Transport {
@Getter
@ToString
@EqualsAndHashCode
public static final class Config implements Transport.Config {
public static final class Config implements TransportConfig {
public static Config from(Path dataDir, com.typesafe.config.Config config) {
return new Config(dataDir, (int) TimeUnit.SECONDS.toMillis(config.getInt("socketTimeout")));
}
Expand All @@ -36,10 +37,10 @@ public Config(Path dataDir, int socketTimeout) {
}
}

private final Transport.Config config;
private final TransportConfig config;
private boolean initializeCalled;

public ClearNetTransport(Transport.Config config) {
public ClearNetTransport(TransportConfig config) {
this.config = config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import bisq.i2p.I2pClient;
import bisq.i2p.I2pEmbeddedRouter;
import bisq.network.NetworkService;
import bisq.network.common.TransportConfig;
import bisq.network.p2p.node.Address;
import bisq.network.p2p.node.ConnectionException;
import lombok.EqualsAndHashCode;
Expand All @@ -24,7 +25,7 @@ public class I2PTransport implements Transport {
@Getter
@ToString
@EqualsAndHashCode
public static final class Config implements Transport.Config {
public static final class Config implements TransportConfig {
public static Config from(Path dataDir, com.typesafe.config.Config config) {
return new Config(dataDir,
(int) TimeUnit.SECONDS.toMillis(config.getInt("socketTimeout")),
Expand Down Expand Up @@ -69,7 +70,7 @@ public Config(Path dataDir, int socketTimeout, int inboundKBytesPerSecond, int o

private I2PTransport.Config config;

public I2PTransport(Transport.Config config) {
public I2PTransport(TransportConfig config) {
// Demonstrate potential usage of specific config.
// Would be likely passed to i2p router not handled here...

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bisq.network.p2p.node.transport;

import bisq.network.NetworkService;
import bisq.network.common.TransportConfig;
import bisq.network.p2p.node.Address;
import bisq.network.p2p.node.ConnectionException;
import bisq.tor.DirectoryAuthority;
Expand Down Expand Up @@ -34,7 +35,7 @@ public class TorTransport implements Transport {
@Getter
@ToString
@EqualsAndHashCode
public static final class Config implements Transport.Config {
public static final class Config implements TransportConfig {
public static Config from(Path dataDir, com.typesafe.config.Config config) {
return new Config(
dataDir, (int) TimeUnit.SECONDS.toMillis(config.getInt("socketTimeout")), config.getBoolean("testNetwork"),
Expand Down Expand Up @@ -96,7 +97,7 @@ public Config(Path dataDir, int socketTimeout, boolean isTestNetwork,

private final TorService torService;

public TorTransport(Transport.Config config) {
public TorTransport(TransportConfig config) {
Path torDirPath = config.getDataDir();
torService = new TorService(NetworkService.NETWORK_IO_POOL, torDirPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

Expand All @@ -53,12 +52,6 @@ public static Type from(Address address) {
}
}

interface Config {
Path getDataDir();

int getSocketTimeout();
}

@Getter
@ToString
@EqualsAndHashCode
Expand Down
1 change: 1 addition & 0 deletions network/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencyResolutionManagement {
includeBuild('..')
includeBuild('tor')

include 'common'
include 'i2p'
include 'network'
include 'socks5-socket-channel'
Expand Down

0 comments on commit 37bf450

Please sign in to comment.