From bd82695d2ddbca65e0a18d9771a69aabc6c8bde7 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 2 Aug 2017 22:12:40 +0200 Subject: [PATCH] Made changes to the StorageSystem (IOExceptions, default serialization manager, etc). Minor refactoring. --- src/dorkbox/network/Configuration.java | 4 +- src/dorkbox/network/connection/EndPoint.java | 4 +- .../network/connection/PropertyStore.java | 21 ++- src/dorkbox/network/store/DB_Server.java | 138 ++++++++++++++++++ .../{util => }/store/NullSettingsStore.java | 14 +- .../{util => }/store/SettingsStore.java | 13 +- .../connection/PropertyStoreAccessTest.java | 15 +- 7 files changed, 169 insertions(+), 40 deletions(-) create mode 100644 src/dorkbox/network/store/DB_Server.java rename src/dorkbox/network/{util => }/store/NullSettingsStore.java (95%) rename src/dorkbox/network/{util => }/store/SettingsStore.java (99%) diff --git a/src/dorkbox/network/Configuration.java b/src/dorkbox/network/Configuration.java index 5814696a..a9a596d6 100644 --- a/src/dorkbox/network/Configuration.java +++ b/src/dorkbox/network/Configuration.java @@ -15,10 +15,10 @@ */ package dorkbox.network; -import dorkbox.network.util.store.SettingsStore; - import java.util.concurrent.Executor; +import dorkbox.network.store.SettingsStore; + public class Configuration { /** diff --git a/src/dorkbox/network/connection/EndPoint.java b/src/dorkbox/network/connection/EndPoint.java index f821a265..41986be5 100644 --- a/src/dorkbox/network/connection/EndPoint.java +++ b/src/dorkbox/network/connection/EndPoint.java @@ -43,9 +43,9 @@ import dorkbox.network.connection.wrapper.ChannelWrapper; import dorkbox.network.pipeline.KryoEncoder; import dorkbox.network.pipeline.KryoEncoderCrypto; import dorkbox.network.rmi.RmiBridge; +import dorkbox.network.store.NullSettingsStore; +import dorkbox.network.store.SettingsStore; import dorkbox.network.util.CryptoSerializationManager; -import dorkbox.network.util.store.NullSettingsStore; -import dorkbox.network.util.store.SettingsStore; import dorkbox.util.OS; import dorkbox.util.Property; import dorkbox.util.crypto.CryptoECC; diff --git a/src/dorkbox/network/connection/PropertyStore.java b/src/dorkbox/network/connection/PropertyStore.java index 188b7af3..1e02f1ac 100644 --- a/src/dorkbox/network/connection/PropertyStore.java +++ b/src/dorkbox/network/connection/PropertyStore.java @@ -15,7 +15,6 @@ */ package dorkbox.network.connection; -import java.io.IOException; import java.security.SecureRandom; import java.util.HashMap; import java.util.Map; @@ -23,11 +22,10 @@ import java.util.Map; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; -import dorkbox.network.util.store.SettingsStore; +import dorkbox.network.store.DB_Server; +import dorkbox.network.store.SettingsStore; import dorkbox.util.SerializationManager; import dorkbox.util.bytes.ByteArrayWrapper; -import dorkbox.util.database.DB_Server; -import dorkbox.util.database.DatabaseStorage; import dorkbox.util.exceptions.SecurityException; import dorkbox.util.storage.Storage; import dorkbox.util.storage.StorageSystem; @@ -38,7 +36,6 @@ import dorkbox.util.storage.StorageSystem; */ final class PropertyStore extends SettingsStore { - private Storage storage; private Map servers; @@ -52,7 +49,7 @@ class PropertyStore extends SettingsStore { */ @Override public - void init(final SerializationManager serializationManager, final Storage ignored) throws IOException { + void init(final SerializationManager serializationManager, final Storage ignored) { // make sure our custom types are registered // only register if not ALREADY initialized, since we can initialize in the server and in the client. This creates problems if // running inside the same JVM (we don't permit it) @@ -63,9 +60,9 @@ class PropertyStore extends SettingsStore { } this.storage = StorageSystem.Memory() - .make(); + .build(); - servers = this.storage.getAndPut(DatabaseStorage.SERVERS, new HashMap(16)); + servers = this.storage.getAndPut(DB_Server.STORAGE_KEY, new HashMap(16)); DB_Server localServer = servers.get(DB_Server.IP_SELF); // this will always be null and is here to help people that copy/paste code if (localServer == null) { @@ -73,7 +70,7 @@ class PropertyStore extends SettingsStore { servers.put(DB_Server.IP_SELF, localServer); // have to always specify what we are saving - this.storage.putAndSave(DatabaseStorage.SERVERS, servers); + this.storage.putAndSave(DB_Server.STORAGE_KEY, servers); } } @@ -101,7 +98,7 @@ class PropertyStore extends SettingsStore { .setPrivateKey(serverPrivateKey); // have to always specify what we are saving - storage.putAndSave(DatabaseStorage.SERVERS, servers); + storage.putAndSave(DB_Server.STORAGE_KEY, servers); } /** @@ -128,7 +125,7 @@ class PropertyStore extends SettingsStore { .setPublicKey(serverPublicKey); // have to always specify what we are saving - storage.putAndSave(DatabaseStorage.SERVERS, servers); + storage.putAndSave(DB_Server.STORAGE_KEY, servers); } /** @@ -153,7 +150,7 @@ class PropertyStore extends SettingsStore { localServer.setSalt(bytes); // have to always specify what we are saving - storage.putAndSave(DatabaseStorage.SERVERS, servers); + storage.putAndSave(DB_Server.STORAGE_KEY, servers); } return salt; diff --git a/src/dorkbox/network/store/DB_Server.java b/src/dorkbox/network/store/DB_Server.java new file mode 100644 index 00000000..4debe16a --- /dev/null +++ b/src/dorkbox/network/store/DB_Server.java @@ -0,0 +1,138 @@ +/* + * Copyright 2014 dorkbox, llc + * + * 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 dorkbox.network.store; + +import java.util.Arrays; + +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; + +import dorkbox.util.bytes.ByteArrayWrapper; +import dorkbox.util.storage.StorageKey; + +public +class DB_Server { + /** + * The storage key used to save all server connections + */ + public static final StorageKey STORAGE_KEY = new StorageKey("servers"); + + /** + * Address 0.0.0.0/32 may be used as a source address for this host on this network. + */ + public static final ByteArrayWrapper IP_SELF = ByteArrayWrapper.wrap(new byte[] {0, 0, 0, 0}); + + // salt + IP address is used for equals! + private byte[] ipAddress; + private byte[] salt; + + private ECPrivateKeyParameters privateKey; + private ECPublicKeyParameters publicKey; + + // must have empty constructor + public + DB_Server() { + } + + public + byte[] getAddress() { + if (this.ipAddress == null) { + return null; + } + return this.ipAddress; + } + + public + void setAddress(byte[] ipAddress) { + this.ipAddress = ipAddress; + } + + public + byte[] getSalt() { + return this.salt; + } + + public + void setSalt(byte[] salt) { + this.salt = salt; + } + + + public + ECPrivateKeyParameters getPrivateKey() { + return this.privateKey; + } + + public + void setPrivateKey(ECPrivateKeyParameters privateKey) { + this.privateKey = privateKey; + } + + + public + ECPublicKeyParameters getPublicKey() { + return this.publicKey; + } + + public + void setPublicKey(ECPublicKeyParameters publicKey) { + this.publicKey = publicKey; + } + + @Override + public + int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (this.ipAddress == null ? 0 : Arrays.hashCode(this.ipAddress)); + return result; + } + + @Override + public + boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + DB_Server other = (DB_Server) obj; + if (this.ipAddress == null) { + if (other.ipAddress != null) { + return false; + } + } + else if (!Arrays.equals(this.ipAddress, other.ipAddress)) { + return false; + } + return Arrays.equals(this.salt, other.salt); + } + + @Override + public + String toString() { + byte[] bytes = this.ipAddress; + if (bytes != null) { + return "DB_Server " + Arrays.toString(bytes); + } + + return "DB_Server [no-ip-set]"; + } +} diff --git a/src/dorkbox/network/util/store/NullSettingsStore.java b/src/dorkbox/network/store/NullSettingsStore.java similarity index 95% rename from src/dorkbox/network/util/store/NullSettingsStore.java rename to src/dorkbox/network/store/NullSettingsStore.java index bc7e7ca9..656c2332 100644 --- a/src/dorkbox/network/util/store/NullSettingsStore.java +++ b/src/dorkbox/network/store/NullSettingsStore.java @@ -13,16 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.network.util.store; +package dorkbox.network.store; + +import java.security.SecureRandom; + +import org.bouncycastle.crypto.params.ECPrivateKeyParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; import dorkbox.util.SerializationManager; import dorkbox.util.exceptions.SecurityException; import dorkbox.util.storage.Storage; -import org.bouncycastle.crypto.params.ECPrivateKeyParameters; -import org.bouncycastle.crypto.params.ECPublicKeyParameters; - -import java.io.IOException; -import java.security.SecureRandom; public class NullSettingsStore extends SettingsStore { @@ -31,7 +31,7 @@ class NullSettingsStore extends SettingsStore { @Override public - void init(final SerializationManager serializationManager, final Storage storage) throws IOException { + void init(final SerializationManager serializationManager, final Storage storage) { } diff --git a/src/dorkbox/network/util/store/SettingsStore.java b/src/dorkbox/network/store/SettingsStore.java similarity index 99% rename from src/dorkbox/network/util/store/SettingsStore.java rename to src/dorkbox/network/store/SettingsStore.java index 69ab59bb..3c742c73 100644 --- a/src/dorkbox/network/util/store/SettingsStore.java +++ b/src/dorkbox/network/store/SettingsStore.java @@ -13,18 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dorkbox.network.util.store; +package dorkbox.network.store; -import dorkbox.util.OS; -import dorkbox.util.SerializationManager; -import dorkbox.util.exceptions.SecurityException; -import dorkbox.util.storage.Storage; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; +import dorkbox.util.OS; +import dorkbox.util.SerializationManager; +import dorkbox.util.exceptions.SecurityException; +import dorkbox.util.storage.Storage; /** * This class provides a way for the network stack to use the server's database, instead of a property file (which it uses when stand-alone) @@ -39,7 +38,7 @@ class SettingsStore { * Initialize the settingsStore with the provided serialization manager. */ public abstract - void init(SerializationManager serializationManager, Storage storage) throws IOException; + void init(SerializationManager serializationManager, Storage storage); private static String getCallingClass() { diff --git a/test/dorkbox/network/connection/PropertyStoreAccessTest.java b/test/dorkbox/network/connection/PropertyStoreAccessTest.java index fc0ee473..2b1028cd 100644 --- a/test/dorkbox/network/connection/PropertyStoreAccessTest.java +++ b/test/dorkbox/network/connection/PropertyStoreAccessTest.java @@ -16,13 +16,12 @@ package dorkbox.network.connection; -import dorkbox.network.BaseTest; -import dorkbox.util.exceptions.SecurityException; +import static org.junit.Assert.fail; + import org.junit.Test; -import java.io.IOException; - -import static org.junit.Assert.fail; +import dorkbox.network.BaseTest; +import dorkbox.util.exceptions.SecurityException; public @@ -32,11 +31,7 @@ class PropertyStoreAccessTest extends BaseTest { public void testAccess() throws SecurityException { PropertyStore store = new PropertyStore(); - try { - store.init(null, null); - } catch (IOException e) { - e.printStackTrace(); - } + store.init(null, null); System.out.println(); System.out.println();