From 4dd639e6515c41630a21dcbf8fe5289d4c33adea Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 24 Feb 2016 22:51:35 +0100 Subject: [PATCH] Code cleanup --- .../bitmessage/BaseMessageCallback.java | 47 +++++++++++++++++++ .../dissem/bitmessage/BitmessageContext.java | 18 +------ .../bitmessage/DefaultMessageListener.java | 5 +- .../ch/dissem/bitmessage/InternalContext.java | 5 +- .../bitmessage/entity/NetworkMessage.java | 3 +- .../bitmessage/entity/ObjectMessage.java | 3 -- .../entity/valueobject/NetworkAddress.java | 3 +- .../exception/ApplicationException.java | 26 ++++++++++ .../bitmessage/entity/SerializationTest.java | 1 + .../bitmessage/security/CryptographyTest.java | 8 +++- .../bitmessage/networking/Connection.java | 2 +- .../networking/DefaultNetworkHandler.java | 2 + .../networking/NetworkHandlerTest.java | 16 +++---- .../bitmessage/repository/JdbcInventory.java | 5 +- 14 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 core/src/main/java/ch/dissem/bitmessage/BaseMessageCallback.java create mode 100644 core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java diff --git a/core/src/main/java/ch/dissem/bitmessage/BaseMessageCallback.java b/core/src/main/java/ch/dissem/bitmessage/BaseMessageCallback.java new file mode 100644 index 0000000..bf46b74 --- /dev/null +++ b/core/src/main/java/ch/dissem/bitmessage/BaseMessageCallback.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016 Christian Basler + * + * 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 ch.dissem.bitmessage; + +import ch.dissem.bitmessage.entity.payload.ObjectPayload; +import ch.dissem.bitmessage.entity.valueobject.InventoryVector; + +/** + * Default implementation that doesn't do anything. + * + * @author Christian Basler + */ +public class BaseMessageCallback implements MessageCallback { + @Override + public void proofOfWorkStarted(ObjectPayload message) { + // No op + } + + @Override + public void proofOfWorkCompleted(ObjectPayload message) { + // No op + } + + @Override + public void messageOffered(ObjectPayload message, InventoryVector iv) { + // No op + } + + @Override + public void messageAcknowledged(InventoryVector iv) { + // No op + } +} diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 24a4aaf..94cf3c3 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -410,23 +410,7 @@ public class BitmessageContext { proofOfWorkEngine = new MultiThreadedPOWEngine(); } if (messageCallback == null) { - messageCallback = new MessageCallback() { - @Override - public void proofOfWorkStarted(ObjectPayload message) { - } - - @Override - public void proofOfWorkCompleted(ObjectPayload message) { - } - - @Override - public void messageOffered(ObjectPayload message, InventoryVector iv) { - } - - @Override - public void messageAcknowledged(InventoryVector iv) { - } - }; + messageCallback = new BaseMessageCallback(); } if (customCommandHandler == null) { customCommandHandler = new CustomCommandHandler() { diff --git a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index b2366e4..dcc62b9 100644 --- a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -65,6 +65,9 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { receive(object, (Broadcast) payload); break; } + default: { + throw new IllegalArgumentException("Unknown payload type " + payload.getType()); + } } } @@ -96,7 +99,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { } } - private void updatePubkey(BitmessageAddress address, Pubkey pubkey){ + private void updatePubkey(BitmessageAddress address, Pubkey pubkey) { address.setPubkey(pubkey); LOG.info("Got pubkey for contact " + address); ctx.getAddressRepository().save(address); diff --git a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java index bfd4f30..33ee15b 100644 --- a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java @@ -20,6 +20,7 @@ import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.Encrypted; import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.payload.*; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.TTL; @@ -187,7 +188,7 @@ public class InternalContext { messageCallback.proofOfWorkStarted(payload); proofOfWorkService.doProofOfWork(to, object); } catch (IOException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } @@ -206,7 +207,7 @@ public class InternalContext { // TODO: remember that the pubkey is just about to be sent, and on which stream! proofOfWorkService.doProofOfWork(response); } catch (IOException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java index 8790d3a..5e8552e 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/NetworkMessage.java @@ -16,6 +16,7 @@ package ch.dissem.bitmessage.entity; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.utils.Encode; import java.io.ByteArrayOutputStream; @@ -84,7 +85,7 @@ public class NetworkMessage implements Streamable { try { out.write(getChecksum(payloadBytes)); } catch (GeneralSecurityException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } // message payload diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java index 99b3aec..3425c63 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java @@ -191,9 +191,6 @@ public class ObjectMessage implements MessagePayload { private long streamNumber; private ObjectPayload payload; - public Builder() { - } - public Builder nonce(byte[] nonce) { this.nonce = nonce; return this; diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java index 794ae44..da99350 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java @@ -17,6 +17,7 @@ package ch.dissem.bitmessage.entity.valueobject; import ch.dissem.bitmessage.entity.Streamable; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.utils.Encode; import ch.dissem.bitmessage.utils.UnixTime; @@ -85,7 +86,7 @@ public class NetworkAddress implements Streamable { try { return InetAddress.getByAddress(ipv6); } catch (UnknownHostException e) { - throw new RuntimeException(e); + throw new ApplicationException(e); } } diff --git a/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java b/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java new file mode 100644 index 0000000..998284d --- /dev/null +++ b/core/src/main/java/ch/dissem/bitmessage/exception/ApplicationException.java @@ -0,0 +1,26 @@ +/* + * Copyright 2016 Christian Basler + * + * 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 ch.dissem.bitmessage.exception; + +/** + * @author Christian Basler + */ +public class ApplicationException extends RuntimeException { + public ApplicationException(Throwable cause) { + super(cause); + } +} diff --git a/core/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java b/core/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java index 1bcb8e7..5d8777a 100644 --- a/core/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java @@ -111,6 +111,7 @@ public class SerializationTest extends TestBase { before.write(out); NetworkMessage after = Factory.getNetworkMessage(3, new ByteArrayInputStream(out.toByteArray())); + assertNotNull(after); Inv invAfter = (Inv) after.getPayload(); assertEquals(ivs, invAfter.getInventory()); } diff --git a/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java b/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java index ef3fcbf..3e5695c 100644 --- a/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java +++ b/cryptography-bc/src/test/java/ch/dissem/bitmessage/security/CryptographyTest.java @@ -5,6 +5,7 @@ import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.payload.GenericPayload; import ch.dissem.bitmessage.entity.valueobject.PrivateKey; +import ch.dissem.bitmessage.exception.InsufficientProofOfWorkException; import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine; import ch.dissem.bitmessage.ports.ProofOfWorkEngine; import ch.dissem.bitmessage.utils.CallbackWaiter; @@ -22,6 +23,7 @@ import static ch.dissem.bitmessage.utils.UnixTime.MINUTE; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -102,7 +104,11 @@ public class CryptographyTest { } }); objectMessage.setNonce(waiter.waitForValue()); - crypto.checkProofOfWork(objectMessage, 1000, 1000); + try { + crypto.checkProofOfWork(objectMessage, 1000, 1000); + } catch (InsufficientProofOfWorkException e) { + fail(e.getMessage()); + } } @Test diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index 54bfee4..fef1d32 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -114,7 +114,7 @@ class Connection { public static Connection sync(InternalContext ctx, InetAddress address, int port, MessageListener listener, long timeoutInSeconds) throws IOException { - return new Connection(ctx, Mode.SYNC, listener, new Socket(address, port), + return new Connection(ctx, SYNC, listener, new Socket(address, port), new HashSet(), new HashSet(), new NetworkAddress.Builder().ip(address).port(port).stream(1).build(), diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java b/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java index d3bec17..f69af2c 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/DefaultNetworkHandler.java @@ -171,6 +171,8 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder { case ACTIVE: active++; break; + default: + // nothing to do } } } diff --git a/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java b/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java index a45ec47..2e63e3a 100644 --- a/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java +++ b/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java @@ -52,30 +52,30 @@ public class NetworkHandlerTest { public static void setUp() { peerInventory = new TestInventory(); peer = new BitmessageContext.Builder() - .addressRepo(Mockito.mock(AddressRepository.class)) + .addressRepo(mock(AddressRepository.class)) .inventory(peerInventory) - .messageRepo(Mockito.mock(MessageRepository.class)) - .powRepo(Mockito.mock(ProofOfWorkRepository.class)) + .messageRepo(mock(MessageRepository.class)) + .powRepo(mock(ProofOfWorkRepository.class)) .port(6001) .nodeRegistry(new TestNodeRegistry()) .networkHandler(new DefaultNetworkHandler()) .cryptography(new BouncyCryptography()) - .listener(Mockito.mock(BitmessageContext.Listener.class)) + .listener(mock(BitmessageContext.Listener.class)) .build(); peer.startup(); nodeInventory = new TestInventory(); networkHandler = new DefaultNetworkHandler(); node = new BitmessageContext.Builder() - .addressRepo(Mockito.mock(AddressRepository.class)) + .addressRepo(mock(AddressRepository.class)) .inventory(nodeInventory) - .messageRepo(Mockito.mock(MessageRepository.class)) - .powRepo(Mockito.mock(ProofOfWorkRepository.class)) + .messageRepo(mock(MessageRepository.class)) + .powRepo(mock(ProofOfWorkRepository.class)) .port(6002) .nodeRegistry(new TestNodeRegistry(localhost)) .networkHandler(networkHandler) .cryptography(new BouncyCryptography()) - .listener(Mockito.mock(BitmessageContext.Listener.class)) + .listener(mock(BitmessageContext.Listener.class)) .build(); } diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java index 3336475..aa41e5c 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java @@ -19,6 +19,7 @@ package ch.dissem.bitmessage.repository; import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.payload.ObjectType; import ch.dissem.bitmessage.entity.valueobject.InventoryVector; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.ports.Inventory; import org.slf4j.Logger; @@ -99,7 +100,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory { } } catch (Exception e) { LOG.error(e.getMessage(), e); - throw new RuntimeException(e); + throw new ApplicationException(e); } } @@ -126,7 +127,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory { return result; } catch (Exception e) { LOG.error(e.getMessage(), e); - throw new RuntimeException(e); + throw new ApplicationException(e); } }